Skip to content

Commit

Permalink
tools: update ESLint custom rules to not use the deprecated format
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
aduh95 authored and MylesBorins committed Feb 18, 2023
1 parent c526f9f commit 51c6c61
Show file tree
Hide file tree
Showing 5 changed files with 200 additions and 199 deletions.
75 changes: 37 additions & 38 deletions tools/eslint-rules/lowercase-name-for-primitive.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,46 +14,45 @@ const astSelector = 'NewExpression[callee.property.name="TypeError"]' +

const primitives = [ 'number', 'string', 'boolean', 'null', 'undefined' ];

module.exports = function(context) {
function checkNamesArgument(node) {
const names = node.arguments[2];

switch (names.type) {
case 'Literal':
checkName(names);
break;
case 'ArrayExpression':
names.elements.forEach((name) => {
checkName(name);
});
break;
}
}

function checkName(node) {
const name = node.value;
const lowercaseName = name.toLowerCase();
if (name !== lowercaseName && primitives.includes(lowercaseName)) {
const msg = `primitive should use lowercase: ${name}`;
context.report({
node,
message: msg,
fix: (fixer) => {
return fixer.replaceText(
node,
`'${lowercaseName}'`,
);
},
});
module.exports = {
meta: { fixable: 'code' },
create(context) {
function checkNamesArgument(node) {
const names = node.arguments[2];

switch (names.type) {
case 'Literal':
checkName(names);
break;
case 'ArrayExpression':
names.elements.forEach((name) => {
checkName(name);
});
break;
}
}

}
function checkName(node) {
const name = node.value;
const lowercaseName = name.toLowerCase();
if (name !== lowercaseName && primitives.includes(lowercaseName)) {
const msg = `primitive should use lowercase: ${name}`;
context.report({
node,
message: msg,
fix: (fixer) => {
return fixer.replaceText(
node,
`'${lowercaseName}'`,
);
},
});
}

return {
[astSelector]: (node) => checkNamesArgument(node),
};
};
}

module.exports.meta = {
fixable: 'code',
return {
[astSelector]: (node) => checkNamesArgument(node),
};
},
};
51 changes: 25 additions & 26 deletions tools/eslint-rules/prefer-assert-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,29 @@ const preferredAssertMethod = {
'!=': 'notEqual',
};

module.exports = function(context) {
return {
[astSelector]: function(node) {
const arg = node.expression.arguments[0];
const assertMethod = preferredAssertMethod[arg.operator];
if (assertMethod) {
context.report({
node,
message: parseError(assertMethod, arg.operator),
fix: (fixer) => {
const sourceCode = context.getSourceCode();
const left = sourceCode.getText(arg.left);
const right = sourceCode.getText(arg.right);
return fixer.replaceText(
node,
`assert.${assertMethod}(${left}, ${right});`,
);
},
});
}
},
};
};

module.exports.meta = {
fixable: 'code',
module.exports = {
meta: { fixable: 'code' },
create(context) {
return {
[astSelector]: function(node) {
const arg = node.expression.arguments[0];
const assertMethod = preferredAssertMethod[arg.operator];
if (assertMethod) {
context.report({
node,
message: parseError(assertMethod, arg.operator),
fix: (fixer) => {
const sourceCode = context.getSourceCode();
const left = sourceCode.getText(arg.left);
const right = sourceCode.getText(arg.right);
return fixer.replaceText(
node,
`assert.${assertMethod}(${left}, ${right});`,
);
},
});
}
},
};
},
};
20 changes: 11 additions & 9 deletions tools/eslint-rules/prefer-common-mustnotcall.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,18 @@ const mustCallSelector = 'CallExpression[callee.object.name="common"]' +
const arg0Selector = `${mustCallSelector}[arguments.0.value=0]`;
const arg1Selector = `${mustCallSelector}[arguments.1.value=0]`;

module.exports = function(context) {
function report(node) {
context.report(node, msg);
}
module.exports = {
create(context) {
function report(node) {
context.report(node, msg);
}

return {
return {
// Catch common.mustCall(0)
[arg0Selector]: report,
[arg0Selector]: report,

// Catch common.mustCall(fn, 0)
[arg1Selector]: report,
};
// Catch common.mustCall(fn, 0)
[arg1Selector]: report,
};
},
};
110 changes: 56 additions & 54 deletions tools/eslint-rules/require-common-first.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,72 +10,74 @@ const { isRequireCall, isString } = require('./rules-utils.js');
// Rule Definition
//------------------------------------------------------------------------------

module.exports = function(context) {
const requiredModule = 'common';
const isESM = context.parserOptions.sourceType === 'module';
const foundModules = [];
module.exports = {
create(context) {
const requiredModule = 'common';
const isESM = context.parserOptions.sourceType === 'module';
const foundModules = [];

/**
* Function to check if the path is a module and return its name.
* @param {string} str The path to check
* @returns {string} module name
*/
function getModuleName(str) {
if (str === '../common/index.mjs') {
return 'common';
}
/**
* Function to check if the path is a module and return its name.
* @param {string} str The path to check
* @returns {string} module name
*/
function getModuleName(str) {
if (str === '../common/index.mjs') {
return 'common';
}

return path.basename(str);
}
return path.basename(str);
}

/**
* Function to check if a node has an argument that is a module and
* return its name.
* @param {ASTNode} node The node to check
* @returns {undefined | string} module name or undefined
*/
function getModuleNameFromCall(node) {
/**
* Function to check if a node has an argument that is a module and
* return its name.
* @param {ASTNode} node The node to check
* @returns {undefined | string} module name or undefined
*/
function getModuleNameFromCall(node) {
// Node has arguments and first argument is string
if (node.arguments.length && isString(node.arguments[0])) {
return getModuleName(node.arguments[0].value.trim());
}
if (node.arguments.length && isString(node.arguments[0])) {
return getModuleName(node.arguments[0].value.trim());
}

return undefined;
}
return undefined;
}

const rules = {
'Program:exit'(node) {
const rules = {
'Program:exit'(node) {
// The common module should be loaded in the first place.
const notLoadedFirst = foundModules.indexOf(requiredModule) !== 0;
if (notLoadedFirst) {
context.report(
node,
'Mandatory module "{{moduleName}}" must be loaded ' +
const notLoadedFirst = foundModules.indexOf(requiredModule) !== 0;
if (notLoadedFirst) {
context.report(
node,
'Mandatory module "{{moduleName}}" must be loaded ' +
'before any other modules.',
{ moduleName: requiredModule },
);
}
},
};

if (isESM) {
rules.ImportDeclaration = (node) => {
const moduleName = getModuleName(node.source.value);
if (moduleName) {
foundModules.push(moduleName);
}
{ moduleName: requiredModule },
);
}
},
};
} else {
rules.CallExpression = (node) => {
if (isRequireCall(node)) {
const moduleName = getModuleNameFromCall(node);

if (isESM) {
rules.ImportDeclaration = (node) => {
const moduleName = getModuleName(node.source.value);
if (moduleName) {
foundModules.push(moduleName);
}
}
};
}
};
} else {
rules.CallExpression = (node) => {
if (isRequireCall(node)) {
const moduleName = getModuleNameFromCall(node);

if (moduleName) {
foundModules.push(moduleName);
}
}
};
}

return rules;
return rules;
},
};
Loading

0 comments on commit 51c6c61

Please sign in to comment.