prefer-const and conditional return
See original GitHub issueTell us about your environment
- ESLint Version: 3.19
- Node Version: 8.5
- npm Version: 5.5.1 What parser (default, Babel-ESLint, etc.) are you using? default Please show your full configuration:
Configuration
{
"parserOptions": {
"ecmaVersion": 6,
"sourceType": "script",
"ecmaFeatures": {}
},
"rules": {
"constructor-super": 2,
"no-case-declarations": 2,
"no-class-assign": 2,
"no-compare-neg-zero": 2,
"no-cond-assign": 2,
"no-console": 2,
"no-const-assign": 2,
"no-constant-condition": 2,
"no-control-regex": 2,
"no-debugger": 2,
"no-delete-var": 2,
"no-dupe-args": 2,
"no-dupe-class-members": 2,
"no-dupe-keys": 2,
"no-duplicate-case": 2,
"no-empty-character-class": 2,
"no-empty-pattern": 2,
"no-empty": 2,
"no-ex-assign": 2,
"no-extra-boolean-cast": 2,
"no-extra-semi": 2,
"no-fallthrough": 2,
"no-func-assign": 2,
"no-global-assign": 2,
"no-inner-declarations": 2,
"no-invalid-regexp": 2,
"no-irregular-whitespace": 2,
"no-mixed-spaces-and-tabs": 2,
"no-new-symbol": 2,
"no-obj-calls": 2,
"no-octal": 2,
"no-redeclare": 2,
"no-regex-spaces": 2,
"no-self-assign": 2,
"no-sparse-arrays": 2,
"no-this-before-super": 2,
"no-undef": 2,
"no-unexpected-multiline": 2,
"no-unreachable": 2,
"no-unsafe-finally": 2,
"no-unsafe-negation": 2,
"no-unused-labels": 2,
"no-unused-vars": 2,
"no-useless-escape": 2,
"require-yield": 2,
"use-isnan": 2,
"valid-typeof": 2,
"prefer-const": 2
},
"env": {}
}
What did you do? Please include the actual source code causing the issue, as well as the command that you used to run ESLint.
function fn() {
let foo;
if (Math.random() > .5) {
foo = 'hop';
return foo;
}
foo = 'baz';
return foo;
}
fn();
eslint filename
What did you expect to happen?
I expected ESlint to pickup on the fact that either we enter the if, in which case the return makes it so the foo var is never reassigned, or we don’t, in which case the foo bar is also never reassigned.
What actually happened? Please include the actual, raw output from ESLint. ESlint doesn’t report an error
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (4 by maintainers)
Top Results From Across the Web
ESLint prefer-const error with conditional reassign [duplicate]
The const declaration creates a read-only reference to a value. It does not mean the value it holds is immutable, just that the...
Read more >prefer-as-const | typescript-eslint
This rule reports when an as with an explicit literal type can be replaced with an as const . .eslintrc.cjs. module.exports = {...
Read more >prefer-const - ESLint - Pluggable JavaScript Linter
A pluggable and configurable linter tool for identifying and reporting on patterns in JavaScript. Maintain your code quality with ease.
Read more >Use `const` and make your JavaScript code better - Medium
const prevents the variable to be assigned to another value. We could say it makes the pointer immutable, but it doesn't make the...
Read more >Prefer const rule triggered in loops : r/typescript - Reddit
Prefer const rule triggered in loops · Why do you have that rule enabled? If you don't like the rule, you can always...
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 Free
Top 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

I don’t think this would be in scope for the
prefer-construle. The goal of the rule is to reportletdeclarations that can be replaced withconst, or moved to a different location and then replaced withconst. However, the rule is not intended to tell the user when they could create a new variable to accomplish the same thing.Hi, thanks for the report.
I’m confused about the behavior that you were expecting. There are two locations where
foocould be assigned, even though only one of them will actually be selected at runtime. This means that it wouldn’t be possible to declarefoowithconst, so I wouldn’t expectprefer-constto report an error for this case.Simpler example:
Similarly, in this example
foowill only get assigned once at runtime, but it doesn’t seem like it would be possible to declarefoowithconst(assuming we keep the general structure of the code rather than reducing it to a ternary expression).