Skip to content

Commit 7c61e9a

Browse files
committed
fix: use sourceCode.getScope() when available
1 parent c5bfa68 commit 7c61e9a

14 files changed

+47
-47
lines changed

lib/rules/fixer-return.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,10 @@ module.exports = {
8080
// An empty array is not a fix.
8181
return false;
8282
}
83-
84-
const staticValue = getStaticValue(node, context.getScope());
83+
const scope =
84+
(context.sourceCode || context.getSourceCode())?.getScope(node) ||
85+
context.getScope(); // TODO: just use sourceCode.getScope() when we drop support for ESLint < 9.0.0
86+
const staticValue = getStaticValue(node, scope);
8587
if (!staticValue) {
8688
// If we can't find a static value, assume it's a real fix value.
8789
return true;

lib/rules/no-missing-message-ids.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ module.exports = {
8080
val.value,
8181
ruleInfo,
8282
scopeManager,
83-
context.getScope()
83+
sourceCode.getScope(node)
8484
)
8585
)
8686
// Couldn't find this messageId in `meta.messages`.

lib/rules/no-missing-placeholders.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ module.exports = {
4848
contextIdentifiers = utils.getContextIdentifiers(scopeManager, ast);
4949
},
5050
CallExpression(node) {
51+
const scope = sourceCode.getScope(node) || context.getScope(); // TODO: just use sourceCode.getScope() when we drop support for ESLint < 9.0.0
5152
if (
5253
node.callee.type === 'MemberExpression' &&
5354
contextIdentifiers.has(node.callee.object) &&
@@ -75,7 +76,7 @@ module.exports = {
7576
obj.messageId.value,
7677
ruleInfo,
7778
scopeManager,
78-
context.getScope()
79+
scope
7980
);
8081
if (correspondingMessage) {
8182
obj.message = correspondingMessage.value;
@@ -89,10 +90,7 @@ module.exports = {
8990
messageId,
9091
data,
9192
} of reportMessagesAndDataArray.filter((obj) => obj.message)) {
92-
const messageStaticValue = getStaticValue(
93-
message,
94-
context.getScope()
95-
);
93+
const messageStaticValue = getStaticValue(message, scope);
9694
if (
9795
((message.type === 'Literal' &&
9896
typeof message.value === 'string') ||

lib/rules/no-only-tests.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ module.exports = {
5151
{
5252
messageId: 'removeOnly',
5353
*fix(fixer) {
54-
const sourceCode = context.sourceCode || context.getSourceCode();
54+
const sourceCode =
55+
context.sourceCode || context.getSourceCode();
5556

5657
const tokenBefore =
5758
sourceCode.getTokenBefore(onlyProperty);

lib/rules/no-unused-message-ids.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ module.exports = {
4747
contextIdentifiers = utils.getContextIdentifiers(scopeManager, ast);
4848
},
4949

50-
'Program:exit'() {
50+
'Program:exit'(ast) {
5151
if (hasSeenUnknownMessageId || !hasSeenViolationReport) {
5252
/*
5353
Bail out when the rule is likely to have false positives.
@@ -57,9 +57,10 @@ module.exports = {
5757
return;
5858
}
5959

60+
const scope = sourceCode.getScope(ast);
61+
6062
const messageIdNodesUnused = messageIdNodes.filter(
61-
(node) =>
62-
!messageIdsUsed.has(utils.getKeyName(node, context.getScope()))
63+
(node) => !messageIdsUsed.has(utils.getKeyName(node, scope))
6364
);
6465

6566
// Report any messageIds that were never used.
@@ -68,7 +69,7 @@ module.exports = {
6869
node: messageIdNode,
6970
messageId: 'unusedMessage',
7071
data: {
71-
messageId: utils.getKeyName(messageIdNode, context.getScope()),
72+
messageId: utils.getKeyName(messageIdNode, scope),
7273
},
7374
});
7475
}

lib/rules/no-unused-placeholders.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ module.exports = {
7474
obj.messageId.value,
7575
ruleInfo,
7676
scopeManager,
77-
context.getScope()
77+
sourceCode.getScope(node)
7878
);
7979
if (correspondingMessage) {
8080
obj.message = correspondingMessage.value;
@@ -88,7 +88,7 @@ module.exports = {
8888
)) {
8989
const messageStaticValue = getStaticValue(
9090
message,
91-
context.getScope()
91+
sourceCode.getScope(node)
9292
);
9393
if (
9494
((message.type === 'Literal' &&

lib/rules/prefer-message-ids.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ module.exports = {
4343

4444
return {
4545
Program(ast) {
46+
const scope = sourceCode.getScope(ast) || context.getScope(); // TODO: just use sourceCode.getScope() when we drop support for ESLint < v9.0.0
4647
contextIdentifiers = utils.getContextIdentifiers(
4748
sourceCode.scopeManager,
4849
ast
@@ -64,10 +65,7 @@ module.exports = {
6465
return;
6566
}
6667

67-
const staticValue = getStaticValue(
68-
messagesNode.value,
69-
context.getScope()
70-
);
68+
const staticValue = getStaticValue(messagesNode.value, scope);
7169
if (!staticValue) {
7270
return;
7371
}

lib/rules/report-message-format.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ module.exports = {
3838
* @param {ASTNode} message The message AST node
3939
* @returns {void}
4040
*/
41-
function processMessageNode(message) {
42-
const staticValue = getStaticValue(message, context.getScope());
41+
function processMessageNode(message, scope) {
42+
const staticValue = getStaticValue(message, scope);
4343
if (
4444
(message.type === 'Literal' &&
4545
typeof message.value === 'string' &&
@@ -69,6 +69,7 @@ module.exports = {
6969

7070
return {
7171
Program(ast) {
72+
const scope = sourceCode.getScope(ast) || context.getScope(); // TODO: just use sourceCode.getScope() when we drop support for ESLint < v9.0.0
7273
contextIdentifiers = utils.getContextIdentifiers(
7374
sourceCode.scopeManager,
7475
ast
@@ -93,9 +94,10 @@ module.exports = {
9394
messagesObject.value.properties
9495
.filter((prop) => prop.type === 'Property')
9596
.map((prop) => prop.value)
96-
.forEach(processMessageNode);
97+
.forEach((it) => processMessageNode(it, scope));
9798
},
9899
CallExpression(node) {
100+
const scope = sourceCode.getScope(node) || context.getScope(); // TODO: just use sourceCode.getScope() when we drop support for ESLint < v9.0.0
99101
if (
100102
node.callee.type === 'MemberExpression' &&
101103
contextIdentifiers.has(node.callee.object) &&
@@ -107,7 +109,7 @@ module.exports = {
107109
const suggest = reportInfo && reportInfo.suggest;
108110

109111
if (message) {
110-
processMessageNode(message);
112+
processMessageNode(message, scope);
111113
}
112114

113115
if (suggest && suggest.type === 'ArrayExpression') {
@@ -122,7 +124,7 @@ module.exports = {
122124
prop.key.name === 'message'
123125
)
124126
.map((prop) => prop.value)
125-
.forEach(processMessageNode);
127+
.forEach((it) => processMessageNode(it, scope));
126128
}
127129
}
128130
},

lib/rules/require-meta-docs-description.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ module.exports = {
4949
}
5050

5151
return {
52-
Program() {
52+
Program(ast) {
53+
const scope = sourceCode.getScope(ast) || context.getScope(); // TODO: just use sourceCode.getScope() when we drop support for ESLint < v9.0.0
5354
const { scopeManager } = sourceCode;
5455

5556
const pattern =
@@ -77,10 +78,7 @@ module.exports = {
7778
return;
7879
}
7980

80-
const staticValue = getStaticValue(
81-
descriptionNode.value,
82-
context.getScope()
83-
);
81+
const staticValue = getStaticValue(descriptionNode.value, scope);
8482
if (!staticValue) {
8583
// Ignore non-static values since we can't determine what they look like.
8684
return;

lib/rules/require-meta-docs-url.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ module.exports = {
7979
}
8080

8181
return {
82-
Program() {
82+
Program(ast) {
83+
const scope = sourceCode.getScope(ast) || context.getScope(); // TODO: just use sourceCode.getScope() when we drop support for ESLint < v9.0.0
8384
const { scopeManager } = sourceCode;
8485

8586
const metaNode = ruleInfo.meta;
@@ -94,7 +95,7 @@ module.exports = {
9495
.find((p) => p.type === 'Property' && util.getKeyName(p) === 'url');
9596

9697
const staticValue = urlPropNode
97-
? getStaticValue(urlPropNode.value, context.getScope())
98+
? getStaticValue(urlPropNode.value, scope)
9899
: undefined;
99100
if (urlPropNode && !staticValue) {
100101
// Ignore non-static values since we can't determine what they look like.

lib/rules/require-meta-fixable.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -76,18 +76,16 @@ module.exports = {
7676
usesFixFunctions = true;
7777
}
7878
},
79-
'Program:exit'() {
79+
'Program:exit'(ast) {
80+
const scope = sourceCode.getScope(ast) || context.getScope(); // TODO: just use sourceCode.getScope() when we drop support for ESLint < v9.0.0
8081
const metaFixableProp =
8182
ruleInfo &&
8283
utils
8384
.evaluateObjectProperties(ruleInfo.meta, scopeManager)
8485
.find((prop) => utils.getKeyName(prop) === 'fixable');
8586

8687
if (metaFixableProp) {
87-
const staticValue = getStaticValue(
88-
metaFixableProp.value,
89-
context.getScope()
90-
);
88+
const staticValue = getStaticValue(metaFixableProp.value, scope);
9189
if (!staticValue) {
9290
// Ignore non-static values since we can't determine what they look like.
9391
return;

lib/rules/require-meta-has-suggestions.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ module.exports = {
4444
* @returns {boolean} whether this property should be considered to contain suggestions
4545
*/
4646
function doesPropertyContainSuggestions(node) {
47-
const staticValue = getStaticValue(node.value, context.getScope());
47+
const scope = sourceCode.getScope(node) || context.getScope(); // TODO: just use sourceCode.getScope() when we drop support for ESLint < v9.0.0
48+
const staticValue = getStaticValue(node.value, scope);
4849
if (
4950
!staticValue ||
5051
(Array.isArray(staticValue.value) && staticValue.value.length > 0) ||

lib/rules/require-meta-type.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ module.exports = {
4545
}
4646

4747
return {
48-
Program() {
48+
Program(node) {
49+
const scope = sourceCode.getScope(node) || context.getScope(); // TODO: just use sourceCode.getScope() when we drop support for ESLint < v9.0.0
4950
const { scopeManager } = sourceCode;
5051

5152
const metaNode = ruleInfo.meta;
@@ -61,7 +62,7 @@ module.exports = {
6162
return;
6263
}
6364

64-
const staticValue = getStaticValue(typeNode.value, context.getScope());
65+
const staticValue = getStaticValue(typeNode.value, scope);
6566
if (!staticValue) {
6667
// Ignore non-static values since we can't determine what they look like.
6768
return;

lib/utils.js

+7-8
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ module.exports = {
584584
* @param {Context} context
585585
*/
586586
getReportInfo(node, context) {
587-
const reportArgs = node.arguments
587+
const reportArgs = node.arguments;
588588

589589
// If there is exactly one argument, the API expects an object.
590590
// Otherwise, if the second argument is a string, the arguments are interpreted as
@@ -610,11 +610,11 @@ module.exports = {
610610
}
611611

612612
let keys;
613+
const scope =
614+
(context.sourceCode || context.getSourceCode()).getScope?.(node) ||
615+
context.getScope(); // TODO: just use sourceCode.getScope() when dropping eslint < v9
616+
const secondArgStaticValue = getStaticValue(reportArgs[1], scope);
613617

614-
const secondArgStaticValue = getStaticValue(
615-
reportArgs[1],
616-
(context.sourceCode || context.getSourceCode()).getScope?.(node) || context.getScope() // TODO: just use sourceCode.getScope() when dropping eslint < v9
617-
);
618618
if (
619619
(secondArgStaticValue &&
620620
typeof secondArgStaticValue.value === 'string') ||
@@ -768,9 +768,8 @@ module.exports = {
768768
) &&
769769
parent.parent.parent.parent.parent.parent.callee.property.name ===
770770
'report' &&
771-
module.exports.getReportInfo(
772-
parent.parent.parent.parent.parent.parent
773-
).suggest === parent.parent.parent
771+
module.exports.getReportInfo(parent.parent.parent.parent.parent.parent)
772+
.suggest === parent.parent.parent
774773
);
775774
},
776775

0 commit comments

Comments
 (0)