Skip to content

Commit

Permalink
tools: simplify lowercase-name-for-primitive rule
Browse files Browse the repository at this point in the history
PR-URL: #17653
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Jon Moss <me@jonathanmoss.me>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
  • Loading branch information
cjihrig authored and MylesBorins committed Jan 8, 2018
1 parent b826528 commit eaa2d91
Showing 1 changed file with 25 additions and 28 deletions.
53 changes: 25 additions & 28 deletions tools/eslint-rules/lowercase-name-for-primitive.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,41 +9,38 @@
// 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) {
return {
NewExpression(node) {
if (
node.callee.property &&
node.callee.property.name === 'TypeError' &&
node.arguments[0].value === 'ERR_INVALID_ARG_TYPE'
) {
checkNamesArgument(node.arguments[2]);
}
function checkNamesArgument(node) {
const names = node.arguments[2];

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

function checkName(name) {
const lowercaseName = name.toLowerCase();
if (primitives.includes(lowercaseName) && !primitives.includes(name)) {
const msg = `primitive should use lowercase: ${name}`;
context.report(node, msg);
}
}
function checkName(node, name) {
const lowercaseName = name.toLowerCase();
if (primitives.includes(lowercaseName) && !primitives.includes(name)) {
const msg = `primitive should use lowercase: ${name}`;
context.report(node, msg);
}
}

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

0 comments on commit eaa2d91

Please sign in to comment.