babel-plugin-transform-taroapi 4.1.12-beta.3 → 4.1.12-beta.32
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 +159 -6
- package/package.json +1 -1
- package/src/index.ts +197 -6
- package/tests/__mocks__/h5-definition.json +1 -6
package/dist/index.js
CHANGED
|
@@ -16,6 +16,63 @@ 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
|
+
}
|
|
19
76
|
// 这些变量需要在每个 program 里重置
|
|
20
77
|
const invokedApis = new Map();
|
|
21
78
|
let taroName;
|
|
@@ -84,10 +141,40 @@ const plugin = function (babel) {
|
|
|
84
141
|
}
|
|
85
142
|
});
|
|
86
143
|
},
|
|
87
|
-
MemberExpression(ast) {
|
|
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
|
+
}
|
|
88
175
|
/* 处理 Taro.xxx */
|
|
89
|
-
const isTaro = t.isIdentifier(
|
|
90
|
-
const property =
|
|
176
|
+
const isTaro = t.isIdentifier(node.object, { name: taroName });
|
|
177
|
+
const property = node.property;
|
|
91
178
|
let propertyName = null;
|
|
92
179
|
let propName = 'name';
|
|
93
180
|
if (!isTaro)
|
|
@@ -102,7 +189,7 @@ const plugin = function (babel) {
|
|
|
102
189
|
// 同一 api 使用多次,读取变量名
|
|
103
190
|
if (this.apis.has(propertyName)) {
|
|
104
191
|
const parentNode = ast.parent;
|
|
105
|
-
const isAssignment = t.isAssignmentExpression(parentNode) && parentNode.left ===
|
|
192
|
+
const isAssignment = t.isAssignmentExpression(parentNode) && parentNode.left === node;
|
|
106
193
|
if (!isAssignment) {
|
|
107
194
|
let identifier;
|
|
108
195
|
if (invokedApis.has(propertyName)) {
|
|
@@ -121,10 +208,37 @@ const plugin = function (babel) {
|
|
|
121
208
|
needDefault = true;
|
|
122
209
|
}
|
|
123
210
|
},
|
|
124
|
-
CallExpression(ast) {
|
|
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
|
+
}
|
|
125
240
|
if (!ast.scope.hasReference(this.canIUse))
|
|
126
241
|
return;
|
|
127
|
-
const callee = ast.node.callee;
|
|
128
242
|
if (t.isMemberExpression(callee) && t.isIdentifier(callee.object, { name: taroName })) {
|
|
129
243
|
let propertyName = null;
|
|
130
244
|
let propName = 'name';
|
|
@@ -166,6 +280,45 @@ const plugin = function (babel) {
|
|
|
166
280
|
taroName = ast.scope.getBinding(this.bindingName)
|
|
167
281
|
? ast.scope.generateUid(this.bindingName)
|
|
168
282
|
: 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
|
+
}
|
|
169
322
|
},
|
|
170
323
|
exit(ast) {
|
|
171
324
|
const that = this;
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -27,6 +27,84 @@ 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
|
+
|
|
30
108
|
// 这些变量需要在每个 program 里重置
|
|
31
109
|
const invokedApis: Map<string, string> = new Map()
|
|
32
110
|
let taroName: string
|
|
@@ -105,10 +183,41 @@ const plugin = function (babel: typeof BabelCore): BabelCore.PluginObj<IState> {
|
|
|
105
183
|
}
|
|
106
184
|
})
|
|
107
185
|
},
|
|
108
|
-
MemberExpression (ast: BabelCore.NodePath<any>) {
|
|
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
|
+
|
|
109
218
|
/* 处理 Taro.xxx */
|
|
110
|
-
const isTaro = t.isIdentifier(
|
|
111
|
-
const property =
|
|
219
|
+
const isTaro = t.isIdentifier(node.object, { name: taroName })
|
|
220
|
+
const property = node.property
|
|
112
221
|
let propertyName: string | null = null
|
|
113
222
|
let propName = 'name'
|
|
114
223
|
|
|
@@ -125,7 +234,7 @@ const plugin = function (babel: typeof BabelCore): BabelCore.PluginObj<IState> {
|
|
|
125
234
|
// 同一 api 使用多次,读取变量名
|
|
126
235
|
if (this.apis.has(propertyName)) {
|
|
127
236
|
const parentNode = ast.parent as BabelCore.types.AssignmentExpression
|
|
128
|
-
const isAssignment = t.isAssignmentExpression(parentNode) && parentNode.left ===
|
|
237
|
+
const isAssignment = t.isAssignmentExpression(parentNode) && parentNode.left === node
|
|
129
238
|
|
|
130
239
|
if (!isAssignment) {
|
|
131
240
|
let identifier: BabelCore.types.Identifier
|
|
@@ -143,9 +252,40 @@ const plugin = function (babel: typeof BabelCore): BabelCore.PluginObj<IState> {
|
|
|
143
252
|
needDefault = true
|
|
144
253
|
}
|
|
145
254
|
},
|
|
146
|
-
CallExpression (ast: BabelCore.NodePath<any>) {
|
|
147
|
-
if (!ast.scope.hasReference(this.canIUse)) return
|
|
255
|
+
'CallExpression|OptionalCallExpression' (ast: BabelCore.NodePath<any>) {
|
|
148
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
|
+
|
|
288
|
+
if (!ast.scope.hasReference(this.canIUse)) return
|
|
149
289
|
if (t.isMemberExpression(callee) && t.isIdentifier(callee.object, { name: taroName })) {
|
|
150
290
|
let propertyName: string | null = null
|
|
151
291
|
let propName = 'name'
|
|
@@ -187,6 +327,57 @@ const plugin = function (babel: typeof BabelCore): BabelCore.PluginObj<IState> {
|
|
|
187
327
|
taroName = ast.scope.getBinding(this.bindingName)
|
|
188
328
|
? ast.scope.generateUid(this.bindingName)
|
|
189
329
|
: 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
|
+
}
|
|
190
381
|
},
|
|
191
382
|
exit (ast) {
|
|
192
383
|
const that = this
|
|
@@ -1122,11 +1122,7 @@
|
|
|
1122
1122
|
"success": "void",
|
|
1123
1123
|
"return": "Promise<Partial<CallbackResult> & Record<string, unknown> & CallbackResult>"
|
|
1124
1124
|
},
|
|
1125
|
-
"createMapContext":
|
|
1126
|
-
"object": "*",
|
|
1127
|
-
"success": "void",
|
|
1128
|
-
"return": "Promise<Partial<CallbackResult> & Record<string, unknown> & CallbackResult>"
|
|
1129
|
-
},
|
|
1125
|
+
"createMapContext": "*",
|
|
1130
1126
|
"createMediaRecorder": {
|
|
1131
1127
|
"object": "*",
|
|
1132
1128
|
"success": "void",
|
|
@@ -2189,7 +2185,6 @@
|
|
|
2189
2185
|
"type": "Type"
|
|
2190
2186
|
},
|
|
2191
2187
|
"image": {
|
|
2192
|
-
"lang": "string",
|
|
2193
2188
|
"lazyLoad": "boolean",
|
|
2194
2189
|
"mode": "Mode",
|
|
2195
2190
|
"nativeProps": "*",
|