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` is reported when assignment occurs in different scope.

See original GitHub issue

Tell us about your environment

Environment Info:

Node version: v16.7.0 npm version: v7.20.6 Local ESLint version: Not found Global ESLint version: Not found Operating System: linux 5.13.10-arch1-1

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

We’re using the @typescript-eslint/parser.

Please show your full configuration:

https://github.com/thenativeweb/eslint-config-es/blob/main/browser.js which extends https://github.com/thenativeweb/eslint-config-es/blob/main/node.js

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

We’re shipping the new eslint version in our build tool roboter. After updating, we’ve encountered a couple of reported issues that hadn’t been reported before while linting one of our packages. We believe the prefer-const rule is reported falsely in the following case. Eslint is called by roboter. It uses the JS API as seen here.

const getBrokenUrls = async function ([...]): Promise<BrokenUrl[]> {
  
  [...]

  // eslint-disable-next-line no-constant-condition, @typescript-eslint/no-unnecessary-condition
  while (true) {
    
    [...]

    let page: Page;

    try {
      page = await getPage({ url: currentUrl });
    } catch {
      brokenUrls.push({ url: currentUrl, referrers: referrers[currentUrl] });
      onBrokenUrl({ url: currentUrl, referrers: referrers[currentUrl] });
      continue;
    }

    if (!currentUrl.startsWith(baseUrl)) {
      continue;
    }

    const externalUrls = getExternalUrlsFromPage({ page, baseUrl });

   [...]
};

What did you expect to happen?

The prefer-const rule should not apply to the file shown, as page is declared in a different scope than it is first assigned. The declaration of page can’t be moved into the try block without moving other statements into the block as well.

What actually happened? Please copy-paste the actual, raw output from ESLint.

Eslint reports the error: error 'page' is never reassigned. Use 'const' instead prefer-const.

Steps to reproduce this issue:

  1. git clone git@github.com:thenativeweb/thenativeweb-ux.git --branch dependabot/npm_and_yarn/roboter-12.0.0
  2. cd thenativeweb-ux && npm install
  3. npx roboter

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

Yes

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
strangedevcommented, Aug 26, 2021

The documentation confirms your suspicion @mdjermanovic and further testing showed that the scope analysis works as expected when a valid version is used. Thank you for your help, your input has saved me a lot of time and frustration! 😊

1reaction
mdjermanoviccommented, Aug 26, 2021

I think that @typescript-eslint/parser doesn’t support ecmaVersion: "latest". and I’m guessing that it then falls back to analyzing scope as if it was ecmaVersion: "5" (without block scopes). Can you try with a number value?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Prefer const rule triggered in loops : r/typescript - Reddit
Each loop run creates its own context, so it will not reassign the variable, the old context is lost by that point.
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 >
What is the preferred declaration convention for objects or arrays
The const keyword in front of an object implies that there is an object, and you're working with references to alter it. It...
Read more >
When should use const and let instead of var in Javascript?
It means that the `var` declarations are processed during the compile time and the JavaScript engine will know that there is a variable...
Read more >
11 Variables and assignment - Exploring JS
In JavaScript, const only means that the binding (the association between variable name and variable value) is immutable. The value itself may be...
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