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 and conditional return

See original GitHub issue

Tell 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:closed
  • Created 6 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
not-an-aardvarkcommented, Nov 28, 2017

I don’t think this would be in scope for the prefer-const rule. The goal of the rule is to report let declarations that can be replaced with const, or moved to a different location and then replaced with const. However, the rule is not intended to tell the user when they could create a new variable to accomplish the same thing.

1reaction
not-an-aardvarkcommented, Nov 20, 2017

Hi, thanks for the report.

I’m confused about the behavior that you were expecting. There are two locations where foo could be assigned, even though only one of them will actually be selected at runtime. This means that it wouldn’t be possible to declare foo with const, so I wouldn’t expect prefer-const to report an error for this case.

Simpler example:

let foo;
if (x) {
  foo = 1;
} else {
  foo = 2;
}
console.log(foo);

Similarly, in this example foo will only get assigned once at runtime, but it doesn’t seem like it would be possible to declare foo with const (assuming we keep the general structure of the code rather than reducing it to a ternary expression).

Read more comments on GitHub >

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

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