Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tools: add lowercase-name-for-primitive eslint rule #17568

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 <starkwang@126.com>
*/
'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);
}
}
}
};
};