@tarojs/taroize 4.0.0-canary.8 → 4.0.0
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 +17 -3
- package/lib/src/cache.js +2 -1
- package/lib/src/cache.js.map +1 -1
- package/lib/src/global.js +4 -0
- package/lib/src/global.js.map +1 -1
- package/lib/src/index.js +33 -20
- package/lib/src/index.js.map +1 -1
- package/lib/src/json.js.map +1 -1
- package/lib/src/script.js +8 -2
- package/lib/src/script.js.map +1 -1
- package/lib/src/template.js +62 -20
- package/lib/src/template.js.map +1 -1
- package/lib/src/utils.js +147 -46
- package/lib/src/utils.js.map +1 -1
- package/lib/src/wxml.js +182 -62
- package/lib/src/wxml.js.map +1 -1
- package/package.json +22 -30
- package/lib/src/vue.js +0 -357
- package/lib/src/vue.js.map +0 -1
package/lib/src/wxml.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.parseStyle = exports.parseContent = exports.removeEmptyTextAndComment = exports.parseWXML = exports.createWxmlVistor = exports.getWxsImports = exports.createPreWxmlVistor = exports.convertStyleUnit = exports.wxTemplateCommand = exports.WX_SHOW = exports.WX_ELSE = exports.WX_KEY = exports.WX_FOR_INDEX = exports.WX_FOR_ITEM = exports.WX_FOR = exports.WX_ELSE_IF = exports.WX_IF = exports.NodeType = void 0;
|
|
4
4
|
/* eslint-disable camelcase */
|
|
5
|
+
const path = require("node:path");
|
|
5
6
|
const parser_1 = require("@babel/parser");
|
|
6
7
|
const traverse_1 = require("@babel/traverse");
|
|
7
8
|
const t = require("@babel/types");
|
|
@@ -10,14 +11,13 @@ const helper_1 = require("@tarojs/helper");
|
|
|
10
11
|
const shared_1 = require("@tarojs/shared");
|
|
11
12
|
const himalaya_wxml_1 = require("himalaya-wxml");
|
|
12
13
|
const lodash_1 = require("lodash");
|
|
14
|
+
const prettier = require("prettier");
|
|
13
15
|
const cache_1 = require("./cache");
|
|
14
16
|
const constant_1 = require("./constant");
|
|
15
17
|
const events_1 = require("./events");
|
|
16
18
|
const global_1 = require("./global");
|
|
17
19
|
const template_1 = require("./template");
|
|
18
20
|
const utils_1 = require("./utils");
|
|
19
|
-
const { prettyPrint } = require('html');
|
|
20
|
-
const pathTool = require('path');
|
|
21
21
|
const allCamelCase = (str) => str.charAt(0).toUpperCase() + (0, lodash_1.camelCase)(str.substr(1));
|
|
22
22
|
function buildSlotName(slotName) {
|
|
23
23
|
return `render${slotName[0].toUpperCase() + slotName.replace('-', '').slice(1)}`;
|
|
@@ -27,7 +27,7 @@ var NodeType;
|
|
|
27
27
|
NodeType["Element"] = "element";
|
|
28
28
|
NodeType["Comment"] = "comment";
|
|
29
29
|
NodeType["Text"] = "text";
|
|
30
|
-
})(NodeType
|
|
30
|
+
})(NodeType || (exports.NodeType = NodeType = {}));
|
|
31
31
|
exports.WX_IF = 'wx:if';
|
|
32
32
|
exports.WX_ELSE_IF = 'wx:elif';
|
|
33
33
|
exports.WX_FOR = 'wx:for';
|
|
@@ -47,7 +47,7 @@ function buildElement(name, children = [], attributes = []) {
|
|
|
47
47
|
}
|
|
48
48
|
// 将 style 属性中属性名转小驼峰格式 并且将 {{}} 转为 ${}格式生成对应ast节点
|
|
49
49
|
function convertStyleAttrs(styleAttrsMap) {
|
|
50
|
-
(0, utils_1.
|
|
50
|
+
(0, utils_1.updateLogFileContent)(`INFO [taroize] convertStyleAttrs - 进入函数 ${(0, utils_1.getLineBreak)()}`);
|
|
51
51
|
styleAttrsMap.forEach((attr) => {
|
|
52
52
|
attr.attrName = (0, shared_1.toCamelCase)(attr.attrName.trim());
|
|
53
53
|
// 匹配 {{}} 内部以及左右两边值
|
|
@@ -73,7 +73,7 @@ function convertStyleAttrs(styleAttrsMap) {
|
|
|
73
73
|
* @param { any[] } attrKeyValueMap 属性解析为 {attrName: attrValue} 形式的数组
|
|
74
74
|
*/
|
|
75
75
|
function parseStyleAttrs(styleAttrsMap, attrKeyValueMap) {
|
|
76
|
-
(0, utils_1.
|
|
76
|
+
(0, utils_1.updateLogFileContent)(`INFO [taroize] parseStyleAttrs - 进入函数 ${(0, utils_1.getLineBreak)()}`);
|
|
77
77
|
styleAttrsMap.forEach((attr) => {
|
|
78
78
|
if (attr) {
|
|
79
79
|
// 对含三元运算符的写法 style="width:{{ xx ? xx : xx }}" 匹配第一个 : 避免匹配三元表达式中的 : 运算符
|
|
@@ -98,20 +98,13 @@ function convertStyleUnit(value) {
|
|
|
98
98
|
if (Number(size) === 0) {
|
|
99
99
|
return match.replace(size, '0').replace(unit, 'rem');
|
|
100
100
|
}
|
|
101
|
-
|
|
102
|
-
if (parseInt(size, 10) === 0) {
|
|
103
|
-
return match.replace(size, '1').replace(unit, 'rem');
|
|
104
|
-
}
|
|
105
|
-
return match.replace(size, parseInt(size, 10) / 20 + '').replace(unit, 'rem');
|
|
101
|
+
return match.replace(size, parseFloat(size) / 20 + '').replace(unit, 'rem');
|
|
106
102
|
})
|
|
107
103
|
.replace(/\s*-?([0-9.]+)(rpx)\b/gi, function (match, size, unit) {
|
|
108
104
|
if (Number(size) === 0) {
|
|
109
105
|
return match.replace(size, '0').replace(unit, 'rem');
|
|
110
106
|
}
|
|
111
|
-
|
|
112
|
-
return match.replace(size, '1').replace(unit, 'rem');
|
|
113
|
-
}
|
|
114
|
-
return match.replace(size, parseInt(size, 10) / 40 + '').replace(unit, 'rem');
|
|
107
|
+
return match.replace(size, parseFloat(size) / 40 + '').replace(unit, 'rem');
|
|
115
108
|
});
|
|
116
109
|
// 把 xx="...{{参数}}rpx/px"的尺寸单位都转为rem,比如"{{参数}}rpx" -> "{{参数/40}}rem"
|
|
117
110
|
tempValue = tempValue
|
|
@@ -130,8 +123,9 @@ function convertStyleUnit(value) {
|
|
|
130
123
|
});
|
|
131
124
|
}
|
|
132
125
|
catch (error) {
|
|
126
|
+
(0, utils_1.createErrorCodeMsg)('WxmlUnitConversionError', `wxml内px/rpx单位转换失败: ${error}`, tempValue, global_1.globals.currentParseFile);
|
|
133
127
|
(0, helper_1.printLog)("error" /* processTypeEnum.ERROR */, `wxml内px/rpx单位转换失败: ${error}`);
|
|
134
|
-
(0, utils_1.
|
|
128
|
+
(0, utils_1.updateLogFileContent)(`WARN [taroize] convertStyleUnit - wxml内px/rpx单位转换异常 ${(0, utils_1.getLineBreak)()}${error} ${(0, utils_1.getLineBreak)()}`);
|
|
135
129
|
}
|
|
136
130
|
}
|
|
137
131
|
return tempValue;
|
|
@@ -144,6 +138,7 @@ exports.convertStyleUnit = convertStyleUnit;
|
|
|
144
138
|
* @returns Visitor
|
|
145
139
|
*/
|
|
146
140
|
const createPreWxmlVistor = (templates) => {
|
|
141
|
+
(0, utils_1.updateLogFileContent)(`INFO [taroize] createPreWxmlVistor - 入参 ${(0, utils_1.getLineBreak)()}templates: ${templates} ${(0, utils_1.getLineBreak)()}`);
|
|
147
142
|
// const Applys = new Map<string, string[]>()
|
|
148
143
|
return {
|
|
149
144
|
JSXElement: {
|
|
@@ -179,19 +174,20 @@ exports.createPreWxmlVistor = createPreWxmlVistor;
|
|
|
179
174
|
* @returns 需要导入的wxs语句集合
|
|
180
175
|
*/
|
|
181
176
|
function getWxsImports(templateFileName, usedWxses, dirPath) {
|
|
182
|
-
const templatePath =
|
|
177
|
+
const templatePath = path.join(global_1.globals.rootPath, 'imports', `${templateFileName}.js`);
|
|
183
178
|
const wxsImports = [];
|
|
184
179
|
for (const usedWxs of usedWxses) {
|
|
185
|
-
const wxsAbsPath =
|
|
186
|
-
const wxsRelPath =
|
|
180
|
+
const wxsAbsPath = path.resolve(dirPath, `${usedWxs.src}.js`);
|
|
181
|
+
const wxsRelPath = path.relative(path.dirname(templatePath), wxsAbsPath);
|
|
187
182
|
wxsImports.push((0, utils_1.buildImportStatement)((0, utils_1.normalizePath)(wxsRelPath), [], usedWxs.module));
|
|
188
183
|
}
|
|
189
184
|
return wxsImports;
|
|
190
185
|
}
|
|
191
186
|
exports.getWxsImports = getWxsImports;
|
|
192
187
|
const createWxmlVistor = (loopIds, refIds, dirPath, wxses = [], imports = [], templates) => {
|
|
193
|
-
(0, utils_1.
|
|
188
|
+
(0, utils_1.updateLogFileContent)(`INFO [taroize] createWxmlVistor - 入参 ${(0, utils_1.getLineBreak)()}dirPath: ${dirPath} ${(0, utils_1.getLineBreak)()}`);
|
|
194
189
|
const jsxAttrVisitor = (path) => {
|
|
190
|
+
(0, utils_1.updateLogFileContent)(`INFO [taroize] createWxmlVistor - 解析JSXAttribute ${(0, utils_1.getLineBreak)()}${path} ${(0, utils_1.getLineBreak)()}`);
|
|
195
191
|
const name = path.node.name;
|
|
196
192
|
const jsx = path.findParent((p) => p.isJSXElement());
|
|
197
193
|
// 把 hidden 转换为 wxif
|
|
@@ -228,16 +224,31 @@ const createWxmlVistor = (loopIds, refIds, dirPath, wxses = [], imports = [], te
|
|
|
228
224
|
}
|
|
229
225
|
};
|
|
230
226
|
const renameJSXKey = (path) => {
|
|
227
|
+
var _a, _b, _c, _d;
|
|
228
|
+
(0, utils_1.updateLogFileContent)(`INFO [taroize] createWxmlVistor - 解析JSXIdentifier ${(0, utils_1.getLineBreak)()}${path} ${(0, utils_1.getLineBreak)()}`);
|
|
231
229
|
const nodeName = path.node.name;
|
|
232
230
|
if (path.parentPath.isJSXAttribute()) {
|
|
231
|
+
const cacheNode = (0, lodash_1.cloneDeep)(path.parentPath.parentPath.parent);
|
|
233
232
|
if (nodeName === exports.WX_SHOW) {
|
|
234
233
|
path.replaceWith(t.jSXIdentifier(exports.WX_IF)); // wx:show转换后不支持,不频繁切换的话wx:if可替代
|
|
234
|
+
const position = {
|
|
235
|
+
col: ((_a = cacheNode.position) === null || _a === void 0 ? void 0 : _a.start.column) || 0,
|
|
236
|
+
row: ((_b = cacheNode.position) === null || _b === void 0 ? void 0 : _b.start.line) || 0
|
|
237
|
+
};
|
|
238
|
+
(0, utils_1.createErrorCodeMsg)('uncompilableAttribute', `属性 ${nodeName}不能编译,会被替换为wx:if`, (0, utils_1.astToCode)(cacheNode) || '', global_1.globals.currentParseFile, position);
|
|
235
239
|
// eslint-disable-next-line no-console
|
|
236
240
|
console.log(`属性 ${nodeName}不能编译,会被替换为wx:if`);
|
|
241
|
+
(0, utils_1.updateLogFileContent)(`WARN [taroize] createWxmlVistor - ${nodeName} 属性不能编译,会被替换为 wx:if ${(0, utils_1.getLineBreak)()}`);
|
|
237
242
|
}
|
|
238
243
|
else if (nodeName.startsWith('wx:') && !exports.wxTemplateCommand.includes(nodeName)) {
|
|
244
|
+
const position = {
|
|
245
|
+
col: ((_c = cacheNode.position) === null || _c === void 0 ? void 0 : _c.start.column) || 0,
|
|
246
|
+
row: ((_d = cacheNode.position) === null || _d === void 0 ? void 0 : _d.start.line) || 0
|
|
247
|
+
};
|
|
248
|
+
(0, utils_1.createErrorCodeMsg)('unknownScopeAttribute', `未知 wx 作用域属性: ${nodeName},该属性会被移除掉。`, (0, utils_1.astToCode)(cacheNode) || '', global_1.globals.currentParseFile, position);
|
|
239
249
|
// eslint-disable-next-line no-console
|
|
240
250
|
console.log(`未知 wx 作用域属性: ${nodeName},该属性会被移除掉。`);
|
|
251
|
+
(0, utils_1.updateLogFileContent)(`WARN [taroize] createWxmlVistor - 未知 wx 作用域属性: ${nodeName},该属性会被移除掉 ${(0, utils_1.getLineBreak)()}`);
|
|
241
252
|
path.parentPath.remove();
|
|
242
253
|
}
|
|
243
254
|
}
|
|
@@ -247,6 +258,7 @@ const createWxmlVistor = (loopIds, refIds, dirPath, wxses = [], imports = [], te
|
|
|
247
258
|
JSXIdentifier: renameJSXKey,
|
|
248
259
|
Identifier: {
|
|
249
260
|
enter(path) {
|
|
261
|
+
(0, utils_1.updateLogFileContent)(`INFO [taroize] createWxmlVistor - 解析Identifier ${(0, utils_1.getLineBreak)()}${path} ${(0, utils_1.getLineBreak)()}`);
|
|
250
262
|
if (!path.isReferencedIdentifier()) {
|
|
251
263
|
return;
|
|
252
264
|
}
|
|
@@ -261,7 +273,8 @@ const createWxmlVistor = (loopIds, refIds, dirPath, wxses = [], imports = [], te
|
|
|
261
273
|
},
|
|
262
274
|
JSXElement: {
|
|
263
275
|
enter(path) {
|
|
264
|
-
var _a;
|
|
276
|
+
var _a, _b, _c;
|
|
277
|
+
(0, utils_1.updateLogFileContent)(`INFO [taroize] createWxmlVistor - 解析JSXElement ${(0, utils_1.getLineBreak)()}${path} ${(0, utils_1.getLineBreak)()}`);
|
|
265
278
|
const openingElement = path.get('openingElement');
|
|
266
279
|
const jsxName = openingElement.get('name');
|
|
267
280
|
const attrs = openingElement.get('attributes');
|
|
@@ -299,6 +312,7 @@ const createWxmlVistor = (loopIds, refIds, dirPath, wxses = [], imports = [], te
|
|
|
299
312
|
}
|
|
300
313
|
const tagName = jsxName.node.name;
|
|
301
314
|
if (tagName === 'Slot') {
|
|
315
|
+
(0, utils_1.updateLogFileContent)(`INFO [taroize] createWxmlVistor - tagName: Slot ${(0, utils_1.getLineBreak)()}`);
|
|
302
316
|
const nameAttr = attrs.find((a) => t.isJSXAttribute(a.node) && a.node.name.name === 'name');
|
|
303
317
|
let slotName = '';
|
|
304
318
|
if (nameAttr && t.isJSXAttribute(nameAttr.node)) {
|
|
@@ -306,7 +320,12 @@ const createWxmlVistor = (loopIds, refIds, dirPath, wxses = [], imports = [], te
|
|
|
306
320
|
slotName = nameAttr.node.value.value;
|
|
307
321
|
}
|
|
308
322
|
else {
|
|
309
|
-
|
|
323
|
+
(0, utils_1.updateLogFileContent)(`ERROR [taroize] createWxmlVistor - slot 的值不是一个字符串 ${(0, utils_1.getLineBreak)()}`);
|
|
324
|
+
// const error = codeFrameError(jsxName.node, 'slot 的值必须是一个字符串')
|
|
325
|
+
// @ts-ignore
|
|
326
|
+
const { line, column } = ((_b = (_a = path.node) === null || _a === void 0 ? void 0 : _a.position) === null || _b === void 0 ? void 0 : _b.start) || { line: 0, column: 0 };
|
|
327
|
+
const position = { col: column, row: line };
|
|
328
|
+
throw new utils_1.IReportError('slot 的值必须是一个字符串', 'SlotValueTypeError', 'WXML_FILE', (0, utils_1.astToCode)(path.node) || '', position);
|
|
310
329
|
}
|
|
311
330
|
}
|
|
312
331
|
const children = t.memberExpression(t.memberExpression(t.thisExpression(), t.identifier('props')), t.identifier(slotName ? buildSlotName(slotName) : 'children'));
|
|
@@ -314,13 +333,16 @@ const createWxmlVistor = (loopIds, refIds, dirPath, wxses = [], imports = [], te
|
|
|
314
333
|
path.replaceWith(path.parentPath.isJSXElement() ? t.jSXExpressionContainer(children) : children);
|
|
315
334
|
}
|
|
316
335
|
catch (error) {
|
|
336
|
+
(0, utils_1.updateLogFileContent)(`WARN [taroize] createWxmlVistor - Slot 节点替换异常 ${(0, utils_1.getLineBreak)()}${error} ${(0, utils_1.getLineBreak)()}`);
|
|
317
337
|
//
|
|
318
338
|
}
|
|
319
339
|
}
|
|
320
340
|
if (tagName === 'Wxs') {
|
|
341
|
+
(0, utils_1.updateLogFileContent)(`INFO [taroize] createWxmlVistor - tagName: Wxs ${(0, utils_1.getLineBreak)()}`);
|
|
321
342
|
wxses.push(getWXS(attrs.map((a) => a.node), path, imports));
|
|
322
343
|
}
|
|
323
344
|
if (tagName === 'Template') {
|
|
345
|
+
(0, utils_1.updateLogFileContent)(`INFO [taroize] createWxmlVistor - tagName: Template ${(0, utils_1.getLineBreak)()}`);
|
|
324
346
|
const template = (0, template_1.parseTemplate)(path, dirPath, wxses);
|
|
325
347
|
if (template) {
|
|
326
348
|
let funcs = new Set();
|
|
@@ -336,13 +358,14 @@ const createWxmlVistor = (loopIds, refIds, dirPath, wxses = [], imports = [], te
|
|
|
336
358
|
const usedTemplate = new Set();
|
|
337
359
|
// funcs的值首先来源于预解析结果
|
|
338
360
|
if (templates) {
|
|
339
|
-
const applyFuncs = (
|
|
361
|
+
const applyFuncs = (_c = templates.get(name)) === null || _c === void 0 ? void 0 : _c.funcs;
|
|
340
362
|
if (applyFuncs) {
|
|
341
363
|
funcs = applyFuncs;
|
|
342
364
|
}
|
|
343
365
|
}
|
|
344
366
|
(0, traverse_1.default)(ast, {
|
|
345
367
|
JSXIdentifier(path) {
|
|
368
|
+
(0, utils_1.updateLogFileContent)(`INFO [taroize] createWxmlVistor - 解析JSXIdentifier ${(0, utils_1.getLineBreak)()}${path} ${(0, utils_1.getLineBreak)()}`);
|
|
346
369
|
const node = path.node;
|
|
347
370
|
if (node.name.endsWith('Tmpl') && node.name.length > 4 && path.parentPath.isJSXOpeningElement()) {
|
|
348
371
|
usedTemplate.add(node.name);
|
|
@@ -374,6 +397,7 @@ const createWxmlVistor = (loopIds, refIds, dirPath, wxses = [], imports = [], te
|
|
|
374
397
|
}
|
|
375
398
|
},
|
|
376
399
|
JSXAttribute(path) {
|
|
400
|
+
(0, utils_1.updateLogFileContent)(`INFO [taroize] createWxmlVistor - 解析JSXAttribute ${(0, utils_1.getLineBreak)()}${path} ${(0, utils_1.getLineBreak)()}`);
|
|
377
401
|
// 识别template使用到的处理事件的func
|
|
378
402
|
const node = path.node;
|
|
379
403
|
if (t.isJSXExpressionContainer(node.value) &&
|
|
@@ -389,6 +413,7 @@ const createWxmlVistor = (loopIds, refIds, dirPath, wxses = [], imports = [], te
|
|
|
389
413
|
(0, traverse_1.default)(ast, {
|
|
390
414
|
// 将使用到的处理事件的func写入到props
|
|
391
415
|
BlockStatement(path) {
|
|
416
|
+
(0, utils_1.updateLogFileContent)(`INFO [taroize] createWxmlVistor - 解析BlockStatement ${(0, utils_1.getLineBreak)()}${path} ${(0, utils_1.getLineBreak)()}`);
|
|
392
417
|
if (funcs.size > 0) {
|
|
393
418
|
const body = path.node.body;
|
|
394
419
|
if (t.isVariableDeclaration(body[0])) {
|
|
@@ -431,12 +456,14 @@ const createWxmlVistor = (loopIds, refIds, dirPath, wxses = [], imports = [], te
|
|
|
431
456
|
}
|
|
432
457
|
}
|
|
433
458
|
if (tagName === 'Import') {
|
|
459
|
+
(0, utils_1.updateLogFileContent)(`INFO [taroize] createWxmlVistor - tagName: Import ${(0, utils_1.getLineBreak)()}`);
|
|
434
460
|
const mods = (0, template_1.parseModule)(path, dirPath, 'import');
|
|
435
461
|
if (mods) {
|
|
436
462
|
imports.push(...mods);
|
|
437
463
|
}
|
|
438
464
|
}
|
|
439
465
|
if (tagName === 'Include') {
|
|
466
|
+
(0, utils_1.updateLogFileContent)(`INFO [taroize] createWxmlVistor - tagName: Include ${(0, utils_1.getLineBreak)()}`);
|
|
440
467
|
(0, template_1.parseModule)(path, dirPath, 'include');
|
|
441
468
|
}
|
|
442
469
|
},
|
|
@@ -456,6 +483,7 @@ const createWxmlVistor = (loopIds, refIds, dirPath, wxses = [], imports = [], te
|
|
|
456
483
|
path.replaceWith(caller);
|
|
457
484
|
}
|
|
458
485
|
catch (error) {
|
|
486
|
+
(0, utils_1.updateLogFileContent)(`WARN [taroize] createWxmlVistor - block 节点替换异常 ${(0, utils_1.getLineBreak)()}${error} ${(0, utils_1.getLineBreak)()}`);
|
|
459
487
|
//
|
|
460
488
|
}
|
|
461
489
|
}
|
|
@@ -471,7 +499,7 @@ exports.createWxmlVistor = createWxmlVistor;
|
|
|
471
499
|
*/
|
|
472
500
|
function templateBfs(templates) {
|
|
473
501
|
var _a;
|
|
474
|
-
(0, utils_1.
|
|
502
|
+
(0, utils_1.updateLogFileContent)(`INFO [taroize] templateBfs - 进入函数 ${(0, utils_1.getLineBreak)()}`);
|
|
475
503
|
const names = [];
|
|
476
504
|
const applys = new Map();
|
|
477
505
|
for (const key of templates.keys()) {
|
|
@@ -517,15 +545,24 @@ function templateBfs(templates) {
|
|
|
517
545
|
}
|
|
518
546
|
}
|
|
519
547
|
function parseWXML(dirPath, wxml, parseImport) {
|
|
520
|
-
(0, utils_1.
|
|
548
|
+
(0, utils_1.updateLogFileContent)(`INFO [taroize] parseWXML - 入参 ${(0, utils_1.getLineBreak)()}dirPath: ${dirPath} ${(0, utils_1.getLineBreak)()}parseImport: ${parseImport} ${(0, utils_1.getLineBreak)()}`);
|
|
521
549
|
try {
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
550
|
+
if (wxml) {
|
|
551
|
+
const REG_WXS = /(<wxs)[^>]*>[\s\S]*?(<\/ *wxs *>)/g;
|
|
552
|
+
if (REG_WXS.test(wxml)) {
|
|
553
|
+
// prettier html parser 只对 <srcipt> 标签里的 JS 脚本进行格式化,<wxs> 里的脚本会被格式化在一行中,此时脚本如果存在注释的话会报错。
|
|
554
|
+
// 因此需要把 <wxs> 转换为 <script> 后再使用 prettier 进行格式化。
|
|
555
|
+
wxml = wxml.replace(/<wxs/g, '<script').replace(/<\/ *wxs *>/g, '</script>');
|
|
556
|
+
wxml = prettier.format(wxml, { parser: 'html' });
|
|
557
|
+
wxml = wxml.replace(/<script/g, '<wxs').replace(/<\/ *script *>/g, '</wxs>');
|
|
558
|
+
}
|
|
559
|
+
else {
|
|
560
|
+
wxml = prettier.format(wxml, { parser: 'html' });
|
|
561
|
+
}
|
|
562
|
+
}
|
|
527
563
|
}
|
|
528
564
|
catch (error) {
|
|
565
|
+
(0, utils_1.updateLogFileContent)(`WARN [taroize] parseWXML - wxml代码格式化异常 ${(0, utils_1.getLineBreak)()}${error} ${(0, utils_1.getLineBreak)()}`);
|
|
529
566
|
//
|
|
530
567
|
}
|
|
531
568
|
if (!parseImport) {
|
|
@@ -547,7 +584,7 @@ function parseWXML(dirPath, wxml, parseImport) {
|
|
|
547
584
|
wxml: t.nullLiteral(),
|
|
548
585
|
};
|
|
549
586
|
}
|
|
550
|
-
const nodes = removeEmptyTextAndComment((0, himalaya_wxml_1.parse)(wxml.trim()));
|
|
587
|
+
const nodes = removeEmptyTextAndComment((0, himalaya_wxml_1.parse)(wxml.trim(), Object.assign(Object.assign({}, himalaya_wxml_1.parseDefaults), { includePositions: true })));
|
|
551
588
|
const ast = t.file(t.program([t.expressionStatement(parseNode(buildElement('block', nodes)))], []));
|
|
552
589
|
// 确认当前解析页面是否已经解析过,如果解析过则直接获取缓存解析
|
|
553
590
|
let parseResult = (0, cache_1.getCacheWxml)(dirPath, hydrate(ast));
|
|
@@ -580,6 +617,7 @@ function parseWXML(dirPath, wxml, parseImport) {
|
|
|
580
617
|
}
|
|
581
618
|
exports.parseWXML = parseWXML;
|
|
582
619
|
function getWXS(attrs, path, imports) {
|
|
620
|
+
var _a, _b, _c, _d, _e, _f;
|
|
583
621
|
let moduleName = null;
|
|
584
622
|
let src = null;
|
|
585
623
|
for (const attr of attrs) {
|
|
@@ -588,7 +626,11 @@ function getWXS(attrs, path, imports) {
|
|
|
588
626
|
const attrValue = attr.value;
|
|
589
627
|
let value = null;
|
|
590
628
|
if (attrValue === null) {
|
|
591
|
-
|
|
629
|
+
// @ts-ignore
|
|
630
|
+
const { line, column } = ((_b = (_a = path.node) === null || _a === void 0 ? void 0 : _a.position) === null || _b === void 0 ? void 0 : _b.start) || { line: 0, column: 0 };
|
|
631
|
+
const position = { col: column, row: line };
|
|
632
|
+
(0, utils_1.updateLogFileContent)(`ERROR [taroize] getWXS - wxs 标签的属性值为空 ${(0, utils_1.getLineBreak)()}`);
|
|
633
|
+
throw new utils_1.IReportError('wxs 标签的属性值不得为空', 'WxsTagAttributeEmptyError', 'WXML_FILE', (0, utils_1.astToCode)(path.node) || '', position);
|
|
592
634
|
}
|
|
593
635
|
if (t.isStringLiteral(attrValue)) {
|
|
594
636
|
value = attrValue.value;
|
|
@@ -607,13 +649,18 @@ function getWXS(attrs, path, imports) {
|
|
|
607
649
|
if (!src) {
|
|
608
650
|
const { children: [script], } = path.node;
|
|
609
651
|
if (!t.isJSXText(script)) {
|
|
610
|
-
|
|
652
|
+
// @ts-ignore
|
|
653
|
+
const { line, column } = ((_d = (_c = path.node) === null || _c === void 0 ? void 0 : _c.position) === null || _d === void 0 ? void 0 : _d.start) || { line: 0, column: 0 };
|
|
654
|
+
const position = { col: column, row: line };
|
|
655
|
+
(0, utils_1.updateLogFileContent)(`ERROR [taroize] getWXS - wxs 没有 src 属性且标签内部没有 wxs 代码 ${(0, utils_1.getLineBreak)()}`);
|
|
656
|
+
throw new utils_1.IReportError('wxs 如果没有 src 属性,标签内部必须有 wxs 代码。', 'WxsTagCodeMissingError', 'WXML_FILE', (0, utils_1.astToCode)(path.node) || '', position);
|
|
611
657
|
}
|
|
612
658
|
src = './wxs__' + moduleName;
|
|
613
659
|
const ast = (0, utils_1.parseCode)(script.value);
|
|
614
660
|
(0, traverse_1.default)(ast, {
|
|
615
661
|
CallExpression(path) {
|
|
616
|
-
var _a, _b, _c;
|
|
662
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
|
|
663
|
+
(0, utils_1.updateLogFileContent)(`INFO [taroize] getWXS - 解析CallExpression ${(0, utils_1.getLineBreak)()}${path} ${(0, utils_1.getLineBreak)()}`);
|
|
617
664
|
// wxs标签中getRegExp转换为new RegExp
|
|
618
665
|
if (t.isIdentifier(path.node.callee, { name: 'getRegExp' })) {
|
|
619
666
|
// 根据正则表达式是否定义了正则匹配修饰符,有则不变,没有就用默认
|
|
@@ -631,25 +678,41 @@ function getWXS(attrs, path, imports) {
|
|
|
631
678
|
path.replaceWith(newExpr);
|
|
632
679
|
}
|
|
633
680
|
else if (t.isIdentifier(regex) || t.isIdentifier(modifier)) {
|
|
634
|
-
|
|
681
|
+
// @ts-ignore
|
|
682
|
+
const { line, column } = ((_d = (_c = path.node) === null || _c === void 0 ? void 0 : _c.position) === null || _d === void 0 ? void 0 : _d.start) || { line: 0, column: 0 };
|
|
683
|
+
const position = { col: column, row: line };
|
|
684
|
+
(0, utils_1.updateLogFileContent)(`ERROR [taroize] getWXS - getRegExp 函数暂不支持传入变量类型的参数 ${(0, utils_1.getLineBreak)()}`);
|
|
685
|
+
throw new utils_1.IReportError('getRegExp 函数暂不支持传入变量类型的参数', 'GetRegExpVariableTypeError', 'WXML_FILE', (0, utils_1.astToCode)(path.node) || '', position);
|
|
635
686
|
}
|
|
636
687
|
else {
|
|
637
|
-
|
|
688
|
+
// @ts-ignore
|
|
689
|
+
const { line, column } = ((_f = (_e = path.node) === null || _e === void 0 ? void 0 : _e.position) === null || _f === void 0 ? void 0 : _f.start) || { line: 0, column: 0 };
|
|
690
|
+
const position = { col: column, row: line };
|
|
691
|
+
(0, utils_1.updateLogFileContent)(`ERROR [taroize] getWXS - getRegExp 函数暂不支持传入非字符串类型的参数 ${(0, utils_1.getLineBreak)()}`);
|
|
692
|
+
throw new utils_1.IReportError('getRegExp 函数暂不支持传入非字符串类型的参数', 'GetRegExpParameterTypeError', 'WXML_FILE', (0, utils_1.astToCode)(path.node) || '', position);
|
|
638
693
|
}
|
|
639
694
|
}
|
|
640
695
|
else if (path.node.arguments.length === 1) {
|
|
641
696
|
const regex = path.node.arguments[0];
|
|
642
697
|
if (t.isStringLiteral(regex)) {
|
|
643
|
-
const regexStr = (
|
|
698
|
+
const regexStr = (_g = regex.extra) === null || _g === void 0 ? void 0 : _g.raw;
|
|
644
699
|
const regexWithoutQuotes = regexStr.replace(/^['"](.*)['"]$/, '$1');
|
|
645
700
|
const newExpr = t.newExpression(t.identifier('RegExp'), [t.stringLiteral(regexWithoutQuotes)]);
|
|
646
701
|
path.replaceWith(newExpr);
|
|
647
702
|
}
|
|
648
703
|
else if (t.isIdentifier(regex)) {
|
|
649
|
-
|
|
704
|
+
// @ts-ignore
|
|
705
|
+
const { line, column } = ((_j = (_h = path.node) === null || _h === void 0 ? void 0 : _h.position) === null || _j === void 0 ? void 0 : _j.start) || { line: 0, column: 0 };
|
|
706
|
+
const position = { col: column, row: line };
|
|
707
|
+
(0, utils_1.updateLogFileContent)(`ERROR [taroize] getWXS - getRegExp 函数暂不支持传入变量类型的参数 ${(0, utils_1.getLineBreak)()}`);
|
|
708
|
+
throw new utils_1.IReportError('getRegExp 函数暂不支持传入变量类型的参数', 'GetRegExpVariableTypeError', 'WXML_FILE', (0, utils_1.astToCode)(path.node) || '', position);
|
|
650
709
|
}
|
|
651
710
|
else {
|
|
652
|
-
|
|
711
|
+
// @ts-ignore
|
|
712
|
+
const { line, column } = ((_l = (_k = path.node) === null || _k === void 0 ? void 0 : _k.position) === null || _l === void 0 ? void 0 : _l.start) || { line: 0, column: 0 };
|
|
713
|
+
const position = { col: column, row: line };
|
|
714
|
+
(0, utils_1.updateLogFileContent)(`ERROR [taroize] getWXS - getRegExp 函数暂不支持传入非字符串类型的参数 ${(0, utils_1.getLineBreak)()}`);
|
|
715
|
+
throw new utils_1.IReportError('getRegExp 函数暂不支持传入非字符串类型的参数', 'GetRegExpParameterTypeError', 'WXML_FILE', (0, utils_1.astToCode)(path.node) || '', position);
|
|
653
716
|
}
|
|
654
717
|
}
|
|
655
718
|
else {
|
|
@@ -684,7 +747,11 @@ function getWXS(attrs, path, imports) {
|
|
|
684
747
|
});
|
|
685
748
|
}
|
|
686
749
|
if (!moduleName || !src) {
|
|
687
|
-
|
|
750
|
+
// @ts-ignore
|
|
751
|
+
const { line, column } = ((_f = (_e = path.node) === null || _e === void 0 ? void 0 : _e.position) === null || _f === void 0 ? void 0 : _f.start) || { line: 0, column: 0 };
|
|
752
|
+
const position = { col: column, row: line };
|
|
753
|
+
(0, utils_1.updateLogFileContent)(`ERROR [taroize] getWXS - wxs 未同时存在 wxs、src 两个属性 ${(0, utils_1.getLineBreak)()}`);
|
|
754
|
+
throw new utils_1.IReportError('一个 wxs 需要同时存在两个属性:`module`, `src`', 'WxsTagAttributesMissingError', 'WXML_FILE', (0, utils_1.astToCode)(path.node) || '', position);
|
|
688
755
|
}
|
|
689
756
|
path.remove();
|
|
690
757
|
return {
|
|
@@ -706,6 +773,7 @@ function hydrate(file) {
|
|
|
706
773
|
}
|
|
707
774
|
}
|
|
708
775
|
function transformLoop(name, attr, jsx, value) {
|
|
776
|
+
var _a, _b;
|
|
709
777
|
const jsxElement = jsx.get('openingElement');
|
|
710
778
|
if (!jsxElement.node) {
|
|
711
779
|
return;
|
|
@@ -715,7 +783,11 @@ function transformLoop(name, attr, jsx, value) {
|
|
|
715
783
|
const hasSinglewxForItem = wxForItem && t.isJSXAttribute(wxForItem) && wxForItem.value && t.isJSXExpressionContainer(wxForItem.value);
|
|
716
784
|
if (hasSinglewxForItem || name === exports.WX_FOR || name === 'wx:for-items') {
|
|
717
785
|
if (!value || !t.isJSXExpressionContainer(value)) {
|
|
718
|
-
|
|
786
|
+
// @ts-ignore
|
|
787
|
+
const { line, column } = ((_b = (_a = jsx.node) === null || _a === void 0 ? void 0 : _a.position) === null || _b === void 0 ? void 0 : _b.start) || { line: 0, column: 0 };
|
|
788
|
+
const position = { col: column, row: line };
|
|
789
|
+
(0, utils_1.updateLogFileContent)(`ERROR [taroize] transformLoop - wx:for 的值未用 "{{}}" 包裹 ${(0, utils_1.getLineBreak)()}`);
|
|
790
|
+
throw new utils_1.IReportError('wx:for 的值必须使用 "{{}}" 包裹', 'WxForValueFormatError', 'WXML_FILE', (0, utils_1.astToCode)(jsx.node) || '', position);
|
|
719
791
|
}
|
|
720
792
|
attr.remove();
|
|
721
793
|
let item = t.stringLiteral('item');
|
|
@@ -724,17 +796,26 @@ function transformLoop(name, attr, jsx, value) {
|
|
|
724
796
|
.get('openingElement')
|
|
725
797
|
.get('attributes')
|
|
726
798
|
.forEach((p) => {
|
|
799
|
+
var _a, _b, _c, _d;
|
|
727
800
|
const node = p.node;
|
|
728
801
|
if (node.name.name === exports.WX_FOR_ITEM) {
|
|
729
802
|
if (!node.value || !t.isStringLiteral(node.value)) {
|
|
730
|
-
|
|
803
|
+
// @ts-ignore
|
|
804
|
+
const { line, column } = ((_b = (_a = jsx.node) === null || _a === void 0 ? void 0 : _a.position) === null || _b === void 0 ? void 0 : _b.start) || { line: 0, column: 0 };
|
|
805
|
+
const position = { col: column, row: line };
|
|
806
|
+
(0, utils_1.updateLogFileContent)(`ERROR [taroize] transformLoop - ${exports.WX_FOR_ITEM} 的值不是一个字符串 ${(0, utils_1.getLineBreak)()}`);
|
|
807
|
+
throw new utils_1.IReportError(exports.WX_FOR_ITEM + ' 的值必须是一个字符串', 'WxForItemValueError', 'WXML_FILE', (0, utils_1.astToCode)(jsx.node) || '', position);
|
|
731
808
|
}
|
|
732
809
|
item = node.value;
|
|
733
810
|
p.remove();
|
|
734
811
|
}
|
|
735
812
|
if (node.name.name === exports.WX_FOR_INDEX) {
|
|
736
813
|
if (!node.value || !t.isStringLiteral(node.value)) {
|
|
737
|
-
|
|
814
|
+
// @ts-ignore
|
|
815
|
+
const { line, column } = ((_d = (_c = jsx.node) === null || _c === void 0 ? void 0 : _c.position) === null || _d === void 0 ? void 0 : _d.start) || { line: 0, column: 0 };
|
|
816
|
+
const position = { col: column, row: line };
|
|
817
|
+
(0, utils_1.updateLogFileContent)(`ERROR [taroize] transformLoop - ${exports.WX_FOR_INDEX} 的值不是一个字符串 ${(0, utils_1.getLineBreak)()}`);
|
|
818
|
+
throw new utils_1.IReportError(exports.WX_FOR_INDEX + ' 的值必须是一个字符串', 'WxForIndexValueError', 'WXML_FILE', (0, utils_1.astToCode)(jsx.node) || '', position);
|
|
738
819
|
}
|
|
739
820
|
index = node.value;
|
|
740
821
|
p.remove();
|
|
@@ -774,6 +855,7 @@ function transformLoop(name, attr, jsx, value) {
|
|
|
774
855
|
jsx.replaceWith(ifBlock);
|
|
775
856
|
}
|
|
776
857
|
catch (error) {
|
|
858
|
+
(0, utils_1.updateLogFileContent)(`WARN [taroize] transformLoop - wx:if和wx:for合用时父组件使用wx:if导致使用replaceWith异常 ${(0, utils_1.getLineBreak)()}${error} ${(0, utils_1.getLineBreak)()}`);
|
|
777
859
|
// jsx外层是wx:if的转换,替换(replaceWith)时会抛出异常
|
|
778
860
|
// catch异常后,正常替换
|
|
779
861
|
}
|
|
@@ -793,6 +875,7 @@ function transformLoop(name, attr, jsx, value) {
|
|
|
793
875
|
jsx.replaceWith(block);
|
|
794
876
|
}
|
|
795
877
|
catch (error) {
|
|
878
|
+
(0, utils_1.updateLogFileContent)(`WARN [taroize] transformLoop - 节点替换异常 ${(0, utils_1.getLineBreak)()}${error} ${(0, utils_1.getLineBreak)()}`);
|
|
796
879
|
//
|
|
797
880
|
}
|
|
798
881
|
return {
|
|
@@ -802,6 +885,7 @@ function transformLoop(name, attr, jsx, value) {
|
|
|
802
885
|
}
|
|
803
886
|
}
|
|
804
887
|
function transformIf(name, attr, jsx, value) {
|
|
888
|
+
var _a, _b;
|
|
805
889
|
if (name !== exports.WX_IF) {
|
|
806
890
|
return;
|
|
807
891
|
}
|
|
@@ -820,10 +904,18 @@ function transformIf(name, attr, jsx, value) {
|
|
|
820
904
|
.filter((s) => !(s.isJSXExpressionContainer() && t.isJSXEmptyExpression(s.get('expression'))));
|
|
821
905
|
}
|
|
822
906
|
catch (error) {
|
|
907
|
+
(0, utils_1.updateLogFileContent)(`WARN [taroize] transformIf - 节点过滤异常 ${(0, utils_1.getLineBreak)()}${error} ${(0, utils_1.getLineBreak)()}`);
|
|
823
908
|
return;
|
|
824
909
|
}
|
|
825
910
|
if (value === null || !t.isJSXExpressionContainer(value)) {
|
|
911
|
+
const cacheNode = (0, lodash_1.cloneDeep)(attr.parentPath.parent);
|
|
912
|
+
const position = {
|
|
913
|
+
col: ((_a = cacheNode.position) === null || _a === void 0 ? void 0 : _a.start.column) || 0,
|
|
914
|
+
row: ((_b = cacheNode.position) === null || _b === void 0 ? void 0 : _b.start.line) || 0,
|
|
915
|
+
};
|
|
916
|
+
(0, utils_1.createErrorCodeMsg)('wxIfValueFormatError', 'wx:if 的值需要用双括号 `{{}}` 包裹它的值', (0, utils_1.astToCode)(cacheNode) || '', global_1.globals.currentParseFile, position);
|
|
826
917
|
console.error('wx:if 的值需要用双括号 `{{}}` 包裹它的值');
|
|
918
|
+
(0, utils_1.updateLogFileContent)(`WARN [taroize] transformIf - wx:if 的值需要用双括号 {{}} 包裹它的值 ${(0, utils_1.getLineBreak)()}`);
|
|
827
919
|
if (value && t.isStringLiteral(value)) {
|
|
828
920
|
value = t.jSXExpressionContainer((0, utils_1.buildTemplate)(value.value));
|
|
829
921
|
}
|
|
@@ -832,10 +924,12 @@ function transformIf(name, attr, jsx, value) {
|
|
|
832
924
|
condition: exports.WX_IF,
|
|
833
925
|
path: jsx,
|
|
834
926
|
tester: value,
|
|
927
|
+
cachePath: (0, lodash_1.cloneDeep)(jsx)
|
|
835
928
|
});
|
|
836
929
|
attr.remove();
|
|
837
930
|
for (let index = 0; index < siblings.length; index++) {
|
|
838
931
|
const sibling = siblings[index];
|
|
932
|
+
const cacheSibling = (0, lodash_1.cloneDeep)(sibling);
|
|
839
933
|
const next = (0, lodash_1.cloneDeep)(siblings[index + 1]);
|
|
840
934
|
const currMatches = findWXIfProps(sibling);
|
|
841
935
|
const nextMatches = findWXIfProps(next);
|
|
@@ -846,6 +940,7 @@ function transformIf(name, attr, jsx, value) {
|
|
|
846
940
|
condition: currMatches.reg.input,
|
|
847
941
|
path: sibling,
|
|
848
942
|
tester: currMatches.tester,
|
|
943
|
+
cachePath: cacheSibling
|
|
849
944
|
});
|
|
850
945
|
if (nextMatches === null) {
|
|
851
946
|
break;
|
|
@@ -854,6 +949,7 @@ function transformIf(name, attr, jsx, value) {
|
|
|
854
949
|
handleConditions(conditions);
|
|
855
950
|
}
|
|
856
951
|
function handleConditions(conditions) {
|
|
952
|
+
var _a, _b;
|
|
857
953
|
if (conditions.length === 1) {
|
|
858
954
|
const ct = conditions[0];
|
|
859
955
|
if (!t.isJSXEmptyExpression(ct.tester.expression)) {
|
|
@@ -861,6 +957,7 @@ function handleConditions(conditions) {
|
|
|
861
957
|
ct.path.replaceWith(t.jSXExpressionContainer(t.logicalExpression('&&', ct.tester.expression, (0, lodash_1.cloneDeep)(ct.path.node))));
|
|
862
958
|
}
|
|
863
959
|
catch (error) {
|
|
960
|
+
(0, utils_1.updateLogFileContent)(`WARN [taroize] handleConditions - 替换节点异常 ${(0, utils_1.getLineBreak)()}${error} ${(0, utils_1.getLineBreak)()}`);
|
|
864
961
|
//
|
|
865
962
|
}
|
|
866
963
|
}
|
|
@@ -868,14 +965,18 @@ function handleConditions(conditions) {
|
|
|
868
965
|
if (conditions.length > 1) {
|
|
869
966
|
const lastLength = conditions.length - 1;
|
|
870
967
|
const lastCon = conditions[lastLength];
|
|
968
|
+
// 记录当前操作的 condition
|
|
969
|
+
let currentCondition = null;
|
|
871
970
|
let lastAlternate = (0, lodash_1.cloneDeep)(lastCon.path.node);
|
|
872
971
|
try {
|
|
873
972
|
if (lastCon.condition === exports.WX_ELSE_IF && !t.isJSXEmptyExpression(lastCon.tester.expression)) {
|
|
874
973
|
lastAlternate = t.logicalExpression('&&', lastCon.tester.expression, lastAlternate);
|
|
875
974
|
}
|
|
876
975
|
const node = conditions.slice(0, lastLength).reduceRight((acc, condition) => {
|
|
976
|
+
currentCondition = condition;
|
|
877
977
|
if (t.isJSXEmptyExpression(condition.tester.expression)) {
|
|
878
978
|
(0, helper_1.printLog)("warning" /* processTypeEnum.WARNING */, 'condition.tester.expression', 't.isJSXEmptyExpression(condition.tester.expression)');
|
|
979
|
+
(0, utils_1.updateLogFileContent)(`WARN [taroize] handleConditions - t.isJSXEmptyExpression(condition.tester.expression) ${(0, utils_1.getLineBreak)()}`);
|
|
879
980
|
return null;
|
|
880
981
|
}
|
|
881
982
|
return t.conditionalExpression(condition.tester.expression, (0, lodash_1.cloneDeep)(condition.path.node), acc);
|
|
@@ -886,8 +987,10 @@ function handleConditions(conditions) {
|
|
|
886
987
|
}
|
|
887
988
|
}
|
|
888
989
|
catch (error) {
|
|
889
|
-
|
|
890
|
-
|
|
990
|
+
(0, utils_1.updateLogFileContent)(`WARN [taroize] handleConditions - wx:elif 转换异常 ${(0, utils_1.getLineBreak)()}${error} ${(0, utils_1.getLineBreak)()}`);
|
|
991
|
+
const { line, column } = ((_b = (_a = currentCondition.cachePath.node) === null || _a === void 0 ? void 0 : _a.position) === null || _b === void 0 ? void 0 : _b.start) || { line: 0, column: 0 };
|
|
992
|
+
const position = { col: column, row: line };
|
|
993
|
+
throw new utils_1.IReportError('属性转换错误 wx:elif 的值需要用双括号 `{{}}` 包裹它的值', 'wxElifValueFormatError', 'WXML_FILE', (0, utils_1.astToCode)(currentCondition.cachePath.node) || '', position);
|
|
891
994
|
}
|
|
892
995
|
}
|
|
893
996
|
}
|
|
@@ -920,7 +1023,7 @@ function findWXIfProps(jsx) {
|
|
|
920
1023
|
return matches;
|
|
921
1024
|
}
|
|
922
1025
|
function parseNode(node, tagName) {
|
|
923
|
-
(0, utils_1.
|
|
1026
|
+
(0, utils_1.updateLogFileContent)(`INFO [taroize] parseNode - 入参 ${(0, utils_1.getLineBreak)()}tagName: ${tagName} ${(0, utils_1.getLineBreak)()}`);
|
|
924
1027
|
if (node.type === NodeType.Text) {
|
|
925
1028
|
return parseText(node, tagName);
|
|
926
1029
|
}
|
|
@@ -932,12 +1035,13 @@ function parseNode(node, tagName) {
|
|
|
932
1035
|
value: ' ' + node.content + ' ',
|
|
933
1036
|
},
|
|
934
1037
|
];
|
|
935
|
-
|
|
1038
|
+
const jsxExpressionContainer = t.jSXExpressionContainer(emptyStatement);
|
|
1039
|
+
return (0, utils_1.addLocInfo)(jsxExpressionContainer, node);
|
|
936
1040
|
}
|
|
937
1041
|
return parseElement(node);
|
|
938
1042
|
}
|
|
939
1043
|
function parseElement(element) {
|
|
940
|
-
(0, utils_1.
|
|
1044
|
+
(0, utils_1.updateLogFileContent)(`INFO [taroize] parseElement - 进入函数 ${(0, utils_1.getLineBreak)()}`);
|
|
941
1045
|
const tagName = t.jSXIdentifier(global_1.THIRD_PARTY_COMPONENTS.has(element.tagName) ? element.tagName : allCamelCase(element.tagName));
|
|
942
1046
|
if (utils_1.DEFAULT_Component_SET.has(tagName.name)) {
|
|
943
1047
|
global_1.usedComponents.add(tagName.name);
|
|
@@ -984,7 +1088,14 @@ function parseElement(element) {
|
|
|
984
1088
|
});
|
|
985
1089
|
}
|
|
986
1090
|
}
|
|
987
|
-
return t.jSXElement(
|
|
1091
|
+
// return t.jSXElement(
|
|
1092
|
+
// t.jSXOpeningElement(tagName, attributes.map(parseAttribute)),
|
|
1093
|
+
// t.jSXClosingElement(tagName),
|
|
1094
|
+
// removeEmptyTextAndComment(element.children).map((el) => parseNode(el, element.tagName)),
|
|
1095
|
+
// false
|
|
1096
|
+
// )
|
|
1097
|
+
const jSXElement = t.jSXElement(t.jSXOpeningElement(tagName, attributes.map(parseAttribute)), t.jSXClosingElement(tagName), removeEmptyTextAndComment(element.children).map((el) => parseNode(el, element.tagName)), false);
|
|
1098
|
+
return (0, utils_1.addLocInfo)(jSXElement, element);
|
|
988
1099
|
}
|
|
989
1100
|
function removeEmptyTextAndComment(nodes) {
|
|
990
1101
|
return nodes
|
|
@@ -997,16 +1108,19 @@ function removeEmptyTextAndComment(nodes) {
|
|
|
997
1108
|
}
|
|
998
1109
|
exports.removeEmptyTextAndComment = removeEmptyTextAndComment;
|
|
999
1110
|
function parseText(node, tagName) {
|
|
1000
|
-
(0, utils_1.
|
|
1111
|
+
(0, utils_1.updateLogFileContent)(`INFO [taroize] parseText - 进入函数 ${(0, utils_1.getLineBreak)()}`);
|
|
1001
1112
|
if (tagName === 'wxs') {
|
|
1002
|
-
return t.jSXText(node.content)
|
|
1113
|
+
// return t.jSXText(node.content)
|
|
1114
|
+
return (0, utils_1.addLocInfo)(t.jSXText(node.content), node);
|
|
1003
1115
|
}
|
|
1004
1116
|
const { type, content } = parseContent(node.content);
|
|
1005
1117
|
if (type === 'raw') {
|
|
1006
1118
|
const text = content.replace(/([{}]+)/g, "{'$1'}");
|
|
1007
|
-
return t.jSXText(text)
|
|
1119
|
+
// return t.jSXText(text)
|
|
1120
|
+
return (0, utils_1.addLocInfo)(t.jSXText(text), node);
|
|
1008
1121
|
}
|
|
1009
|
-
return t.jSXExpressionContainer(
|
|
1122
|
+
// return t.jSXExpressionContainer(buildTemplate(content))
|
|
1123
|
+
return (0, utils_1.addLocInfo)(t.jSXExpressionContainer((0, utils_1.buildTemplate)(content)), node);
|
|
1010
1124
|
}
|
|
1011
1125
|
// 匹配{{content}}
|
|
1012
1126
|
const handlebarsRE = /\{\{((?:.|\n)+?)\}\}/g;
|
|
@@ -1014,7 +1128,7 @@ function singleQuote(s) {
|
|
|
1014
1128
|
return `'${s}'`;
|
|
1015
1129
|
}
|
|
1016
1130
|
function parseContent(content, single = false) {
|
|
1017
|
-
(0, utils_1.
|
|
1131
|
+
(0, utils_1.updateLogFileContent)(`INFO [taroize] parseContent - 进入函数 ${(0, utils_1.getLineBreak)()}`);
|
|
1018
1132
|
content = content.trim();
|
|
1019
1133
|
if (!handlebarsRE.test(content)) {
|
|
1020
1134
|
return {
|
|
@@ -1069,7 +1183,7 @@ function isAllKeyValueFormat(styleAttrsMap) {
|
|
|
1069
1183
|
* @returns
|
|
1070
1184
|
*/
|
|
1071
1185
|
function parseStyle(key, value) {
|
|
1072
|
-
(0, utils_1.
|
|
1186
|
+
(0, utils_1.updateLogFileContent)(`INFO [taroize] parseStyle - 入参 ${(0, utils_1.getLineBreak)()}key: ${key} ${(0, utils_1.getLineBreak)()}value: ${value} ${(0, utils_1.getLineBreak)()}`);
|
|
1073
1187
|
const styleAttrs = value.trim().split(';');
|
|
1074
1188
|
// 针对attrName: attrValue 格式做转换处理, 其他类型采用'+'连接符
|
|
1075
1189
|
if (isAllKeyValueFormat(styleAttrs)) {
|
|
@@ -1085,14 +1199,17 @@ function parseStyle(key, value) {
|
|
|
1085
1199
|
}
|
|
1086
1200
|
exports.parseStyle = parseStyle;
|
|
1087
1201
|
function parseAttribute(attr) {
|
|
1088
|
-
(0, utils_1.
|
|
1202
|
+
(0, utils_1.updateLogFileContent)(`INFO [taroize] parseAttribute - 入参 ${(0, utils_1.getLineBreak)()}attr: ${JSON.stringify(attr)} ${(0, utils_1.getLineBreak)()}`);
|
|
1089
1203
|
let { key, value } = attr;
|
|
1090
1204
|
let jsxValue = null;
|
|
1091
1205
|
let type = '';
|
|
1092
1206
|
let content = '';
|
|
1093
1207
|
if (value) {
|
|
1208
|
+
const cacheValue = value;
|
|
1094
1209
|
if (key === 'class' && value.startsWith('[') && value.endsWith(']')) {
|
|
1095
1210
|
value = value.slice(1, value.length - 1).replace(',', '');
|
|
1211
|
+
(0, utils_1.createErrorCodeMsg)('unsupportedClassArray', 'Taro/React 不支持 class 传入数组,此写法可能无法得到正确的 class', `class=${JSON.stringify(cacheValue).replace(/"/g, "'")}`, global_1.globals.currentParseFile);
|
|
1212
|
+
(0, utils_1.updateLogFileContent)(`WARN [taroize] parseAttribute - Taro/React 不支持 class 传入数组,此写法可能无法得到正确的 class ${(0, utils_1.getLineBreak)()}`);
|
|
1096
1213
|
// eslint-disable-next-line no-console
|
|
1097
1214
|
console.log((0, utils_1.codeFrameError)(attr, 'Taro/React 不支持 class 传入数组,此写法可能无法得到正确的 class'));
|
|
1098
1215
|
}
|
|
@@ -1110,10 +1227,8 @@ function parseAttribute(attr) {
|
|
|
1110
1227
|
}
|
|
1111
1228
|
}
|
|
1112
1229
|
catch (error) {
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
(0, utils_1.printToLogFile)(`package: taroize, style="${value}" 解析异常 ${(0, utils_1.getLineBreak)()}`);
|
|
1116
|
-
throw new Error(errorMsg);
|
|
1230
|
+
(0, utils_1.updateLogFileContent)(`ERROR [taroize] parseAttribute - 属性 style="${value}" 解析异常 ${(0, utils_1.getLineBreak)()}${error} ${(0, utils_1.getLineBreak)()}`);
|
|
1231
|
+
throw new utils_1.IReportError(`属性解析失败 style="${value}"解析失败,${error}`, 'StyleAttributeParsingError', 'WXML_FILE', `style="${value}"`);
|
|
1117
1232
|
}
|
|
1118
1233
|
}
|
|
1119
1234
|
else {
|
|
@@ -1137,7 +1252,8 @@ function parseAttribute(attr) {
|
|
|
1137
1252
|
expr = t.stringLiteral('');
|
|
1138
1253
|
}
|
|
1139
1254
|
else {
|
|
1140
|
-
|
|
1255
|
+
(0, utils_1.updateLogFileContent)(`ERROR [taroize] parseAttribute - 模板参数转换异常 ${(0, utils_1.getLineBreak)()}${err} ${(0, utils_1.getLineBreak)()}`);
|
|
1256
|
+
throw new utils_1.IReportError(err, 'TemplateParameterConversionError', 'WXML_FILE', `${key}: ${value}`);
|
|
1141
1257
|
}
|
|
1142
1258
|
}
|
|
1143
1259
|
else if (content.includes(':') || content.includes('...')) {
|
|
@@ -1148,10 +1264,13 @@ function parseAttribute(attr) {
|
|
|
1148
1264
|
}
|
|
1149
1265
|
else {
|
|
1150
1266
|
const err = `转换模板参数: \`${key}: ${value}\` 报错`;
|
|
1151
|
-
|
|
1267
|
+
(0, utils_1.updateLogFileContent)(`ERROR [taroize] parseAttribute - 模板参数转换异常 ${(0, utils_1.getLineBreak)()}${err} ${(0, utils_1.getLineBreak)()}`);
|
|
1268
|
+
throw new utils_1.IReportError(err, 'TemplateParameterConversionError', 'WXML_FILE', `${key}: ${value}`);
|
|
1152
1269
|
}
|
|
1153
1270
|
}
|
|
1154
1271
|
if (t.isThisExpression(expr)) {
|
|
1272
|
+
(0, utils_1.createErrorCodeMsg)('ThisKeywordUsageWarning', '在参数中使用 `this` 可能会造成意想不到的结果,已将此参数修改为 `__placeholder__`,你可以在转换后的代码查找这个关键字修改。', value, global_1.globals.currentParseFile);
|
|
1273
|
+
(0, utils_1.updateLogFileContent)(`WARN [taroize] parseAttribute - 在参数中使用 this 可能会造成意想不到的结果 ${(0, utils_1.getLineBreak)()}`);
|
|
1155
1274
|
console.error('在参数中使用 `this` 可能会造成意想不到的结果,已将此参数修改为 `__placeholder__`,你可以在转换后的代码查找这个关键字修改。');
|
|
1156
1275
|
expr = t.stringLiteral('__placeholder__');
|
|
1157
1276
|
}
|
|
@@ -1195,7 +1314,8 @@ function handleAttrKey(key) {
|
|
|
1195
1314
|
key = key.replace(/^(bind:|catch:|bind|catch)/, 'on');
|
|
1196
1315
|
key = (0, lodash_1.camelCase)(key);
|
|
1197
1316
|
if (!(0, utils_1.isValidVarName)(key)) {
|
|
1198
|
-
|
|
1317
|
+
(0, utils_1.updateLogFileContent)(`ERROR [taroize] handleAttrKey - ${key} 不是一个有效 JavaScript 变量名 ${(0, utils_1.getLineBreak)()}`);
|
|
1318
|
+
throw new utils_1.IReportError(`属性名"${key}" 不是一个有效 JavaScript 变量名`, 'InvalidVariableNameError', 'WXML_FILE', `${key}`);
|
|
1199
1319
|
}
|
|
1200
1320
|
return key.substr(0, 2) + key[2].toUpperCase() + key.substr(3);
|
|
1201
1321
|
}
|