-
Notifications
You must be signed in to change notification settings - Fork 29.1k
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
errors: consistent format for error messages #16904
Closed
apapirovski
wants to merge
1
commit into
nodejs:master
from
apapirovski:patch-consistent-error-format
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 hasSequentialPar |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've been meaning to do this, but while you're here, can you extract this to
rules-utils.js
, and update the other usage of this?