Skip to content

Commit

Permalink
tools: add lowercase-name-for-primitive eslint rule
Browse files Browse the repository at this point in the history
Primitives should use lowercase in error message.

Refs: #16401
PR-URL: #17568
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Michaël Zasso <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
starkwang authored and MylesBorins committed Feb 12, 2018
1 parent 22bc6a1 commit f0c1855
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/.eslintrc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ rules:
require-buffer: error
buffer-constructor: error
no-let-in-for-declaration: error
lowercase-name-for-primitive: error
41 changes: 41 additions & 0 deletions test/parallel/test-eslint-lowercase-name-for-primitive.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict';

require('../common');

const RuleTester = require('../../tools/eslint').RuleTester;
const rule = require('../../tools/eslint-rules/lowercase-name-for-primitive');

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

new RuleTester().run('lowercase-name-for-primitive', rule, {
valid: [
'new errors.TypeError("ERR_INVALID_ARG_TYPE", "a", ["string", "number"])',
...valid.map((name) =>
`new errors.TypeError("ERR_INVALID_ARG_TYPE", "name", "${name}")`
)
],
invalid: [
{
code: 'new errors.TypeError("ERR_INVALID_ARG_TYPE", "a", "Number")',
errors: [{ message: 'primitive should use lowercase: Number' }]
},
{
code: 'new errors.TypeError("ERR_INVALID_ARG_TYPE", "a", "STRING")',
errors: [{ message: 'primitive should use lowercase: STRING' }]
},
{
code: 'new errors.TypeError("ERR_INVALID_ARG_TYPE", "a",' +
'["String", "Number"])',
errors: [
{ message: 'primitive should use lowercase: String' },
{ message: 'primitive should use lowercase: Number' }
]
}
]
});
49 changes: 49 additions & 0 deletions tools/eslint-rules/lowercase-name-for-primitive.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* @fileoverview Check that TypeError[ERR_INVALID_ARG_TYPE] uses
* lowercase for primitive types
* @author Weijia Wang <[email protected]>
*/
'use strict';

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------

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(names) {
switch (names.type) {
case 'Literal':
checkName(names.value);
break;
case 'ArrayExpression':
names.elements.forEach((name) => {
checkName(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);
}
}
}
};
};

0 comments on commit f0c1855

Please sign in to comment.