1'use strict'; 2 3const { isDefiningError } = require('./rules-utils.js'); 4 5const errMsg = 'Please use a printf-like formatted string that util.format' + 6 ' can consume.'; 7 8function isArrowFunctionWithTemplateLiteral(node) { 9 return node.type === 'ArrowFunctionExpression' && 10 node.body.type === 'TemplateLiteral'; 11} 12 13module.exports = { 14 create: function(context) { 15 return { 16 ExpressionStatement: function(node) { 17 if (!isDefiningError(node) || node.expression.arguments.length < 2) 18 return; 19 20 const msg = node.expression.arguments[1]; 21 if (!isArrowFunctionWithTemplateLiteral(msg)) 22 return; 23 24 // Checks to see if order of arguments to function is the same as the 25 // order of them being concatenated in the template string. The idea is 26 // that if both match, then you can use `util.format`-style args. 27 // Would pass rule: (a, b) => `${b}${a}`. 28 // Would fail rule: (a, b) => `${a}${b}`, and needs to be rewritten. 29 const { expressions } = msg.body; 30 const hasSequentialParams = msg.params.every((param, index) => { 31 const expr = expressions[index]; 32 return expr && expr.type === 'Identifier' && param.name === expr.name; 33 }); 34 if (hasSequentialParams) 35 context.report(msg, errMsg); 36 } 37 }; 38 } 39}; 40