Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tools: prepare custom rules for ESLint v9 #52889

Merged
merged 2 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tools/eslint-rules/no-array-destructuring.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ module.exports = {
schema: [],
},
create(context) {
const sourceCode = context.getSourceCode();
const sourceCode = context.sourceCode;

return {
ArrayPattern(node) {
Expand Down
4 changes: 3 additions & 1 deletion tools/eslint-rules/no-duplicate-requires.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ function isTopLevel(node) {
return false;
}

module.exports = (context) => {
module.exports = {
create(context) {
if (context.parserOptions.sourceType === 'module') {
return {};
}
Expand Down Expand Up @@ -62,4 +63,5 @@ module.exports = (context) => {
};

return rules;
},
};
2 changes: 1 addition & 1 deletion tools/eslint-rules/no-unescaped-regexp-dot.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

module.exports = {
create(context) {
const sourceCode = context.getSourceCode();
const sourceCode = context.sourceCode;
const regexpStack = [];
let regexpBuffer = [];
let inRegExp = false;
Expand Down
6 changes: 4 additions & 2 deletions tools/eslint-rules/non-ascii-character.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ const suggestions = {
'—': '-',
};

module.exports = (context) => {
module.exports = {
create(context) {

const reportIfError = (node, sourceCode) => {

Expand All @@ -50,6 +51,7 @@ module.exports = (context) => {
};

return {
Program: (node) => reportIfError(node, context.getSourceCode()),
Program: (node) => reportIfError(node, context.sourceCode),
};
},
};
2 changes: 1 addition & 1 deletion tools/eslint-rules/prefer-assert-iferror.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module.exports = {
fixable: 'code',
},
create(context) {
const sourceCode = context.getSourceCode();
const sourceCode = context.sourceCode;
let assertImported = false;

function hasSameTokens(nodeA, nodeB) {
Expand Down
2 changes: 1 addition & 1 deletion tools/eslint-rules/prefer-assert-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ module.exports = {
node,
message: parseError(assertMethod, arg.operator),
fix: (fixer) => {
const sourceCode = context.getSourceCode();
const sourceCode = context.sourceCode;
const left = sourceCode.getText(arg.left);
const right = sourceCode.getText(arg.right);
return fixer.replaceText(
Expand Down
4 changes: 3 additions & 1 deletion tools/eslint-rules/prefer-common-mustsucceed.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
const mustCall = 'CallExpression[callee.object.name="common"]' +
'[callee.property.name="mustCall"]';

module.exports = (context) => {
module.exports = {
create(context) {
function isAssertIfError(node) {
return node.type === 'MemberExpression' &&
node.object.type === 'Identifier' &&
Expand Down Expand Up @@ -63,4 +64,5 @@ module.exports = (context) => {
}
},
};
},
};
24 changes: 21 additions & 3 deletions tools/eslint-rules/prefer-primordials.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,27 @@ module.exports = {
messages: {
error: 'Use `const { {{name}} } = primordials;` instead of the global.',
},
schema: {
type: 'array',
items: [
{
type: 'object',
required: ['name'],
properties: {
name: { type: 'string' },
ignore: {
type: 'array',
items: { type: 'string' },
},
into: { type: 'string' },
},
additionalProperties: false,
},
],
},
},
create(context) {
const globalScope = context.getSourceCode().scopeManager.globalScope;
const globalScope = context.sourceCode.scopeManager.globalScope;

const nameMap = new Map();
const renameMap = new Map();
Expand Down Expand Up @@ -110,7 +128,7 @@ module.exports = {
}
const name = node.name;
const parent = getDestructuringAssignmentParent(
context.getScope(),
context.sourceCode.getScope(node),
node,
);
const parentName = parent?.name;
Expand Down Expand Up @@ -155,7 +173,7 @@ module.exports = {
}

const variables =
context.getSourceCode().scopeManager.getDeclaredVariables(node);
context.sourceCode.scopeManager.getDeclaredVariables(node);
if (variables.length === 0) {
context.report({
node,
Expand Down
8 changes: 4 additions & 4 deletions tools/eslint-rules/prefer-proto.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
'use strict';

// Cribbed from `eslint-module-utils/declaredScope`
function declaredScope(context, name) {
const references = context.getScope().references;
function declaredScope(context, node, name) {
const references = context.sourceCode.getScope(node).references;
const reference = references.find((x) => x.identifier.name === name);
if (!reference) return undefined;
return reference.resolved.scope.type;
Expand All @@ -33,12 +33,12 @@ module.exports = {
[callee.type="MemberExpression"][callee.object.name="Object"][callee.property.name="create"]\
)'(node) {
if (node.callee.type === 'MemberExpression') {
const scope = declaredScope(context, node.callee.object);
const scope = declaredScope(context, node, node.callee.object);
if (scope && scope !== 'module' && scope !== 'global') {
return;
}
}
const value = context.getSourceCode().getText(node.arguments[0]);
const value = context.sourceCode.getText(node.arguments[0]);
context.report({
node,
messageId: 'error',
Expand Down
2 changes: 1 addition & 1 deletion tools/eslint-rules/set-proto-to-null-in-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ module.exports = {
message: 'Every object must have __proto__: null',
fix: function(fixer) {
// Generate the fix suggestion to add __proto__: null
const sourceCode = context.getSourceCode();
const sourceCode = context.sourceCode;
const firstProperty = properties[0];
const firstPropertyToken = sourceCode.getFirstToken(firstProperty);

Expand Down