blob: f6993e627782dd2552e64bd52c673a8979b065ba (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
'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;
// 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);
}
};
}
};
|