Bug: prefer-const error in try catch
See original GitHub issueEnvironment
Node version: 16.13.0 npm version: 8.1.0 Local ESLint version: 8.5.0 Global ESLint version: N/A Operating System: Ubuntu 20
What parser are you using?
What did you do?
Configuration
module.exports = {
parser: '@typescript-eslint/parser',
parserOptions: {
project: './tsconfig.json',
ecmaVersion: 'es2020',
sourceType: 'module',
},
root: true,
plugins: ['@typescript-eslint'],
extends: ['plugin:@typescript-eslint/recommended'],
rules: {
'prefer-const': 2,
},
};
function something(): number {
let x: number;
function dangerousFunction(): number {
if (Math.random() > 0.5) throw new Error('Boom')
return 123
}
try {
x = dangerousFunction() // ESLint: 'x' is never reassigned. Use 'const' instead.(prefer-const)
} catch (e) {
return -1
}
return x * 20 + 5
}
A set up repo: https://github.com/timvandam/eslint-bug
What did you expect to happen?
It should not error when assigning a variable once in a try catch (as per #15460)
What actually happened?
It does error!
Participation
- I am willing to submit a pull request for this issue.
Additional comments
If I can figure it out I would be willing to submit a pull request. I’m not sure how eslint and the typescript parser interact, but from what I’ve seen while exploring it quickly, the typescript eslint plugin does not change the prefer-const rule whatsoever, it just takes it from eslint
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
JavaScript: define a constant inside try / catch with strict mode
Today I run into a weird JS bug, working with const inside a try/catch block, and I'd like to better understand what is...
Read more >Error handling, "try...catch" - The Modern JavaScript Tutorial
It works like this: First, the code in try {...} is executed. If there were no errors, then catch (err) ...
Read more >Prefer const rule triggered in loops : r/typescript - Reddit
I like that rule. If i change it to const, inside the loop, TS will be happy but JS will throw an error....
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 >Exceptions and Error Handling, C++ FAQ - Standard C++
So compared to error reporting via return-codes and if , using try / catch / throw is likely to result in code that...
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 FreeTop 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
Top GitHub Comments
Your configuration:
Per TypeScript ESLint Parser documentation:
Can you try with
ecmaVersion: 2020
?Since
'es2020'
isn’t an expected value, I believe it defaults to5
, which affects scoping in a way thatprefer-const
gets info that the assignment is in the same scope as the declaration, and therefore reports the error.I forgot to include that in my code snippet, I added it just now. If
x
is used below the try catch, the error persists.