Skip to content

Commit 728f968

Browse files
aduh95danielleadams
authored andcommitted
tools: update ESLint custom rules to not use the deprecated format
Refs: https://eslint.org/docs/latest/extend/custom-rules-deprecated PR-URL: #46460 Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 6c454af commit 728f968

File tree

5 files changed

+200
-199
lines changed

5 files changed

+200
-199
lines changed

‎tools/eslint-rules/lowercase-name-for-primitive.js‎

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -14,46 +14,45 @@ const astSelector = 'NewExpression[callee.property.name="TypeError"]' +
1414

1515
constprimitives=['number','string','boolean','null','undefined'];
1616

17-
module.exports=function(context){
18-
functioncheckNamesArgument(node){
19-
constnames=node.arguments[2];
20-
21-
switch(names.type){
22-
case'Literal':
23-
checkName(names);
24-
break;
25-
case'ArrayExpression':
26-
names.elements.forEach((name)=>{
27-
checkName(name);
28-
});
29-
break;
30-
}
31-
}
32-
33-
functioncheckName(node){
34-
constname=node.value;
35-
constlowercaseName=name.toLowerCase();
36-
if(name!==lowercaseName&&primitives.includes(lowercaseName)){
37-
constmsg=`primitive should use lowercase: ${name}`;
38-
context.report({
39-
node,
40-
message: msg,
41-
fix: (fixer)=>{
42-
returnfixer.replaceText(
43-
node,
44-
`'${lowercaseName}'`,
45-
);
46-
},
47-
});
17+
module.exports={
18+
meta: {fixable: 'code'},
19+
create(context){
20+
functioncheckNamesArgument(node){
21+
constnames=node.arguments[2];
22+
23+
switch(names.type){
24+
case'Literal':
25+
checkName(names);
26+
break;
27+
case'ArrayExpression':
28+
names.elements.forEach((name)=>{
29+
checkName(name);
30+
});
31+
break;
32+
}
4833
}
4934

50-
}
35+
functioncheckName(node){
36+
constname=node.value;
37+
constlowercaseName=name.toLowerCase();
38+
if(name!==lowercaseName&&primitives.includes(lowercaseName)){
39+
constmsg=`primitive should use lowercase: ${name}`;
40+
context.report({
41+
node,
42+
message: msg,
43+
fix: (fixer)=>{
44+
returnfixer.replaceText(
45+
node,
46+
`'${lowercaseName}'`,
47+
);
48+
},
49+
});
50+
}
5151

52-
return{
53-
[astSelector]: (node)=>checkNamesArgument(node),
54-
};
55-
};
52+
}
5653

57-
module.exports.meta={
58-
fixable: 'code',
54+
return{
55+
[astSelector]: (node)=>checkNamesArgument(node),
56+
};
57+
},
5958
};

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

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,29 @@ const preferredAssertMethod ={
1919
'!=': 'notEqual',
2020
};
2121

22-
module.exports=function(context){
23-
return{
24-
[astSelector]: function(node){
25-
constarg=node.expression.arguments[0];
26-
constassertMethod=preferredAssertMethod[arg.operator];
27-
if(assertMethod){
28-
context.report({
29-
node,
30-
message: parseError(assertMethod,arg.operator),
31-
fix: (fixer)=>{
32-
constsourceCode=context.getSourceCode();
33-
constleft=sourceCode.getText(arg.left);
34-
constright=sourceCode.getText(arg.right);
35-
returnfixer.replaceText(
36-
node,
37-
`assert.${assertMethod}(${left}, ${right});`,
38-
);
39-
},
40-
});
41-
}
42-
},
43-
};
44-
};
45-
46-
module.exports.meta={
47-
fixable: 'code',
22+
module.exports={
23+
meta: {fixable: 'code'},
24+
create(context){
25+
return{
26+
[astSelector]: function(node){
27+
constarg=node.expression.arguments[0];
28+
constassertMethod=preferredAssertMethod[arg.operator];
29+
if(assertMethod){
30+
context.report({
31+
node,
32+
message: parseError(assertMethod,arg.operator),
33+
fix: (fixer)=>{
34+
constsourceCode=context.getSourceCode();
35+
constleft=sourceCode.getText(arg.left);
36+
constright=sourceCode.getText(arg.right);
37+
returnfixer.replaceText(
38+
node,
39+
`assert.${assertMethod}(${left}, ${right});`,
40+
);
41+
},
42+
});
43+
}
44+
},
45+
};
46+
},
4847
};

‎tools/eslint-rules/prefer-common-mustnotcall.js‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,18 @@ const mustCallSelector = 'CallExpression[callee.object.name="common"]' +
1515
constarg0Selector=`${mustCallSelector}[arguments.0.value=0]`;
1616
constarg1Selector=`${mustCallSelector}[arguments.1.value=0]`;
1717

18-
module.exports=function(context){
19-
functionreport(node){
20-
context.report(node,msg);
21-
}
18+
module.exports={
19+
create(context){
20+
functionreport(node){
21+
context.report(node,msg);
22+
}
2223

23-
return{
24+
return{
2425
// Catch common.mustCall(0)
25-
[arg0Selector]: report,
26+
[arg0Selector]: report,
2627

27-
// Catch common.mustCall(fn, 0)
28-
[arg1Selector]: report,
29-
};
28+
// Catch common.mustCall(fn, 0)
29+
[arg1Selector]: report,
30+
};
31+
},
3032
};

‎tools/eslint-rules/require-common-first.js‎

Lines changed: 56 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -10,72 +10,74 @@ const{isRequireCall, isString } = require('./rules-utils.js');
1010
// Rule Definition
1111
//------------------------------------------------------------------------------
1212

13-
module.exports=function(context){
14-
constrequiredModule='common';
15-
constisESM=context.parserOptions.sourceType==='module';
16-
constfoundModules=[];
13+
module.exports={
14+
create(context){
15+
constrequiredModule='common';
16+
constisESM=context.parserOptions.sourceType==='module';
17+
constfoundModules=[];
1718

18-
/**
19-
* Function to check if the path is a module and return its name.
20-
* @param{string} str The path to check
21-
* @returns{string} module name
22-
*/
23-
functiongetModuleName(str){
24-
if(str==='../common/index.mjs'){
25-
return'common';
26-
}
19+
/**
20+
* Function to check if the path is a module and return its name.
21+
* @param{string} str The path to check
22+
* @returns{string} module name
23+
*/
24+
functiongetModuleName(str){
25+
if(str==='../common/index.mjs'){
26+
return'common';
27+
}
2728

28-
returnpath.basename(str);
29-
}
29+
returnpath.basename(str);
30+
}
3031

31-
/**
32-
* Function to check if a node has an argument that is a module and
33-
* return its name.
34-
* @param{ASTNode} node The node to check
35-
* @returns{undefined | string} module name or undefined
36-
*/
37-
functiongetModuleNameFromCall(node){
32+
/**
33+
* Function to check if a node has an argument that is a module and
34+
* return its name.
35+
* @param{ASTNode} node The node to check
36+
* @returns{undefined | string} module name or undefined
37+
*/
38+
functiongetModuleNameFromCall(node){
3839
// Node has arguments and first argument is string
39-
if(node.arguments.length&&isString(node.arguments[0])){
40-
returngetModuleName(node.arguments[0].value.trim());
41-
}
40+
if(node.arguments.length&&isString(node.arguments[0])){
41+
returngetModuleName(node.arguments[0].value.trim());
42+
}
4243

43-
returnundefined;
44-
}
44+
returnundefined;
45+
}
4546

46-
construles={
47-
'Program:exit'(node){
47+
construles={
48+
'Program:exit'(node){
4849
// The common module should be loaded in the first place.
49-
constnotLoadedFirst=foundModules.indexOf(requiredModule)!==0;
50-
if(notLoadedFirst){
51-
context.report(
52-
node,
53-
'Mandatory module "{{moduleName}}" must be loaded '+
50+
constnotLoadedFirst=foundModules.indexOf(requiredModule)!==0;
51+
if(notLoadedFirst){
52+
context.report(
53+
node,
54+
'Mandatory module "{{moduleName}}" must be loaded '+
5455
'before any other modules.',
55-
{moduleName: requiredModule},
56-
);
57-
}
58-
},
59-
};
60-
61-
if(isESM){
62-
rules.ImportDeclaration=(node)=>{
63-
constmoduleName=getModuleName(node.source.value);
64-
if(moduleName){
65-
foundModules.push(moduleName);
66-
}
56+
{moduleName: requiredModule},
57+
);
58+
}
59+
},
6760
};
68-
}else{
69-
rules.CallExpression=(node)=>{
70-
if(isRequireCall(node)){
71-
constmoduleName=getModuleNameFromCall(node);
7261

62+
if(isESM){
63+
rules.ImportDeclaration=(node)=>{
64+
constmoduleName=getModuleName(node.source.value);
7365
if(moduleName){
7466
foundModules.push(moduleName);
7567
}
76-
}
77-
};
78-
}
68+
};
69+
}else{
70+
rules.CallExpression=(node)=>{
71+
if(isRequireCall(node)){
72+
constmoduleName=getModuleNameFromCall(node);
73+
74+
if(moduleName){
75+
foundModules.push(moduleName);
76+
}
77+
}
78+
};
79+
}
7980

80-
returnrules;
81+
returnrules;
82+
},
8183
};

0 commit comments

Comments
(0)