Skip to content

Commit

Permalink
fix: use sourceCode.getScope() when available
Browse files Browse the repository at this point in the history
  • Loading branch information
aladdin-add committed Dec 8, 2023
1 parent c5bfa68 commit 60ce5c6
Show file tree
Hide file tree
Showing 13 changed files with 40 additions and 29 deletions.
4 changes: 2 additions & 2 deletions lib/rules/fixer-return.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ module.exports = {
// An empty array is not a fix.
return false;
}

const staticValue = getStaticValue(node, context.getScope());
const scope = (context.sourceCode || context.getSourceCode())?.getScope(node) || context.getScope() // TODO: just use sourceCode.getScope() when we drop support for ESLint < 9.0.0

Check failure on line 83 in lib/rules/fixer-return.js

View workflow job for this annotation

GitHub Actions / lint

Replace `·(context.sourceCode·||·context.getSourceCode())?.getScope(node)·||·context.getScope()` with `⏎········(context.sourceCode·||·context.getSourceCode())?.getScope(node)·||⏎········context.getScope();`
const staticValue = getStaticValue(node, scope);
if (!staticValue) {
// If we can't find a static value, assume it's a real fix value.
return true;
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/no-missing-message-ids.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ module.exports = {
val.value,
ruleInfo,
scopeManager,
context.getScope()
sourceCode.getScope(node)
)
)
// Couldn't find this messageId in `meta.messages`.
Expand Down
5 changes: 3 additions & 2 deletions lib/rules/no-missing-placeholders.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ module.exports = {
contextIdentifiers = utils.getContextIdentifiers(scopeManager, ast);
},
CallExpression(node) {
const scope = sourceCode.getScope(node) || context.getScope() // TODO: just use sourceCode.getScope() when we drop support for ESLint < 9.0.0

Check failure on line 51 in lib/rules/no-missing-placeholders.js

View workflow job for this annotation

GitHub Actions / lint

Insert `;`
if (
node.callee.type === 'MemberExpression' &&
contextIdentifiers.has(node.callee.object) &&
Expand Down Expand Up @@ -75,7 +76,7 @@ module.exports = {
obj.messageId.value,
ruleInfo,
scopeManager,
context.getScope()
scope,

Check failure on line 79 in lib/rules/no-missing-placeholders.js

View workflow job for this annotation

GitHub Actions / lint

Delete `,`

Check failure on line 79 in lib/rules/no-missing-placeholders.js

View workflow job for this annotation

GitHub Actions / lint

Unexpected trailing comma
);
if (correspondingMessage) {
obj.message = correspondingMessage.value;
Expand All @@ -91,7 +92,7 @@ module.exports = {
} of reportMessagesAndDataArray.filter((obj) => obj.message)) {
const messageStaticValue = getStaticValue(

Check failure on line 93 in lib/rules/no-missing-placeholders.js

View workflow job for this annotation

GitHub Actions / lint

Replace `⏎··············message,⏎··············scope,⏎············` with `message,·scope`
message,
context.getScope()
scope,

Check failure on line 95 in lib/rules/no-missing-placeholders.js

View workflow job for this annotation

GitHub Actions / lint

Unexpected trailing comma
);
if (
((message.type === 'Literal' &&
Expand Down
8 changes: 5 additions & 3 deletions lib/rules/no-unused-message-ids.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ module.exports = {
contextIdentifiers = utils.getContextIdentifiers(scopeManager, ast);
},

'Program:exit'() {
'Program:exit'(ast) {
if (hasSeenUnknownMessageId || !hasSeenViolationReport) {
/*
Bail out when the rule is likely to have false positives.
Expand All @@ -57,9 +57,11 @@ module.exports = {
return;
}

const scope = sourceCode.getScope(ast)

Check failure on line 60 in lib/rules/no-unused-message-ids.js

View workflow job for this annotation

GitHub Actions / lint

Insert `;`

const messageIdNodesUnused = messageIdNodes.filter(
(node) =>

Check failure on line 63 in lib/rules/no-unused-message-ids.js

View workflow job for this annotation

GitHub Actions / lint

Delete `⏎···········`
!messageIdsUsed.has(utils.getKeyName(node, context.getScope()))
!messageIdsUsed.has(utils.getKeyName(node, scope))
);

// Report any messageIds that were never used.
Expand All @@ -68,7 +70,7 @@ module.exports = {
node: messageIdNode,
messageId: 'unusedMessage',
data: {
messageId: utils.getKeyName(messageIdNode, context.getScope()),
messageId: utils.getKeyName(messageIdNode, scope),
},
});
}
Expand Down
4 changes: 2 additions & 2 deletions lib/rules/no-unused-placeholders.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ module.exports = {
obj.messageId.value,
ruleInfo,
scopeManager,
context.getScope()
sourceCode.getScope(node)
);
if (correspondingMessage) {
obj.message = correspondingMessage.value;
Expand All @@ -88,7 +88,7 @@ module.exports = {
)) {
const messageStaticValue = getStaticValue(
message,
context.getScope()
sourceCode.getScope(node)
);
if (
((message.type === 'Literal' &&
Expand Down
3 changes: 2 additions & 1 deletion lib/rules/prefer-message-ids.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ module.exports = {

return {
Program(ast) {
const scope = sourceCode.getScope(ast) || context.getScope() // TODO: just use sourceCode.getScope() when we drop support for ESLint < v9.0.0

Check failure on line 46 in lib/rules/prefer-message-ids.js

View workflow job for this annotation

GitHub Actions / lint

Insert `;`
contextIdentifiers = utils.getContextIdentifiers(
sourceCode.scopeManager,
ast
Expand All @@ -66,7 +67,7 @@ module.exports = {

const staticValue = getStaticValue(
messagesNode.value,
context.getScope()
scope
);
if (!staticValue) {
return;
Expand Down
13 changes: 8 additions & 5 deletions lib/rules/report-message-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ module.exports = {
* @param {ASTNode} message The message AST node
* @returns {void}
*/
function processMessageNode(message) {
const staticValue = getStaticValue(message, context.getScope());
function processMessageNode(message, scope) {
const staticValue = getStaticValue(message, scope);
if (
(message.type === 'Literal' &&
typeof message.value === 'string' &&
Expand Down Expand Up @@ -69,11 +69,13 @@ module.exports = {

return {
Program(ast) {
const scope = sourceCode.getScope(ast) || context.getScope() // TODO: just use sourceCode.getScope() when we drop support for ESLint < v9.0.0
contextIdentifiers = utils.getContextIdentifiers(
sourceCode.scopeManager,
ast
);


const messagesObject =
ruleInfo &&
ruleInfo.meta &&
Expand All @@ -93,9 +95,10 @@ module.exports = {
messagesObject.value.properties
.filter((prop) => prop.type === 'Property')
.map((prop) => prop.value)
.forEach(processMessageNode);
.forEach(it => processMessageNode(it, scope));
},
CallExpression(node) {
const scope = sourceCode.getScope(node) || context.getScope() // TODO: just use sourceCode.getScope() when we drop support for ESLint < v9.0.0
if (
node.callee.type === 'MemberExpression' &&
contextIdentifiers.has(node.callee.object) &&
Expand All @@ -107,7 +110,7 @@ module.exports = {
const suggest = reportInfo && reportInfo.suggest;

if (message) {
processMessageNode(message);
processMessageNode(message, scope);
}

if (suggest && suggest.type === 'ArrayExpression') {
Expand All @@ -122,7 +125,7 @@ module.exports = {
prop.key.name === 'message'
)
.map((prop) => prop.value)
.forEach(processMessageNode);
.forEach(it => processMessageNode(it, scope));
}
}
},
Expand Down
5 changes: 3 additions & 2 deletions lib/rules/require-meta-docs-description.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ module.exports = {
}

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

const pattern =
Expand Down Expand Up @@ -79,7 +80,7 @@ module.exports = {

const staticValue = getStaticValue(
descriptionNode.value,
context.getScope()
scope,
);
if (!staticValue) {
// Ignore non-static values since we can't determine what they look like.
Expand Down
5 changes: 3 additions & 2 deletions lib/rules/require-meta-docs-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ module.exports = {
}

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

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

const staticValue = urlPropNode
? getStaticValue(urlPropNode.value, context.getScope())
? getStaticValue(urlPropNode.value, scope)
: undefined;
if (urlPropNode && !staticValue) {
// Ignore non-static values since we can't determine what they look like.
Expand Down
6 changes: 4 additions & 2 deletions lib/rules/require-meta-fixable.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ module.exports = {
usesFixFunctions = true;
}
},
'Program:exit'() {
'Program:exit'(ast) {
const sourceCode = context.sourceCode || context.getSourceCode();
const scope = sourceCode.getScope(ast) || context.getScope() // TODO: just use sourceCode.getScope() when we drop support for ESLint < v9.0.0
const metaFixableProp =
ruleInfo &&
utils
Expand All @@ -86,7 +88,7 @@ module.exports = {
if (metaFixableProp) {
const staticValue = getStaticValue(
metaFixableProp.value,
context.getScope()
scope
);
if (!staticValue) {
// Ignore non-static values since we can't determine what they look like.
Expand Down
3 changes: 2 additions & 1 deletion lib/rules/require-meta-has-suggestions.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ module.exports = {
* @returns {boolean} whether this property should be considered to contain suggestions
*/
function doesPropertyContainSuggestions(node) {
const staticValue = getStaticValue(node.value, context.getScope());
const scope = sourceCode.getScope(node) || context.getScope() // TODO: just use sourceCode.getScope() when we drop support for ESLint < v9.0.0
const staticValue = getStaticValue(node.value, scope);
if (
!staticValue ||
(Array.isArray(staticValue.value) && staticValue.value.length > 0) ||
Expand Down
5 changes: 3 additions & 2 deletions lib/rules/require-meta-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ module.exports = {
}

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

const metaNode = ruleInfo.meta;
Expand All @@ -61,7 +62,7 @@ module.exports = {
return;
}

const staticValue = getStaticValue(typeNode.value, context.getScope());
const staticValue = getStaticValue(typeNode.value, scope);
if (!staticValue) {
// Ignore non-static values since we can't determine what they look like.
return;
Expand Down
6 changes: 2 additions & 4 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -610,11 +610,9 @@ module.exports = {
}

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

const secondArgStaticValue = getStaticValue(
reportArgs[1],
(context.sourceCode || context.getSourceCode()).getScope?.(node) || context.getScope() // TODO: just use sourceCode.getScope() when dropping eslint < v9
);
if (
(secondArgStaticValue &&
typeof secondArgStaticValue.value === 'string') ||
Expand Down

0 comments on commit 60ce5c6

Please sign in to comment.