Skip to content

Commit e11b084

Browse files
authored
Use context.sourceCode.isGlobalReference() in more places (#2685)
1 parent 2b8ebe5 commit e11b084

File tree

6 files changed

+34
-57
lines changed

6 files changed

+34
-57
lines changed

rules/error-message.js

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import {getStaticValue} from '@eslint-community/eslint-utils';
2-
import isShadowed from './utils/is-shadowed.js';
32
import {isCallOrNewExpression} from './ast/index.js';
43
import builtinErrors from './shared/builtin-errors.js';
54

@@ -20,15 +19,13 @@ const messageArgumentIndexes = new Map([
2019
/** @param {import('eslint').Rule.RuleContext} context */
2120
const create = context => {
2221
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+
)) {
3229
return;
3330
}
3431

@@ -61,7 +58,7 @@ const create = context => {
6158
};
6259
}
6360

64-
const staticResult = getStaticValue(node, scope);
61+
const staticResult = getStaticValue(node, context.sourceCode.getScope(node));
6562

6663
// We don't know the value of `message`
6764
if (!staticResult) {

rules/no-typeof-undefined.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
needsSemicolon,
88
isParenthesized,
99
isOnSameLine,
10-
isShadowed,
10+
isUnresolvedVariable,
1111
} from './utils/index.js';
1212

1313
const MESSAGE_ID_ERROR = 'no-typeof-undefined/error';
@@ -49,7 +49,7 @@ const create = context => {
4949

5050
const valueNode = typeofNode.argument;
5151
const isGlobalVariable = valueNode.type === 'Identifier'
52-
&& !isShadowed(sourceCode.getScope(valueNode), valueNode);
52+
&& (sourceCode.isGlobalReference(valueNode) || isUnresolvedVariable(valueNode, context));
5353

5454
if (!checkGlobalVariables && isGlobalVariable) {
5555
return;

rules/prefer-module.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import isShadowed from './utils/is-shadowed.js';
21
import assertToken from './utils/assert-token.js';
32
import {getCallExpressionTokens} from './utils/index.js';
4-
import {isStaticRequire, isReferenceIdentifier, isFunction} from './ast/index.js';
3+
import {isStaticRequire, isFunction} from './ast/index.js';
54
import {removeParentheses, replaceReferenceIdentifier, removeSpacesAfter} from './fix/index.js';
65

76
const ERROR_USE_STRICT_DIRECTIVE = 'error/use-strict-directive';
@@ -56,6 +55,14 @@ const suggestions = new Map([
5655
],
5756
]);
5857

58+
const commonJsGlobals = new Set([
59+
'exports',
60+
'require',
61+
'module',
62+
'__filename',
63+
'__dirname',
64+
]);
65+
5966
function fixRequireCall(node, sourceCode) {
6067
if (!isStaticRequire(node.parent) || node.parent.callee !== node) {
6168
return;
@@ -281,16 +288,7 @@ function create(context) {
281288
});
282289

283290
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)) {
294292
return;
295293
}
296294

rules/utils/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export {default as isNodeValueNotFunction} from './is-node-value-not-function.js
4040
export {default as isOnSameLine} from './is-on-same-line.js';
4141
export {default as isSameIdentifier} from './is-same-identifier.js';
4242
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';
4444
export {default as isShorthandImportLocal} from './is-shorthand-import-local.js';
4545
export {default as isShorthandPropertyValue} from './is-shorthand-property-value.js';
4646
export {default as isValueNotUsable} from './is-value-not-usable.js';

rules/utils/is-shadowed.js

Lines changed: 0 additions & 31 deletions
This file was deleted.

rules/utils/is-unresolved-variable.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
}

0 commit comments

Comments
 (0)