0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/tools/eslint-rules/lowercase-name-for-primitive.js
Shobhit Chittora 639770c8d6 tools: autofixer for lowercase-name-for-primitive
PR-URL: https://github.com/nodejs/node/pull/17715
Refs: https://github.com/nodejs/node/issues/16636
Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
Reviewed-By: Weijia Wang <starkwang@126.com>
2017-12-26 13:51:53 +08:00

58 lines
1.4 KiB
JavaScript

/**
* @fileoverview Check that TypeError[ERR_INVALID_ARG_TYPE] uses
* lowercase for primitive types
* @author Weijia Wang <starkwang@126.com>
*/
'use strict';
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
const astSelector = 'NewExpression[callee.property.name="TypeError"]' +
'[arguments.0.value="ERR_INVALID_ARG_TYPE"]';
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}'`
);
}
});
}
}
return {
[astSelector]: (node) => checkNamesArgument(node)
};
};