node/tools/eslint-rules/prefer-util-format-errors.js
Ruben Bridgewater 7107c9201d
tools: fix custom eslint rule errors
This fixes a few rules by making sure the input is actually ready
to be checked. Otherwise those can throw TypeErrors or result in
faulty error messages.

PR-URL: https://github.com/nodejs/node/pull/18853
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
2018-02-22 12:10:22 +01:00

40 lines
1.3 KiB
JavaScript

'use strict';
const { isDefiningError } = require('./rules-utils.js');
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';
}
module.exports = {
create: function(context) {
return {
ExpressionStatement: function(node) {
if (!isDefiningError(node) || node.expression.arguments.length < 2)
return;
const msg = node.expression.arguments[1];
if (!isArrowFunctionWithTemplateLiteral(msg))
return;
// Checks to see if order of arguments to function is the same as the
// order of them being concatenated in the template string. The idea is
// that if both match, then you can use `util.format`-style args.
// Would pass rule: (a, b) => `${b}${a}`.
// Would fail rule: (a, b) => `${a}${b}`, and needs to be rewritten.
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);
}
};
}
};