`prefer-const` is reported when assignment occurs in different scope.
See original GitHub issueTell 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:
git clone git@github.com:thenativeweb/thenativeweb-ux.git --branch dependabot/npm_and_yarn/roboter-12.0.0
cd thenativeweb-ux && npm install
npx roboter
Are you willing to submit a pull request to fix this bug?
Yes
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (2 by maintainers)
Top GitHub Comments
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! 😊
I think that
@typescript-eslint/parser
doesn’t supportecmaVersion: "latest"
. and I’m guessing that it then falls back to analyzing scope as if it wasecmaVersion: "5"
(without block scopes). Can you try with a number value?