babel-plugin-transform-taroapi 4.2.1-beta.1 → 4.2.1-beta.3

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/dist/index.js CHANGED
@@ -16,63 +16,6 @@ const plugin = function (babel) {
16
16
  ariaValuenow: 'aria-valuenow',
17
17
  ariaValuetext: 'aria-valuetext',
18
18
  };
19
- function stripTSCast(node) {
20
- while (t.isTSAsExpression(node) ||
21
- t.isTSTypeAssertion(node) ||
22
- t.isTSNonNullExpression(node) ||
23
- (t.isTSSatisfiesExpression && t.isTSSatisfiesExpression(node))) {
24
- node = node.expression;
25
- }
26
- return node;
27
- }
28
- function getTaroNamespaceCall(t, callee, taroName, file) {
29
- var _a;
30
- let target = callee;
31
- // 如果是可选调用(例如 Taro.JDMTA.isTrafficMapEnable?.()),先取里面的 callee
32
- if (t.isOptionalCallExpression(target)) {
33
- target = target.callee;
34
- }
35
- // 去掉 TS 断言 / 非空等包裹
36
- target = stripTSCast(target);
37
- if (!t.isMemberExpression(target) && !t.isOptionalMemberExpression(target)) {
38
- return null;
39
- }
40
- // target.object 通常是 Taro.xx 这一层,
41
- // 也可能是形如 (_tmp = Taro.xx) 这样的赋值表达式,需要再解一层。
42
- let inner = stripTSCast(target.object);
43
- // 兼容 222 这类形态:(_tmp = Taro.JDMTA).isTrafficMapEnable?.()
44
- if (t.isAssignmentExpression(inner)) {
45
- inner = stripTSCast(inner.right);
46
- }
47
- if (!t.isMemberExpression(inner) && !t.isOptionalMemberExpression(inner)) {
48
- return null;
49
- }
50
- if (!t.isIdentifier(inner.object, { name: taroName })) {
51
- return null;
52
- }
53
- let namespaceName = null;
54
- if (t.isIdentifier(inner.property)) {
55
- namespaceName = inner.property.name;
56
- }
57
- else if (t.isStringLiteral(inner.property)) {
58
- namespaceName = inner.property.value;
59
- }
60
- let methodName = null;
61
- if (t.isIdentifier(target.property)) {
62
- methodName = target.property.name;
63
- }
64
- else if (t.isStringLiteral(target.property)) {
65
- methodName = target.property.value;
66
- }
67
- if (!namespaceName || !methodName)
68
- return null;
69
- if (process.env.JDAPI_DEBUG_TAROAPI === 'true') {
70
- const filename = ((_a = file === null || file === void 0 ? void 0 : file.opts) === null || _a === void 0 ? void 0 : _a.filename) || '';
71
- // eslint-disable-next-line no-console
72
- console.log('[jdapi-core-taroapi] getTaroNamespaceCall hit:', `${namespaceName}_${methodName}`, 'file =', filename);
73
- }
74
- return { namespaceName, methodName };
75
- }
76
19
  // 这些变量需要在每个 program 里重置
77
20
  const invokedApis = new Map();
78
21
  let taroName;
@@ -141,40 +84,10 @@ const plugin = function (babel) {
141
84
  }
142
85
  });
143
86
  },
144
- 'MemberExpression|OptionalMemberExpression'(ast) {
145
- const node = ast.node;
146
- // 处理两层命名空间属性访问:Taro.xx.yy / Taro?.xx?.yy(非调用场景)
147
- // 调用场景由 CallExpression|OptionalCallExpression 负责
148
- const isCalleeOfCall = (t.isCallExpression(ast.parent) || t.isOptionalCallExpression(ast.parent)) && ast.parent.callee === node;
149
- if (!isCalleeOfCall) {
150
- const innerObj = stripTSCast(node.object);
151
- if (t.isMemberExpression(innerObj) || t.isOptionalMemberExpression(innerObj)) {
152
- const isTaroNamespace = t.isIdentifier(innerObj.object, { name: taroName });
153
- if (isTaroNamespace) {
154
- const namespaceName = t.isIdentifier(innerObj.property) ? innerObj.property.name : (t.isStringLiteral(innerObj.property) ? innerObj.property.value : null);
155
- const methodName = t.isIdentifier(node.property) ? node.property.name : (t.isStringLiteral(node.property) ? node.property.value : null);
156
- if (namespaceName && methodName) {
157
- const flatName = `${namespaceName}_${methodName}`;
158
- if (this.apis.has(flatName)) {
159
- let identifier;
160
- if (invokedApis.has(flatName)) {
161
- identifier = t.identifier(invokedApis.get(flatName));
162
- }
163
- else {
164
- const newName = ast.scope.generateUid(flatName);
165
- invokedApis.set(flatName, newName);
166
- identifier = t.identifier(newName);
167
- }
168
- ast.replaceWith(identifier);
169
- return;
170
- }
171
- }
172
- }
173
- }
174
- }
87
+ MemberExpression(ast) {
175
88
  /* 处理 Taro.xxx */
176
- const isTaro = t.isIdentifier(node.object, { name: taroName });
177
- const property = node.property;
89
+ const isTaro = t.isIdentifier(ast.node.object, { name: taroName });
90
+ const property = ast.node.property;
178
91
  let propertyName = null;
179
92
  let propName = 'name';
180
93
  if (!isTaro)
@@ -189,7 +102,7 @@ const plugin = function (babel) {
189
102
  // 同一 api 使用多次,读取变量名
190
103
  if (this.apis.has(propertyName)) {
191
104
  const parentNode = ast.parent;
192
- const isAssignment = t.isAssignmentExpression(parentNode) && parentNode.left === node;
105
+ const isAssignment = t.isAssignmentExpression(parentNode) && parentNode.left === ast.node;
193
106
  if (!isAssignment) {
194
107
  let identifier;
195
108
  if (invokedApis.has(propertyName)) {
@@ -208,37 +121,10 @@ const plugin = function (babel) {
208
121
  needDefault = true;
209
122
  }
210
123
  },
211
- 'CallExpression|OptionalCallExpression'(ast) {
212
- const callee = ast.node.callee;
213
- // 对存在命名空间的 API 支持 tree-shaking:Taro.xx.yy -> xx_yy
214
- // 同时兼容:可选链调用(Taro?.JDMTA.pv() / Taro.JDMTA?.pv())、TS 类型断言(as any / ! / satisfies)
215
- const nsInfo = getTaroNamespaceCall(t, callee, taroName, this.file);
216
- if (nsInfo) {
217
- const { namespaceName, methodName } = nsInfo;
218
- const flatName = `${namespaceName}_${methodName}`;
219
- if (this.apis.has(flatName)) {
220
- let identifier;
221
- if (invokedApis.has(flatName)) {
222
- identifier = t.identifier(invokedApis.get(flatName));
223
- }
224
- else {
225
- const newName = ast.scope.generateUid(flatName);
226
- invokedApis.set(flatName, newName);
227
- identifier = t.identifier(newName);
228
- }
229
- // 如果当前是可选调用(OptionalCallExpression),直接将整条调用改写为普通函数调用,
230
- // 避免后续 preset-env 再对可选链做降级,导致重新依赖 Taro.JDMTA。
231
- if (t.isOptionalCallExpression(ast.node)) {
232
- ast.replaceWith(t.callExpression(identifier, ast.node.arguments));
233
- }
234
- else {
235
- ast.node.callee = identifier;
236
- }
237
- return;
238
- }
239
- }
124
+ CallExpression(ast) {
240
125
  if (!ast.scope.hasReference(this.canIUse))
241
126
  return;
127
+ const callee = ast.node.callee;
242
128
  if (t.isMemberExpression(callee) && t.isIdentifier(callee.object, { name: taroName })) {
243
129
  let propertyName = null;
244
130
  let propName = 'name';
@@ -280,45 +166,6 @@ const plugin = function (babel) {
280
166
  taroName = ast.scope.getBinding(this.bindingName)
281
167
  ? ast.scope.generateUid(this.bindingName)
282
168
  : this.bindingName;
283
- // 预扫描:在正式 visitor 遍历之前,先找到 import 确定 taroName,
284
- // 然后把所有 namespace API 的 OptionalCallExpression 降级为普通 CallExpression。
285
- // 这样可以抢在 @babel/plugin-transform-optional-chaining 展开可选链之前完成替换。
286
- const pkgName = this.packageName;
287
- let preScanTaroName = '';
288
- ast.traverse({
289
- ImportDeclaration(importPath) {
290
- if (importPath.node.source.value !== pkgName)
291
- return;
292
- for (const spec of importPath.node.specifiers) {
293
- if (t.isImportDefaultSpecifier(spec)) {
294
- preScanTaroName = spec.local.name;
295
- break;
296
- }
297
- }
298
- importPath.stop();
299
- }
300
- });
301
- if (preScanTaroName) {
302
- const apis = this.apis;
303
- const file = this.file;
304
- ast.traverse({
305
- OptionalCallExpression(optPath) {
306
- var _a;
307
- const nsInfo = getTaroNamespaceCall(t, optPath.node.callee, preScanTaroName, file);
308
- if (nsInfo) {
309
- const flatName = `${nsInfo.namespaceName}_${nsInfo.methodName}`;
310
- if (apis.has(flatName)) {
311
- if (process.env.JDAPI_DEBUG_TAROAPI === 'true') {
312
- const filename = ((_a = file === null || file === void 0 ? void 0 : file.opts) === null || _a === void 0 ? void 0 : _a.filename) || '';
313
- // eslint-disable-next-line no-console
314
- console.log('[jdapi-core-taroapi] pre-transform: strip optional call for', flatName, 'in file:', filename, 'code =', optPath.toString());
315
- }
316
- optPath.replaceWith(t.callExpression(optPath.node.callee, optPath.node.arguments));
317
- }
318
- }
319
- }
320
- });
321
- }
322
169
  },
323
170
  exit(ast) {
324
171
  const that = this;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "babel-plugin-transform-taroapi",
3
- "version": "4.2.1-beta.1",
3
+ "version": "4.2.1-beta.3",
4
4
  "author": "O2Team",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.js",
package/src/index.ts CHANGED
@@ -27,84 +27,6 @@ const plugin = function (babel: typeof BabelCore): BabelCore.PluginObj<IState> {
27
27
  ariaValuetext: 'aria-valuetext',
28
28
  }
29
29
 
30
- function stripTSCast (node: any): any {
31
- while (
32
- t.isTSAsExpression(node) ||
33
- t.isTSTypeAssertion(node) ||
34
- t.isTSNonNullExpression(node) ||
35
- (t.isTSSatisfiesExpression && t.isTSSatisfiesExpression(node))
36
- ) {
37
- node = node.expression
38
- }
39
- return node
40
- }
41
-
42
- function getTaroNamespaceCall (
43
- t: typeof BabelCore.types,
44
- callee: BabelCore.types.Expression,
45
- taroName: string,
46
- file?: BabelCore.BabelFile | null
47
- ): { namespaceName: string, methodName: string } | null {
48
- let target: any = callee
49
-
50
- // 如果是可选调用(例如 Taro.JDMTA.isTrafficMapEnable?.()),先取里面的 callee
51
- if (t.isOptionalCallExpression(target)) {
52
- target = target.callee
53
- }
54
-
55
- // 去掉 TS 断言 / 非空等包裹
56
- target = stripTSCast(target)
57
-
58
- if (!t.isMemberExpression(target) && !t.isOptionalMemberExpression(target)) {
59
- return null
60
- }
61
-
62
- // target.object 通常是 Taro.xx 这一层,
63
- // 也可能是形如 (_tmp = Taro.xx) 这样的赋值表达式,需要再解一层。
64
- let inner: any = stripTSCast(target.object)
65
- // 兼容 222 这类形态:(_tmp = Taro.JDMTA).isTrafficMapEnable?.()
66
- if (t.isAssignmentExpression(inner)) {
67
- inner = stripTSCast(inner.right)
68
- }
69
-
70
- if (!t.isMemberExpression(inner) && !t.isOptionalMemberExpression(inner)) {
71
- return null
72
- }
73
-
74
- if (!t.isIdentifier(inner.object, { name: taroName })) {
75
- return null
76
- }
77
-
78
- let namespaceName: string | null = null
79
- if (t.isIdentifier(inner.property)) {
80
- namespaceName = inner.property.name
81
- } else if (t.isStringLiteral(inner.property)) {
82
- namespaceName = inner.property.value
83
- }
84
-
85
- let methodName: string | null = null
86
- if (t.isIdentifier(target.property)) {
87
- methodName = target.property.name
88
- } else if (t.isStringLiteral(target.property)) {
89
- methodName = target.property.value
90
- }
91
-
92
- if (!namespaceName || !methodName) return null
93
-
94
- if (process.env.JDAPI_DEBUG_TAROAPI === 'true') {
95
- const filename = file?.opts?.filename || ''
96
- // eslint-disable-next-line no-console
97
- console.log(
98
- '[jdapi-core-taroapi] getTaroNamespaceCall hit:',
99
- `${namespaceName}_${methodName}`,
100
- 'file =',
101
- filename
102
- )
103
- }
104
-
105
- return { namespaceName, methodName }
106
- }
107
-
108
30
  // 这些变量需要在每个 program 里重置
109
31
  const invokedApis: Map<string, string> = new Map()
110
32
  let taroName: string
@@ -183,41 +105,10 @@ const plugin = function (babel: typeof BabelCore): BabelCore.PluginObj<IState> {
183
105
  }
184
106
  })
185
107
  },
186
- 'MemberExpression|OptionalMemberExpression' (ast: BabelCore.NodePath<any>) {
187
- const node = ast.node
188
-
189
- // 处理两层命名空间属性访问:Taro.xx.yy / Taro?.xx?.yy(非调用场景)
190
- // 调用场景由 CallExpression|OptionalCallExpression 负责
191
- const isCalleeOfCall = (t.isCallExpression(ast.parent) || t.isOptionalCallExpression(ast.parent)) && (ast.parent as any).callee === node
192
- if (!isCalleeOfCall) {
193
- const innerObj = stripTSCast(node.object)
194
- if (t.isMemberExpression(innerObj) || t.isOptionalMemberExpression(innerObj)) {
195
- const isTaroNamespace = t.isIdentifier(innerObj.object, { name: taroName })
196
- if (isTaroNamespace) {
197
- const namespaceName = t.isIdentifier(innerObj.property) ? innerObj.property.name : (t.isStringLiteral(innerObj.property) ? innerObj.property.value : null)
198
- const methodName = t.isIdentifier(node.property) ? node.property.name : (t.isStringLiteral(node.property) ? node.property.value : null)
199
- if (namespaceName && methodName) {
200
- const flatName = `${namespaceName}_${methodName}`
201
- if (this.apis.has(flatName)) {
202
- let identifier: BabelCore.types.Identifier
203
- if (invokedApis.has(flatName)) {
204
- identifier = t.identifier(invokedApis.get(flatName)!)
205
- } else {
206
- const newName = ast.scope.generateUid(flatName)
207
- invokedApis.set(flatName, newName)
208
- identifier = t.identifier(newName)
209
- }
210
- ast.replaceWith(identifier as any)
211
- return
212
- }
213
- }
214
- }
215
- }
216
- }
217
-
108
+ MemberExpression (ast: BabelCore.NodePath<any>) {
218
109
  /* 处理 Taro.xxx */
219
- const isTaro = t.isIdentifier(node.object, { name: taroName })
220
- const property = node.property
110
+ const isTaro = t.isIdentifier(ast.node.object, { name: taroName })
111
+ const property = ast.node.property
221
112
  let propertyName: string | null = null
222
113
  let propName = 'name'
223
114
 
@@ -234,7 +125,7 @@ const plugin = function (babel: typeof BabelCore): BabelCore.PluginObj<IState> {
234
125
  // 同一 api 使用多次,读取变量名
235
126
  if (this.apis.has(propertyName)) {
236
127
  const parentNode = ast.parent as BabelCore.types.AssignmentExpression
237
- const isAssignment = t.isAssignmentExpression(parentNode) && parentNode.left === node
128
+ const isAssignment = t.isAssignmentExpression(parentNode) && parentNode.left === ast.node
238
129
 
239
130
  if (!isAssignment) {
240
131
  let identifier: BabelCore.types.Identifier
@@ -252,40 +143,9 @@ const plugin = function (babel: typeof BabelCore): BabelCore.PluginObj<IState> {
252
143
  needDefault = true
253
144
  }
254
145
  },
255
- 'CallExpression|OptionalCallExpression' (ast: BabelCore.NodePath<any>) {
256
- const callee = ast.node.callee
257
- // 对存在命名空间的 API 支持 tree-shaking:Taro.xx.yy -> xx_yy
258
- // 同时兼容:可选链调用(Taro?.JDMTA.pv() / Taro.JDMTA?.pv())、TS 类型断言(as any / ! / satisfies)
259
- const nsInfo = getTaroNamespaceCall(t, callee as any, taroName, this.file as any)
260
- if (nsInfo) {
261
- const { namespaceName, methodName } = nsInfo
262
- const flatName = `${namespaceName}_${methodName}`
263
- if (this.apis.has(flatName)) {
264
- let identifier: BabelCore.types.Identifier
265
- if (invokedApis.has(flatName)) {
266
- identifier = t.identifier(invokedApis.get(flatName)!)
267
- } else {
268
- const newName = ast.scope.generateUid(flatName)
269
- invokedApis.set(flatName, newName)
270
- identifier = t.identifier(newName)
271
- }
272
- // 如果当前是可选调用(OptionalCallExpression),直接将整条调用改写为普通函数调用,
273
- // 避免后续 preset-env 再对可选链做降级,导致重新依赖 Taro.JDMTA。
274
- if (t.isOptionalCallExpression(ast.node)) {
275
- ast.replaceWith(
276
- t.callExpression(
277
- identifier,
278
- ast.node.arguments as any
279
- )
280
- )
281
- } else {
282
- ast.node.callee = identifier as any
283
- }
284
- return
285
- }
286
- }
287
-
146
+ CallExpression (ast: BabelCore.NodePath<any>) {
288
147
  if (!ast.scope.hasReference(this.canIUse)) return
148
+ const callee = ast.node.callee
289
149
  if (t.isMemberExpression(callee) && t.isIdentifier(callee.object, { name: taroName })) {
290
150
  let propertyName: string | null = null
291
151
  let propName = 'name'
@@ -327,57 +187,6 @@ const plugin = function (babel: typeof BabelCore): BabelCore.PluginObj<IState> {
327
187
  taroName = ast.scope.getBinding(this.bindingName)
328
188
  ? ast.scope.generateUid(this.bindingName)
329
189
  : this.bindingName
330
-
331
- // 预扫描:在正式 visitor 遍历之前,先找到 import 确定 taroName,
332
- // 然后把所有 namespace API 的 OptionalCallExpression 降级为普通 CallExpression。
333
- // 这样可以抢在 @babel/plugin-transform-optional-chaining 展开可选链之前完成替换。
334
- const pkgName = this.packageName
335
- let preScanTaroName = ''
336
- ast.traverse({
337
- ImportDeclaration (importPath: BabelCore.NodePath<BabelCore.types.ImportDeclaration>) {
338
- if (importPath.node.source.value !== pkgName) return
339
- for (const spec of importPath.node.specifiers) {
340
- if (t.isImportDefaultSpecifier(spec)) {
341
- preScanTaroName = spec.local.name
342
- break
343
- }
344
- }
345
- importPath.stop()
346
- }
347
- })
348
-
349
- if (preScanTaroName) {
350
- const apis = this.apis
351
- const file = this.file as any
352
- ast.traverse({
353
- OptionalCallExpression (optPath: BabelCore.NodePath<BabelCore.types.OptionalCallExpression>) {
354
- const nsInfo = getTaroNamespaceCall(t, optPath.node.callee as any, preScanTaroName, file)
355
- if (nsInfo) {
356
- const flatName = `${nsInfo.namespaceName}_${nsInfo.methodName}`
357
- if (apis.has(flatName)) {
358
- if (process.env.JDAPI_DEBUG_TAROAPI === 'true') {
359
- const filename = file?.opts?.filename || ''
360
- // eslint-disable-next-line no-console
361
- console.log(
362
- '[jdapi-core-taroapi] pre-transform: strip optional call for',
363
- flatName,
364
- 'in file:',
365
- filename,
366
- 'code =',
367
- optPath.toString()
368
- )
369
- }
370
- optPath.replaceWith(
371
- t.callExpression(
372
- optPath.node.callee as any,
373
- optPath.node.arguments as any
374
- )
375
- )
376
- }
377
- }
378
- }
379
- })
380
- }
381
190
  },
382
191
  exit (ast) {
383
192
  const that = this
@@ -1122,7 +1122,11 @@
1122
1122
  "success": "void",
1123
1123
  "return": "Promise<Partial<CallbackResult> & Record<string, unknown> & CallbackResult>"
1124
1124
  },
1125
- "createMapContext": "*",
1125
+ "createMapContext": {
1126
+ "object": "*",
1127
+ "success": "void",
1128
+ "return": "Promise<Partial<CallbackResult> & Record<string, unknown> & CallbackResult>"
1129
+ },
1126
1130
  "createMediaRecorder": {
1127
1131
  "object": "*",
1128
1132
  "success": "void",
@@ -2185,7 +2189,7 @@
2185
2189
  "type": "Type"
2186
2190
  },
2187
2191
  "image": {
2188
- "disableDefaultSize": "boolean",
2192
+ "lang": "string",
2189
2193
  "lazyLoad": "boolean",
2190
2194
  "mode": "Mode",
2191
2195
  "nativeProps": "*",