Skip to content

Commit 35e14cd

Browse files
authored
feat: preparing for eslint v9 (#400)
1 parent b8ed4ca commit 35e14cd

28 files changed

+133
-98
lines changed

Diff for: lib/rules/fixer-return.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,13 @@ module.exports = {
7676
* @returns {boolean}
7777
*/
7878
function isFix(node) {
79+
const sourceCode = context.sourceCode || context.getSourceCode(); // TODO: use context.sourceCode when dropping eslint < v9
7980
if (node.type === 'ArrayExpression' && node.elements.length === 0) {
8081
// An empty array is not a fix.
8182
return false;
8283
}
83-
84-
const staticValue = getStaticValue(node, context.getScope());
84+
const scope = sourceCode.getScope?.(node) || context.getScope(); // TODO: just use sourceCode.getScope() when we drop support for ESLint < 9.0.0
85+
const staticValue = getStaticValue(node, scope);
8586
if (!staticValue) {
8687
// If we can't find a static value, assume it's a real fix value.
8788
return true;
@@ -98,7 +99,7 @@ module.exports = {
9899

99100
return {
100101
Program(ast) {
101-
const sourceCode = context.getSourceCode();
102+
const sourceCode = context.sourceCode || context.getSourceCode(); // TODO: just use context.sourceCode when dropping eslint < v9
102103
contextIdentifiers = utils.getContextIdentifiers(
103104
sourceCode.scopeManager,
104105
ast
@@ -148,7 +149,8 @@ module.exports = {
148149
// Ensure the current (arrow) fixer function returned a fix.
149150
'ArrowFunctionExpression:exit'(node) {
150151
if (funcInfo.shouldCheck) {
151-
const loc = context.getSourceCode().getTokenBefore(node.body).loc; // Show violation on arrow (=>).
152+
const sourceCode = context.sourceCode || context.getSourceCode();
153+
const loc = sourceCode.getTokenBefore(node.body).loc; // Show violation on arrow (=>).
152154
if (node.expression) {
153155
// When the return is implied (no curly braces around the body), we have to check the single body node directly.
154156
if (!isFix(node.body)) {

Diff for: lib/rules/meta-property-ordering.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ module.exports = {
3434
},
3535

3636
create(context) {
37-
const sourceCode = context.getSourceCode();
37+
const sourceCode = context.sourceCode || context.getSourceCode(); // TODO: just use context.sourceCode when dropping eslint < v9
3838
const ruleInfo = getRuleInfo(sourceCode);
3939
if (!ruleInfo) {
4040
return {};

Diff for: lib/rules/no-deprecated-context-methods.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ module.exports = {
5454
},
5555

5656
create(context) {
57-
const sourceCode = context.getSourceCode();
57+
const sourceCode = context.sourceCode || context.getSourceCode(); // TODO: just use context.sourceCode when dropping eslint < v9
5858

5959
// ----------------------------------------------------------------------
6060
// Public

Diff for: lib/rules/no-deprecated-report-api.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ module.exports = {
3030
},
3131

3232
create(context) {
33-
const sourceCode = context.getSourceCode();
33+
const sourceCode = context.sourceCode || context.getSourceCode(); // TODO: just use context.sourceCode when dropping eslint < v9
3434
let contextIdentifiers;
3535

3636
// ----------------------------------------------------------------------
@@ -60,7 +60,7 @@ module.exports = {
6060
fix(fixer) {
6161
const openingParen = sourceCode.getTokenBefore(node.arguments[0]);
6262
const closingParen = sourceCode.getLastToken(node);
63-
const reportInfo = utils.getReportInfo(node.arguments, context);
63+
const reportInfo = utils.getReportInfo(node, context);
6464

6565
if (!reportInfo) {
6666
return null;

Diff for: lib/rules/no-identical-tests.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ module.exports = {
3232
// ----------------------------------------------------------------------
3333
// Public
3434
// ----------------------------------------------------------------------
35-
const sourceCode = context.getSourceCode();
35+
const sourceCode = context.sourceCode || context.getSourceCode(); // TODO: just use context.sourceCode when dropping eslint < v9
3636

3737
// ----------------------------------------------------------------------
3838
// Helpers

Diff for: lib/rules/no-missing-message-ids.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ module.exports = {
2626
},
2727

2828
create(context) {
29-
const sourceCode = context.getSourceCode();
29+
const sourceCode = context.sourceCode || context.getSourceCode(); // TODO: just use context.sourceCode when dropping eslint < v9
3030
const { scopeManager } = sourceCode;
3131
const ruleInfo = utils.getRuleInfo(sourceCode);
3232
if (!ruleInfo) {
@@ -48,14 +48,15 @@ module.exports = {
4848
},
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
// Check for messageId properties used in known calls to context.report();
5253
if (
5354
node.callee.type === 'MemberExpression' &&
5455
contextIdentifiers.has(node.callee.object) &&
5556
node.callee.property.type === 'Identifier' &&
5657
node.callee.property.name === 'report'
5758
) {
58-
const reportInfo = utils.getReportInfo(node.arguments, context);
59+
const reportInfo = utils.getReportInfo(node, context);
5960
if (!reportInfo) {
6061
return;
6162
}
@@ -80,7 +81,7 @@ module.exports = {
8081
val.value,
8182
ruleInfo,
8283
scopeManager,
83-
context.getScope()
84+
scope
8485
)
8586
)
8687
// Couldn't find this messageId in `meta.messages`.

Diff for: lib/rules/no-missing-placeholders.js

+5-7
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ module.exports = {
3131
},
3232

3333
create(context) {
34-
const sourceCode = context.getSourceCode();
34+
const sourceCode = context.sourceCode || context.getSourceCode(); // TODO: just use context.sourceCode when dropping eslint < v9
3535
const { scopeManager } = sourceCode;
3636

3737
let contextIdentifiers;
@@ -48,13 +48,14 @@ 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) &&
5455
node.callee.property.type === 'Identifier' &&
5556
node.callee.property.name === 'report'
5657
) {
57-
const reportInfo = utils.getReportInfo(node.arguments, context);
58+
const reportInfo = utils.getReportInfo(node, context);
5859
if (!reportInfo) {
5960
return;
6061
}
@@ -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') ||

Diff for: 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.getSourceCode();
54+
const sourceCode =
55+
context.sourceCode || context.getSourceCode(); // TODO: just use context.sourceCode when dropping eslint < v9
5556

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

Diff for: lib/rules/no-unused-message-ids.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ module.exports = {
2424
},
2525

2626
create(context) {
27-
const sourceCode = context.getSourceCode();
27+
const sourceCode = context.sourceCode || context.getSourceCode(); // TODO: just use context.sourceCode when dropping eslint < v9
2828
const { scopeManager } = sourceCode;
2929
const ruleInfo = utils.getRuleInfo(sourceCode);
3030
if (!ruleInfo) {
@@ -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) || context.getScope(); // TODO: just use sourceCode.getScope() when we drop support for ESLint < 9.0.0
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
}
@@ -82,7 +83,7 @@ module.exports = {
8283
node.callee.property.type === 'Identifier' &&
8384
node.callee.property.name === 'report'
8485
) {
85-
const reportInfo = utils.getReportInfo(node.arguments, context);
86+
const reportInfo = utils.getReportInfo(node, context);
8687
if (!reportInfo) {
8788
return;
8889
}

Diff for: lib/rules/no-unused-placeholders.js

+5-7
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ module.exports = {
3131
},
3232

3333
create(context) {
34-
const sourceCode = context.getSourceCode();
34+
const sourceCode = context.sourceCode || context.getSourceCode(); // TODO: just use context.sourceCode when dropping eslint < v9
3535
const { scopeManager } = sourceCode;
3636

3737
let contextIdentifiers;
@@ -47,13 +47,14 @@ module.exports = {
4747
contextIdentifiers = utils.getContextIdentifiers(scopeManager, ast);
4848
},
4949
CallExpression(node) {
50+
const scope = sourceCode.getScope?.(node) || context.getScope(); // TODO: just use sourceCode.getScope() when we drop support for ESLint < 9.0.0
5051
if (
5152
node.callee.type === 'MemberExpression' &&
5253
contextIdentifiers.has(node.callee.object) &&
5354
node.callee.property.type === 'Identifier' &&
5455
node.callee.property.name === 'report'
5556
) {
56-
const reportInfo = utils.getReportInfo(node.arguments, context);
57+
const reportInfo = utils.getReportInfo(node, context);
5758
if (!reportInfo) {
5859
return;
5960
}
@@ -74,7 +75,7 @@ module.exports = {
7475
obj.messageId.value,
7576
ruleInfo,
7677
scopeManager,
77-
context.getScope()
78+
scope
7879
);
7980
if (correspondingMessage) {
8081
obj.message = correspondingMessage.value;
@@ -86,10 +87,7 @@ module.exports = {
8687
for (const { message, data } of reportMessagesAndDataArray.filter(
8788
(obj) => obj.message
8889
)) {
89-
const messageStaticValue = getStaticValue(
90-
message,
91-
context.getScope()
92-
);
90+
const messageStaticValue = getStaticValue(message, scope);
9391
if (
9492
((message.type === 'Literal' &&
9593
typeof message.value === 'string') ||

Diff for: lib/rules/no-useless-token-range.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ module.exports = {
3030
},
3131

3232
create(context) {
33-
const sourceCode = context.getSourceCode();
33+
const sourceCode = context.sourceCode || context.getSourceCode(); // TODO: just use context.sourceCode when dropping eslint < v9
3434

3535
// ----------------------------------------------------------------------
3636
// Helpers

Diff for: lib/rules/prefer-message-ids.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ module.exports = {
2929
},
3030

3131
create(context) {
32-
const sourceCode = context.getSourceCode();
32+
const sourceCode = context.sourceCode || context.getSourceCode(); // TODO: just use context.sourceCode when dropping eslint < v9
3333
const ruleInfo = utils.getRuleInfo(sourceCode);
3434
if (!ruleInfo) {
3535
return {};
@@ -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
}
@@ -90,7 +88,7 @@ module.exports = {
9088
node.callee.property.type === 'Identifier' &&
9189
node.callee.property.name === 'report'
9290
) {
93-
const reportInfo = utils.getReportInfo(node.arguments, context);
91+
const reportInfo = utils.getReportInfo(node, context);
9492
if (!reportInfo) {
9593
return;
9694
}

Diff for: lib/rules/prefer-object-rule.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ module.exports = {
3232
// Public
3333
// ----------------------------------------------------------------------
3434

35-
const sourceCode = context.getSourceCode();
35+
const sourceCode = context.sourceCode || context.getSourceCode(); // TODO: just use context.sourceCode when dropping eslint < v9
3636
const ruleInfo = utils.getRuleInfo(sourceCode);
3737
if (!ruleInfo) {
3838
return {};

Diff for: lib/rules/prefer-output-null.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ module.exports = {
3535
// Public
3636
// ----------------------------------------------------------------------
3737

38-
const sourceCode = context.getSourceCode();
38+
const sourceCode = context.sourceCode || context.getSourceCode(); // TODO: just use context.sourceCode when dropping eslint < v9
3939

4040
return {
4141
Program(ast) {

Diff for: lib/rules/prefer-placeholders.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ module.exports = {
3333
create(context) {
3434
let contextIdentifiers;
3535

36-
const sourceCode = context.getSourceCode();
36+
const sourceCode = context.sourceCode || context.getSourceCode(); // TODO: just use context.sourceCode when dropping eslint < v9
3737
const { scopeManager } = sourceCode;
3838

3939
// ----------------------------------------------------------------------
@@ -51,7 +51,7 @@ module.exports = {
5151
node.callee.property.type === 'Identifier' &&
5252
node.callee.property.name === 'report'
5353
) {
54-
const reportInfo = utils.getReportInfo(node.arguments, context);
54+
const reportInfo = utils.getReportInfo(node, context);
5555

5656
if (!reportInfo) {
5757
return;

Diff for: lib/rules/prefer-replace-text.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ module.exports = {
3030
},
3131

3232
create(context) {
33-
const sourceCode = context.getSourceCode();
33+
const sourceCode = context.sourceCode || context.getSourceCode(); // TODO: just use context.sourceCode when dropping eslint < v9
3434
let funcInfo = {
3535
upper: null,
3636
codePath: null,

0 commit comments

Comments
 (0)