forked from typescript-eslint/typescript-eslint
/
getWrappingFixer.ts
211 lines (184 loc) · 5.66 KB
/
getWrappingFixer.ts
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import {
AST_NODE_TYPES,
TSESLint,
ASTUtils,
TSESTree,
} from '@typescript-eslint/experimental-utils';
interface WrappingFixerParams {
/** Source code. */
sourceCode: Readonly<TSESLint.SourceCode>;
/** The node we want to modify. */
node: TSESTree.Node;
/**
* Descendant of `node` we want to preserve.
* Use this to replace some code with another.
* By default it's the node we are modifying (so nothing is removed).
* You can pass multiple nodes as an array.
*/
innerNode?: TSESTree.Node | TSESTree.Node[];
/**
* The function which gets the code of the `innerNode` and returns some code around it.
* Receives multiple arguments if there are multiple innerNodes.
* E.g. ``code => `${code} != null` ``
*/
wrap: (...code: string[]) => string;
}
/**
* Wraps node with some code. Adds parenthesis as necessary.
* @returns Fixer which adds the specified code and parens if necessary.
*/
export function getWrappingFixer(
params: WrappingFixerParams,
): TSESLint.ReportFixFunction {
const { sourceCode, node, innerNode = node, wrap } = params;
const innerNodes = Array.isArray(innerNode) ? innerNode : [innerNode];
return (fixer): TSESLint.RuleFix => {
const innerCodes = innerNodes.map(innerNode => {
let code = sourceCode.getText(innerNode);
// check the inner expression's precedence
if (!isStrongPrecedenceNode(innerNode)) {
// the code we are adding might have stronger precedence than our wrapped node
// let's wrap our node in parens in case it has a weaker precedence than the code we are wrapping it in
code = `(${code})`;
}
return code;
});
// do the wrapping
let code = wrap(...innerCodes);
// check the outer expression's precedence
if (isWeakPrecedenceParent(node)) {
// we wrapped the node in some expression which very likely has a different precedence than original wrapped node
// let's wrap the whole expression in parens just in case
if (!ASTUtils.isParenthesized(node, sourceCode)) {
code = `(${code})`;
}
}
// check if we need to insert semicolon
if (/^[`([]/.exec(code) && isMissingSemicolonBefore(node, sourceCode)) {
code = `;${code}`;
}
return fixer.replaceText(node, code);
};
}
/**
* Check if a node will always have the same precedence if it's parent changes.
*/
function isStrongPrecedenceNode(innerNode: TSESTree.Node): boolean {
return (
innerNode.type === AST_NODE_TYPES.Literal ||
innerNode.type === AST_NODE_TYPES.Identifier ||
innerNode.type === AST_NODE_TYPES.ArrayExpression ||
innerNode.type === AST_NODE_TYPES.ObjectExpression ||
innerNode.type === AST_NODE_TYPES.MemberExpression ||
innerNode.type === AST_NODE_TYPES.CallExpression ||
innerNode.type === AST_NODE_TYPES.NewExpression ||
innerNode.type === AST_NODE_TYPES.TaggedTemplateExpression
);
}
/**
* Check if a node's parent could have different precedence if the node changes.
*/
function isWeakPrecedenceParent(node: TSESTree.Node): boolean {
const parent = node.parent!;
if (
parent.type === AST_NODE_TYPES.UpdateExpression ||
parent.type === AST_NODE_TYPES.UnaryExpression ||
parent.type === AST_NODE_TYPES.BinaryExpression ||
parent.type === AST_NODE_TYPES.LogicalExpression ||
parent.type === AST_NODE_TYPES.ConditionalExpression ||
parent.type === AST_NODE_TYPES.AwaitExpression
) {
return true;
}
if (
parent.type === AST_NODE_TYPES.MemberExpression &&
parent.object === node
) {
return true;
}
if (
(parent.type === AST_NODE_TYPES.CallExpression ||
parent.type === AST_NODE_TYPES.NewExpression) &&
parent.callee === node
) {
return true;
}
if (
parent.type === AST_NODE_TYPES.TaggedTemplateExpression &&
parent.tag === node
) {
return true;
}
return false;
}
/**
* Returns true if a node is at the beginning of expression statement and the statement above doesn't end with semicolon.
* Doesn't check if the node begins with `(`, `[` or `` ` ``.
*/
function isMissingSemicolonBefore(
node: TSESTree.Node,
sourceCode: TSESLint.SourceCode,
): boolean {
for (;;) {
const parent = node.parent!;
if (parent.type === AST_NODE_TYPES.ExpressionStatement) {
const block = parent.parent!;
if (
block.type === AST_NODE_TYPES.Program ||
block.type === AST_NODE_TYPES.BlockStatement
) {
// parent is an expression statement in a block
const statementIndex = block.body.indexOf(parent);
const previousStatement = block.body[statementIndex - 1];
if (
statementIndex > 0 &&
sourceCode.getLastToken(previousStatement)!.value !== ';'
) {
return true;
}
}
}
if (!isLeftHandSide(node)) {
return false;
}
node = parent;
}
}
/**
* Checks if a node is LHS of an operator.
*/
function isLeftHandSide(node: TSESTree.Node): boolean {
const parent = node.parent!;
// a++
if (parent.type === AST_NODE_TYPES.UpdateExpression) {
return true;
}
// a + b
if (
(parent.type === AST_NODE_TYPES.BinaryExpression ||
parent.type === AST_NODE_TYPES.LogicalExpression ||
parent.type === AST_NODE_TYPES.AssignmentExpression) &&
node === parent.left
) {
return true;
}
// a ? b : c
if (
parent.type === AST_NODE_TYPES.ConditionalExpression &&
node === parent.test
) {
return true;
}
// a(b)
if (parent.type === AST_NODE_TYPES.CallExpression && node === parent.callee) {
return true;
}
// a`b`
if (
parent.type === AST_NODE_TYPES.TaggedTemplateExpression &&
node === parent.tag
) {
return true;
}
return false;
}