Rule Proposal: `prefer-smaller-scope`
See original GitHub issueCurrently prefer-const
has two behaviors.
- detecting it’s never modified.
- detecting it can be moved to a smaller scope.
This seems to be confusing people (e.g. #5283, #5251) (I’m sorry…).
So this proposal is separating the 2nd behavior to new rule: prefer-smaller-scope
.
prefer-small-scope
is aimed at eliminating variables in too wide scope.
This rule warns variables which can move to a smaller scope.
This improves readability because the scope which can read/write the variable gets smaller.
The following patterns are considered problems:
/* eslint prefer-smaller-scope: 2 */
let foo; /*error 'foo' declaration can move into a block 'if (a) {...}'.*/
if (a) {
foo = 100;
bar(foo);
} else {
bar(200);
}
The following patterns are not considered problems:
/* eslint prefer-smaller-scope: 2 */
let foo;
if (a) {
foo = 100;
} else {
foo = 200:
}
baz(foo);
let foo;
try {
foo = bar();
} catch (err) {
foo = 0:
}
baz(foo);
Then the 2nd behavior of prefer-const
is removed.
I think this may be not a breaking change because the rule is defused than now.
If accepted, I’m happy to work.
Issue Analytics
- State:
- Created 8 years ago
- Comments:18 (17 by maintainers)
Top Results From Across the Web
Proposed rule: Order Competition Rule - SEC.gov
proposed rule would prohibit a restricted competition trading center from internally executing certain orders of individual investors at a ...
Read more >Default Address Selection for Internet Protocol version 6 (IPv6)
Document, Type, RFC - Proposed Standard (March 2003) ... [Page 13] RFC 3484 Default Address Selection for IPv6 February 2003 Rule 8: Prefer...
Read more >'[musl] Proposed RFC 3484/6724 subset for getaddrinfo' - MARC
[prev in thread] [next in thread] List: musl Subject: [musl] Proposed RFC ... label (B,D) Rule 6: Prefer higher precedence (E) Rule 8:...
Read more >eslint | Yarn - Package Manager
Website | Configuring | Rules | Contributing | Reporting Bugs | Code of Conduct | Twitter | Mailing List | Chat Room. ESLint...
Read more >SEC proposes substantive changes in 4 stock market rules
The Securities and Exchange Commission today proposed amendments that would update the disclosure required under Rule 605 of Regulation NMS for ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’m sorry, I’m closing for now.
I have encountered a hard problem.
prefer-smaller-scope
needs to move variable declarations, it’s very difficult to distinguish if it can move the declarations because of references in their initializers. I will revisit this after I investigate the way of type/value tracking in our scope analysis.@mysticatea Where does this stand?