File tree Expand file tree Collapse file tree 6 files changed +34
-57
lines changed Expand file tree Collapse file tree 6 files changed +34
-57
lines changed Original file line number Diff line number Diff line change 1
1
import { getStaticValue } from '@eslint-community/eslint-utils' ;
2
- import isShadowed from './utils/is-shadowed.js' ;
3
2
import { isCallOrNewExpression } from './ast/index.js' ;
4
3
import builtinErrors from './shared/builtin-errors.js' ;
5
4
@@ -20,15 +19,13 @@ const messageArgumentIndexes = new Map([
20
19
/** @param {import('eslint').Rule.RuleContext } context */
21
20
const create = context => {
22
21
context . on ( [ 'CallExpression' , 'NewExpression' ] , expression => {
23
- if ( ! isCallOrNewExpression ( expression , {
24
- names : builtinErrors ,
25
- optional : false ,
26
- } ) ) {
27
- return ;
28
- }
29
-
30
- const scope = context . sourceCode . getScope ( expression ) ;
31
- if ( isShadowed ( scope , expression . callee ) ) {
22
+ if ( ! (
23
+ isCallOrNewExpression ( expression , {
24
+ names : builtinErrors ,
25
+ optional : false ,
26
+ } )
27
+ && context . sourceCode . isGlobalReference ( expression . callee )
28
+ ) ) {
32
29
return ;
33
30
}
34
31
@@ -61,7 +58,7 @@ const create = context => {
61
58
} ;
62
59
}
63
60
64
- const staticResult = getStaticValue ( node , scope ) ;
61
+ const staticResult = getStaticValue ( node , context . sourceCode . getScope ( node ) ) ;
65
62
66
63
// We don't know the value of `message`
67
64
if ( ! staticResult ) {
Original file line number Diff line number Diff line change 7
7
needsSemicolon ,
8
8
isParenthesized ,
9
9
isOnSameLine ,
10
- isShadowed ,
10
+ isUnresolvedVariable ,
11
11
} from './utils/index.js' ;
12
12
13
13
const MESSAGE_ID_ERROR = 'no-typeof-undefined/error' ;
@@ -49,7 +49,7 @@ const create = context => {
49
49
50
50
const valueNode = typeofNode . argument ;
51
51
const isGlobalVariable = valueNode . type === 'Identifier'
52
- && ! isShadowed ( sourceCode . getScope ( valueNode ) , valueNode ) ;
52
+ && ( sourceCode . isGlobalReference ( valueNode ) || isUnresolvedVariable ( valueNode , context ) ) ;
53
53
54
54
if ( ! checkGlobalVariables && isGlobalVariable ) {
55
55
return ;
Original file line number Diff line number Diff line change 1
- import isShadowed from './utils/is-shadowed.js' ;
2
1
import assertToken from './utils/assert-token.js' ;
3
2
import { getCallExpressionTokens } from './utils/index.js' ;
4
- import { isStaticRequire , isReferenceIdentifier , isFunction } from './ast/index.js' ;
3
+ import { isStaticRequire , isFunction } from './ast/index.js' ;
5
4
import { removeParentheses , replaceReferenceIdentifier , removeSpacesAfter } from './fix/index.js' ;
6
5
7
6
const ERROR_USE_STRICT_DIRECTIVE = 'error/use-strict-directive' ;
@@ -56,6 +55,14 @@ const suggestions = new Map([
56
55
] ,
57
56
] ) ;
58
57
58
+ const commonJsGlobals = new Set ( [
59
+ 'exports' ,
60
+ 'require' ,
61
+ 'module' ,
62
+ '__filename' ,
63
+ '__dirname' ,
64
+ ] ) ;
65
+
59
66
function fixRequireCall ( node , sourceCode ) {
60
67
if ( ! isStaticRequire ( node . parent ) || node . parent . callee !== node ) {
61
68
return ;
@@ -281,16 +288,7 @@ function create(context) {
281
288
} ) ;
282
289
283
290
context . on ( 'Identifier' , node => {
284
- if (
285
- ! isReferenceIdentifier ( node , [
286
- 'exports' ,
287
- 'require' ,
288
- 'module' ,
289
- '__filename' ,
290
- '__dirname' ,
291
- ] )
292
- || isShadowed ( sourceCode . getScope ( node ) , node )
293
- ) {
291
+ if ( ! commonJsGlobals . has ( node . name ) || ! context . sourceCode . isGlobalReference ( node ) ) {
294
292
return ;
295
293
}
296
294
Original file line number Diff line number Diff line change @@ -40,7 +40,7 @@ export {default as isNodeValueNotFunction} from './is-node-value-not-function.js
40
40
export { default as isOnSameLine } from './is-on-same-line.js' ;
41
41
export { default as isSameIdentifier } from './is-same-identifier.js' ;
42
42
export { default as isSameReference } from './is-same-reference.js' ;
43
- export { default as isShadowed } from './is-shadowed .js' ;
43
+ export { default as isUnresolvedVariable } from './is-unresolved-variable .js' ;
44
44
export { default as isShorthandImportLocal } from './is-shorthand-import-local.js' ;
45
45
export { default as isShorthandPropertyValue } from './is-shorthand-property-value.js' ;
46
46
export { default as isValueNotUsable } from './is-value-not-usable.js' ;
Load Diff This file was deleted.
Original file line number Diff line number Diff line change
1
+ /**
2
+ Checks if the given identifier node is shadowed in the given scope.
3
+
4
+ @param {string } node - The identifier node to check
5
+ @param {import('eslint').Rule.RuleContext } context - The ESLint rule context.
6
+ @returns {boolean } Whether or not the name is unresolved.
7
+ */
8
+ export default function isUnresolvedVariable ( node , context ) {
9
+ const scope = context . sourceCode . getScope ( node ) ;
10
+ const reference = scope . references
11
+ . find ( reference => reference . identifier === node ) ;
12
+ return ! reference . resolved ;
13
+ }
You can’t perform that action at this time.
0 commit comments