2017-12-09 15:22:39 +01:00
|
|
|
/**
|
|
|
|
* @fileoverview Check that TypeError[ERR_INVALID_ARG_TYPE] uses
|
|
|
|
* lowercase for primitive types
|
|
|
|
* @author Weijia Wang <starkwang@126.com>
|
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Rule Definition
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2017-12-13 17:14:02 +01:00
|
|
|
const astSelector = 'NewExpression[callee.property.name="TypeError"]' +
|
|
|
|
'[arguments.0.value="ERR_INVALID_ARG_TYPE"]';
|
|
|
|
|
2018-06-04 19:34:37 +02:00
|
|
|
const primitives = [ 'number', 'string', 'boolean', 'null', 'undefined' ];
|
2017-12-09 15:22:39 +01:00
|
|
|
|
2023-02-03 10:55:29 +01:00
|
|
|
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;
|
|
|
|
}
|
2017-12-09 15:22:39 +01:00
|
|
|
}
|
2017-12-17 11:41:34 +01:00
|
|
|
|
2023-02-03 10:55:29 +01:00
|
|
|
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}'`,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2017-12-13 17:14:02 +01:00
|
|
|
|
2023-02-03 10:55:29 +01:00
|
|
|
}
|
2020-08-01 20:22:51 +02:00
|
|
|
|
2023-02-03 10:55:29 +01:00
|
|
|
return {
|
|
|
|
[astSelector]: (node) => checkNamesArgument(node),
|
|
|
|
};
|
|
|
|
},
|
2020-08-01 20:22:51 +02:00
|
|
|
};
|