eslint / eslint-scope

eslint-scope: ECMAScript scope analyzer
BSD 2-Clause "Simplified" License
125 stars 30 forks source link

fix: `Definition#name` in catch patterns should be `Identifier` node #127

Closed mdjermanovic closed 4 months ago

mdjermanovic commented 4 months ago

Refs https://github.com/eslint/eslint/pull/18636#pullrequestreview-2150587740

Currently, if catch param is a pattern, Definition#name of variables created in the pattern points to the pattern instead of the Identifier nodes.

Repro:

import * as eslintScope from 'eslint-scope';
import * as espree from 'espree';

const code = "try {} catch({ message }) {}";

const ast = espree.parse(code, { range: true, ecmaVersion: 2015 });
const scopeManager = eslintScope.analyze(ast, { ecmaVersion: 2015 });

const catchScope = scopeManager.scopes[2];
const variable = catchScope.variables[0];

console.log(variable.name); // "message"

console.log(variable.defs[0].name.type); // "ObjectPattern" !

This fixes the mentioned Definitions to point to Identifier nodes.

I've verified that all tests in eslint/eslint are still passing after this change. All new tests added in https://github.com/eslint/eslint/pull/18636 are also passing after this change.