question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Rule Proposal: `prefer-smaller-scope`

See original GitHub issue

Currently prefer-const has two behaviors.

  1. detecting it’s never modified.
  2. 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:closed
  • Created 8 years ago
  • Comments:18 (17 by maintainers)

github_iconTop GitHub Comments

2reactions
mysticateacommented, Jan 15, 2017

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.

0reactions
kaicataldocommented, Jan 8, 2017

@mysticatea Where does this stand?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found