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.

Error with prefer-const rule

See original GitHub issue

Tell us about your environment

Environment Info:

Node version: v16.4.2 npm version: v7.18.1 Local ESLint version: v7.27.0 (Currently used) Global ESLint version: Not found Operating System: darwin 20.6.0

What parser (default, @babel/eslint-parser, @typescript-eslint/parser, etc.) are you using?

"parser": "@typescript-eslint/parser"

Please show your full configuration:

Configuration
{
  "parser": "@typescript-eslint/parser",
  "extends": ["eslint:recommended", "plugin:react/recommended", "plugin:@typescript-eslint/eslint-recommended", "plugin:@typescript-eslint/recommended", "prettier"],
  "plugins": ["@typescript-eslint", "react-hooks", "prettier"],
  "parserOptions": {
    "ecmaVersion": 2018,
    "sourceType": "module",
    "ecmaFeatures": {"jsx": true}
  },
  "rules": {
    "@typescript-eslint/ban-types": "off",
    "@typescript-eslint/explicit-function-return-type": "off",
    "@typescript-eslint/explicit-member-accessibility": ["off"],
    "@typescript-eslint/explicit-module-boundary-types": "off",
    "@typescript-eslint/member-ordering": [
      "error",
      {
        "default": {
          "memberTypes": [
            "public-static-field",
            "protected-static-field",
            "private-static-field",
            "public-static-method",
            "protected-static-method",
            "private-static-method",
            "public-instance-field",
            "protected-instance-field",
            "private-instance-field",
            "public-abstract-field",
            "protected-abstract-field",
            "private-abstract-field",
            "public-constructor",
            "protected-constructor",
            "private-constructor",
            "public-instance-method",
            "protected-instance-method",
            "private-instance-method",
            "public-abstract-method",
            "protected-abstract-method",
            "private-abstract-method"
          ],
          "order": "as-written"
        }
      }
    ],
    "@typescript-eslint/no-empty-function": "off",
    "@typescript-eslint/no-empty-interface": "off",
    "@typescript-eslint/no-explicit-any": "off",
    "@typescript-eslint/no-inferrable-types": "off",
    "@typescript-eslint/no-non-null-assertion": "off",
    "@typescript-eslint/no-unused-vars": "error",
    "@typescript-eslint/no-var-requires": "off",
    "@typescript-eslint/no-use-before-define": ["error", "nofunc"],
    "no-async-promise-executor": "off",
    "arrow-body-style": "off",
    "eqeqeq": ["error", "smart"],
    "no-console": ["error", {"allow": ["info"]}],
    "no-duplicate-imports": ["error"],
    "no-useless-computed-key": ["error"],
    "no-useless-rename": ["error"],
    "no-var": ["error"],
    "no-fallthrough": "off",
    "object-shorthand": ["error"],
    "prefer-const": ["error"],
    "prefer-arrow-callback": "off",
    "prettier/prettier": "error",
    "react-hooks/exhaustive-deps": ["off"],
    "react-hooks/rules-of-hooks": ["error"],
    "react/display-name": "off",
    "react/jsx-boolean-value": ["error", "never"],
    "react/jsx-curly-brace-presence": ["error", {"props": "never", "children": "ignore"}],
    "react/jsx-fragments": ["error", "syntax"],
    "react/jsx-no-target-blank": "off",
    "react/no-unescaped-entities": ["off"],
    "react/prop-types": "off",
    "require-yield": "off"
  },
  "settings": {
    "react": {"version": "detect"}
  },
  "overrides": [
    {
      "files": ["**/module/*/*/index.ts", "**/module/*/*/index.tsx", "**/module/main/index.ts", "**/module/main/index.tsx"],
      "rules": {
        "@typescript-eslint/explicit-function-return-type": ["error"]
      }
    }
  ]
}

What did you do? Please include the actual source code causing the issue, as well as the command that you used to run ESLint.

        let timer: NodeJS.Timeout;

        const clear = () => {
            clearInterval(timer);
            // other code
            window.location.reload();
        };

        const child = window.open("example.com", "Login", "toolbar=no,scrollbars=yes,resizable=yes");

        const checkChild = () => {
            if (child && child.closed) {
                clear();
            }
        };

        timer = setInterval(checkChild, 500);

(I have the configuration in a different file and in the root I have a reference to that file as well. Like it’s shown in this project)

eslint -c webpack/lint/.eslintrc.json \"{src,test}/**/*.{ts,tsx}\"

What did you expect to happen? The code should open a window and check with an interval of 500 check if the new opened page is closed to run the code inside clear.

What actually happened? Please copy-paste the actual, raw output from ESLint. The eslint configuration throws an error claiming that timer should be a const when I’m assigning it at the end of my code example. And due to the rules I have configures if I move the functions around it will throw errors that the variables are being used before they are declared. Output:

23:13 error ‘timer’ is never reassigned. Use ‘const’ instead prefer-const

✖ 1 problem (1 error, 0 warnings)

Steps to reproduce this issue:

  1. Copy the configurations I have inserted here for a react / typescript project
  2. Create a new react function component
  3. Copy the code I added here and run eslint check

Are you willing to submit a pull request to fix this bug?

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
mdjermanoviccommented, Sep 23, 2021

Thanks for the info! Per the thumbs up reaction on https://github.com/eslint/eslint/issues/15022#issuecomment-912360438 it seems that the original problem has been resolved as well, so I’m closing this issue.

1reaction
mdjermanoviccommented, Sep 23, 2021

@Shinigami92 your example is different because there’s assignment in an inner scope, and that would be a bug.

I think the cause is same as in https://github.com/eslint/eslint/issues/14977, you’re using ecmaVersion: 'latest' with @typescript-eslint/parser which doesn’t support that value, so the scope analysis is done as if it was es5 code (without block scopes). The solution is to not use 'latest' with @typescript-eslint/parser because it’s not a valid option for that parser.

Read more comments on GitHub >

github_iconTop Results From Across the Web

prefer-const - ESLint - Pluggable JavaScript Linter
This rule is aimed at flagging variables that are declared using let keyword, ... /*eslint prefer-const: "error"*/ // it's initialized and never reassigned....
Read more >
ESLint: prefer-const doesn't throw an error even if variable is ...
eslintrc file, the value of prefer-const has been set to 2 and I do not override rules in the file. All the rules...
Read more >
prefer-const - Rule
An optional object containing the property “destructuring” with two possible values: “any” (default) - If any variable in destructuring can be const, this...
Read more >
Linter rules - Dart
These rules identify possible errors and other mistakes in your code. ... Prefer const literals as parameters of constructors on @immutable classes.
Read more >
Prefer const rule triggered in loops : r/typescript - Reddit
You obviously don't have a eslint error but a code error. Edit: if its being assigned inside the loop you should declare the...
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