-
Notifications
You must be signed in to change notification settings - Fork 29.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
errors: consistent format for error message
Consistently use printf-style strings for error messages that do not need a custom argument order or processing of arguments. Backport-PR-URL: #17624 PR-URL: #16904 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
- Loading branch information
1 parent
dcd87ac
commit a333e71
Showing
3 changed files
with
93 additions
and
41 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,27 @@ | ||
'use strict'; | ||
|
||
/* eslint-disable no-template-curly-in-string */ | ||
|
||
require('../common'); | ||
|
||
const RuleTester = require('../../tools/eslint').RuleTester; | ||
const rule = require('../../tools/eslint-rules/prefer-util-format-errors'); | ||
|
||
new RuleTester({ parserOptions: { ecmaVersion: 6 } }) | ||
.run('prefer-util-format-errors', rule, { | ||
valid: [ | ||
'E(\'ABC\', \'abc\');', | ||
'E(\'ABC\', (arg1, arg2) => `${arg2}${arg1}`);', | ||
'E(\'ABC\', (arg1, arg2) => `${arg1}{arg2.something}`);', | ||
'E(\'ABC\', (arg1, arg2) => fn(arg1, arg2));' | ||
], | ||
invalid: [ | ||
{ | ||
code: 'E(\'ABC\', (arg1, arg2) => `${arg1}${arg2}`);', | ||
errors: [{ | ||
message: 'Please use a printf-like formatted string that ' + | ||
'util.format can consume.' | ||
}] | ||
} | ||
] | ||
}); |
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,39 @@ | ||
'use strict'; | ||
|
||
const errMsg = 'Please use a printf-like formatted string that util.format' + | ||
' can consume.'; | ||
|
||
function isArrowFunctionWithTemplateLiteral(node) { | ||
return node.type === 'ArrowFunctionExpression' && | ||
node.body.type === 'TemplateLiteral'; | ||
} | ||
|
||
function isDefiningError(node) { | ||
return node.expression && | ||
node.expression.type === 'CallExpression' && | ||
node.expression.callee && | ||
node.expression.callee.name === 'E'; | ||
} | ||
|
||
module.exports = { | ||
create: function(context) { | ||
return { | ||
ExpressionStatement: function(node) { | ||
if (!isDefiningError(node)) | ||
return; | ||
|
||
const msg = node.expression.arguments[1]; | ||
if (!isArrowFunctionWithTemplateLiteral(msg)) | ||
return; | ||
|
||
const { expressions } = msg.body; | ||
const hasSequentialParams = msg.params.every((param, index) => { | ||
const expr = expressions[index]; | ||
return expr && expr.type === 'Identifier' && param.name === expr.name; | ||
}); | ||
if (hasSequentialParams) | ||
context.report(msg, errMsg); | ||
} | ||
}; | ||
} | ||
}; |