Skip to content

Commit

Permalink
feat: preparing for eslint v9 (#400)
Browse files Browse the repository at this point in the history
  • Loading branch information
aladdin-add committed Dec 11, 2023
1 parent b8ed4ca commit 35e14cd
Show file tree
Hide file tree
Showing 28 changed files with 133 additions and 98 deletions.
10 changes: 6 additions & 4 deletions lib/rules/fixer-return.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,13 @@ module.exports = {
* @returns {boolean}
*/
function isFix(node) {
const sourceCode = context.sourceCode || context.getSourceCode(); // TODO: use context.sourceCode when dropping eslint < v9
if (node.type === 'ArrayExpression' && node.elements.length === 0) {
// An empty array is not a fix.
return false;
}

const staticValue = getStaticValue(node, context.getScope());
const scope = sourceCode.getScope?.(node) || context.getScope(); // TODO: just use sourceCode.getScope() when we drop support for ESLint < 9.0.0
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 All @@ -98,7 +99,7 @@ module.exports = {

return {
Program(ast) {
const sourceCode = context.getSourceCode();
const sourceCode = context.sourceCode || context.getSourceCode(); // TODO: just use context.sourceCode when dropping eslint < v9
contextIdentifiers = utils.getContextIdentifiers(
sourceCode.scopeManager,
ast
Expand Down Expand Up @@ -148,7 +149,8 @@ module.exports = {
// Ensure the current (arrow) fixer function returned a fix.
'ArrowFunctionExpression:exit'(node) {
if (funcInfo.shouldCheck) {
const loc = context.getSourceCode().getTokenBefore(node.body).loc; // Show violation on arrow (=>).
const sourceCode = context.sourceCode || context.getSourceCode();
const loc = sourceCode.getTokenBefore(node.body).loc; // Show violation on arrow (=>).
if (node.expression) {
// When the return is implied (no curly braces around the body), we have to check the single body node directly.
if (!isFix(node.body)) {
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/meta-property-ordering.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ module.exports = {
},

create(context) {
const sourceCode = context.getSourceCode();
const sourceCode = context.sourceCode || context.getSourceCode(); // TODO: just use context.sourceCode when dropping eslint < v9
const ruleInfo = getRuleInfo(sourceCode);
if (!ruleInfo) {
return {};
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/no-deprecated-context-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ module.exports = {
},

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

// ----------------------------------------------------------------------
// Public
Expand Down
4 changes: 2 additions & 2 deletions lib/rules/no-deprecated-report-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ module.exports = {
},

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

// ----------------------------------------------------------------------
Expand Down Expand Up @@ -60,7 +60,7 @@ module.exports = {
fix(fixer) {
const openingParen = sourceCode.getTokenBefore(node.arguments[0]);
const closingParen = sourceCode.getLastToken(node);
const reportInfo = utils.getReportInfo(node.arguments, context);
const reportInfo = utils.getReportInfo(node, context);

if (!reportInfo) {
return null;
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/no-identical-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ module.exports = {
// ----------------------------------------------------------------------
// Public
// ----------------------------------------------------------------------
const sourceCode = context.getSourceCode();
const sourceCode = context.sourceCode || context.getSourceCode(); // TODO: just use context.sourceCode when dropping eslint < v9

// ----------------------------------------------------------------------
// Helpers
Expand Down
7 changes: 4 additions & 3 deletions lib/rules/no-missing-message-ids.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ module.exports = {
},

create(context) {
const sourceCode = context.getSourceCode();
const sourceCode = context.sourceCode || context.getSourceCode(); // TODO: just use context.sourceCode when dropping eslint < v9
const { scopeManager } = sourceCode;
const ruleInfo = utils.getRuleInfo(sourceCode);
if (!ruleInfo) {
Expand All @@ -48,14 +48,15 @@ module.exports = {
},

CallExpression(node) {
const scope = sourceCode.getScope?.(node) || context.getScope(); // TODO: just use sourceCode.getScope() when we drop support for ESLint < 9.0.0
// Check for messageId properties used in known calls to context.report();
if (
node.callee.type === 'MemberExpression' &&
contextIdentifiers.has(node.callee.object) &&
node.callee.property.type === 'Identifier' &&
node.callee.property.name === 'report'
) {
const reportInfo = utils.getReportInfo(node.arguments, context);
const reportInfo = utils.getReportInfo(node, context);
if (!reportInfo) {
return;
}
Expand All @@ -80,7 +81,7 @@ module.exports = {
val.value,
ruleInfo,
scopeManager,
context.getScope()
scope
)
)
// Couldn't find this messageId in `meta.messages`.
Expand Down
12 changes: 5 additions & 7 deletions lib/rules/no-missing-placeholders.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ module.exports = {
},

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

let contextIdentifiers;
Expand All @@ -48,13 +48,14 @@ 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
if (
node.callee.type === 'MemberExpression' &&
contextIdentifiers.has(node.callee.object) &&
node.callee.property.type === 'Identifier' &&
node.callee.property.name === 'report'
) {
const reportInfo = utils.getReportInfo(node.arguments, context);
const reportInfo = utils.getReportInfo(node, context);
if (!reportInfo) {
return;
}
Expand All @@ -75,7 +76,7 @@ module.exports = {
obj.messageId.value,
ruleInfo,
scopeManager,
context.getScope()
scope
);
if (correspondingMessage) {
obj.message = correspondingMessage.value;
Expand All @@ -89,10 +90,7 @@ module.exports = {
messageId,
data,
} of reportMessagesAndDataArray.filter((obj) => obj.message)) {
const messageStaticValue = getStaticValue(
message,
context.getScope()
);
const messageStaticValue = getStaticValue(message, scope);
if (
((message.type === 'Literal' &&
typeof message.value === 'string') ||
Expand Down
3 changes: 2 additions & 1 deletion lib/rules/no-only-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ module.exports = {
{
messageId: 'removeOnly',
*fix(fixer) {
const sourceCode = context.getSourceCode();
const sourceCode =
context.sourceCode || context.getSourceCode(); // TODO: just use context.sourceCode when dropping eslint < v9

const tokenBefore =
sourceCode.getTokenBefore(onlyProperty);
Expand Down
13 changes: 7 additions & 6 deletions lib/rules/no-unused-message-ids.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module.exports = {
},

create(context) {
const sourceCode = context.getSourceCode();
const sourceCode = context.sourceCode || context.getSourceCode(); // TODO: just use context.sourceCode when dropping eslint < v9
const { scopeManager } = sourceCode;
const ruleInfo = utils.getRuleInfo(sourceCode);
if (!ruleInfo) {
Expand All @@ -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,10 @@ module.exports = {
return;
}

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

const messageIdNodesUnused = messageIdNodes.filter(
(node) =>
!messageIdsUsed.has(utils.getKeyName(node, context.getScope()))
(node) => !messageIdsUsed.has(utils.getKeyName(node, scope))
);

// Report any messageIds that were never used.
Expand All @@ -68,7 +69,7 @@ module.exports = {
node: messageIdNode,
messageId: 'unusedMessage',
data: {
messageId: utils.getKeyName(messageIdNode, context.getScope()),
messageId: utils.getKeyName(messageIdNode, scope),
},
});
}
Expand All @@ -82,7 +83,7 @@ module.exports = {
node.callee.property.type === 'Identifier' &&
node.callee.property.name === 'report'
) {
const reportInfo = utils.getReportInfo(node.arguments, context);
const reportInfo = utils.getReportInfo(node, context);
if (!reportInfo) {
return;
}
Expand Down
12 changes: 5 additions & 7 deletions lib/rules/no-unused-placeholders.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ module.exports = {
},

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

let contextIdentifiers;
Expand All @@ -47,13 +47,14 @@ 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
if (
node.callee.type === 'MemberExpression' &&
contextIdentifiers.has(node.callee.object) &&
node.callee.property.type === 'Identifier' &&
node.callee.property.name === 'report'
) {
const reportInfo = utils.getReportInfo(node.arguments, context);
const reportInfo = utils.getReportInfo(node, context);
if (!reportInfo) {
return;
}
Expand All @@ -74,7 +75,7 @@ module.exports = {
obj.messageId.value,
ruleInfo,
scopeManager,
context.getScope()
scope
);
if (correspondingMessage) {
obj.message = correspondingMessage.value;
Expand All @@ -86,10 +87,7 @@ module.exports = {
for (const { message, data } of reportMessagesAndDataArray.filter(
(obj) => obj.message
)) {
const messageStaticValue = getStaticValue(
message,
context.getScope()
);
const messageStaticValue = getStaticValue(message, scope);
if (
((message.type === 'Literal' &&
typeof message.value === 'string') ||
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/no-useless-token-range.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ module.exports = {
},

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

// ----------------------------------------------------------------------
// Helpers
Expand Down
10 changes: 4 additions & 6 deletions lib/rules/prefer-message-ids.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ module.exports = {
},

create(context) {
const sourceCode = context.getSourceCode();
const sourceCode = context.sourceCode || context.getSourceCode(); // TODO: just use context.sourceCode when dropping eslint < v9
const ruleInfo = utils.getRuleInfo(sourceCode);
if (!ruleInfo) {
return {};
Expand All @@ -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
contextIdentifiers = utils.getContextIdentifiers(
sourceCode.scopeManager,
ast
Expand All @@ -64,10 +65,7 @@ module.exports = {
return;
}

const staticValue = getStaticValue(
messagesNode.value,
context.getScope()
);
const staticValue = getStaticValue(messagesNode.value, scope);
if (!staticValue) {
return;
}
Expand All @@ -90,7 +88,7 @@ module.exports = {
node.callee.property.type === 'Identifier' &&
node.callee.property.name === 'report'
) {
const reportInfo = utils.getReportInfo(node.arguments, context);
const reportInfo = utils.getReportInfo(node, context);
if (!reportInfo) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/prefer-object-rule.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ module.exports = {
// Public
// ----------------------------------------------------------------------

const sourceCode = context.getSourceCode();
const sourceCode = context.sourceCode || context.getSourceCode(); // TODO: just use context.sourceCode when dropping eslint < v9
const ruleInfo = utils.getRuleInfo(sourceCode);
if (!ruleInfo) {
return {};
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/prefer-output-null.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ module.exports = {
// Public
// ----------------------------------------------------------------------

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

return {
Program(ast) {
Expand Down
4 changes: 2 additions & 2 deletions lib/rules/prefer-placeholders.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ module.exports = {
create(context) {
let contextIdentifiers;

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

// ----------------------------------------------------------------------
Expand All @@ -51,7 +51,7 @@ module.exports = {
node.callee.property.type === 'Identifier' &&
node.callee.property.name === 'report'
) {
const reportInfo = utils.getReportInfo(node.arguments, context);
const reportInfo = utils.getReportInfo(node, context);

if (!reportInfo) {
return;
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/prefer-replace-text.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ module.exports = {
},

create(context) {
const sourceCode = context.getSourceCode();
const sourceCode = context.sourceCode || context.getSourceCode(); // TODO: just use context.sourceCode when dropping eslint < v9
let funcInfo = {
upper: null,
codePath: null,
Expand Down

0 comments on commit 35e14cd

Please sign in to comment.