Skip to content

Commit 816e344

Browse files
committed
[Fix] jsx-no-undef: handle the TS parser combined with an invalid ecmaVersion
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
1 parent b85b8fc commit 816e344

File tree

2 files changed

+4
-6
lines changed

2 files changed

+4
-6
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
2121
* [`no-unknown-property`]: avoid crash with prop named with Object.prototype key ([#2879][] @ljharb, @AriPerkkio)
2222
* [`prop-types`]: default argument does not count as props-types declaration ([#2877][] @golopot)
2323
* [`jsx-props-no-multi-spaces`]: fix a false positive for beside comments ([#2878][] @golopot)
24+
* [`jsx-no-undef`]: handle the TS parser combined with an invalid ecmaVersion ([#2882][] @ljharb)
2425

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

29+
[#2882]: https://github.com/yannickcr/eslint-plugin-react/issues/2882
2830
[#2879]: https://github.com/yannickcr/eslint-plugin-react/issues/2879
2931
[#2878]: https://github.com/yannickcr/eslint-plugin-react/pull/2878
3032
[#2877]: https://github.com/yannickcr/eslint-plugin-react/pull/2877

lib/rules/jsx-no-undef.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ module.exports = {
4444
let scope = context.getScope();
4545
const sourceCode = context.getSourceCode();
4646
const sourceType = sourceCode.ast.sourceType;
47+
const scopeUpperBound = !allowGlobals && sourceType === 'module' ? 'module' : 'global';
4748
let variables = scope.variables;
48-
let scopeType = 'global';
4949
let i;
5050
let len;
5151

@@ -54,11 +54,7 @@ module.exports = {
5454
return;
5555
}
5656

57-
if (!allowGlobals && sourceType === 'module') {
58-
scopeType = 'module';
59-
}
60-
61-
while (scope.type !== scopeType) {
57+
while (scope.type !== scopeUpperBound && scope.type !== 'global') {
6258
scope = scope.upper;
6359
variables = scope.variables.concat(variables);
6460
}

0 commit comments

Comments
 (0)