This repository has been archived by the owner on Aug 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 660
feat(rome_js_analyze): relax useLiteralEnumMembers
#4720
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
✅ Deploy Preview for docs-rometools canceled.Built without sensitive environment variables
|
github-actions
bot
added
A-Website
Area: website and documentation
A-Linter
Area: linter
L-JavaScript
Langauge: JavaScript
A-Parser
Area: parser
labels
Jul 21, 2023
ematipico
approved these changes
Jul 23, 2023
Comment on lines
80
to
104
(move || { | ||
let enum_declaration = ctx.query(); | ||
let mut result = Vec::new(); | ||
let mut enum_member_names = FxHashSet::default(); | ||
let enum_name = enum_declaration.id().ok()?; | ||
let enum_name = enum_name.as_js_identifier_binding()?; | ||
let enum_name = enum_name.name_token().ok()?; | ||
let enum_name = enum_name.text_trimmed(); | ||
for enum_member in enum_declaration.members() { | ||
let enum_member = enum_member.ok()?; | ||
// no initializer => sequentially assigned literal integer | ||
if let Some(initializer) = enum_member.initializer() { | ||
let initializer = initializer.expression().ok()?; | ||
let range = initializer.range(); | ||
if !is_constant_enum_expression(initializer, enum_name, &enum_member_names) { | ||
result.push(range); | ||
} | ||
}; | ||
if let Some(name) = enum_member.name().ok()?.name() { | ||
enum_member_names.insert(name.text().to_string()); | ||
} | ||
} | ||
} else if let Some(expr) = expr.as_js_template_expression() { | ||
if expr.is_constant() { | ||
return None; | ||
} | ||
} | ||
Some(()) | ||
Some(result) | ||
})() | ||
.unwrap_or_default() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can avoid the closure by using .map
on any ok()
, returning Option<Vec<TextRange>>
and using unwrap_or_default
. Or create a function.
If you think about it, this code doesn't feel right.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really don't like all this ceremonial about error handling.
In the linter we still abort as soon as we encounter a syntax error.
Sometimes I dream of a monad that filters these errors, exposing to the linter only well-formed nodes.
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Labels
A-Linter
Area: linter
A-Parser
Area: parser
A-Website
Area: website and documentation
L-JavaScript
Langauge: JavaScript
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR relaxes
useLiteralEnumMembers
to allow referencing to previous enum members. This is commonly used in enum flags (it is used multiple times in the TypeScript Compiler source code). For example:A wide range of references are recognized (
Write
,FileAccess.Write
,FileAccess["Write"]
FileAccess[`Write`]
)To be more consistent, the rule is also relaxed to allow arbitrary numeric and string expressions that involve string and number literals.
I wonder if the rule should be renamed to
useConstantEnumMembers
to better reflect the new implementation.Test Plan
Tests updated and extended.