Skip to content

Commit

Permalink
[Fix] jsx-no-undef: handle the TS parser combined with an invalid e…
Browse files Browse the repository at this point in the history
…cmaVersion

Context: this is because typically, in a Module, a function's scope's
parent ends up hitting "module" before it hits "global". However, when
`ecmaVersion` is invalid, it falls back to `5`, and there's no `module`
scope there - but the TS parser still parses ESM syntax in that case.

Fixes #2882
  • Loading branch information
ljharb committed Dec 23, 2020
1 parent b85b8fc commit 816e344
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -21,10 +21,12 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
* [`no-unknown-property`]: avoid crash with prop named with Object.prototype key ([#2879][] @ljharb, @AriPerkkio)
* [`prop-types`]: default argument does not count as props-types declaration ([#2877][] @golopot)
* [`jsx-props-no-multi-spaces`]: fix a false positive for beside comments ([#2878][] @golopot)
* [`jsx-no-undef`]: handle the TS parser combined with an invalid ecmaVersion ([#2882][] @ljharb)

### Docs
* [`no-unused-prop-types`]: Add new example to rule ([#2852][] @thehereward)

[#2882]: https://github.com/yannickcr/eslint-plugin-react/issues/2882
[#2879]: https://github.com/yannickcr/eslint-plugin-react/issues/2879
[#2878]: https://github.com/yannickcr/eslint-plugin-react/pull/2878
[#2877]: https://github.com/yannickcr/eslint-plugin-react/pull/2877
Expand Down
8 changes: 2 additions & 6 deletions lib/rules/jsx-no-undef.js
Expand Up @@ -44,8 +44,8 @@ module.exports = {
let scope = context.getScope();
const sourceCode = context.getSourceCode();
const sourceType = sourceCode.ast.sourceType;
const scopeUpperBound = !allowGlobals && sourceType === 'module' ? 'module' : 'global';
let variables = scope.variables;
let scopeType = 'global';
let i;
let len;

Expand All @@ -54,11 +54,7 @@ module.exports = {
return;
}

if (!allowGlobals && sourceType === 'module') {
scopeType = 'module';
}

while (scope.type !== scopeType) {
while (scope.type !== scopeUpperBound && scope.type !== 'global') {
scope = scope.upper;
variables = scope.variables.concat(variables);
}
Expand Down

0 comments on commit 816e344

Please sign in to comment.