From 816e3449cfa01adfe66fa52dfc0aaccbe0f8dd41 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Wed, 23 Dec 2020 11:38:05 -0800 Subject: [PATCH] [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 --- CHANGELOG.md | 2 ++ lib/rules/jsx-no-undef.js | 8 ++------ 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7e44835fcf..8efc7fb0d2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/rules/jsx-no-undef.js b/lib/rules/jsx-no-undef.js index a887e48bb1..f266d5de64 100644 --- a/lib/rules/jsx-no-undef.js +++ b/lib/rules/jsx-no-undef.js @@ -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; @@ -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); }