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.

'Prefer-Const' Multiple Variable Declaration Option

See original GitHub issue

What rule do you want to change? prefer-const.js

Does this change cause the rule to produce more or fewer warnings? If the rule option is enabled via .eslintrc then instances of multiple variable declarations can be automatically fixed. This should cause fewer errors prompting manual refactorings of these instances.

This comment from the existing source is relevant:

/*
 * If there are multiple variable declarations, like {let a = 1, b = 2}, then
 * do not attempt to fix if one of the declarations should be `const`. It's
 * too hard to know how the developer would want to automatically resolve the issue.
 */

With the new option, developers could choose to use multiple variable declarations in a consistent fashion and have the tool work accordingly.

How will the change be implemented? (New option, new default behavior, etc.)? A new option will be added to the rule allowing the developer to opt in for automatic fixes of multiple variable declarations.

Please provide some example code that this change will affect:

let debug = require('../../')('http')
  , http = require('http');
let a = 1, b = 2;

What does the rule currently do for this code? The rule labels it as an error or warning but fails to automatically fix it.

What will the rule do after it’s changed? If the option is enabled then multiple variable declarations would be automatically fixed and the developer would have to use multiple variable declarations consistently.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
not-an-aardvarkcommented, Jun 6, 2018

IIRC, the difficult case is when one of the variables is reassigned and the other variable is not.

let a = 1, b = 2;

b = 3;

prefer-const will report an error for b, but will not autofix it.

I think we should autofix the case where all of the variables types can be replaced with const, if we’re not doing that already.

edit: It seems like we’re not autofixing that case. I’d be fine with autofixing that case by default (without adding an option), and continuing to not autofix the case where some of the variables are reassigned.

0reactions
not-an-aardvarkcommented, Jul 22, 2018

Thanks for your interest in improving ESLint. Unfortunately, it looks like this issue didn’t get consensus from the team, so I’m closing it. We define consensus as having three 👍s from team members, as well as a team member willing to champion the proposal. This is a high bar by design – we can’t realistically accept and maintain every feature request in the long term, so we only accept feature requests which are useful enough that there is consensus among the team that they’re worth adding.

Since ESLint is pluggable and can load custom rules at runtime, the lack of consensus among the ESLint team doesn’t need to be a blocker for you using this in your project, if you’d find it useful. It just means that you would need to implement the rule yourself, rather than using a bundled rule that is packaged with ESLint.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Declaring multiple variables in JavaScript - Stack Overflow
The first way is easier to maintain. Each declaration is a single statement on a single line, so you can easily add, remove,...
Read more >
prefer-const - ESLint - Pluggable JavaScript Linter
This rule is aimed at flagging variables that are declared using let keyword, but never reassigned after the initial assignment. Examples of incorrect...
Read more >
How the let, const, and var Keywords Work in JavaScript
The variable declared with var inside a function is not accessible outside of it. The keyword var has function-scope. How to Use JavaScript ......
Read more >
const - JavaScript - MDN Web Docs - Mozilla
The const declaration creates block-scoped constants, much like variables declared using the let keyword. The value of a constant can't be ...
Read more >
Documentation - Variable Declaration - TypeScript
const declarations are another way of declaring variables. ... const numLivesForCat = 9;. They are like let declarations but, as their name implies,...
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