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.

Bug: prefer-const error in try catch

See original GitHub issue

Environment

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?

@typescript-eslint/parser

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

github_iconTop GitHub Comments

2reactions
mdjermanoviccommented, Dec 28, 2021

Your configuration:

ecmaVersion: ‘es2020’,

Per TypeScript ESLint Parser documentation:

When it’s a version or a year, the value must be a number - so do not include the es prefix.

Can you try with ecmaVersion: 2020?

Since 'es2020' isn’t an expected value, I believe it defaults to 5, which affects scoping in a way that prefer-const gets info that the assignment is in the same scope as the declaration, and therefore reports the error.

1reaction
timvandamcommented, Dec 28, 2021

Why shouldn’t it error, if you’re not referencing x anywhere else?

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.

Read more comments on GitHub >

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

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