2017-11-09 15:18:12 +00:00
|
|
|
'use strict';
|
|
|
|
|
2018-02-18 19:19:20 +00:00
|
|
|
const { isDefiningError } = require('./rules-utils.js');
|
|
|
|
|
2017-11-09 15:18:12 +00:00
|
|
|
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) {
|
2018-02-18 19:19:20 +00:00
|
|
|
if (!isDefiningError(node) || node.expression.arguments.length < 2)
|
2017-11-09 15:18:12 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
const msg = node.expression.arguments[1];
|
|
|
|
if (!isArrowFunctionWithTemplateLiteral(msg))
|
|
|
|
return;
|
|
|
|
|
2017-11-28 21:56:45 +00:00
|
|
|
// 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.
|
2017-11-09 15:18:12 +00:00
|
|
|
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);
|
2022-12-18 16:39:39 +00:00
|
|
|
},
|
2017-11-09 15:18:12 +00:00
|
|
|
};
|
2022-12-18 16:39:39 +00:00
|
|
|
},
|
2017-11-09 15:18:12 +00:00
|
|
|
};
|