@tarojs/transformer-wx 2.2.19 → 3.6.19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +140 -1
- package/index.js +1 -0
- package/lib/src/adapter.js +24 -15
- package/lib/src/adapter.js.map +1 -1
- package/lib/src/class-method-renamer.js +13 -12
- package/lib/src/class-method-renamer.js.map +1 -1
- package/lib/src/class.js +203 -181
- package/lib/src/class.js.map +1 -1
- package/lib/src/constant.js +26 -26
- package/lib/src/constant.js.map +1 -1
- package/lib/src/create-html-element.js +20 -15
- package/lib/src/create-html-element.js.map +1 -1
- package/lib/src/eslint.js +20 -16
- package/lib/src/eslint.js.map +1 -1
- package/lib/src/functional.js +38 -43
- package/lib/src/functional.js.map +1 -1
- package/lib/src/index.js +165 -125
- package/lib/src/index.js.map +1 -1
- package/lib/src/jsx.js +71 -57
- package/lib/src/jsx.js.map +1 -1
- package/lib/src/lifecycle.js +2 -2
- package/lib/src/options.js +11 -9
- package/lib/src/options.js.map +1 -1
- package/lib/src/plugins/remove-dead-code.js +111 -143
- package/lib/src/plugins/remove-dead-code.js.map +1 -1
- package/lib/src/plugins.js +2 -2
- package/lib/src/plugins.js.map +1 -1
- package/lib/src/render-props.js +28 -22
- package/lib/src/render-props.js.map +1 -1
- package/lib/src/render.js +1763 -1719
- package/lib/src/render.js.map +1 -1
- package/lib/src/utils.js +115 -116
- package/lib/src/utils.js.map +1 -1
- package/lib/src/wxs.js +72 -0
- package/lib/src/wxs.js.map +1 -0
- package/package.json +37 -24
- package/CHANGELOG.md +0 -43
package/lib/src/utils.js
CHANGED
|
@@ -1,21 +1,27 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.setAncestorCondition = exports.findParentLoops = exports.genCompid = exports.findIdentifierFromStatement = exports.toLetters = exports.isEmptyDeclarator = exports.reverseBoolean = exports.isAllLiteral = exports.getArgumentName = exports.generateMemberExpressionArray = exports.findFirstIdentifierFromMemberExpression = exports.hasComplexExpression = exports.isContainJSXElement = exports.getSlotName = exports.newJSXIfAttr = exports.buildJSXAttr = exports.isNumeric = exports.buildCodeFrame = exports.isBlockIfStatement = exports.createRandomLetters = exports.createUUID = exports.codeFrameError = exports.setting = exports.pathResolver = exports.isContainFunction = exports.setTemplate = exports.buildConstVariableDeclaration = exports.isArrayMapCallExpression = exports.generateAnonymousState = exports.setParentCondition = exports.findMethodName = exports.isVarName = exports.decodeUnicode = exports.isContainStopPropagation = exports.getSuperClassPath = exports.getSuperClassCode = exports.noop = exports.incrementId = exports.isDerivedFromThis = exports.isDerivedFromProps = exports.replaceJSXTextWithTextComponent = void 0;
|
|
4
|
-
const t = require("babel-types");
|
|
5
|
-
const babel_generator_1 = require("babel-generator");
|
|
6
4
|
const code_frame_1 = require("@babel/code-frame");
|
|
7
|
-
const
|
|
8
|
-
const
|
|
5
|
+
const generator_1 = require("@babel/generator");
|
|
6
|
+
const t = require("@babel/types");
|
|
9
7
|
const fs = require("fs");
|
|
8
|
+
const lodash_1 = require("lodash");
|
|
10
9
|
const path = require("path");
|
|
11
|
-
const jsx_1 = require("./jsx");
|
|
12
10
|
const adapter_1 = require("./adapter");
|
|
11
|
+
const constant_1 = require("./constant");
|
|
12
|
+
const jsx_1 = require("./jsx");
|
|
13
13
|
const options_1 = require("./options");
|
|
14
|
-
const template = require('babel-template')
|
|
14
|
+
// const template = require('babel-template')
|
|
15
|
+
const template = require('@babel/template');
|
|
15
16
|
function replaceJSXTextWithTextComponent(path) {
|
|
16
|
-
const parent = path.findParent(p => p.isJSXElement());
|
|
17
|
-
if (parent &&
|
|
18
|
-
|
|
17
|
+
const parent = path.findParent((p) => p.isJSXElement());
|
|
18
|
+
if (parent &&
|
|
19
|
+
parent.isJSXElement() &&
|
|
20
|
+
t.isJSXIdentifier(parent.node.openingElement.name) &&
|
|
21
|
+
parent.node.openingElement.name.name !== 'Text') {
|
|
22
|
+
path.replaceWith(t.jSXElement(t.jSXOpeningElement(t.jSXIdentifier('Text'), []), t.jSXClosingElement(t.jSXIdentifier('Text')), [
|
|
23
|
+
path.isJSXText() ? t.jSXText(path.node.value) : path.node,
|
|
24
|
+
]));
|
|
19
25
|
}
|
|
20
26
|
}
|
|
21
27
|
exports.replaceJSXTextWithTextComponent = replaceJSXTextWithTextComponent;
|
|
@@ -23,14 +29,14 @@ function isDerivedFromProps(scope, bindingName) {
|
|
|
23
29
|
const binding = scope.getBinding(bindingName);
|
|
24
30
|
if (binding && binding.path.isVariableDeclarator()) {
|
|
25
31
|
const init = binding.path.get('init');
|
|
26
|
-
if (
|
|
27
|
-
const { object, property } = init
|
|
32
|
+
if (t.isMemberExpression(init)) {
|
|
33
|
+
const { object, property } = init;
|
|
28
34
|
if (t.isThisExpression(object) && t.isIdentifier(property, { name: 'props' })) {
|
|
29
35
|
return true;
|
|
30
36
|
}
|
|
31
37
|
}
|
|
32
|
-
if (
|
|
33
|
-
return isDerivedFromProps(scope, init.
|
|
38
|
+
if (t.isIdentifier(init)) {
|
|
39
|
+
return isDerivedFromProps(scope, init.name);
|
|
34
40
|
}
|
|
35
41
|
}
|
|
36
42
|
return false;
|
|
@@ -40,19 +46,21 @@ function isDerivedFromThis(scope, bindingName) {
|
|
|
40
46
|
const binding = scope.getBinding(bindingName);
|
|
41
47
|
if (binding && binding.path.isVariableDeclarator()) {
|
|
42
48
|
const init = binding.path.get('init');
|
|
43
|
-
if (
|
|
49
|
+
if (t.isThisExpression(init)) {
|
|
44
50
|
return true;
|
|
45
51
|
}
|
|
46
52
|
}
|
|
47
53
|
return false;
|
|
48
54
|
}
|
|
49
55
|
exports.isDerivedFromThis = isDerivedFromThis;
|
|
50
|
-
|
|
56
|
+
const incrementId = () => {
|
|
51
57
|
let id = 0;
|
|
52
58
|
return () => id++;
|
|
53
59
|
};
|
|
60
|
+
exports.incrementId = incrementId;
|
|
54
61
|
// tslint:disable-next-line:no-empty
|
|
55
|
-
|
|
62
|
+
const noop = function () { };
|
|
63
|
+
exports.noop = noop;
|
|
56
64
|
function getSuperClassCode(path) {
|
|
57
65
|
const obj = getSuperClassPath(path);
|
|
58
66
|
if (obj) {
|
|
@@ -61,12 +69,10 @@ function getSuperClassCode(path) {
|
|
|
61
69
|
const code = fs.readFileSync(resolvePath + (options_1.transformOptions.isTyped ? '.tsx' : '.js'), 'utf8');
|
|
62
70
|
return {
|
|
63
71
|
code,
|
|
64
|
-
sourcePath: sourceValue
|
|
72
|
+
sourcePath: sourceValue,
|
|
65
73
|
};
|
|
66
74
|
}
|
|
67
|
-
catch (error) {
|
|
68
|
-
return;
|
|
69
|
-
}
|
|
75
|
+
catch (error) { }
|
|
70
76
|
}
|
|
71
77
|
}
|
|
72
78
|
exports.getSuperClassCode = getSuperClassCode;
|
|
@@ -76,14 +82,14 @@ function getSuperClassPath(path) {
|
|
|
76
82
|
const binding = path.scope.getBinding(superClass.name);
|
|
77
83
|
if (binding && binding.kind === 'module') {
|
|
78
84
|
const bindingPath = binding.path.parentPath;
|
|
79
|
-
if (
|
|
85
|
+
if (t.isImportDeclaration(bindingPath)) {
|
|
80
86
|
const source = bindingPath.node.source;
|
|
81
87
|
if (source.value === constant_1.TARO_PACKAGE_NAME) {
|
|
82
88
|
return;
|
|
83
89
|
}
|
|
84
90
|
return {
|
|
85
91
|
sourceValue: source.value,
|
|
86
|
-
resolvePath: pathResolver(source.value, options_1.transformOptions.sourcePath)
|
|
92
|
+
resolvePath: pathResolver(source.value, options_1.transformOptions.sourcePath),
|
|
87
93
|
};
|
|
88
94
|
}
|
|
89
95
|
}
|
|
@@ -95,11 +101,10 @@ function isContainStopPropagation(path) {
|
|
|
95
101
|
if (path) {
|
|
96
102
|
const visitor = {
|
|
97
103
|
Identifier(p) {
|
|
98
|
-
if (p.node.name === 'stopPropagation' &&
|
|
99
|
-
p.parentPath.parentPath.isCallExpression()) {
|
|
104
|
+
if (p.node.name === 'stopPropagation' && p.parentPath.parentPath.isCallExpression()) {
|
|
100
105
|
matched = true;
|
|
101
106
|
}
|
|
102
|
-
}
|
|
107
|
+
},
|
|
103
108
|
};
|
|
104
109
|
if (path.isIdentifier()) {
|
|
105
110
|
const binding = path.scope.getBinding(path.node.name);
|
|
@@ -137,16 +142,14 @@ function isVarName(str) {
|
|
|
137
142
|
exports.isVarName = isVarName;
|
|
138
143
|
function findMethodName(expression) {
|
|
139
144
|
let methodName;
|
|
140
|
-
if (t.isIdentifier(expression) ||
|
|
141
|
-
t.isJSXIdentifier(expression)) {
|
|
145
|
+
if (t.isIdentifier(expression) || t.isJSXIdentifier(expression)) {
|
|
142
146
|
methodName = expression.name;
|
|
143
147
|
}
|
|
144
148
|
else if (t.isStringLiteral(expression)) {
|
|
145
149
|
methodName = expression.value;
|
|
146
150
|
}
|
|
147
|
-
else if (t.isMemberExpression(expression) &&
|
|
148
|
-
|
|
149
|
-
const { code } = babel_generator_1.default(expression);
|
|
151
|
+
else if (t.isMemberExpression(expression) && t.isIdentifier(expression.property)) {
|
|
152
|
+
const { code } = (0, generator_1.default)(expression);
|
|
150
153
|
const ids = code.split('.');
|
|
151
154
|
if (ids[0] === 'this' && ids[1] === 'props' && ids[2]) {
|
|
152
155
|
methodName = code.replace('this.props.', '');
|
|
@@ -175,29 +178,29 @@ function findMethodName(expression) {
|
|
|
175
178
|
}
|
|
176
179
|
exports.findMethodName = findMethodName;
|
|
177
180
|
function setParentCondition(jsx, expr, array = false) {
|
|
178
|
-
const conditionExpr = jsx.findParent(p => p.isConditionalExpression());
|
|
179
|
-
const logicExpr = jsx.findParent(p => p.isLogicalExpression({ operator: '&&' }));
|
|
181
|
+
const conditionExpr = jsx.findParent((p) => p.isConditionalExpression());
|
|
182
|
+
const logicExpr = jsx.findParent((p) => p.isLogicalExpression({ operator: '&&' }));
|
|
180
183
|
if (array) {
|
|
181
|
-
const ifAttrSet = new Set([
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
]);
|
|
185
|
-
const logicalJSX = jsx.findParent(p => p.isJSXElement() && p.node.openingElement.attributes.some(a => t.isJSXIdentifier(a.name) && ifAttrSet.has(a.name.name)));
|
|
184
|
+
const ifAttrSet = new Set([adapter_1.Adapter.if, adapter_1.Adapter.else]);
|
|
185
|
+
const logicalJSX = jsx.findParent((p) => p.isJSXElement() &&
|
|
186
|
+
p.node.openingElement.attributes.some((a) => t.isJSXAttribute(a) && t.isJSXIdentifier(a.name) && ifAttrSet.has(a.name.name)));
|
|
186
187
|
if (logicalJSX) {
|
|
187
|
-
const attr = logicalJSX.node.openingElement.attributes.find(a => ifAttrSet.has(a.name.name));
|
|
188
|
+
const attr = logicalJSX.node.openingElement.attributes.find((a) => t.isJSXAttribute(a) && ifAttrSet.has(a.name.name));
|
|
188
189
|
if (attr) {
|
|
189
|
-
if (attr.name.name === adapter_1.Adapter.else) {
|
|
190
|
+
if (t.isJSXAttribute(attr) && attr.name.name === adapter_1.Adapter.else) {
|
|
190
191
|
const prevElement = logicalJSX.getPrevSibling();
|
|
191
192
|
if (prevElement && prevElement.isJSXElement()) {
|
|
192
|
-
const attr = prevElement.node.openingElement.attributes.find(a => a.name.name === adapter_1.Adapter.if);
|
|
193
|
-
if (attr && t.isJSXExpressionContainer(attr.value)) {
|
|
194
|
-
expr = t.conditionalExpression(reverseBoolean(lodash_1.cloneDeep(attr.value.expression)), expr, t.arrayExpression());
|
|
193
|
+
const attr = prevElement.node.openingElement.attributes.find((a) => t.isJSXAttribute(a) && a.name.name === adapter_1.Adapter.if);
|
|
194
|
+
if (attr && t.isJSXAttribute(attr) && t.isJSXExpressionContainer(attr.value)) {
|
|
195
|
+
expr = t.conditionalExpression(reverseBoolean((0, lodash_1.cloneDeep)(attr.value.expression)), expr, t.arrayExpression());
|
|
195
196
|
return expr;
|
|
196
197
|
}
|
|
197
198
|
}
|
|
198
199
|
}
|
|
199
|
-
else if (t.
|
|
200
|
-
|
|
200
|
+
else if (t.isJSXAttribute(attr) &&
|
|
201
|
+
t.isJSXExpressionContainer(attr.value) &&
|
|
202
|
+
!t.isJSXEmptyExpression(attr.value)) {
|
|
203
|
+
expr = t.conditionalExpression((0, lodash_1.cloneDeep)(attr.value.expression), expr, t.arrayExpression());
|
|
201
204
|
return expr;
|
|
202
205
|
}
|
|
203
206
|
}
|
|
@@ -205,14 +208,14 @@ function setParentCondition(jsx, expr, array = false) {
|
|
|
205
208
|
}
|
|
206
209
|
if (conditionExpr && conditionExpr.isConditionalExpression()) {
|
|
207
210
|
const consequent = conditionExpr.get('consequent');
|
|
208
|
-
if (consequent === jsx || jsx.findParent(p => p === consequent)) {
|
|
209
|
-
expr = t.conditionalExpression(lodash_1.cloneDeep(conditionExpr.get('test').node), expr, array ? t.arrayExpression([]) : t.nullLiteral());
|
|
211
|
+
if (consequent === jsx || jsx.findParent((p) => p === consequent)) {
|
|
212
|
+
expr = t.conditionalExpression((0, lodash_1.cloneDeep)(conditionExpr.get('test').node), expr, array ? t.arrayExpression([]) : t.nullLiteral());
|
|
210
213
|
}
|
|
211
214
|
}
|
|
212
215
|
if (logicExpr && logicExpr.isLogicalExpression({ operator: '&&' })) {
|
|
213
216
|
const consequent = logicExpr.get('right');
|
|
214
|
-
if (consequent === jsx || jsx.findParent(p => p === consequent)) {
|
|
215
|
-
expr = t.conditionalExpression(lodash_1.cloneDeep(logicExpr.get('left').node), expr, array ? t.arrayExpression([]) : t.nullLiteral());
|
|
217
|
+
if (consequent === jsx || jsx.findParent((p) => p === consequent)) {
|
|
218
|
+
expr = t.conditionalExpression((0, lodash_1.cloneDeep)(logicExpr.get('left').node), expr, array ? t.arrayExpression([]) : t.nullLiteral());
|
|
216
219
|
}
|
|
217
220
|
}
|
|
218
221
|
return expr;
|
|
@@ -224,11 +227,11 @@ function generateAnonymousState(scope, expression, refIds, isLogical) {
|
|
|
224
227
|
if (!statementParent) {
|
|
225
228
|
throw codeFrameError(expression.node.loc, '无法生成匿名 State,尝试先把值赋到一个变量上再把变量调换。');
|
|
226
229
|
}
|
|
227
|
-
const jsx = isLogical ? expression : expression.findParent(p => p.isJSXElement());
|
|
228
|
-
const callExpr = jsx.findParent(p => p.isCallExpression() && isArrayMapCallExpression(p));
|
|
229
|
-
const ifExpr = jsx.findParent(p => p.isIfStatement());
|
|
230
|
-
const blockStatement = jsx.findParent(p => p.isBlockStatement() && p.parentPath === ifExpr);
|
|
231
|
-
const expr = setParentCondition(jsx, lodash_1.cloneDeep(expression.node));
|
|
230
|
+
const jsx = isLogical ? expression : expression.findParent((p) => p.isJSXElement());
|
|
231
|
+
const callExpr = jsx === null || jsx === void 0 ? void 0 : jsx.findParent((p) => p.isCallExpression() && isArrayMapCallExpression(p));
|
|
232
|
+
const ifExpr = jsx === null || jsx === void 0 ? void 0 : jsx.findParent((p) => p.isIfStatement());
|
|
233
|
+
const blockStatement = jsx === null || jsx === void 0 ? void 0 : jsx.findParent((p) => p.isBlockStatement() && p.parentPath === ifExpr);
|
|
234
|
+
const expr = setParentCondition(jsx, (0, lodash_1.cloneDeep)(expression.node));
|
|
232
235
|
if (!callExpr) {
|
|
233
236
|
refIds.add(t.identifier(variableName));
|
|
234
237
|
statementParent.insertBefore(buildConstVariableDeclaration(variableName, expr));
|
|
@@ -236,9 +239,12 @@ function generateAnonymousState(scope, expression, refIds, isLogical) {
|
|
|
236
239
|
blockStatement.traverse({
|
|
237
240
|
VariableDeclarator: (path) => {
|
|
238
241
|
const { id, init } = path.node;
|
|
239
|
-
const isArrowFunctionInJSX = path.findParent(p => p.isJSXAttribute() ||
|
|
240
|
-
(p.isAssignmentExpression() &&
|
|
241
|
-
|
|
242
|
+
const isArrowFunctionInJSX = path.findParent((p) => p.isJSXAttribute() ||
|
|
243
|
+
(p.isAssignmentExpression() &&
|
|
244
|
+
t.isMemberExpression(p.node.left) &&
|
|
245
|
+
t.isThisExpression(p.node.left.object) &&
|
|
246
|
+
t.isIdentifier(p.node.left.property) &&
|
|
247
|
+
p.node.left.property.name.startsWith('')));
|
|
242
248
|
if (isArrowFunctionInJSX) {
|
|
243
249
|
return;
|
|
244
250
|
}
|
|
@@ -251,7 +257,7 @@ function generateAnonymousState(scope, expression, refIds, isLogical) {
|
|
|
251
257
|
}
|
|
252
258
|
});
|
|
253
259
|
variableName = newId.name;
|
|
254
|
-
if (adapter_1.Adapter.type === "quickapp" /* quickapp */ && variableName.startsWith('_$')) {
|
|
260
|
+
if (adapter_1.Adapter.type === "quickapp" /* Adapters.quickapp */ && variableName.startsWith('_$')) {
|
|
255
261
|
const newVarName = variableName.slice(2);
|
|
256
262
|
scope.rename(variableName, newVarName);
|
|
257
263
|
variableName = newVarName;
|
|
@@ -260,7 +266,7 @@ function generateAnonymousState(scope, expression, refIds, isLogical) {
|
|
|
260
266
|
blockStatement.scope.rename(id.name, newId.name);
|
|
261
267
|
path.parentPath.replaceWith(template('ID = INIT;')({ ID: newId, INIT: init }));
|
|
262
268
|
}
|
|
263
|
-
}
|
|
269
|
+
},
|
|
264
270
|
});
|
|
265
271
|
}
|
|
266
272
|
}
|
|
@@ -269,21 +275,18 @@ function generateAnonymousState(scope, expression, refIds, isLogical) {
|
|
|
269
275
|
const func = callExpr.node.arguments[0];
|
|
270
276
|
if (t.isArrowFunctionExpression(func)) {
|
|
271
277
|
if (!t.isBlockStatement(func.body)) {
|
|
272
|
-
func.body = t.blockStatement([
|
|
273
|
-
buildConstVariableDeclaration(variableName, expr),
|
|
274
|
-
t.returnStatement(func.body)
|
|
275
|
-
]);
|
|
278
|
+
func.body = t.blockStatement([buildConstVariableDeclaration(variableName, expr), t.returnStatement(func.body)]);
|
|
276
279
|
}
|
|
277
280
|
else {
|
|
278
|
-
if (ifExpr && ifExpr.isIfStatement() && ifExpr.findParent(p => p === callExpr)) {
|
|
281
|
+
if (ifExpr && ifExpr.isIfStatement() && ifExpr.findParent((p) => p === callExpr)) {
|
|
279
282
|
const consequent = ifExpr.get('consequent');
|
|
280
283
|
const test = ifExpr.get('test');
|
|
281
|
-
if (
|
|
282
|
-
if (jsx === test || jsx.findParent(p => p === test)) {
|
|
284
|
+
if (t.isBlockStatement(consequent)) {
|
|
285
|
+
if ((jsx != null && jsx === test) || (jsx === null || jsx === void 0 ? void 0 : jsx.findParent((p) => p === test))) {
|
|
283
286
|
func.body.body.unshift(buildConstVariableDeclaration(variableName, expr));
|
|
284
287
|
}
|
|
285
288
|
else {
|
|
286
|
-
func.body.body.unshift(t.variableDeclaration('let', [t.variableDeclarator(t.identifier(variableName), t.nullLiteral())]))
|
|
289
|
+
// func.body.body.unshift(t.variableDeclaration('let', [t.variableDeclarator(t.identifier(variableName), t.nullLiteral())]))
|
|
287
290
|
consequent.node.body.push(t.expressionStatement(t.assignmentExpression('=', t.identifier(variableName), expr)));
|
|
288
291
|
}
|
|
289
292
|
}
|
|
@@ -303,21 +306,19 @@ function generateAnonymousState(scope, expression, refIds, isLogical) {
|
|
|
303
306
|
}
|
|
304
307
|
exports.generateAnonymousState = generateAnonymousState;
|
|
305
308
|
function isArrayMapCallExpression(callExpression) {
|
|
306
|
-
return callExpression &&
|
|
309
|
+
return (callExpression &&
|
|
307
310
|
t.isCallExpression(callExpression.node) &&
|
|
308
311
|
t.isMemberExpression(callExpression.node.callee) &&
|
|
309
|
-
t.isIdentifier(callExpression.node.callee.property, { name: 'map' });
|
|
312
|
+
t.isIdentifier(callExpression.node.callee.property, { name: 'map' }));
|
|
310
313
|
}
|
|
311
314
|
exports.isArrayMapCallExpression = isArrayMapCallExpression;
|
|
312
315
|
function buildConstVariableDeclaration(variableName, expresion) {
|
|
313
|
-
return t.variableDeclaration('const', [
|
|
314
|
-
t.variableDeclarator(t.identifier(variableName), expresion)
|
|
315
|
-
]);
|
|
316
|
+
return t.variableDeclaration('const', [t.variableDeclarator(t.identifier(variableName), expresion)]);
|
|
316
317
|
}
|
|
317
318
|
exports.buildConstVariableDeclaration = buildConstVariableDeclaration;
|
|
318
319
|
function setTemplate(name, path, templates) {
|
|
319
320
|
const parentPath = path.parentPath;
|
|
320
|
-
const jsxChildren = parentPath.findParent(p => p.isJSXElement());
|
|
321
|
+
const jsxChildren = parentPath === null || parentPath === void 0 ? void 0 : parentPath.findParent((p) => p.isJSXElement());
|
|
321
322
|
if (name && !jsxChildren) {
|
|
322
323
|
templates.set(name, path.node);
|
|
323
324
|
}
|
|
@@ -328,7 +329,7 @@ function isContainFunction(p) {
|
|
|
328
329
|
p.traverse({
|
|
329
330
|
CallExpression() {
|
|
330
331
|
bool = true;
|
|
331
|
-
}
|
|
332
|
+
},
|
|
332
333
|
});
|
|
333
334
|
return bool;
|
|
334
335
|
}
|
|
@@ -366,13 +367,13 @@ function pathResolver(source, location) {
|
|
|
366
367
|
}
|
|
367
368
|
exports.pathResolver = pathResolver;
|
|
368
369
|
exports.setting = {
|
|
369
|
-
sourceCode: ''
|
|
370
|
+
sourceCode: '',
|
|
370
371
|
};
|
|
371
372
|
function codeFrameError(node, msg) {
|
|
372
373
|
let errMsg = '';
|
|
373
374
|
try {
|
|
374
|
-
errMsg = code_frame_1.codeFrameColumns(exports.setting.sourceCode, node && node.type && node.loc ? node.loc : node, {
|
|
375
|
-
highlightCode: true
|
|
375
|
+
errMsg = (0, code_frame_1.codeFrameColumns)(exports.setting.sourceCode, node && node.type && node.loc ? node.loc : node, {
|
|
376
|
+
highlightCode: true,
|
|
376
377
|
});
|
|
377
378
|
}
|
|
378
379
|
catch (error) {
|
|
@@ -384,11 +385,15 @@ ${errMsg}`);
|
|
|
384
385
|
}
|
|
385
386
|
exports.codeFrameError = codeFrameError;
|
|
386
387
|
function createUUID() {
|
|
387
|
-
return '$' +
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
388
|
+
return ('$' +
|
|
389
|
+
'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'
|
|
390
|
+
.replace(/[xy]/g, function (c) {
|
|
391
|
+
let r = (Math.random() * 16) | 0;
|
|
392
|
+
let v = c === 'x' ? r : (r & 0x3) | 0x8;
|
|
393
|
+
return v.toString(16);
|
|
394
|
+
})
|
|
395
|
+
.replace(/-/g, '')
|
|
396
|
+
.slice(0, 8));
|
|
392
397
|
}
|
|
393
398
|
exports.createUUID = createUUID;
|
|
394
399
|
let count = 0;
|
|
@@ -406,13 +411,11 @@ function createRandomLetters(n) {
|
|
|
406
411
|
}
|
|
407
412
|
exports.createRandomLetters = createRandomLetters;
|
|
408
413
|
function isBlockIfStatement(ifStatement, blockStatement) {
|
|
409
|
-
return ifStatement && blockStatement &&
|
|
410
|
-
ifStatement.isIfStatement() &&
|
|
411
|
-
blockStatement.isBlockStatement();
|
|
414
|
+
return ifStatement && blockStatement && ifStatement.isIfStatement() && blockStatement.isBlockStatement();
|
|
412
415
|
}
|
|
413
416
|
exports.isBlockIfStatement = isBlockIfStatement;
|
|
414
417
|
function buildCodeFrame(code) {
|
|
415
|
-
return (loc) => code_frame_1.codeFrameColumns(code, loc);
|
|
418
|
+
return (loc) => (0, code_frame_1.codeFrameColumns)(code, loc);
|
|
416
419
|
}
|
|
417
420
|
exports.buildCodeFrame = buildCodeFrame;
|
|
418
421
|
function isNumeric(n) {
|
|
@@ -430,18 +433,18 @@ function newJSXIfAttr(jsx, value, path) {
|
|
|
430
433
|
}
|
|
431
434
|
if (element.name.name === 'Block' || element.name.name === 'block' || !path) {
|
|
432
435
|
const attrs = element.attributes;
|
|
433
|
-
if (!attrs.some(a => a.name.name === adapter_1.Adapter.for)) {
|
|
436
|
+
if (!attrs.some((a) => t.isJSXAttribute(a) && a.name.name === adapter_1.Adapter.for)) {
|
|
434
437
|
element.attributes.push(buildJSXAttr(adapter_1.Adapter.if, value));
|
|
435
438
|
}
|
|
436
439
|
else if (path) {
|
|
437
|
-
const block = jsx_1.buildBlockElement();
|
|
440
|
+
const block = (0, jsx_1.buildBlockElement)();
|
|
438
441
|
newJSXIfAttr(block, value);
|
|
439
442
|
block.children.push(jsx);
|
|
440
443
|
path.node = block;
|
|
441
444
|
}
|
|
442
445
|
}
|
|
443
446
|
else {
|
|
444
|
-
const block = jsx_1.buildBlockElement();
|
|
447
|
+
const block = (0, jsx_1.buildBlockElement)();
|
|
445
448
|
newJSXIfAttr(block, value);
|
|
446
449
|
block.children.push(jsx);
|
|
447
450
|
path.node = block;
|
|
@@ -458,7 +461,7 @@ function isContainJSXElement(path) {
|
|
|
458
461
|
JSXElement(p) {
|
|
459
462
|
matched = true;
|
|
460
463
|
p.stop();
|
|
461
|
-
}
|
|
464
|
+
},
|
|
462
465
|
});
|
|
463
466
|
return matched;
|
|
464
467
|
}
|
|
@@ -476,7 +479,7 @@ function hasComplexExpression(path) {
|
|
|
476
479
|
}
|
|
477
480
|
if (path.isArrayExpression()) {
|
|
478
481
|
const { elements } = path.node;
|
|
479
|
-
if (elements.some(el => t.isObjectExpression(el) || t.isArrayExpression(el))) {
|
|
482
|
+
if (elements.some((el) => t.isObjectExpression(el) || t.isArrayExpression(el))) {
|
|
480
483
|
return true;
|
|
481
484
|
}
|
|
482
485
|
}
|
|
@@ -495,7 +498,7 @@ function hasComplexExpression(path) {
|
|
|
495
498
|
},
|
|
496
499
|
ArrayExpression(p) {
|
|
497
500
|
const { elements } = p.node;
|
|
498
|
-
if (elements.some(el => t.isObjectExpression(el))) {
|
|
501
|
+
if (elements.some((el) => t.isObjectExpression(el))) {
|
|
499
502
|
return true;
|
|
500
503
|
}
|
|
501
504
|
},
|
|
@@ -504,7 +507,7 @@ function hasComplexExpression(path) {
|
|
|
504
507
|
p.stop();
|
|
505
508
|
},
|
|
506
509
|
MemberExpression(path) {
|
|
507
|
-
const jsxElement = path.findParent(p => p.isJSXExpressionContainer());
|
|
510
|
+
const jsxElement = path.findParent((p) => p.isJSXExpressionContainer());
|
|
508
511
|
const object = path.get('object');
|
|
509
512
|
const property = path.get('property');
|
|
510
513
|
const parentPath = path.parentPath;
|
|
@@ -519,7 +522,7 @@ function hasComplexExpression(path) {
|
|
|
519
522
|
path.stop();
|
|
520
523
|
}
|
|
521
524
|
}
|
|
522
|
-
}
|
|
525
|
+
},
|
|
523
526
|
});
|
|
524
527
|
return matched;
|
|
525
528
|
}
|
|
@@ -569,13 +572,13 @@ function getArgumentName(arg) {
|
|
|
569
572
|
return arg.name;
|
|
570
573
|
}
|
|
571
574
|
else {
|
|
572
|
-
return
|
|
575
|
+
return (0, generator_1.default)(arg).code;
|
|
573
576
|
}
|
|
574
577
|
throw new Error(`bind 不支持传入该参数: ${arg}`);
|
|
575
578
|
}
|
|
576
579
|
exports.getArgumentName = getArgumentName;
|
|
577
580
|
function isAllLiteral(...args) {
|
|
578
|
-
return args.every(p => t.isLiteral(p));
|
|
581
|
+
return args.every((p) => t.isLiteral(p));
|
|
579
582
|
}
|
|
580
583
|
exports.isAllLiteral = isAllLiteral;
|
|
581
584
|
function reverseBoolean(expression) {
|
|
@@ -585,8 +588,7 @@ exports.reverseBoolean = reverseBoolean;
|
|
|
585
588
|
function isEmptyDeclarator(node) {
|
|
586
589
|
if (t.isVariableDeclarator(node) &&
|
|
587
590
|
// tslint:disable-next-line: strict-type-predicates
|
|
588
|
-
(node.init === null ||
|
|
589
|
-
t.isNullLiteral(node.init))) {
|
|
591
|
+
(node.init === null || t.isNullLiteral(node.init))) {
|
|
590
592
|
return true;
|
|
591
593
|
}
|
|
592
594
|
return false;
|
|
@@ -594,7 +596,7 @@ function isEmptyDeclarator(node) {
|
|
|
594
596
|
exports.isEmptyDeclarator = isEmptyDeclarator;
|
|
595
597
|
function toLetters(num) {
|
|
596
598
|
let mod = num % 26;
|
|
597
|
-
let pow = num / 26 | 0;
|
|
599
|
+
let pow = (num / 26) | 0;
|
|
598
600
|
let out = mod ? String.fromCharCode(64 + mod) : (--pow, 'Z');
|
|
599
601
|
const letter = pow ? toLetters(pow) + out : out;
|
|
600
602
|
return letter.toLowerCase();
|
|
@@ -602,7 +604,7 @@ function toLetters(num) {
|
|
|
602
604
|
exports.toLetters = toLetters;
|
|
603
605
|
function findIdentifierFromStatement(statement) {
|
|
604
606
|
if (t.isVariableDeclaration(statement)) {
|
|
605
|
-
const declarator = statement.declarations.find(s => t.isIdentifier(s.id));
|
|
607
|
+
const declarator = statement.declarations.find((s) => t.isIdentifier(s.id));
|
|
606
608
|
if (declarator && t.isIdentifier(declarator.id)) {
|
|
607
609
|
return declarator.id.name;
|
|
608
610
|
}
|
|
@@ -633,44 +635,41 @@ function findParentLoops(callee, names, loops) {
|
|
|
633
635
|
}
|
|
634
636
|
loops.elements.unshift(t.objectExpression([
|
|
635
637
|
t.objectProperty(t.identifier('indexId'), indexId),
|
|
636
|
-
t.objectProperty(t.identifier('name'), t.stringLiteral(name))
|
|
638
|
+
t.objectProperty(t.identifier('name'), t.stringLiteral(name)),
|
|
637
639
|
]));
|
|
638
|
-
const parentCallExpr = callee.findParent(p => p.isCallExpression());
|
|
640
|
+
const parentCallExpr = callee.findParent((p) => p.isCallExpression());
|
|
639
641
|
if (parentCallExpr && parentCallExpr.isCallExpression()) {
|
|
640
642
|
const callee = parentCallExpr.node.callee;
|
|
641
|
-
if (t.isMemberExpression(callee) &&
|
|
642
|
-
t.isIdentifier(callee.property) &&
|
|
643
|
-
callee.property.name === 'map') {
|
|
643
|
+
if (t.isMemberExpression(callee) && t.isIdentifier(callee.property) && callee.property.name === 'map') {
|
|
644
644
|
findParentLoops(parentCallExpr, names, loops);
|
|
645
645
|
}
|
|
646
646
|
}
|
|
647
647
|
}
|
|
648
648
|
exports.findParentLoops = findParentLoops;
|
|
649
649
|
function setAncestorCondition(jsx, expr) {
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
const logicalJSX = jsx.findParent(p => p.isJSXElement() && p.node.openingElement.attributes.some(a => t.isJSXIdentifier(a.name) && ifAttrSet.has(a.name.name)));
|
|
650
|
+
var _a;
|
|
651
|
+
const ifAttrSet = new Set([adapter_1.Adapter.if, adapter_1.Adapter.else]);
|
|
652
|
+
const logicalJSX = jsx.findParent((p) => p.isJSXElement() &&
|
|
653
|
+
p.node.openingElement.attributes.some((a) => t.isJSXAttribute(a) && t.isJSXIdentifier(a.name) && ifAttrSet.has(a.name.name)));
|
|
655
654
|
if (logicalJSX) {
|
|
656
|
-
const attr = logicalJSX.node.openingElement.attributes.find(a => ifAttrSet.has(a.name.name));
|
|
657
|
-
if (attr) {
|
|
655
|
+
const attr = logicalJSX.node.openingElement.attributes.find((a) => t.isJSXAttribute(a) && ifAttrSet.has(a.name.name));
|
|
656
|
+
if (attr && t.isJSXAttribute(attr)) {
|
|
658
657
|
if (attr.name.name === adapter_1.Adapter.else) {
|
|
659
658
|
const prevElement = logicalJSX.getPrevSibling();
|
|
660
659
|
if (prevElement && prevElement.isJSXElement()) {
|
|
661
|
-
const attr = prevElement.node.openingElement.attributes.find(a => a.name.name === adapter_1.Adapter.if);
|
|
662
|
-
if (attr && t.
|
|
663
|
-
const condition = reverseBoolean(lodash_1.cloneDeep(attr.value.expression));
|
|
660
|
+
const attr = prevElement.node.openingElement.attributes.find((a) => t.isJSXAttribute(a) && a.name.name === adapter_1.Adapter.if);
|
|
661
|
+
if (attr && t.isJSXAttribute(attr) && !t.isJSXElement(attr.value)) {
|
|
662
|
+
const condition = reverseBoolean((0, lodash_1.cloneDeep)((_a = attr.value) === null || _a === void 0 ? void 0 : _a.expression));
|
|
664
663
|
expr = t.logicalExpression('&&', setAncestorCondition(logicalJSX, condition), expr);
|
|
665
664
|
}
|
|
666
665
|
}
|
|
667
666
|
}
|
|
668
667
|
else if (t.isJSXExpressionContainer(attr.value)) {
|
|
669
|
-
const condition = lodash_1.cloneDeep(attr.value.expression);
|
|
668
|
+
const condition = (0, lodash_1.cloneDeep)(attr.value.expression);
|
|
670
669
|
if (t.isJSXIdentifier(condition, { name: constant_1.IS_TARO_READY })) {
|
|
671
670
|
return expr;
|
|
672
671
|
}
|
|
673
|
-
const ifStem = logicalJSX.findParent(p => p.isIfStatement());
|
|
672
|
+
const ifStem = logicalJSX.findParent((p) => p.isIfStatement());
|
|
674
673
|
expr = t.logicalExpression('&&', setAncestorCondition(logicalJSX, ifStem && ifStem.isIfStatement() ? attr.value.expression : condition), expr);
|
|
675
674
|
}
|
|
676
675
|
}
|