1 | "use strict";
|
---|
2 |
|
---|
3 | Object.defineProperty(exports, "__esModule", {
|
---|
4 | value: true
|
---|
5 | });
|
---|
6 | exports.list = exports.nodes = void 0;
|
---|
7 |
|
---|
8 | var t = require("@babel/types");
|
---|
9 |
|
---|
10 | function crawl(node, state = {}) {
|
---|
11 | if (t.isMemberExpression(node) || t.isOptionalMemberExpression(node)) {
|
---|
12 | crawl(node.object, state);
|
---|
13 | if (node.computed) crawl(node.property, state);
|
---|
14 | } else if (t.isBinary(node) || t.isAssignmentExpression(node)) {
|
---|
15 | crawl(node.left, state);
|
---|
16 | crawl(node.right, state);
|
---|
17 | } else if (t.isCallExpression(node) || t.isOptionalCallExpression(node)) {
|
---|
18 | state.hasCall = true;
|
---|
19 | crawl(node.callee, state);
|
---|
20 | } else if (t.isFunction(node)) {
|
---|
21 | state.hasFunction = true;
|
---|
22 | } else if (t.isIdentifier(node)) {
|
---|
23 | state.hasHelper = state.hasHelper || isHelper(node.callee);
|
---|
24 | }
|
---|
25 |
|
---|
26 | return state;
|
---|
27 | }
|
---|
28 |
|
---|
29 | function isHelper(node) {
|
---|
30 | if (t.isMemberExpression(node)) {
|
---|
31 | return isHelper(node.object) || isHelper(node.property);
|
---|
32 | } else if (t.isIdentifier(node)) {
|
---|
33 | return node.name === "require" || node.name[0] === "_";
|
---|
34 | } else if (t.isCallExpression(node)) {
|
---|
35 | return isHelper(node.callee);
|
---|
36 | } else if (t.isBinary(node) || t.isAssignmentExpression(node)) {
|
---|
37 | return t.isIdentifier(node.left) && isHelper(node.left) || isHelper(node.right);
|
---|
38 | } else {
|
---|
39 | return false;
|
---|
40 | }
|
---|
41 | }
|
---|
42 |
|
---|
43 | function isType(node) {
|
---|
44 | return t.isLiteral(node) || t.isObjectExpression(node) || t.isArrayExpression(node) || t.isIdentifier(node) || t.isMemberExpression(node);
|
---|
45 | }
|
---|
46 |
|
---|
47 | const nodes = {
|
---|
48 | AssignmentExpression(node) {
|
---|
49 | const state = crawl(node.right);
|
---|
50 |
|
---|
51 | if (state.hasCall && state.hasHelper || state.hasFunction) {
|
---|
52 | return {
|
---|
53 | before: state.hasFunction,
|
---|
54 | after: true
|
---|
55 | };
|
---|
56 | }
|
---|
57 | },
|
---|
58 |
|
---|
59 | SwitchCase(node, parent) {
|
---|
60 | return {
|
---|
61 | before: !!node.consequent.length || parent.cases[0] === node,
|
---|
62 | after: !node.consequent.length && parent.cases[parent.cases.length - 1] === node
|
---|
63 | };
|
---|
64 | },
|
---|
65 |
|
---|
66 | LogicalExpression(node) {
|
---|
67 | if (t.isFunction(node.left) || t.isFunction(node.right)) {
|
---|
68 | return {
|
---|
69 | after: true
|
---|
70 | };
|
---|
71 | }
|
---|
72 | },
|
---|
73 |
|
---|
74 | Literal(node) {
|
---|
75 | if (t.isStringLiteral(node) && node.value === "use strict") {
|
---|
76 | return {
|
---|
77 | after: true
|
---|
78 | };
|
---|
79 | }
|
---|
80 | },
|
---|
81 |
|
---|
82 | CallExpression(node) {
|
---|
83 | if (t.isFunction(node.callee) || isHelper(node)) {
|
---|
84 | return {
|
---|
85 | before: true,
|
---|
86 | after: true
|
---|
87 | };
|
---|
88 | }
|
---|
89 | },
|
---|
90 |
|
---|
91 | OptionalCallExpression(node) {
|
---|
92 | if (t.isFunction(node.callee)) {
|
---|
93 | return {
|
---|
94 | before: true,
|
---|
95 | after: true
|
---|
96 | };
|
---|
97 | }
|
---|
98 | },
|
---|
99 |
|
---|
100 | VariableDeclaration(node) {
|
---|
101 | for (let i = 0; i < node.declarations.length; i++) {
|
---|
102 | const declar = node.declarations[i];
|
---|
103 | let enabled = isHelper(declar.id) && !isType(declar.init);
|
---|
104 |
|
---|
105 | if (!enabled) {
|
---|
106 | const state = crawl(declar.init);
|
---|
107 | enabled = isHelper(declar.init) && state.hasCall || state.hasFunction;
|
---|
108 | }
|
---|
109 |
|
---|
110 | if (enabled) {
|
---|
111 | return {
|
---|
112 | before: true,
|
---|
113 | after: true
|
---|
114 | };
|
---|
115 | }
|
---|
116 | }
|
---|
117 | },
|
---|
118 |
|
---|
119 | IfStatement(node) {
|
---|
120 | if (t.isBlockStatement(node.consequent)) {
|
---|
121 | return {
|
---|
122 | before: true,
|
---|
123 | after: true
|
---|
124 | };
|
---|
125 | }
|
---|
126 | }
|
---|
127 |
|
---|
128 | };
|
---|
129 | exports.nodes = nodes;
|
---|
130 |
|
---|
131 | nodes.ObjectProperty = nodes.ObjectTypeProperty = nodes.ObjectMethod = function (node, parent) {
|
---|
132 | if (parent.properties[0] === node) {
|
---|
133 | return {
|
---|
134 | before: true
|
---|
135 | };
|
---|
136 | }
|
---|
137 | };
|
---|
138 |
|
---|
139 | nodes.ObjectTypeCallProperty = function (node, parent) {
|
---|
140 | var _parent$properties;
|
---|
141 |
|
---|
142 | if (parent.callProperties[0] === node && !((_parent$properties = parent.properties) != null && _parent$properties.length)) {
|
---|
143 | return {
|
---|
144 | before: true
|
---|
145 | };
|
---|
146 | }
|
---|
147 | };
|
---|
148 |
|
---|
149 | nodes.ObjectTypeIndexer = function (node, parent) {
|
---|
150 | var _parent$properties2, _parent$callPropertie;
|
---|
151 |
|
---|
152 | if (parent.indexers[0] === node && !((_parent$properties2 = parent.properties) != null && _parent$properties2.length) && !((_parent$callPropertie = parent.callProperties) != null && _parent$callPropertie.length)) {
|
---|
153 | return {
|
---|
154 | before: true
|
---|
155 | };
|
---|
156 | }
|
---|
157 | };
|
---|
158 |
|
---|
159 | nodes.ObjectTypeInternalSlot = function (node, parent) {
|
---|
160 | var _parent$properties3, _parent$callPropertie2, _parent$indexers;
|
---|
161 |
|
---|
162 | if (parent.internalSlots[0] === node && !((_parent$properties3 = parent.properties) != null && _parent$properties3.length) && !((_parent$callPropertie2 = parent.callProperties) != null && _parent$callPropertie2.length) && !((_parent$indexers = parent.indexers) != null && _parent$indexers.length)) {
|
---|
163 | return {
|
---|
164 | before: true
|
---|
165 | };
|
---|
166 | }
|
---|
167 | };
|
---|
168 |
|
---|
169 | const list = {
|
---|
170 | VariableDeclaration(node) {
|
---|
171 | return node.declarations.map(decl => decl.init);
|
---|
172 | },
|
---|
173 |
|
---|
174 | ArrayExpression(node) {
|
---|
175 | return node.elements;
|
---|
176 | },
|
---|
177 |
|
---|
178 | ObjectExpression(node) {
|
---|
179 | return node.properties;
|
---|
180 | }
|
---|
181 |
|
---|
182 | };
|
---|
183 | exports.list = list;
|
---|
184 | [["Function", true], ["Class", true], ["Loop", true], ["LabeledStatement", true], ["SwitchStatement", true], ["TryStatement", true]].forEach(function ([type, amounts]) {
|
---|
185 | if (typeof amounts === "boolean") {
|
---|
186 | amounts = {
|
---|
187 | after: amounts,
|
---|
188 | before: amounts
|
---|
189 | };
|
---|
190 | }
|
---|
191 |
|
---|
192 | [type].concat(t.FLIPPED_ALIAS_KEYS[type] || []).forEach(function (type) {
|
---|
193 | nodes[type] = function () {
|
---|
194 | return amounts;
|
---|
195 | };
|
---|
196 | });
|
---|
197 | }); |
---|