1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.getWrappingFixer = void 0;
|
4 | const utils_1 = require("@typescript-eslint/utils");
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | function getWrappingFixer(params) {
|
10 | const { sourceCode, node, innerNode = node, wrap } = params;
|
11 | const innerNodes = Array.isArray(innerNode) ? innerNode : [innerNode];
|
12 | return (fixer) => {
|
13 | const innerCodes = innerNodes.map(innerNode => {
|
14 | let code = sourceCode.getText(innerNode);
|
15 |
|
16 | if (!isStrongPrecedenceNode(innerNode)) {
|
17 |
|
18 |
|
19 | code = `(${code})`;
|
20 | }
|
21 | return code;
|
22 | });
|
23 |
|
24 | let code = wrap(...innerCodes);
|
25 |
|
26 | if (isWeakPrecedenceParent(node)) {
|
27 |
|
28 |
|
29 | if (!utils_1.ASTUtils.isParenthesized(node, sourceCode)) {
|
30 | code = `(${code})`;
|
31 | }
|
32 | }
|
33 |
|
34 | if (/^[`([]/.exec(code) && isMissingSemicolonBefore(node, sourceCode)) {
|
35 | code = `;${code}`;
|
36 | }
|
37 | return fixer.replaceText(node, code);
|
38 | };
|
39 | }
|
40 | exports.getWrappingFixer = getWrappingFixer;
|
41 |
|
42 |
|
43 |
|
44 | function isStrongPrecedenceNode(innerNode) {
|
45 | return (innerNode.type === utils_1.AST_NODE_TYPES.Literal ||
|
46 | innerNode.type === utils_1.AST_NODE_TYPES.Identifier ||
|
47 | innerNode.type === utils_1.AST_NODE_TYPES.ArrayExpression ||
|
48 | innerNode.type === utils_1.AST_NODE_TYPES.ObjectExpression ||
|
49 | innerNode.type === utils_1.AST_NODE_TYPES.MemberExpression ||
|
50 | innerNode.type === utils_1.AST_NODE_TYPES.CallExpression ||
|
51 | innerNode.type === utils_1.AST_NODE_TYPES.NewExpression ||
|
52 | innerNode.type === utils_1.AST_NODE_TYPES.TaggedTemplateExpression);
|
53 | }
|
54 |
|
55 |
|
56 |
|
57 | function isWeakPrecedenceParent(node) {
|
58 | const parent = node.parent;
|
59 | if (parent.type === utils_1.AST_NODE_TYPES.UpdateExpression ||
|
60 | parent.type === utils_1.AST_NODE_TYPES.UnaryExpression ||
|
61 | parent.type === utils_1.AST_NODE_TYPES.BinaryExpression ||
|
62 | parent.type === utils_1.AST_NODE_TYPES.LogicalExpression ||
|
63 | parent.type === utils_1.AST_NODE_TYPES.ConditionalExpression ||
|
64 | parent.type === utils_1.AST_NODE_TYPES.AwaitExpression) {
|
65 | return true;
|
66 | }
|
67 | if (parent.type === utils_1.AST_NODE_TYPES.MemberExpression &&
|
68 | parent.object === node) {
|
69 | return true;
|
70 | }
|
71 | if ((parent.type === utils_1.AST_NODE_TYPES.CallExpression ||
|
72 | parent.type === utils_1.AST_NODE_TYPES.NewExpression) &&
|
73 | parent.callee === node) {
|
74 | return true;
|
75 | }
|
76 | if (parent.type === utils_1.AST_NODE_TYPES.TaggedTemplateExpression &&
|
77 | parent.tag === node) {
|
78 | return true;
|
79 | }
|
80 | return false;
|
81 | }
|
82 |
|
83 |
|
84 |
|
85 |
|
86 | function isMissingSemicolonBefore(node, sourceCode) {
|
87 | for (;;) {
|
88 | const parent = node.parent;
|
89 | if (parent.type === utils_1.AST_NODE_TYPES.ExpressionStatement) {
|
90 | const block = parent.parent;
|
91 | if (block.type === utils_1.AST_NODE_TYPES.Program ||
|
92 | block.type === utils_1.AST_NODE_TYPES.BlockStatement) {
|
93 |
|
94 | const statementIndex = block.body.indexOf(parent);
|
95 | const previousStatement = block.body[statementIndex - 1];
|
96 | if (statementIndex > 0 &&
|
97 | sourceCode.getLastToken(previousStatement).value !== ';') {
|
98 | return true;
|
99 | }
|
100 | }
|
101 | }
|
102 | if (!isLeftHandSide(node)) {
|
103 | return false;
|
104 | }
|
105 | node = parent;
|
106 | }
|
107 | }
|
108 |
|
109 |
|
110 |
|
111 | function isLeftHandSide(node) {
|
112 | const parent = node.parent;
|
113 |
|
114 | if (parent.type === utils_1.AST_NODE_TYPES.UpdateExpression) {
|
115 | return true;
|
116 | }
|
117 |
|
118 | if ((parent.type === utils_1.AST_NODE_TYPES.BinaryExpression ||
|
119 | parent.type === utils_1.AST_NODE_TYPES.LogicalExpression ||
|
120 | parent.type === utils_1.AST_NODE_TYPES.AssignmentExpression) &&
|
121 | node === parent.left) {
|
122 | return true;
|
123 | }
|
124 |
|
125 | if (parent.type === utils_1.AST_NODE_TYPES.ConditionalExpression &&
|
126 | node === parent.test) {
|
127 | return true;
|
128 | }
|
129 |
|
130 | if (parent.type === utils_1.AST_NODE_TYPES.CallExpression && node === parent.callee) {
|
131 | return true;
|
132 | }
|
133 |
|
134 | if (parent.type === utils_1.AST_NODE_TYPES.TaggedTemplateExpression &&
|
135 | node === parent.tag) {
|
136 | return true;
|
137 | }
|
138 | return false;
|
139 | }
|
140 |
|
\ | No newline at end of file |