-
Notifications
You must be signed in to change notification settings - Fork 30.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tools: add lowercase-name-for-primitive eslint rule
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
1 parent
fcc5b99
commit dc480f8
Showing
3 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' } | ||
] | ||
} | ||
] | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
}; | ||
}; |