Skip to content

Commit d90f337

Browse files
authored
Use SourceCode#getScope (#2068)
1 parent f144330 commit d90f337

32 files changed

+101
-86
lines changed

rules/catch-error-name.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ const create = context => {
6060
return;
6161
}
6262

63-
const scope = context.getScope();
63+
const scope = context.getSourceCode().getScope(node);
6464
const variable = findVariable(scope, node);
6565

6666
// This was reported https://github.com/sindresorhus/eslint-plugin-unicorn/issues/1075#issuecomment-768072967

rules/consistent-destructuring.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ const create = context => {
6464
}
6565

6666
declarations.set(sourceCode.getText(node.init), {
67-
scope: context.getScope(),
67+
scope: sourceCode.getScope(node),
6868
variables: sourceCode.getDeclaredVariables(node),
6969
objectPattern: node.id,
7070
});
@@ -81,7 +81,7 @@ const create = context => {
8181
}
8282

8383
const {scope, objectPattern} = declaration;
84-
const memberScope = context.getScope();
84+
const memberScope = sourceCode.getScope(node);
8585

8686
// Property is destructured outside the current scope
8787
if (!isChildInParentScope(memberScope, scope)) {

rules/error-message.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ const selector = callOrNewExpressionSelector([
2828
/** @param {import('eslint').Rule.RuleContext} context */
2929
const create = context => ({
3030
[selector](expression) {
31-
if (isShadowed(context.getScope(), expression.callee)) {
31+
const scope = context.getSourceCode().getScope(expression);
32+
if (isShadowed(scope, expression.callee)) {
3233
return;
3334
}
3435

@@ -59,7 +60,7 @@ const create = context => ({
5960
};
6061
}
6162

62-
const staticResult = getStaticValue(node, context.getScope());
63+
const staticResult = getStaticValue(node, scope);
6364

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

rules/explicit-length-check.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ function create(context) {
147147
return;
148148
}
149149

150-
const staticValue = getStaticValue(lengthNode, context.getScope());
150+
const staticValue = getStaticValue(lengthNode, sourceCode.getScope(lengthNode));
151151
if (staticValue && (!Number.isInteger(staticValue.value) || staticValue.value < 0)) {
152152
// Ignore known, non-positive-integer length properties.
153153
return;

rules/import-style.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ const create = context => {
167167
),
168168
);
169169

170+
const sourceCode = context.getSourceCode();
171+
170172
const report = (node, moduleName, actualImportStyles, allowedImportStyles, isRequire = false) => {
171173
if (!allowedImportStyles || allowedImportStyles.size === 0) {
172174
return;
@@ -206,7 +208,7 @@ const create = context => {
206208
...visitor,
207209

208210
ImportDeclaration(node) {
209-
const moduleName = getStringIfConstant(node.source, context.getScope());
211+
const moduleName = getStringIfConstant(node.source, sourceCode.getScope(node.source));
210212

211213
const allowedImportStyles = styles.get(moduleName);
212214
const actualImportStyles = getActualImportDeclarationStyles(node);
@@ -221,7 +223,7 @@ const create = context => {
221223
...visitor,
222224

223225
'ExpressionStatement > ImportExpression'(node) {
224-
const moduleName = getStringIfConstant(node.source, context.getScope());
226+
const moduleName = getStringIfConstant(node.source, sourceCode.getScope(node.source));
225227
const allowedImportStyles = styles.get(moduleName);
226228
const actualImportStyles = ['unassigned'];
227229

@@ -231,7 +233,7 @@ const create = context => {
231233
[assignedDynamicImportSelector](node) {
232234
const assignmentTargetNode = node.id;
233235
const moduleNameNode = node.init.argument.source;
234-
const moduleName = getStringIfConstant(moduleNameNode, context.getScope());
236+
const moduleName = getStringIfConstant(moduleNameNode, sourceCode.getScope(moduleNameNode));
235237

236238
if (!moduleName) {
237239
return;
@@ -250,7 +252,7 @@ const create = context => {
250252
...visitor,
251253

252254
ExportAllDeclaration(node) {
253-
const moduleName = getStringIfConstant(node.source, context.getScope());
255+
const moduleName = getStringIfConstant(node.source, sourceCode.getScope(node.source));
254256

255257
const allowedImportStyles = styles.get(moduleName);
256258
const actualImportStyles = ['namespace'];
@@ -259,7 +261,7 @@ const create = context => {
259261
},
260262

261263
ExportNamedDeclaration(node) {
262-
const moduleName = getStringIfConstant(node.source, context.getScope());
264+
const moduleName = getStringIfConstant(node.source, sourceCode.getScope(node.source));
263265

264266
const allowedImportStyles = styles.get(moduleName);
265267
const actualImportStyles = getActualExportDeclarationStyles(node);
@@ -274,7 +276,7 @@ const create = context => {
274276
...visitor,
275277

276278
[`ExpressionStatement > ${callExpressionSelector({name: 'require', argumentsLength: 1})}.expression`](node) {
277-
const moduleName = getStringIfConstant(node.arguments[0], context.getScope());
279+
const moduleName = getStringIfConstant(node.arguments[0], sourceCode.getScope(node.arguments[0]));
278280
const allowedImportStyles = styles.get(moduleName);
279281
const actualImportStyles = ['unassigned'];
280282

@@ -284,7 +286,7 @@ const create = context => {
284286
[assignedRequireSelector](node) {
285287
const assignmentTargetNode = node.id;
286288
const moduleNameNode = node.init.arguments[0];
287-
const moduleName = getStringIfConstant(moduleNameNode, context.getScope());
289+
const moduleName = getStringIfConstant(moduleNameNode, sourceCode.getScope(moduleNameNode));
288290

289291
if (!moduleName) {
290292
return;

rules/new-for-builtins.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ const create = context => {
6262
});
6363

6464
return {
65-
* 'Program:exit'() {
66-
const scope = context.getScope();
65+
* 'Program:exit'(program) {
66+
const scope = sourceCode.getScope(program);
6767

6868
yield * newExpressionTracker.track(scope);
6969
yield * callExpressionTracker.track(scope);

rules/no-array-for-each.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ const create = context => {
393393
functionStack.push(node);
394394
functionInfo.set(node, {
395395
returnStatements: [],
396-
scope: context.getScope(),
396+
scope: sourceCode.getScope(node),
397397
});
398398
},
399399
':function:exit'() {
@@ -414,7 +414,7 @@ const create = context => {
414414

415415
callExpressions.push({
416416
node,
417-
scope: context.getScope(),
417+
scope: sourceCode.getScope(node),
418418
});
419419
},
420420
* 'Program:exit'() {

rules/no-for-loop.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ const create = context => {
280280

281281
const arrayIdentifierName = arrayIdentifier.name;
282282

283-
const scope = context.getScope();
283+
const scope = sourceCode.getScope(node);
284284
const staticResult = getStaticValue(arrayIdentifier, scope);
285285
if (staticResult && !Array.isArray(staticResult.value)) {
286286
// Bail out if we can tell that the array variable has a non-array value (i.e. we're looping through the characters of a string constant).

rules/no-new-array.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,14 @@ function getProblem(context, node) {
5050
}
5151

5252
const fromLengthText = `Array.from(${text === 'length' ? '{length}' : `{length: ${text}}`})`;
53-
if (isNumber(argumentNode, context.getScope())) {
53+
const scope = sourceCode.getScope(node);
54+
if (isNumber(argumentNode, scope)) {
5455
problem.fix = fixer => fixer.replaceText(node, fromLengthText);
5556
return problem;
5657
}
5758

5859
const onlyElementText = `${maybeSemiColon}[${text}]`;
59-
const result = getStaticValue(argumentNode, context.getScope());
60+
const result = getStaticValue(argumentNode, scope);
6061
if (result !== null && typeof result.value !== 'number') {
6162
problem.fix = fixer => fixer.replaceText(node, onlyElementText);
6263
return problem;

rules/no-new-buffer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ const create = context => {
5555
const sourceCode = context.getSourceCode();
5656
return {
5757
[newExpressionSelector('Buffer')](node) {
58-
const method = inferMethod(node.arguments, context.getScope());
58+
const method = inferMethod(node.arguments, sourceCode.getScope(node));
5959

6060
if (method) {
6161
return {

rules/no-thenable.js

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ const messages = {
1111
[MESSAGE_ID_CLASS]: 'Do not add `then` to a class.',
1212
};
1313

14-
const isStringThen = (node, context) =>
15-
getStaticValue(node, context.getScope())?.value === 'then';
14+
const isStringThen = (node, sourceCode) =>
15+
getStaticValue(node, sourceCode.getScope(node))?.value === 'then';
1616

1717
const cases = [
1818
// `{then() {}}`,
@@ -21,7 +21,7 @@ const cases = [
2121
// `{get [computedKey]() {}}`,
2222
{
2323
selector: 'ObjectExpression > Property.properties > .key',
24-
test: (node, context) => getPropertyName(node.parent, context.getScope()) === 'then',
24+
test: (node, sourceCode) => getPropertyName(node.parent, sourceCode.getScope(node.parent)) === 'then',
2525
messageId: MESSAGE_ID_OBJECT,
2626
},
2727
// `class Foo {then}`,
@@ -30,14 +30,14 @@ const cases = [
3030
// `class Foo {static get then() {}}`,
3131
{
3232
selector: ':matches(PropertyDefinition, MethodDefinition) > .key',
33-
test: (node, context) => getPropertyName(node.parent, context.getScope()) === 'then',
33+
test: (node, sourceCode) => getPropertyName(node.parent, sourceCode.getScope(node.parent)) === 'then',
3434
messageId: MESSAGE_ID_CLASS,
3535
},
3636
// `foo.then = …`
3737
// `foo[computedKey] = …`
3838
{
3939
selector: 'AssignmentExpression > MemberExpression.left > .property',
40-
test: (node, context) => getPropertyName(node.parent, context.getScope()) === 'then',
40+
test: (node, sourceCode) => getPropertyName(node.parent, sourceCode.getScope(node.parent)) === 'then',
4141
messageId: MESSAGE_ID_OBJECT,
4242
},
4343
// `Object.defineProperty(foo, 'then', …)`
@@ -84,31 +84,35 @@ const cases = [
8484
{
8585
selector: 'ExportNamedDeclaration > VariableDeclaration.declaration',
8686
messageId: MESSAGE_ID_EXPORT,
87-
getNodes: (node, context) => context.getSourceCode().getDeclaredVariables(node).flatMap(({name, identifiers}) => name === 'then' ? identifiers : []),
87+
getNodes: (node, sourceCode) => sourceCode.getDeclaredVariables(node).flatMap(({name, identifiers}) => name === 'then' ? identifiers : []),
8888
},
8989
];
9090

9191
/** @param {import('eslint').Rule.RuleContext} context */
92-
const create = context => Object.fromEntries(
93-
cases.map(({selector, test, messageId, getNodes}) => [
94-
selector,
95-
function * (node) {
96-
if (getNodes) {
97-
for (const problematicNode of getNodes(node, context)) {
98-
yield {node: problematicNode, messageId};
99-
}
92+
const create = context => {
93+
const sourceCode = context.getSourceCode();
10094

101-
return;
102-
}
95+
return Object.fromEntries(
96+
cases.map(({selector, test, messageId, getNodes}) => [
97+
selector,
98+
function * (node) {
99+
if (getNodes) {
100+
for (const problematicNode of getNodes(node, sourceCode)) {
101+
yield {node: problematicNode, messageId};
102+
}
103103

104-
if (test && !test(node, context)) {
105-
return;
106-
}
104+
return;
105+
}
107106

108-
yield {node, messageId};
109-
},
110-
]),
111-
);
107+
if (test && !test(node, sourceCode)) {
108+
return;
109+
}
110+
111+
yield {node, messageId};
112+
},
113+
]),
114+
);
115+
};
112116

113117
/** @type {import('eslint').Rule.RuleModule} */
114118
module.exports = {

rules/no-typeof-undefined.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const create = context => {
3535
checkGlobalVariables: false,
3636
...context.options[0],
3737
};
38+
const sourceCode = context.getSourceCode();
3839

3940
return {
4041
[selector](binaryExpression) {
@@ -45,13 +46,12 @@ const create = context => {
4546

4647
const valueNode = typeofNode.argument;
4748
const isGlobalVariable = valueNode.type === 'Identifier'
48-
&& !isShadowed(context.getScope(), valueNode);
49+
&& !isShadowed(sourceCode.getScope(valueNode), valueNode);
4950

5051
if (!checkGlobalVariables && isGlobalVariable) {
5152
return;
5253
}
5354

54-
const sourceCode = context.getSourceCode();
5555
const [typeofToken, secondToken] = sourceCode.getFirstTokens(typeofNode, 2);
5656

5757
const fix = function * (fixer) {

rules/no-unused-properties.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ const isUnusedVariable = variable => {
8080

8181
/** @param {import('eslint').Rule.RuleContext} context */
8282
const create = context => {
83+
const sourceCode = context.getSourceCode();
8384
const getPropertyDisplayName = property => {
8485
if (property.key.type === 'Identifier') {
8586
return property.key.name;
@@ -89,7 +90,7 @@ const create = context => {
8990
return property.key.value;
9091
}
9192

92-
return context.getSourceCode().getText(property.key);
93+
return sourceCode.getText(property.key);
9394
};
9495

9596
const checkProperty = (property, references, path) => {
@@ -211,8 +212,8 @@ const create = context => {
211212
};
212213

213214
return {
214-
'Program:exit'() {
215-
const scopes = getScopes(context.getScope());
215+
'Program:exit'(program) {
216+
const scopes = getScopes(sourceCode.getScope(program));
216217
for (const scope of scopes) {
217218
if (scope.type === 'global') {
218219
continue;

rules/no-useless-undefined.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,11 @@ const isFunctionBindCall = node =>
108108

109109
/** @param {import('eslint').Rule.RuleContext} context */
110110
const create = context => {
111+
const sourceCode = context.getSourceCode();
112+
111113
const listener = (fix, checkFunctionReturnType) => node => {
112114
if (checkFunctionReturnType) {
113-
const functionNode = getFunction(context.getScope());
115+
const functionNode = getFunction(sourceCode.getScope(node));
114116
if (functionNode?.returnType) {
115117
return;
116118
}
@@ -123,7 +125,6 @@ const create = context => {
123125
};
124126
};
125127

126-
const sourceCode = context.getSourceCode();
127128
const options = {
128129
checkArguments: true,
129130
...context.options[0],

rules/prefer-array-find.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ const create = context => {
304304
};
305305
},
306306
[filterVariableSelector](node) {
307-
const scope = context.getScope();
307+
const scope = sourceCode.getScope(node);
308308
const variable = findVariable(scope, node.id);
309309
const identifiers = getVariableIdentifiers(variable).filter(identifier => identifier !== node.id);
310310

rules/prefer-at.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ function create(context) {
162162
}
163163

164164
// Only if we are sure it's an positive integer
165-
const staticValue = getStaticValue(indexNode, context.getScope());
165+
const staticValue = getStaticValue(indexNode, sourceCode.getScope(indexNode));
166166
if (!staticValue || !Number.isInteger(staticValue.value) || staticValue.value < 0) {
167167
return;
168168
}

rules/prefer-default-parameters.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ const create = context => {
148148
return;
149149
}
150150

151-
const variable = findVariable(context.getScope(), secondId);
151+
const variable = findVariable(sourceCode.getScope(node), secondId);
152152

153153
// This was reported https://github.com/sindresorhus/eslint-plugin-unicorn/issues/1122
154154
// But can't reproduce, just ignore this case

rules/prefer-json-parse-buffer.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ function isUtf8Encoding(node, scope) {
108108
/** @param {import('eslint').Rule.RuleContext} context */
109109
const create = context => ({
110110
[jsonParseArgumentSelector](node) {
111-
const scope = context.getScope();
111+
const sourceCode = context.getSourceCode();
112+
const scope = sourceCode.getScope(node);
112113
node = getIdentifierDeclaration(node, scope);
113114
if (
114115
!(
@@ -137,7 +138,7 @@ const create = context => ({
137138
return {
138139
node: charsetNode,
139140
messageId: MESSAGE_ID,
140-
fix: fixer => removeArgument(fixer, charsetNode, context.getSourceCode()),
141+
fix: fixer => removeArgument(fixer, charsetNode, sourceCode),
141142
};
142143
},
143144
});

0 commit comments

Comments
 (0)