-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
no-unnecessary-ast-path-call.js
56 lines (49 loc) · 1.4 KB
/
no-unnecessary-ast-path-call.js
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
45
46
47
48
49
50
51
52
53
54
55
56
"use strict";
const selector = [
"CallExpression",
"[optional=false]",
'[callee.type="MemberExpression"]',
"[callee.computed=false]",
"[callee.optional=false]",
'[callee.property.type="Identifier"]',
'[callee.property.name="call"]',
"[arguments.length=1]",
'[arguments.0.type!="SpreadElement"]',
].join("");
const messageId = "no-unnecessary-ast-path-call";
module.exports = {
meta: {
type: "suggestion",
docs: {
url: "https://github.com/prettier/prettier/blob/main/scripts/tools/eslint-plugin-prettier-internal-rules/no-unnecessary-ast-path-call.js",
},
messages: {
[messageId]: "Do not use `AstPath.call()` with one argument.",
},
fixable: "code",
},
create(context) {
const sourceCode = context.getSourceCode();
return {
[selector](node) {
const problem = {
node,
messageId,
};
const [callback] = node.arguments;
// Don't fix to IIFE
if (
callback.type !== "ArrowFunctionExpression" &&
callback.type !== "FunctionExpression"
) {
problem.fix = function (fixer) {
const callbackText = sourceCode.getText(callback);
const astPathText = sourceCode.getText(node.callee.object);
return fixer.replaceText(node, `${callbackText}(${astPathText})`);
};
}
context.report(problem);
},
};
},
};