diff options
author | Anatoli Papirovski <apapirovski@mac.com> | 2017-11-09 10:18:12 -0500 |
---|---|---|
committer | Anna Henningsen <anna@addaleax.net> | 2017-11-18 20:27:40 +0100 |
commit | 4b82d892abfcac160a0486d432e23e27315a5765 (patch) | |
tree | a94ded22c5ea2657dd1592ce9bc6b0031e2a74e6 /tools/eslint-rules/prefer-util-format-errors.js | |
parent | 2c0acc0bc191fbe37b91fe39d213fcc56ce23741 (diff) | |
download | android-node-v8-4b82d892abfcac160a0486d432e23e27315a5765.tar.gz android-node-v8-4b82d892abfcac160a0486d432e23e27315a5765.tar.bz2 android-node-v8-4b82d892abfcac160a0486d432e23e27315a5765.zip |
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.
PR-URL: https://github.com/nodejs/node/pull/16904
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Diffstat (limited to 'tools/eslint-rules/prefer-util-format-errors.js')
-rw-r--r-- | tools/eslint-rules/prefer-util-format-errors.js | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/tools/eslint-rules/prefer-util-format-errors.js b/tools/eslint-rules/prefer-util-format-errors.js new file mode 100644 index 0000000000..c3f4819e43 --- /dev/null +++ b/tools/eslint-rules/prefer-util-format-errors.js @@ -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); + } + }; + } +}; |