-
Notifications
You must be signed in to change notification settings - Fork 659
feat(rome_js_analyze): useDefaultSwitchClauseLast
#3791
Conversation
✅ Deploy Preview for docs-rometools ready!Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify site settings. |
useDefaultCaseLast
crates/rome_js_analyze/src/analyzers/nursery/use_default_case_last.rs
Outdated
Show resolved
Hide resolved
crates/rome_js_analyze/tests/specs/nursery/useDefaultCaseLast/invalid.js.snap
Outdated
Show resolved
Hide resolved
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 think we can also provide a code action that moves the default
statement to the last position. We can add the code action in another PR.
|
||
impl Rule for UseDefaultCaseLast { | ||
type Query = Ast<JsSwitchCaseList>; | ||
type State = JsAnySwitchClause; |
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.
The state could be a TextRange
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.
Is there advantages to use TextRange
instead of JsAnySwitchClause
?
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.
Not particularly, simply less information to carry around :)
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.
It is expensive to calculate range in some cases. Specially when you use the trimmed version that needs to skip trivia.
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.
It is expensive to calculate range in some cases. Specially when you use the trimmed version that needs to skip trivia.
Computing the range in run
or in diagnostic
has the same cost?
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.
Not particularly, simply less information to carry around :)
I agree (information hiding). However, I think that in this code, it is good to keep more info (here the next case). This makes the rule more consistent with others and enables to get more context (this could be useful for improving diagnostic in a future revision).
crates/rome_js_analyze/src/analyzers/nursery/use_default_case_last.rs
Outdated
Show resolved
Hide resolved
crates/rome_js_analyze/src/analyzers/nursery/use_default_case_last.rs
Outdated
Show resolved
Hide resolved
The difficulty is when a fallthrough is present. Like in the following code: switch (foo) {
default:
doSomethingIfNotZero();
case 0:
doSomethingAnyway();
} This requires some logic to know whether the block return/break. Have you an idea how to implement this without exploring all descendants? Or we could just handle the case where a block statement ends with In fact I - in constrast to ESLint - we could allow code with default clause that fallthrough (as the previous code). Another rule (noFallthrough) could reject this code. This could make useDefaultCaseLast less restrictive. Also, I wonder if we should rename the rule "useDefaultCaseLast" to "useDefaultClauseLast" or "useDefaultSwitchClauseLast". |
!bench_analyzer |
I'm in favor of renaming
|
Analyzer Benchmark Results
|
useDefaultCaseLast
useDefaultSwitchClauseLast
@ematipico @MichaReiser I will open a new PR for the code action. This allows discussing that in details. Unfortunately, a commit in |
Fixed in #3840 |
* upstream/main: (73 commits) fix(semantic_analyzers): style/noShoutyConstants does not recognize multiple uses of a constant. (rome#3789) feat(rome_js_analyze): useDefaultSwitchClauseLast (rome#3791) chore: run rustfmt and typo fix (rome#3840) feat(rome_js_analyze): use exhaustive deps support properties (rome#3581) website(docs): Fix text formatting (rome#3828) feat(rome_js_analyze): `noVoidTypeReturn` (rome#3806) feat(rome_cli): expose the `--verbose` flag to the CLI (rome#3812) fix(rome_diagnostics): allow diagnostic locations to be created without a resource (rome#3834) feat(rome_js_analyze): add noExtraNonNullAssertion rule (rome#3797) fix(rome_lsp): lsp friendly catch unwind (rome#3740) feat(rome_js_semantic): model improvements (rome#3825) feat(rome_json_parser): JSON Lexer (rome#3809) feat(rome_js_analyze): implement `noDistractingElements` (rome#3820) fix(rome_js_formatter): shothanded named import line break with default import (rome#3826) feat(rome_js_analyze): `noConstructorReturn` (rome#3805) feat(website): change enabledNurseryRules to All/Recommended select (rome#3810) feat(rome_js_analyze): noSetterReturn feat(rome_js_analyze): noConstructorReturn feat(rome_analyze): suppress rule via code actions (rome#3572) feat(rome_js_analyze): `noVar` (rome#3765) ...
Summary
This implements ESLint's default-case-last.
Test Plan
Unit test included.