@tarojs/transformer-wx 2.2.19 → 3.6.20

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/lib/src/index.js CHANGED
@@ -1,19 +1,20 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- const babel_traverse_1 = require("babel-traverse");
4
- const babel_generator_1 = require("babel-generator");
3
+ const generator_1 = require("@babel/generator");
4
+ const parser_1 = require("@babel/parser");
5
+ const traverse_1 = require("@babel/traverse");
6
+ const t = require("@babel/types");
5
7
  const html_1 = require("html");
6
- const babel_core_1 = require("babel-core");
8
+ const lodash_1 = require("lodash");
7
9
  const ts = require("typescript");
10
+ const adapter_1 = require("./adapter");
8
11
  const class_1 = require("./class");
9
- const utils_1 = require("./utils");
10
- const t = require("babel-types");
11
12
  const constant_1 = require("./constant");
12
- const adapter_1 = require("./adapter");
13
- const options_1 = require("./options");
14
- const lodash_1 = require("lodash");
15
13
  const env_1 = require("./env");
16
- const template = require('babel-template');
14
+ const options_1 = require("./options");
15
+ const utils_1 = require("./utils");
16
+ const wxs_1 = require("./wxs");
17
+ const template = require('@babel/template');
17
18
  function getIdsFromMemberProps(member) {
18
19
  let ids = [];
19
20
  const { object, property } = member;
@@ -41,16 +42,14 @@ function resetTSClassProperty(body) {
41
42
  for (const method of body) {
42
43
  if (t.isClassMethod(method) && method.kind === 'constructor') {
43
44
  if (t.isBlockStatement(method.body)) {
44
- method.body.body = method.body.body.filter(statement => {
45
+ method.body.body = method.body.body.filter((statement) => {
45
46
  if (t.isExpressionStatement(statement) && t.isAssignmentExpression(statement.expression)) {
46
47
  const expr = statement.expression;
47
48
  const { left, right } = expr;
48
- if (t.isMemberExpression(left) &&
49
- t.isThisExpression(left.object) &&
50
- t.isIdentifier(left.property)) {
51
- if ((t.isArrowFunctionExpression(right) || t.isFunctionExpression(right))
52
- ||
53
- (left.property.name === 'config' && t.isObjectExpression(right))) {
49
+ if (t.isMemberExpression(left) && t.isThisExpression(left.object) && t.isIdentifier(left.property)) {
50
+ if (t.isArrowFunctionExpression(right) ||
51
+ t.isFunctionExpression(right) ||
52
+ (left.property.name === 'config' && t.isObjectExpression(right))) {
54
53
  const classProp = t.classProperty(left.property, right);
55
54
  body.push(classProp);
56
55
  handleThirdPartyComponent(classProp);
@@ -67,14 +66,14 @@ function resetTSClassProperty(body) {
67
66
  function handleClosureJSXFunc(jsx, mainClass) {
68
67
  // 在 ./functional.ts 会把 FunctionExpression 转化为 arrowFunctionExpr
69
68
  // 所以我们这里只处理一种情况
70
- const arrowFunc = jsx.findParent(p => p.isArrowFunctionExpression());
69
+ const arrowFunc = jsx.findParent((p) => p.isArrowFunctionExpression());
71
70
  if (arrowFunc && arrowFunc.isArrowFunctionExpression()) {
72
71
  const parentPath = arrowFunc.parentPath;
73
72
  if (parentPath.isVariableDeclarator()) {
74
73
  const id = parentPath.node.id;
75
74
  if (t.isIdentifier(id) && /^render[A-Z]/.test(id.name)) {
76
75
  const funcName = `renderClosure${id.name.slice(6, id.name.length)}`;
77
- mainClass.node.body.body.push(t.classProperty(t.identifier(funcName), lodash_1.cloneDeep(arrowFunc.node)));
76
+ mainClass.node.body.body.push(t.classProperty(t.identifier(funcName), (0, lodash_1.cloneDeep)(arrowFunc.node)));
78
77
  parentPath.scope.rename(id.name, funcName);
79
78
  arrowFunc.replaceWith(t.memberExpression(t.thisExpression(), t.identifier(funcName)));
80
79
  }
@@ -82,11 +81,11 @@ function handleClosureJSXFunc(jsx, mainClass) {
82
81
  }
83
82
  }
84
83
  function findDeclarationScope(path, id) {
85
- const scopePath = path.findParent(p => !!p.scope.getOwnBindingIdentifier(id.name));
84
+ const scopePath = path.findParent((p) => !!p.scope.getOwnBindingIdentifier(id.name));
86
85
  if (scopePath) {
87
86
  return scopePath;
88
87
  }
89
- throw utils_1.codeFrameError(path.node, '该引用从未被定义');
88
+ throw (0, utils_1.codeFrameError)(path.node, '该引用从未被定义');
90
89
  }
91
90
  function buildFullPathThisPropsRef(id, memberIds, path) {
92
91
  const scopePath = findDeclarationScope(path, id);
@@ -97,9 +96,9 @@ function buildFullPathThisPropsRef(id, memberIds, path) {
97
96
  const dclId = bindingPath.get('id');
98
97
  const dclInit = bindingPath.get('init');
99
98
  let dclInitIds = [];
100
- if (dclInit.isMemberExpression()) {
99
+ if (t.isMemberExpression(dclInit)) {
101
100
  dclInitIds = getIdsFromMemberProps(dclInit.node);
102
- if (dclId.isIdentifier()) {
101
+ if (t.isIdentifier(dclId)) {
103
102
  memberIds.shift();
104
103
  }
105
104
  if (dclInitIds[0] === 'this' && dclInitIds[1] === 'props') {
@@ -118,7 +117,8 @@ function handleThirdPartyComponent(expr) {
118
117
  function findThirdPartyComponent(properties) {
119
118
  for (const prop of properties) {
120
119
  if (t.isObjectProperty(prop) &&
121
- (t.isIdentifier(prop.key, { name: 'usingComponents' }) || t.isStringLiteral(prop.key, { value: 'usingComponents' })) &&
120
+ (t.isIdentifier(prop.key, { name: 'usingComponents' }) ||
121
+ t.isStringLiteral(prop.key, { value: 'usingComponents' })) &&
122
122
  t.isObjectExpression(prop.value)) {
123
123
  for (const value of prop.value.properties) {
124
124
  if (t.isObjectProperty(value)) {
@@ -133,31 +133,53 @@ function findThirdPartyComponent(properties) {
133
133
  }
134
134
  }
135
135
  }
136
+ function parseCode(code) {
137
+ const ast = (0, parser_1.parse)(code, {
138
+ sourceType: 'module',
139
+ plugins: [
140
+ 'jsx',
141
+ 'flow',
142
+ 'decorators-legacy',
143
+ ['optionalChainingAssign', { version: '2023-07' }],
144
+ 'sourcePhaseImports',
145
+ 'throwExpressions',
146
+ 'deferredImportEvaluation',
147
+ 'exportDefaultFrom'
148
+ ],
149
+ });
150
+ // 移除Flow类型注释
151
+ (0, traverse_1.default)(ast, {
152
+ TypeAnnotation(path) {
153
+ path.remove();
154
+ },
155
+ });
156
+ return ast;
157
+ }
136
158
  function transform(options) {
137
159
  if (options.adapter) {
138
- adapter_1.setAdapter(options.adapter);
139
- if (adapter_1.Adapter.type === "quickapp" /* quickapp */) {
160
+ (0, adapter_1.setAdapter)(options.adapter);
161
+ if (adapter_1.Adapter.type === "quickapp" /* Adapters.quickapp */) {
140
162
  constant_1.DEFAULT_Component_SET.clear();
141
163
  constant_1.DEFAULT_Component_SET.add('div');
142
164
  constant_1.DEFAULT_Component_SET.add('Text');
143
- constant_1.setFnPrefix('prv-fn-');
165
+ (0, constant_1.setFnPrefix)('prv-fn-');
144
166
  }
145
167
  }
146
- if (adapter_1.Adapter.type === "swan" /* swan */ || adapter_1.Adapter.type === "quickapp" /* quickapp */) {
147
- constant_1.setLoopOriginal('privateOriginal');
148
- constant_1.setLoopCallee('anonymousCallee_');
149
- constant_1.setLoopState('loopState');
168
+ if (adapter_1.Adapter.type === "swan" /* Adapters.swan */ || adapter_1.Adapter.type === "quickapp" /* Adapters.quickapp */) {
169
+ (0, constant_1.setLoopOriginal)('privateOriginal');
170
+ (0, constant_1.setLoopCallee)('anonymousCallee_');
171
+ (0, constant_1.setLoopState)('loopState');
150
172
  }
151
- if (adapter_1.Adapter.type === "quickapp" /* quickapp */) {
152
- constant_1.setIsTaroReady('priTaroCompReady');
153
- constant_1.setCompId('priCompid');
173
+ if (adapter_1.Adapter.type === "quickapp" /* Adapters.quickapp */) {
174
+ (0, constant_1.setIsTaroReady)('priTaroCompReady');
175
+ (0, constant_1.setCompId)('priCompid');
154
176
  }
155
177
  const defaultResult = {
156
178
  ast: {},
157
179
  code: '',
158
180
  imageSrcs: '',
159
181
  compressedTemplate: '',
160
- components: []
182
+ components: [],
161
183
  };
162
184
  constant_1.THIRD_PARTY_COMPONENTS.clear();
163
185
  const code = options.isTyped
@@ -166,18 +188,19 @@ function transform(options) {
166
188
  target: ts.ScriptTarget.ESNext,
167
189
  importHelpers: true,
168
190
  noEmitHelpers: true,
169
- emitDecoratorMetadata: process.env.TS_METADATA === 'true'
191
+ emitDecoratorMetadata: process.env.TS_METADATA === 'true',
170
192
  })
171
193
  : options.code;
172
194
  options.env = Object.assign({ 'process.env.TARO_ENV': options.adapter || 'weapp' }, options.env || {});
173
- options_1.setTransformOptions(options);
195
+ (0, options_1.setTransformOptions)(options);
174
196
  utils_1.setting.sourceCode = code;
175
197
  let hasReduxBinding = false;
176
198
  // babel-traverse 无法生成 Hub
177
199
  // 导致 Path#getSource|buildCodeFrameError 都无法直接使用
178
200
  // 原因大概是 babylon.parse 没有生成 File 实例导致 scope 和 path 原型上都没有 `file`
179
201
  // 将来升级到 babel@7 可以直接用 parse 而不是 transform
180
- const ast = babel_core_1.transform(code, options_1.buildBabelTransformOptions()).ast;
202
+ // const ast = parser.parse(code, buildBabelTransformOptions() as any) as t.File
203
+ const ast = parseCode(code);
181
204
  // traverse(ast, {
182
205
  // JSXElement (p) {
183
206
  // setIsNormal(false)
@@ -195,14 +218,18 @@ function transform(options) {
195
218
  // })
196
219
  if (options.isNormal) {
197
220
  if (options.isTyped) {
198
- const mainClassNode = ast.program.body.find(v => {
221
+ const mainClassNode = ast.program.body.find((v) => {
199
222
  return t.isClassDeclaration(v);
200
223
  });
201
224
  if (mainClassNode) {
202
225
  resetTSClassProperty(mainClassNode.body.body);
203
226
  }
204
227
  }
205
- const code = babel_generator_1.default(ast).code;
228
+ // 对wxs文件进行转换
229
+ if (options.sourcePath.endsWith('.wxs')) {
230
+ return (0, wxs_1.traverseWxsFile)(ast, defaultResult);
231
+ }
232
+ const code = (0, generator_1.default)(ast.program).code;
206
233
  return Object.assign(Object.assign({}, defaultResult), { ast,
207
234
  code });
208
235
  }
@@ -217,39 +244,41 @@ function transform(options) {
217
244
  let storeName;
218
245
  let renderMethod;
219
246
  let isImportTaro = false;
220
- babel_traverse_1.default(ast, {
247
+ (0, traverse_1.default)(ast, {
221
248
  Program: {
222
249
  exit(path) {
223
250
  for (const stem of path.node.body) {
224
251
  if (t.isImportDeclaration(stem)) {
225
252
  if (stem.source.value === constant_1.TARO_PACKAGE_NAME) {
226
253
  const specs = stem.specifiers;
227
- if (specs.some(s => t.isImportDefaultSpecifier(s) && s.local.name === 'Taro')) {
254
+ if (specs.some((s) => t.isImportDefaultSpecifier(s) && s.local.name === 'Taro')) {
228
255
  continue;
229
256
  }
230
257
  specs.unshift(t.importDefaultSpecifier(t.identifier('Taro')));
231
258
  }
232
259
  }
233
260
  }
234
- }
261
+ },
235
262
  },
236
263
  MemberExpression(path) {
237
264
  const { property } = path.node;
238
265
  const right = path.getSibling('right');
239
- if (t.isIdentifier(property, { name: 'config' }) && path.parentPath.isAssignmentExpression() && right.isObjectExpression()) {
266
+ if (t.isIdentifier(property, { name: 'config' }) &&
267
+ path.parentPath.isAssignmentExpression() &&
268
+ right.isObjectExpression()) {
240
269
  const properties = right.node.properties;
241
270
  findThirdPartyComponent(properties);
242
271
  }
243
272
  },
244
273
  JSXText(path) {
245
- if (adapter_1.Adapter.type !== "quickapp" /* quickapp */) {
274
+ if (adapter_1.Adapter.type !== "quickapp" /* Adapters.quickapp */) {
246
275
  return;
247
276
  }
248
277
  const value = path.node.value;
249
278
  if (!value.trim()) {
250
279
  return;
251
280
  }
252
- utils_1.replaceJSXTextWithTextComponent(path);
281
+ (0, utils_1.replaceJSXTextWithTextComponent)(path);
253
282
  },
254
283
  TemplateLiteral(path) {
255
284
  const nodes = [];
@@ -281,7 +310,7 @@ function transform(options) {
281
310
  },
282
311
  ClassDeclaration(path) {
283
312
  mainClass = path;
284
- const superClass = utils_1.getSuperClassCode(path);
313
+ const superClass = (0, utils_1.getSuperClassCode)(path);
285
314
  if (superClass) {
286
315
  try {
287
316
  componentProperies = transform({
@@ -290,7 +319,7 @@ function transform(options) {
290
319
  code: superClass.code,
291
320
  isTyped: true,
292
321
  sourcePath: superClass.sourcePath,
293
- sourceDir: options.sourceDir
322
+ sourceDir: options.sourceDir,
294
323
  }).componentProperies;
295
324
  }
296
325
  catch (error) {
@@ -312,30 +341,30 @@ function transform(options) {
312
341
  IfStatement(path) {
313
342
  const consequent = path.get('consequent');
314
343
  if (!consequent.isBlockStatement()) {
315
- consequent.replaceWith(t.blockStatement([
316
- consequent.node
317
- ]));
344
+ consequent.replaceWith(t.blockStatement([consequent.node]));
318
345
  }
319
346
  },
320
347
  CallExpression(path) {
321
348
  const callee = path.get('callee');
322
- if (utils_1.isContainJSXElement(path)) {
349
+ if ((0, utils_1.isContainJSXElement)(path)) {
323
350
  return;
324
351
  }
325
352
  if (callee.isReferencedMemberExpression()) {
326
- const id = utils_1.findFirstIdentifierFromMemberExpression(callee.node);
353
+ const id = (0, utils_1.findFirstIdentifierFromMemberExpression)(callee.node);
327
354
  const property = callee.node.property;
328
355
  if (t.isIdentifier(property) && property.name.startsWith('on')) {
329
- const funcExpr = path.findParent(p => p.isFunctionExpression());
356
+ const funcExpr = path.findParent((p) => p.isFunctionExpression());
330
357
  if (funcExpr && funcExpr.isFunctionExpression()) {
331
- const taroAPI = funcExpr.findParent(p => p.isCallExpression() && t.isMemberExpression(p.node.callee) && t.isIdentifier(p.node.callee.object, { name: 'Taro' }));
358
+ const taroAPI = funcExpr.findParent((p) => p.isCallExpression() &&
359
+ t.isMemberExpression(p.node.callee) &&
360
+ t.isIdentifier(p.node.callee.object, { name: 'Taro' }));
332
361
  if (taroAPI && taroAPI.isCallExpression()) {
333
- throw utils_1.codeFrameError(funcExpr.node, '在回调函数使用从 props 传递的函数时,请把回调函数改造为箭头函数并一直使用 `this` 取值');
362
+ throw (0, utils_1.codeFrameError)(funcExpr.node, '在回调函数使用从 props 传递的函数时,请把回调函数改造为箭头函数并一直使用 `this` 取值');
334
363
  }
335
364
  }
336
365
  }
337
366
  const calleeIds = getIdsFromMemberProps(callee.node);
338
- if (t.isIdentifier(id) && id.name.startsWith('on') && "alipay" /* alipay */ !== adapter_1.Adapter.type) {
367
+ if (t.isIdentifier(id) && id.name.startsWith('on') && "alipay" /* Adapters.alipay */ !== adapter_1.Adapter.type) {
339
368
  const fullPath = buildFullPathThisPropsRef(id, calleeIds, path);
340
369
  if (fullPath) {
341
370
  path.replaceWith(t.callExpression(fullPath, path.node.arguments));
@@ -345,12 +374,14 @@ function transform(options) {
345
374
  if (callee.isReferencedIdentifier()) {
346
375
  const id = callee.node;
347
376
  const ids = [id.name];
348
- if (t.isIdentifier(id) && id.name.startsWith('on')) {
349
- const funcExpr = path.findParent(p => p.isFunctionExpression());
377
+ if (id.name.startsWith('on')) {
378
+ const funcExpr = path.findParent((p) => p.isFunctionExpression());
350
379
  if (funcExpr && funcExpr.isFunctionExpression()) {
351
- const taroAPI = funcExpr.findParent(p => p.isCallExpression() && t.isMemberExpression(p.node.callee) && t.isIdentifier(p.node.callee.object, { name: 'Taro' }));
380
+ const taroAPI = funcExpr.findParent((p) => p.isCallExpression() &&
381
+ t.isMemberExpression(p.node.callee) &&
382
+ t.isIdentifier(p.node.callee.object, { name: 'Taro' }));
352
383
  if (taroAPI && taroAPI.isCallExpression()) {
353
- throw utils_1.codeFrameError(funcExpr.node, '在回调函数使用从 props 传递的函数时,请把回调函数改造为箭头函数并一直使用 `this` 取值');
384
+ throw (0, utils_1.codeFrameError)(funcExpr.node, '在回调函数使用从 props 传递的函数时,请把回调函数改造为箭头函数并一直使用 `this` 取值');
354
385
  }
355
386
  }
356
387
  const fullPath = buildFullPathThisPropsRef(id, ids, path);
@@ -381,13 +412,13 @@ function transform(options) {
381
412
  JSXMemberExpression(path) {
382
413
  const { property, object } = path.node;
383
414
  if (!t.isJSXIdentifier(property, { name: 'Provider' })) {
384
- throw utils_1.codeFrameError(property, '只能在使用 Context.Provider 的情况下才能使用 JSX 成员表达式');
415
+ throw (0, utils_1.codeFrameError)(property, '只能在使用 Context.Provider 的情况下才能使用 JSX 成员表达式');
385
416
  }
386
417
  if (!t.isJSXIdentifier(object)) {
387
418
  return;
388
419
  }
389
420
  const jsx = path.parentPath.parentPath;
390
- if (jsx.isJSXElement()) {
421
+ if (jsx === null || jsx === void 0 ? void 0 : jsx.isJSXElement()) {
391
422
  const componentName = `${object.name}${constant_1.CONTEXT_PROVIDER}`;
392
423
  jsx.node.openingElement.name = t.jSXIdentifier(componentName);
393
424
  if (jsx.node.closingElement) {
@@ -396,7 +427,7 @@ function transform(options) {
396
427
  }
397
428
  },
398
429
  JSXElement(path) {
399
- const assignment = path.findParent(p => p.isAssignmentExpression());
430
+ const assignment = path.findParent((p) => p.isAssignmentExpression());
400
431
  if (assignment && assignment.isAssignmentExpression() && !options.isTyped) {
401
432
  const left = assignment.node.left;
402
433
  if (t.isIdentifier(left)) {
@@ -407,34 +438,34 @@ function transform(options) {
407
438
  assignment.remove();
408
439
  }
409
440
  else {
410
- throw utils_1.codeFrameError(path.node, '同一个作用域的JSX 变量延时赋值没有意义。详见:https://github.com/NervJS/taro/issues/550');
441
+ throw (0, utils_1.codeFrameError)(path.node, '同一个作用域的JSX 变量延时赋值没有意义。详见:https://github.com/NervJS/taro/issues/550');
411
442
  }
412
443
  }
413
444
  }
414
445
  }
415
- const switchStatement = path.findParent(p => p.isSwitchStatement());
446
+ const switchStatement = path.findParent((p) => p.isSwitchStatement());
416
447
  if (switchStatement && switchStatement.isSwitchStatement()) {
417
448
  const { discriminant, cases } = switchStatement.node;
418
- const ifStatement = cases.map((Case, index) => {
449
+ const ifStatement = cases
450
+ .map((Case, index) => {
419
451
  const [consequent] = Case.consequent;
420
452
  if (!t.isBlockStatement(consequent)) {
421
- throw utils_1.codeFrameError(switchStatement.node, '含有 JSX 的 switch case 语句必须每种情况都用花括号 `{}` 包裹结果');
453
+ throw (0, utils_1.codeFrameError)(switchStatement.node, '含有 JSX 的 switch case 语句必须每种情况都用花括号 `{}` 包裹结果');
422
454
  }
423
- const block = t.blockStatement(consequent.body.filter(b => !t.isBreakStatement(b)));
455
+ const block = t.blockStatement(consequent.body.filter((b) => !t.isBreakStatement(b)));
424
456
  if (index !== cases.length - 1 && t.isNullLiteral(Case.test)) {
425
- throw utils_1.codeFrameError(Case, '含有 JSX 的 switch case 语句只有最后一个 case 才能是 default');
457
+ throw (0, utils_1.codeFrameError)(Case, '含有 JSX 的 switch case 语句只有最后一个 case 才能是 default');
426
458
  }
427
459
  // tslint:disable-next-line: strict-type-predicates
428
460
  const test = Case.test === null ? t.nullLiteral() : t.binaryExpression('===', discriminant, Case.test);
429
461
  return { block, test };
430
- }).reduceRight((ifStatement, item) => {
462
+ })
463
+ .reduceRight((ifStatement, item) => {
431
464
  if (t.isNullLiteral(item.test)) {
432
465
  ifStatement.alternate = item.block;
433
466
  return ifStatement;
434
467
  }
435
- const newStatement = t.ifStatement(item.test, item.block, t.isBooleanLiteral(ifStatement.test, { value: false })
436
- ? ifStatement.alternate
437
- : ifStatement);
468
+ const newStatement = t.ifStatement(item.test, item.block, t.isBooleanLiteral(ifStatement.test, { value: false }) ? ifStatement.alternate : ifStatement);
438
469
  return newStatement;
439
470
  }, t.ifStatement(t.booleanLiteral(false), t.blockStatement([])));
440
471
  switchStatement.insertAfter(ifStatement);
@@ -443,35 +474,34 @@ function transform(options) {
443
474
  const isForStatement = (p) => p && (p.isForStatement() || p.isForInStatement() || p.isForOfStatement());
444
475
  const forStatement = path.findParent(isForStatement);
445
476
  if (isForStatement(forStatement)) {
446
- throw utils_1.codeFrameError(forStatement.node, '不能使用 for 循环操作 JSX 元素,详情:https://github.com/NervJS/taro/blob/master/packages/eslint-plugin-taro/docs/manipulate-jsx-as-array.md');
477
+ throw (0, utils_1.codeFrameError)(forStatement === null || forStatement === void 0 ? void 0 : forStatement.node, '不能使用 for 循环操作 JSX 元素,详情:https://github.com/NervJS/taro/blob/master/packages/eslint-plugin-taro/docs/manipulate-jsx-as-array.md');
447
478
  }
448
- const loopCallExpr = path.findParent(p => utils_1.isArrayMapCallExpression(p));
479
+ const loopCallExpr = path.findParent((p) => (0, utils_1.isArrayMapCallExpression)(p));
449
480
  if (loopCallExpr && loopCallExpr.isCallExpression()) {
450
481
  const [func] = loopCallExpr.node.arguments;
451
482
  if (t.isArrowFunctionExpression(func) && !t.isBlockStatement(func.body)) {
452
- func.body = t.blockStatement([
453
- t.returnStatement(func.body)
454
- ]);
483
+ func.body = t.blockStatement([t.returnStatement(func.body)]);
455
484
  }
456
485
  }
457
486
  handleClosureJSXFunc(path, mainClass);
458
487
  },
459
488
  JSXOpeningElement(path) {
489
+ var _a;
460
490
  const { name } = path.node.name;
461
491
  const binding = path.scope.getBinding(name);
462
492
  if (process.env.NODE_ENV !== 'test' && binding && binding.kind === 'module') {
463
493
  const bindingPath = binding.path;
464
- if (bindingPath.parentPath.isImportDeclaration()) {
494
+ if ((_a = bindingPath.parentPath) === null || _a === void 0 ? void 0 : _a.isImportDeclaration()) {
465
495
  const source = bindingPath.parentPath.node.source;
466
496
  if (constant_1.DEFAULT_Component_SET.has(name) && source.value !== constant_1.COMPONENTS_PACKAGE_NAME) {
467
- throw utils_1.codeFrameError(bindingPath.parentPath.node, `内置组件名: '${name}' 只能从 ${constant_1.COMPONENTS_PACKAGE_NAME} 引入。`);
497
+ throw (0, utils_1.codeFrameError)(bindingPath.parentPath.node, `内置组件名: '${name}' 只能从 ${constant_1.COMPONENTS_PACKAGE_NAME} 引入。`);
468
498
  }
469
499
  if (name === 'Fragment') {
470
500
  path.node.name = t.jSXIdentifier('block');
471
501
  }
472
502
  }
473
503
  }
474
- if (adapter_1.Adapter.type === "quickapp" /* quickapp */) {
504
+ if (adapter_1.Adapter.type === "quickapp" /* Adapters.quickapp */) {
475
505
  if (name === 'View') {
476
506
  path.node.name = t.jSXIdentifier('div');
477
507
  }
@@ -484,8 +514,11 @@ function transform(options) {
484
514
  const providerBinding = Object.values(modules).some((m) => m.identifier.name === 'Provider');
485
515
  if (providerBinding) {
486
516
  path.node.name = t.jSXIdentifier('View');
487
- const store = path.node.attributes.find(attr => attr.name.name === 'store');
488
- if (store && t.isJSXExpressionContainer(store.value) && t.isIdentifier(store.value.expression)) {
517
+ const store = path.node.attributes.find((attr) => t.isJSXAttribute(attr) && attr.name.name === 'store');
518
+ if (store &&
519
+ t.isJSXAttribute(store) &&
520
+ t.isJSXExpressionContainer(store.value) &&
521
+ t.isIdentifier(store.value.expression)) {
489
522
  storeName = store.value.expression.name;
490
523
  }
491
524
  path.node.attributes = [];
@@ -493,11 +526,11 @@ function transform(options) {
493
526
  }
494
527
  if (constant_1.IMAGE_COMPONENTS.has(name)) {
495
528
  for (const attr of path.node.attributes) {
496
- if (attr.name.name === 'src') {
497
- if (t.isStringLiteral(attr.value)) {
529
+ if (t.isJSXAttribute(attr) && attr.name.name === 'src') {
530
+ if (t.isJSXAttribute(attr) && t.isStringLiteral(attr.value)) {
498
531
  imageSource.add(attr.value.value);
499
532
  }
500
- else if (t.isJSXExpressionContainer(attr.value)) {
533
+ else if (t.isJSXAttribute(attr) && t.isJSXExpressionContainer(attr.value)) {
501
534
  if (t.isStringLiteral(attr.value.expression)) {
502
535
  imageSource.add(attr.value.expression.value);
503
536
  }
@@ -507,6 +540,7 @@ function transform(options) {
507
540
  }
508
541
  },
509
542
  JSXAttribute(path) {
543
+ var _a;
510
544
  const { name, value } = path.node;
511
545
  if (options.jsxAttributeNameReplace) {
512
546
  for (const r in options.jsxAttributeNameReplace) {
@@ -522,18 +556,26 @@ function transform(options) {
522
556
  if (!t.isJSXIdentifier(name) || value === null || t.isStringLiteral(value) || t.isJSXElement(value)) {
523
557
  return;
524
558
  }
525
- const expr = value.expression;
559
+ const expr = value === null || value === void 0 ? void 0 : value.expression;
526
560
  const exprPath = path.get('value.expression');
527
- const classDecl = path.findParent(p => p.isClassDeclaration());
528
- const classDeclName = classDecl && classDecl.isClassDeclaration() && lodash_1.get(classDecl, 'node.id.name', '');
561
+ const classDecl = path.findParent((p) => p.isClassDeclaration());
562
+ const classDeclName = classDecl && classDecl.isClassDeclaration() && (0, lodash_1.get)(classDecl, 'node.id.name', '');
529
563
  let isConverted = false;
530
564
  if (classDeclName) {
531
565
  isConverted = classDeclName === '_C' || classDeclName.endsWith('Tmpl');
532
566
  }
533
- if (!t.isBinaryExpression(expr, { operator: '+' }) && !t.isLiteral(expr) && name.name === 'style' && !isConverted) {
534
- const jsxID = path.findParent(p => p.isJSXOpeningElement()).get('name');
567
+ if (!t.isBinaryExpression(expr, { operator: '+' }) &&
568
+ !t.isLiteral(expr) &&
569
+ name.name === 'style' &&
570
+ !isConverted) {
571
+ const jsxID = (_a = path.findParent((p) => p.isJSXOpeningElement())) === null || _a === void 0 ? void 0 : _a.get('name');
535
572
  if (jsxID && jsxID.isJSXIdentifier() && constant_1.DEFAULT_Component_SET.has(jsxID.node.name)) {
536
- exprPath.replaceWith(t.callExpression(t.identifier(constant_1.INTERNAL_INLINE_STYLE), [expr]));
573
+ if (!(0, lodash_1.isArray)(exprPath)) {
574
+ exprPath.replaceWith(t.callExpression(t.identifier(constant_1.INTERNAL_INLINE_STYLE), [expr]));
575
+ }
576
+ else {
577
+ exprPath.forEach((item) => item.replaceWith(t.callExpression(t.identifier(constant_1.INTERNAL_INLINE_STYLE), [expr])));
578
+ }
537
579
  }
538
580
  }
539
581
  if (name.name.startsWith('on')) {
@@ -545,7 +587,7 @@ function transform(options) {
545
587
  }
546
588
  }
547
589
  if (exprPath.isReferencedMemberExpression()) {
548
- const id = utils_1.findFirstIdentifierFromMemberExpression(expr);
590
+ const id = (0, utils_1.findFirstIdentifierFromMemberExpression)(expr);
549
591
  const ids = getIdsFromMemberProps(expr);
550
592
  if (t.isIdentifier(id)) {
551
593
  const fullPath = buildFullPathThisPropsRef(id, ids, path);
@@ -558,16 +600,14 @@ function transform(options) {
558
600
  }
559
601
  },
560
602
  ClassProperty(path) {
561
- const { key: { name }, value } = path.node;
603
+ const { key: { name }, value, } = path.node;
562
604
  if (t.isArrowFunctionExpression(value) || t.isFunctionExpression(value)) {
563
605
  classMethods.set(name, path);
564
606
  if (name.startsWith('render')) {
565
- path.replaceWith(t.classMethod('method', t.identifier(name), value.params, t.isBlockStatement(value.body) ? value.body : t.blockStatement([
566
- t.returnStatement(value.body)
567
- ])));
607
+ path.replaceWith(t.classMethod('method', t.identifier(name), value.params, t.isBlockStatement(value.body) ? value.body : t.blockStatement([t.returnStatement(value.body)])));
568
608
  }
569
609
  }
570
- if (adapter_1.Adapter.type !== "quickapp" /* quickapp */) {
610
+ if (adapter_1.Adapter.type !== "quickapp" /* Adapters.quickapp */) {
571
611
  return;
572
612
  }
573
613
  if (path.node.key.name === 'defaultProps' && t.isObjectExpression(path.node.value)) {
@@ -575,29 +615,31 @@ function transform(options) {
575
615
  for (const prop of props) {
576
616
  if (t.isObjectProperty(prop)) {
577
617
  if (t.isStringLiteral(prop.key) && /[A-Z]/.test(prop.key.value) && !prop.key.value.startsWith('on')) {
578
- prop.key = t.stringLiteral(lodash_1.snakeCase(prop.key.value));
618
+ prop.key = t.stringLiteral((0, lodash_1.snakeCase)(prop.key.value));
579
619
  }
580
620
  if (t.isIdentifier(prop.key) && /[A-Z]/.test(prop.key.name) && !prop.key.name.startsWith('on')) {
581
- prop.key = t.identifier(lodash_1.snakeCase(prop.key.name));
621
+ prop.key = t.identifier((0, lodash_1.snakeCase)(prop.key.name));
582
622
  }
583
623
  }
584
624
  }
585
625
  }
586
626
  },
587
627
  AssignmentExpression(path) {
588
- if (adapter_1.Adapter.type !== "quickapp" /* quickapp */) {
628
+ if (adapter_1.Adapter.type !== "quickapp" /* Adapters.quickapp */) {
589
629
  return;
590
630
  }
591
631
  const { left, right } = path.node;
592
- if (t.isMemberExpression(left) && t.isIdentifier(left.property, { name: 'defaultProps' }) && t.isObjectExpression(right)) {
632
+ if (t.isMemberExpression(left) &&
633
+ t.isIdentifier(left.property, { name: 'defaultProps' }) &&
634
+ t.isObjectExpression(right)) {
593
635
  const props = right.properties;
594
636
  for (const prop of props) {
595
637
  if (t.isObjectProperty(prop)) {
596
638
  if (t.isStringLiteral(prop.key) && /[A-Z]/.test(prop.key.value) && !prop.key.value.startsWith('on')) {
597
- prop.key = t.stringLiteral(lodash_1.snakeCase(prop.key.value));
639
+ prop.key = t.stringLiteral((0, lodash_1.snakeCase)(prop.key.value));
598
640
  }
599
641
  if (t.isIdentifier(prop.key) && /[A-Z]/.test(prop.key.name) && !prop.key.name.startsWith('on')) {
600
- prop.key = t.identifier(lodash_1.snakeCase(prop.key.name));
642
+ prop.key = t.identifier((0, lodash_1.snakeCase)(prop.key.name));
601
643
  }
602
644
  }
603
645
  }
@@ -606,13 +648,13 @@ function transform(options) {
606
648
  ImportDeclaration(path) {
607
649
  const source = path.node.source.value;
608
650
  if (importSources.has(source)) {
609
- throw utils_1.codeFrameError(path.node, '无法在同一文件重复 import 相同的包。');
651
+ throw (0, utils_1.codeFrameError)(path.node, '无法在同一文件重复 import 相同的包。');
610
652
  }
611
653
  else {
612
654
  importSources.add(source);
613
655
  }
614
656
  const names = [];
615
- if (source === constant_1.COMPONENTS_PACKAGE_NAME && "quickapp" /* quickapp */ === adapter_1.Adapter.type) {
657
+ if (source === constant_1.COMPONENTS_PACKAGE_NAME && "quickapp" /* Adapters.quickapp */ === adapter_1.Adapter.type) {
616
658
  path.node.specifiers.forEach((s) => {
617
659
  if (t.isImportSpecifier(s)) {
618
660
  const originalName = s.imported.name;
@@ -627,7 +669,7 @@ function transform(options) {
627
669
  if (source === constant_1.TARO_PACKAGE_NAME) {
628
670
  isImportTaro = true;
629
671
  path.node.specifiers.push(t.importSpecifier(t.identifier(constant_1.INTERNAL_SAFE_GET), t.identifier(constant_1.INTERNAL_SAFE_GET)), t.importSpecifier(t.identifier(constant_1.INTERNAL_GET_ORIGNAL), t.identifier(constant_1.INTERNAL_GET_ORIGNAL)), t.importSpecifier(t.identifier(constant_1.INTERNAL_INLINE_STYLE), t.identifier(constant_1.INTERNAL_INLINE_STYLE)), t.importSpecifier(t.identifier(constant_1.HANDLE_LOOP_REF), t.identifier(constant_1.HANDLE_LOOP_REF)), t.importSpecifier(t.identifier(constant_1.GEN_COMP_ID), t.identifier(constant_1.GEN_COMP_ID)), t.importSpecifier(t.identifier(constant_1.GEN_LOOP_COMPID), t.identifier(constant_1.GEN_LOOP_COMPID)));
630
- if (adapter_1.Adapter.type !== "alipay" /* alipay */) {
672
+ if (adapter_1.Adapter.type !== "alipay" /* Adapters.alipay */) {
631
673
  path.node.specifiers.push(t.importSpecifier(t.identifier(constant_1.PROPS_MANAGER), t.identifier(constant_1.PROPS_MANAGER)));
632
674
  }
633
675
  }
@@ -654,10 +696,10 @@ function transform(options) {
654
696
  if (source === constant_1.TARO_PACKAGE_NAME && name === 'Component') {
655
697
  path.node.local = t.identifier('__BaseComponent');
656
698
  }
657
- }
699
+ },
658
700
  });
659
701
  componentSourceMap.set(source, names);
660
- }
702
+ },
661
703
  });
662
704
  if (!isImportTaro) {
663
705
  const specifiers = [
@@ -667,23 +709,25 @@ function transform(options) {
667
709
  t.importSpecifier(t.identifier(constant_1.INTERNAL_INLINE_STYLE), t.identifier(constant_1.INTERNAL_INLINE_STYLE)),
668
710
  t.importSpecifier(t.identifier(constant_1.HANDLE_LOOP_REF), t.identifier(constant_1.HANDLE_LOOP_REF)),
669
711
  t.importSpecifier(t.identifier(constant_1.GEN_COMP_ID), t.identifier(constant_1.GEN_COMP_ID)),
670
- t.importSpecifier(t.identifier(constant_1.GEN_LOOP_COMPID), t.identifier(constant_1.GEN_LOOP_COMPID))
712
+ t.importSpecifier(t.identifier(constant_1.GEN_LOOP_COMPID), t.identifier(constant_1.GEN_LOOP_COMPID)),
671
713
  ];
672
- if (adapter_1.Adapter.type !== "alipay" /* alipay */) {
714
+ if (adapter_1.Adapter.type !== "alipay" /* Adapters.alipay */) {
673
715
  specifiers.push(t.importSpecifier(t.identifier(constant_1.PROPS_MANAGER), t.identifier(constant_1.PROPS_MANAGER)));
674
716
  }
675
717
  ast.program.body.unshift(t.importDeclaration(specifiers, t.stringLiteral('@tarojs/taro')));
676
718
  }
677
719
  if (!mainClass) {
678
- const code = babel_generator_1.default(ast).code;
720
+ const code = (0, generator_1.default)(ast.program).code;
679
721
  return Object.assign(Object.assign({}, defaultResult), { ast,
680
722
  code });
681
723
  }
682
- if (adapter_1.Adapter.type === "alipay" /* alipay */) {
724
+ if (adapter_1.Adapter.type === "alipay" /* Adapters.alipay */) {
683
725
  const body = ast.program.body;
684
726
  for (const i in body) {
685
727
  if (t.isImportDeclaration(body[i]) && !t.isImportDeclaration(body[Number(i) + 1])) {
686
- body.splice(Number(i) + 1, 0, t.variableDeclaration('const', [t.variableDeclarator(t.identifier('propsManager'), t.memberExpression(t.identifier('my'), t.identifier('propsManager')))]));
728
+ body.splice(Number(i) + 1, 0, t.variableDeclaration('const', [
729
+ t.variableDeclarator(t.identifier('propsManager'), t.memberExpression(t.identifier('my'), t.identifier('propsManager'))),
730
+ ]));
687
731
  break;
688
732
  }
689
733
  }
@@ -697,17 +741,13 @@ function transform(options) {
697
741
  ast.program.body.every((node, index, body) => {
698
742
  if (node === statementPath.node) {
699
743
  const settingReduxProvider = t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('ReduxContext'), t.identifier('Provider')), [
700
- t.objectExpression([
701
- t.objectProperty(t.identifier('store'), t.identifier(storeName))
702
- ])
744
+ t.objectExpression([t.objectProperty(t.identifier('store'), t.identifier(storeName))]),
703
745
  ]));
704
746
  const ifStem = t.ifStatement(t.memberExpression(t.identifier('ReduxContext'), t.identifier('Provider')), t.blockStatement([
705
747
  settingReduxProvider,
706
- settingReduxProvider // 第一次调用初始化,第二次赋值
748
+ settingReduxProvider, // 第一次调用初始化,第二次赋值
707
749
  ]));
708
- body.splice(index + 1, 0, t.expressionStatement(t.callExpression(t.identifier('setStore'), [
709
- t.identifier(storeName)
710
- ])), hasReduxBinding ? ifStem : t.emptyStatement());
750
+ body.splice(index + 1, 0, t.expressionStatement(t.callExpression(t.identifier('setStore'), [t.identifier(storeName)])), hasReduxBinding ? ifStem : t.emptyStatement());
711
751
  return false;
712
752
  }
713
753
  return true;
@@ -717,18 +757,18 @@ function transform(options) {
717
757
  resetTSClassProperty(mainClass.node.body.body);
718
758
  if (options.isApp) {
719
759
  renderMethod.replaceWith(t.classMethod('method', t.identifier('_createData'), [], t.blockStatement([])));
720
- const code = babel_generator_1.default(ast).code;
760
+ const code = (0, generator_1.default)(ast.program).code;
721
761
  return Object.assign(Object.assign({}, defaultResult), { ast,
722
762
  code });
723
763
  }
724
764
  result = new class_1.Transformer(mainClass, options.sourcePath, componentProperies, options.sourceDir, classMethods).result;
725
- result.code = babel_generator_1.default(ast).code;
765
+ result.code = (0, generator_1.default)(ast.program).code;
726
766
  result.ast = ast;
727
767
  const lessThanSignReg = new RegExp(constant_1.lessThanSignPlacehold, 'g');
728
768
  result.compressedTemplate = result.template.replace(lessThanSignReg, '<');
729
- result.template = html_1.prettyPrint(result.template, {
769
+ result.template = (0, html_1.prettyPrint)(result.template, {
730
770
  max_char: 0,
731
- unformatted: env_1.isTestEnv ? [] : ['text']
771
+ unformatted: env_1.isTestEnv ? [] : ['text'],
732
772
  });
733
773
  result.template = result.template.replace(lessThanSignReg, '<');
734
774
  result.imageSrcs = Array.from(imageSource);