Skip to content

Commit 646a094

Browse files
committed
tools: prepare custom rules for ESLint v9
Refs: https://eslint.org/docs/latest/use/migrate-to-9.0.0 PR-URL: #52889 Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz.nizipli@sentry.io>
1 parent daeb7db commit 646a094

10 files changed

+138
-114
lines changed

tools/eslint-rules/no-array-destructuring.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ module.exports = {
5656
schema: [],
5757
},
5858
create(context) {
59-
const sourceCode = context.getSourceCode();
59+
const sourceCode = context.sourceCode;
6060

6161
return {
6262
ArrayPattern(node) {

tools/eslint-rules/no-duplicate-requires.js

+33-31
Original file line numberDiff line numberDiff line change
@@ -25,41 +25,43 @@ function isTopLevel(node) {
2525
return false;
2626
}
2727

28-
module.exports = (context) => {
29-
if (context.parserOptions.sourceType === 'module') {
30-
return {};
31-
}
32-
33-
function getRequiredModuleNameFromCall(node) {
34-
// Node has arguments and first argument is string
35-
if (node.arguments.length && isString(node.arguments[0])) {
36-
return node.arguments[0].value.trim();
28+
module.exports = {
29+
create(context) {
30+
if (context.parserOptions.sourceType === 'module') {
31+
return {};
3732
}
3833

39-
return undefined;
40-
}
34+
function getRequiredModuleNameFromCall(node) {
35+
// Node has arguments and first argument is string
36+
if (node.arguments.length && isString(node.arguments[0])) {
37+
return node.arguments[0].value.trim();
38+
}
4139

42-
const required = new Set();
40+
return undefined;
41+
}
4342

44-
const rules = {
45-
CallExpression: (node) => {
46-
if (isRequireCall(node) && isTopLevel(node)) {
47-
const moduleName = getRequiredModuleNameFromCall(node);
48-
if (moduleName === undefined) {
49-
return;
50-
}
51-
if (required.has(moduleName)) {
52-
context.report(
53-
node,
54-
'\'{{moduleName}}\' require is duplicated.',
55-
{ moduleName },
56-
);
57-
} else {
58-
required.add(moduleName);
43+
const required = new Set();
44+
45+
const rules = {
46+
CallExpression: (node) => {
47+
if (isRequireCall(node) && isTopLevel(node)) {
48+
const moduleName = getRequiredModuleNameFromCall(node);
49+
if (moduleName === undefined) {
50+
return;
51+
}
52+
if (required.has(moduleName)) {
53+
context.report(
54+
node,
55+
'\'{{moduleName}}\' require is duplicated.',
56+
{ moduleName },
57+
);
58+
} else {
59+
required.add(moduleName);
60+
}
5961
}
60-
}
61-
},
62-
};
62+
},
63+
};
6364

64-
return rules;
65+
return rules;
66+
},
6567
};

tools/eslint-rules/no-unescaped-regexp-dot.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
module.exports = {
1212
create(context) {
13-
const sourceCode = context.getSourceCode();
13+
const sourceCode = context.sourceCode;
1414
const regexpStack = [];
1515
let regexpBuffer = [];
1616
let inRegExp = false;

tools/eslint-rules/non-ascii-character.js

+22-20
Original file line numberDiff line numberDiff line change
@@ -24,32 +24,34 @@ const suggestions = {
2424
'—': '-',
2525
};
2626

27-
module.exports = (context) => {
27+
module.exports = {
28+
create(context) {
2829

29-
const reportIfError = (node, sourceCode) => {
30+
const reportIfError = (node, sourceCode) => {
3031

31-
const matches = sourceCode.text.match(nonAsciiRegexPattern);
32+
const matches = sourceCode.text.match(nonAsciiRegexPattern);
3233

33-
if (!matches) return;
34+
if (!matches) return;
3435

35-
const offendingCharacter = matches[0];
36-
const offendingCharacterPosition = matches.index;
37-
const suggestion = suggestions[offendingCharacter];
36+
const offendingCharacter = matches[0];
37+
const offendingCharacterPosition = matches.index;
38+
const suggestion = suggestions[offendingCharacter];
3839

39-
let message = `Non-ASCII character '${offendingCharacter}' detected.`;
40+
let message = `Non-ASCII character '${offendingCharacter}' detected.`;
4041

41-
message = suggestion ?
42-
`${message} Consider replacing with: ${suggestion}` :
43-
message;
42+
message = suggestion ?
43+
`${message} Consider replacing with: ${suggestion}` :
44+
message;
4445

45-
context.report({
46-
node,
47-
message,
48-
loc: sourceCode.getLocFromIndex(offendingCharacterPosition),
49-
});
50-
};
46+
context.report({
47+
node,
48+
message,
49+
loc: sourceCode.getLocFromIndex(offendingCharacterPosition),
50+
});
51+
};
5152

52-
return {
53-
Program: (node) => reportIfError(node, context.getSourceCode()),
54-
};
53+
return {
54+
Program: (node) => reportIfError(node, context.sourceCode),
55+
};
56+
},
5557
};

tools/eslint-rules/prefer-assert-iferror.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module.exports = {
1212
fixable: 'code',
1313
},
1414
create(context) {
15-
const sourceCode = context.getSourceCode();
15+
const sourceCode = context.sourceCode;
1616
let assertImported = false;
1717

1818
function hasSameTokens(nodeA, nodeB) {

tools/eslint-rules/prefer-assert-methods.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ module.exports = {
3131
node,
3232
message: parseError(assertMethod, arg.operator),
3333
fix: (fixer) => {
34-
const sourceCode = context.getSourceCode();
34+
const sourceCode = context.sourceCode;
3535
const left = sourceCode.getText(arg.left);
3636
const right = sourceCode.getText(arg.right);
3737
return fixer.replaceText(

tools/eslint-rules/prefer-common-mustsucceed.js

+53-51
Original file line numberDiff line numberDiff line change
@@ -3,64 +3,66 @@
33
const mustCall = 'CallExpression[callee.object.name="common"]' +
44
'[callee.property.name="mustCall"]';
55

6-
module.exports = (context) => {
7-
function isAssertIfError(node) {
8-
return node.type === 'MemberExpression' &&
9-
node.object.type === 'Identifier' &&
10-
node.object.name === 'assert' &&
11-
node.property.type === 'Identifier' &&
12-
node.property.name === 'ifError';
13-
}
6+
module.exports = {
7+
create(context) {
8+
function isAssertIfError(node) {
9+
return node.type === 'MemberExpression' &&
10+
node.object.type === 'Identifier' &&
11+
node.object.name === 'assert' &&
12+
node.property.type === 'Identifier' &&
13+
node.property.name === 'ifError';
14+
}
1415

15-
function isCallToIfError(node, errName) {
16-
return node.type === 'CallExpression' &&
17-
isAssertIfError(node.callee) &&
18-
node.arguments.length > 0 &&
19-
node.arguments[0].type === 'Identifier' &&
20-
node.arguments[0].name === errName;
21-
}
16+
function isCallToIfError(node, errName) {
17+
return node.type === 'CallExpression' &&
18+
isAssertIfError(node.callee) &&
19+
node.arguments.length > 0 &&
20+
node.arguments[0].type === 'Identifier' &&
21+
node.arguments[0].name === errName;
22+
}
2223

23-
function bodyStartsWithCallToIfError(body, errName) {
24-
while (body.type === 'BlockStatement' && body.body.length > 0)
25-
body = body.body[0];
24+
function bodyStartsWithCallToIfError(body, errName) {
25+
while (body.type === 'BlockStatement' && body.body.length > 0)
26+
body = body.body[0];
2627

27-
let expr;
28-
switch (body.type) {
29-
case 'ReturnStatement':
30-
expr = body.argument;
31-
break;
32-
case 'ExpressionStatement':
33-
expr = body.expression;
34-
break;
35-
default:
36-
expr = body;
37-
}
28+
let expr;
29+
switch (body.type) {
30+
case 'ReturnStatement':
31+
expr = body.argument;
32+
break;
33+
case 'ExpressionStatement':
34+
expr = body.expression;
35+
break;
36+
default:
37+
expr = body;
38+
}
3839

39-
return isCallToIfError(expr, errName);
40-
}
40+
return isCallToIfError(expr, errName);
41+
}
4142

42-
return {
43-
[`${mustCall}:exit`]: (mustCall) => {
44-
if (mustCall.arguments.length > 0) {
45-
const callback = mustCall.arguments[0];
46-
if (isAssertIfError(callback)) {
47-
context.report(mustCall, 'Please use common.mustSucceed instead of ' +
48-
'common.mustCall(assert.ifError).');
49-
}
43+
return {
44+
[`${mustCall}:exit`]: (mustCall) => {
45+
if (mustCall.arguments.length > 0) {
46+
const callback = mustCall.arguments[0];
47+
if (isAssertIfError(callback)) {
48+
context.report(mustCall, 'Please use common.mustSucceed instead of ' +
49+
'common.mustCall(assert.ifError).');
50+
}
5051

51-
if (callback.type === 'ArrowFunctionExpression' ||
52-
callback.type === 'FunctionExpression') {
53-
if (callback.params.length > 0 &&
54-
callback.params[0].type === 'Identifier') {
55-
const errName = callback.params[0].name;
56-
if (bodyStartsWithCallToIfError(callback.body, errName)) {
57-
context.report(mustCall, 'Please use common.mustSucceed instead' +
58-
' of common.mustCall with' +
59-
' assert.ifError.');
52+
if (callback.type === 'ArrowFunctionExpression' ||
53+
callback.type === 'FunctionExpression') {
54+
if (callback.params.length > 0 &&
55+
callback.params[0].type === 'Identifier') {
56+
const errName = callback.params[0].name;
57+
if (bodyStartsWithCallToIfError(callback.body, errName)) {
58+
context.report(mustCall, 'Please use common.mustSucceed instead' +
59+
' of common.mustCall with' +
60+
' assert.ifError.');
61+
}
6062
}
6163
}
6264
}
63-
}
64-
},
65-
};
65+
},
66+
};
67+
},
6668
};

tools/eslint-rules/prefer-primordials.js

+21-3
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,27 @@ module.exports = {
7575
messages: {
7676
error: 'Use `const { {{name}} } = primordials;` instead of the global.',
7777
},
78+
schema: {
79+
type: 'array',
80+
items: [
81+
{
82+
type: 'object',
83+
required: ['name'],
84+
properties: {
85+
name: { type: 'string' },
86+
ignore: {
87+
type: 'array',
88+
items: { type: 'string' },
89+
},
90+
into: { type: 'string' },
91+
},
92+
additionalProperties: false,
93+
},
94+
],
95+
},
7896
},
7997
create(context) {
80-
const globalScope = context.getSourceCode().scopeManager.globalScope;
98+
const globalScope = context.sourceCode.scopeManager.globalScope;
8199

82100
const nameMap = new Map();
83101
const renameMap = new Map();
@@ -110,7 +128,7 @@ module.exports = {
110128
}
111129
const name = node.name;
112130
const parent = getDestructuringAssignmentParent(
113-
context.getScope(),
131+
context.sourceCode.getScope(node),
114132
node,
115133
);
116134
const parentName = parent?.name;
@@ -155,7 +173,7 @@ module.exports = {
155173
}
156174

157175
const variables =
158-
context.getSourceCode().scopeManager.getDeclaredVariables(node);
176+
context.sourceCode.scopeManager.getDeclaredVariables(node);
159177
if (variables.length === 0) {
160178
context.report({
161179
node,

tools/eslint-rules/prefer-proto.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
'use strict';
88

99
// Cribbed from `eslint-module-utils/declaredScope`
10-
function declaredScope(context, name) {
11-
const references = context.getScope().references;
10+
function declaredScope(context, node, name) {
11+
const references = context.sourceCode.getScope(node).references;
1212
const reference = references.find((x) => x.identifier.name === name);
1313
if (!reference) return undefined;
1414
return reference.resolved.scope.type;
@@ -33,12 +33,12 @@ module.exports = {
3333
[callee.type="MemberExpression"][callee.object.name="Object"][callee.property.name="create"]\
3434
)'(node) {
3535
if (node.callee.type === 'MemberExpression') {
36-
const scope = declaredScope(context, node.callee.object);
36+
const scope = declaredScope(context, node, node.callee.object);
3737
if (scope && scope !== 'module' && scope !== 'global') {
3838
return;
3939
}
4040
}
41-
const value = context.getSourceCode().getText(node.arguments[0]);
41+
const value = context.sourceCode.getText(node.arguments[0]);
4242
context.report({
4343
node,
4444
messageId: 'error',

tools/eslint-rules/set-proto-to-null-in-object.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ module.exports = {
4646
message: 'Every object must have __proto__: null',
4747
fix: function(fixer) {
4848
// Generate the fix suggestion to add __proto__: null
49-
const sourceCode = context.getSourceCode();
49+
const sourceCode = context.sourceCode;
5050
const firstProperty = properties[0];
5151
const firstPropertyToken = sourceCode.getFirstToken(firstProperty);
5252

0 commit comments

Comments
 (0)