@aiot-toolkit/parser 2.0.6-beta.2 → 2.0.6-beta.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.
@@ -20,8 +20,8 @@ var _reservedWords = _interopRequireDefault(require("reserved-words"));
20
20
  function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
21
21
  function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
22
22
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
23
- /**
24
- * TemplateUtil
23
+ /**
24
+ * TemplateUtil
25
25
  */
26
26
  class TemplateUtil {
27
27
  static DYNAMIC_REG = '{{([\\s\\S]*?)}}';
@@ -34,26 +34,26 @@ class TemplateUtil {
34
34
  this.elementConfigUtil = elementConfigUtil;
35
35
  }
36
36
 
37
- /**
38
- * 属性列表转换为字典格式
39
- *
40
- * @param obj
41
- * @returns
37
+ /**
38
+ * 属性列表转换为字典格式
39
+ *
40
+ * @param obj
41
+ * @returns
42
42
  */
43
43
  async attributesToObject(attributes, identifiers, params) {
44
44
  if (!attributes || !Array.isArray(attributes)) {
45
45
  return {};
46
46
  }
47
47
  const dataSetStart = TemplateUtil.DATA_SET_START;
48
- /**
49
- * 重命名 dataset 属性
50
- *
51
- * 1. 去掉data-
52
- * 2. -字母 转换为大写字母
53
- *
54
- * > data-aa-bbb 格式 转换成 aaBbb
55
- * @param value
56
- * @returns
48
+ /**
49
+ * 重命名 dataset 属性
50
+ *
51
+ * 1. 去掉data-
52
+ * 2. -字母 转换为大写字母
53
+ *
54
+ * > data-aa-bbb 格式 转换成 aaBbb
55
+ * @param value
56
+ * @returns
57
57
  */
58
58
  const renameDatasetAttribute = value => {
59
59
  return value.substring(dataSetStart.length).replace(/-([a-z])/g, function (s, m) {
@@ -100,12 +100,12 @@ class TemplateUtil {
100
100
  return result;
101
101
  }
102
102
 
103
- /**
104
- * 转换属性名称
105
- * 1. 配置中有别名,使用别名
106
- * 2. 如果是@, on开头,则取@ on后面的文本
107
- * 3. 原名称
108
- * @param name
103
+ /**
104
+ * 转换属性名称
105
+ * 1. 配置中有别名,使用别名
106
+ * 2. 如果是@, on开头,则取@ on后面的文本
107
+ * 3. 原名称
108
+ * @param name
109
109
  */
110
110
  translateAttributeName(name) {
111
111
  const config = _AttributeConfig.default[name];
@@ -118,16 +118,16 @@ class TemplateUtil {
118
118
  return name;
119
119
  }
120
120
 
121
- /**
122
- * 转换属性值
123
- * 1. 无内容=true
124
- * 2. 非字符串,直接返回结果
125
- * 3. 属性值转换为 js 代码
126
- * a. 如果有单独的配置,则使用配置转换
127
- * b. 否则,如果是动态值,则分为事件和其它动态值
128
- * c. 否则,返回源内容
129
- * @param attribute
130
- * @returns
121
+ /**
122
+ * 转换属性值
123
+ * 1. 无内容=true
124
+ * 2. 非字符串,直接返回结果
125
+ * 3. 属性值转换为 js 代码
126
+ * a. 如果有单独的配置,则使用配置转换
127
+ * b. 否则,如果是动态值,则分为事件和其它动态值
128
+ * c. 否则,返回源内容
129
+ * @param attribute
130
+ * @returns
131
131
  */
132
132
  async translateAttributeValue(attribute, identifiers) {
133
133
  let disabledWrap = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
@@ -175,21 +175,21 @@ class TemplateUtil {
175
175
  return result;
176
176
  }
177
177
 
178
- /**
179
- * 解析模板值
180
- *
181
- * 1. 使用TemplateValueParser.parse 解析出结果列表
182
- * 2. 循环结果列表
183
- * a. 代码:部分标识符加上前缀
184
- * b. 文字:加上引号
185
- * 3. 使用+连接各项结果
186
- *
187
- * @param source 模板值的原始内容: a{{b}}c
188
- * @param identifiers 已存在的标识符. 存在于此参数中的标识符,不加前缀
189
- * @param options
190
- * @param ownerNode source 模板值所属的属性节点: for="forvalue", 表示for 这个节点
191
- *
192
- * @returns
178
+ /**
179
+ * 解析模板值
180
+ *
181
+ * 1. 使用TemplateValueParser.parse 解析出结果列表
182
+ * 2. 循环结果列表
183
+ * a. 代码:部分标识符加上前缀
184
+ * b. 文字:加上引号
185
+ * 3. 使用+连接各项结果
186
+ *
187
+ * @param source 模板值的原始内容: a{{b}}c
188
+ * @param identifiers 已存在的标识符. 存在于此参数中的标识符,不加前缀
189
+ * @param options
190
+ * @param ownerNode source 模板值所属的属性节点: for="forvalue", 表示for 这个节点
191
+ *
192
+ * @returns
193
193
  */
194
194
  translateTemplateValue(source, identifiers, ownerNode) {
195
195
  const {
@@ -198,15 +198,15 @@ class TemplateUtil {
198
198
  } = this.option;
199
199
  const nameLocation = ownerNode?.nameLocation;
200
200
 
201
- /**
202
- * 遍历标识符,如果满足以下条件,不加前缀
203
- * a. 节点名称在参数的标签符中
204
- * b. 全局定义: console
205
- * c. 在局部作用域定义: `const a = 0; log(a)` 中的 a
206
- * d. 是调用链且是静态值: `data.x` 的 x. *注意*:data[x] 中的 x 是动态值, data[x] --> this.data[this.x]
207
- * e. 是对象的属性: `{x:**}` 的 x
208
- * @param code
209
- * @returns
201
+ /**
202
+ * 遍历标识符,如果满足以下条件,不加前缀
203
+ * a. 节点名称在参数的标签符中
204
+ * b. 全局定义: console
205
+ * c. 在局部作用域定义: `const a = 0; log(a)` 中的 a
206
+ * d. 是调用链且是静态值: `data.x` 的 x. *注意*:data[x] 中的 x 是动态值, data[x] --> this.data[this.x]
207
+ * e. 是对象的属性: `{x:**}` 的 x
208
+ * @param code
209
+ * @returns
210
210
  */
211
211
  const translateJsCode = code => {
212
212
  const {
@@ -327,17 +327,17 @@ class TemplateUtil {
327
327
  })}, ${elementExpression})`;
328
328
  }
329
329
 
330
- /**
331
- * 创建元素调用表达式
332
- * ```
333
- * aiot.__ce__(
334
- * "div",
335
- * {name:'div1'},
336
- * children
337
- * )
338
- * ```
339
- * @param struct
340
- * @returns
330
+ /**
331
+ * 创建元素调用表达式
332
+ * ```
333
+ * aiot.__ce__(
334
+ * "div",
335
+ * {name:'div1'},
336
+ * children
337
+ * )
338
+ * ```
339
+ * @param struct
340
+ * @returns
341
341
  */
342
342
  async createElementCallExpression(struct) {
343
343
  const {
@@ -367,6 +367,7 @@ class TemplateUtil {
367
367
  }
368
368
  const elementConfig = getElementConfig(tag.name);
369
369
  const effectTag = elementConfig?.aliasName || tag.name;
370
+ const aliasAttributes = elementConfig?.aliasAttributes;
370
371
  const {
371
372
  globalInstance,
372
373
  dataInstance,
@@ -374,21 +375,21 @@ class TemplateUtil {
374
375
  } = this.context;
375
376
  const result = `${globalInstance}.${callerName}("${effectTag}", ${_sharedUtils.StringUtil.objectToString({
376
377
  [dataInstance]: dataInstanceValue,
377
- __opts__: await this.attributesToObject(attributes, identifiers, {
378
+ __opts__: await this.attributesToObject(aliasAttributes ? [...aliasAttributes, ...attributes] : attributes, identifiers, {
378
379
  nameToCamel: true
379
380
  })
380
381
  })}, ${children})`;
381
382
  return result;
382
383
  }
383
384
 
384
- /**
385
- * 获取节点指定名称的属性
386
- *
387
- * 此方法会排除修饰符,例如<div for.static="123" />;使用getAttribute(node, "for") 仍可取到值
388
- *
389
- * @param node
390
- * @param name
391
- * @returns
385
+ /**
386
+ * 获取节点指定名称的属性
387
+ *
388
+ * 此方法会排除修饰符,例如<div for.static="123" />;使用getAttribute(node, "for") 仍可取到值
389
+ *
390
+ * @param node
391
+ * @param name
392
+ * @returns
392
393
  */
393
394
  static getAttribute(node, name) {
394
395
  const {
@@ -403,14 +404,14 @@ class TemplateUtil {
403
404
  }
404
405
  }
405
406
 
406
- /**
407
- * 解析属性名为名称、修饰符
408
- *
409
- * 以 attr.x.y.z="123"为例
410
- * + 属性名使用.分隔为数组 [attr, x, y, z]
411
- * + 第0项为属性名,1-最后一项为修饰符名
412
- * @param name
413
- * @returns
407
+ /**
408
+ * 解析属性名为名称、修饰符
409
+ *
410
+ * 以 attr.x.y.z="123"为例
411
+ * + 属性名使用.分隔为数组 [attr, x, y, z]
412
+ * + 第0项为属性名,1-最后一项为修饰符名
413
+ * @param name
414
+ * @returns
414
415
  */
415
416
  static parseAttributeName(name) {
416
417
  if (!name) {
@@ -425,14 +426,14 @@ class TemplateUtil {
425
426
  };
426
427
  }
427
428
 
428
- /**
429
- * 判断是否强制为动态值
430
- * 1. 事件属性
431
- * 2. if for
432
- * 3. 配置值
433
- * @param nodeName 节点名称
434
- * @param attributeName 属性名称
435
- * @returns
429
+ /**
430
+ * 判断是否强制为动态值
431
+ * 1. 事件属性
432
+ * 2. if for
433
+ * 3. 配置值
434
+ * @param nodeName 节点名称
435
+ * @param attributeName 属性名称
436
+ * @returns
436
437
  */
437
438
  isForceDynamicAttribute(nodeName, attributeName) {
438
439
  const {
@@ -468,12 +469,12 @@ class TemplateUtil {
468
469
  return node.nodeName === _TemplateNodeType.default.TEXT;
469
470
  }
470
471
 
471
- /**
472
- * 是否为节点元素
473
- *
474
- * @example <span></span>
475
- * @param node
476
- * @returns
472
+ /**
473
+ * 是否为节点元素
474
+ *
475
+ * @example <span></span>
476
+ * @param node
477
+ * @returns
477
478
  */
478
479
  static isElement(node) {
479
480
  return ![_TemplateNodeType.default.COMMENT, _TemplateNodeType.default.DOCUMENT_TYPE, _TemplateNodeType.default.TEMPLATE, _TemplateNodeType.default.TEXT].includes(node.nodeName);
@@ -488,20 +489,20 @@ class TemplateUtil {
488
489
  return name.startsWith(TemplateUtil.DATA_SET_START);
489
490
  }
490
491
 
491
- /**
492
- * 是否是 model 指令的属性
493
- * @param name
494
- * @returns
492
+ /**
493
+ * 是否是 model 指令的属性
494
+ * @param name
495
+ * @returns
495
496
  */
496
497
  static isModelAttribute(name) {
497
498
  return name.startsWith('model:');
498
499
  }
499
500
 
500
- /**
501
- * 解析model 指令属性的名称为: {modelName: '', modelValue: ''}
502
- *
503
- * 例如 `model:abcde="xyz"` 解析为 `{modelName: 'abcde', modelValue:'xyz'}`
504
- * @param name
501
+ /**
502
+ * 解析model 指令属性的名称为: {modelName: '', modelValue: ''}
503
+ *
504
+ * 例如 `model:abcde="xyz"` 解析为 `{modelName: 'abcde', modelValue:'xyz'}`
505
+ * @param name
505
506
  */
506
507
  parseModelAttribute(node) {
507
508
  const {
@@ -519,12 +520,12 @@ class TemplateUtil {
519
520
  return;
520
521
  }
521
522
 
522
- /**
523
- * 值转换为动态值
524
- *
525
- * 如果已经是动态值,则不做修改
526
- * @param value
527
- * @returns
523
+ /**
524
+ * 值转换为动态值
525
+ *
526
+ * 如果已经是动态值,则不做修改
527
+ * @param value
528
+ * @returns
528
529
  */
529
530
  static toDynamicValue(value) {
530
531
  if (this.isDynamicValue(value)) {
@@ -533,12 +534,12 @@ class TemplateUtil {
533
534
  return `{{${value}}}`;
534
535
  }
535
536
 
536
- /**
537
- * 值转换为静态值
538
- *
539
- * 已经是静态值,不做修改
540
- * @param value
541
- * @returns
537
+ /**
538
+ * 值转换为静态值
539
+ *
540
+ * 已经是静态值,不做修改
541
+ * @param value
542
+ * @returns
542
543
  */
543
544
  static toStaticValue(value) {
544
545
  if (this.isDynamicValue(value)) {
@@ -547,22 +548,22 @@ class TemplateUtil {
547
548
  return value;
548
549
  }
549
550
 
550
- /**
551
- * 创建修饰符数据
552
- *
553
- *
554
- * @example createDecoratorData(node, 'if', 'shown')
555
- *
556
- * 如果 node 有 if 属性, 例如<div if.static1.static2="aaa" />,则创建属性名为shown的修改符对象
557
- * ```
558
- * {
559
- * shown:{static1: true,static2: true}
560
- * }
561
- * ```
562
- * @param node
563
- * @param attributeName
564
- * @param decoratorName
565
- * @returns
551
+ /**
552
+ * 创建修饰符数据
553
+ *
554
+ *
555
+ * @example createDecoratorData(node, 'if', 'shown')
556
+ *
557
+ * 如果 node 有 if 属性, 例如<div if.static1.static2="aaa" />,则创建属性名为shown的修改符对象
558
+ * ```
559
+ * {
560
+ * shown:{static1: true,static2: true}
561
+ * }
562
+ * ```
563
+ * @param node
564
+ * @param attributeName
565
+ * @param decoratorName
566
+ * @returns
566
567
  */
567
568
  static createDecoratorData(node, attributeName, decoratorName) {
568
569
  const attribute = TemplateUtil.getAttribute(node, attributeName);
@@ -579,13 +580,13 @@ class TemplateUtil {
579
580
  return undefined;
580
581
  }
581
582
 
582
- /**
583
- * 条件列表转换为表达式
584
- *
585
- * 转换规则为:前置条件全部取反 && 自身条件
586
- *
587
- * @param condition
588
- * @returns
583
+ /**
584
+ * 条件列表转换为表达式
585
+ *
586
+ * 转换规则为:前置条件全部取反 && 自身条件
587
+ *
588
+ * @param condition
589
+ * @returns
589
590
  */
590
591
  genIfConditionList(condition, identifiers) {
591
592
  const {
@@ -598,14 +599,14 @@ class TemplateUtil {
598
599
  return result;
599
600
  }
600
601
 
601
- /**
602
- * 获取 `if` 的条件列表
603
- *
604
- * 1. selfCondition=需要取节点身的条件
605
- * 2. preConditions=前置节点的条件--当前节点是 elif else 时,需要取前置节点
606
- * @param node
607
- * @param options
608
- * @returns
602
+ /**
603
+ * 获取 `if` 的条件列表
604
+ *
605
+ * 1. selfCondition=需要取节点身的条件
606
+ * 2. preConditions=前置节点的条件--当前节点是 elif else 时,需要取前置节点
607
+ * @param node
608
+ * @param options
609
+ * @returns
609
610
  */
610
611
  getIfCoditionList(node) {
611
612
  const ifAttribute = TemplateUtil.getAttribute(node, 'if');
@@ -729,18 +730,18 @@ class TemplateUtil {
729
730
  }
730
731
  }
731
732
 
732
- /**
733
- * 元素的属性转换为资源路径
734
- * 1. 不转换
735
- * 1.1 属性本身无需转换
736
- * 1.2 值不满足预设的条件: 不转换
737
- * 1.3 绝对路径:不转换
738
- * 2. 动态值:
739
- * 2.1 第一段是静态值: 第一段转换为相对 src 的绝对路径, 示例:../a/b{{x}} --> /a/b{{x}} + 添加 warn
740
- * 2.2 其它:不转换
741
- * 3. 静态值:
742
- * 3.1. 如果文件在src中,转换为相对src的路径, 示例:project/src/assets/a/b.png 转换为 /assets/a/b.png
743
- * 3.2. 否则修改为 require()
733
+ /**
734
+ * 元素的属性转换为资源路径
735
+ * 1. 不转换
736
+ * 1.1 属性本身无需转换
737
+ * 1.2 值不满足预设的条件: 不转换
738
+ * 1.3 绝对路径:不转换
739
+ * 2. 动态值:
740
+ * 2.1 第一段是静态值: 第一段转换为相对 src 的绝对路径, 示例:../a/b{{x}} --> /a/b{{x}} + 添加 warn
741
+ * 2.2 其它:不转换
742
+ * 3. 静态值:
743
+ * 3.1. 如果文件在src中,转换为相对src的路径, 示例:project/src/assets/a/b.png 转换为 /assets/a/b.png
744
+ * 3.2. 否则修改为 require()
744
745
  */
745
746
  convertResourcePath(nodeName, attrs) {
746
747
  const shouldDic = {
@@ -872,28 +873,32 @@ class TemplateUtil {
872
873
  });
873
874
  }
874
875
 
875
- /**
876
- * 获取节点中的文字内容
877
- *
878
- * 1. 如果是文字元素,返回 value
879
- * 1. 循环子元素列表
880
- * 1. 如果子元素是文本,直接返回内容
881
- * 2. 否则,如果子元素允许文本,则递归获取结果
882
- * 2. 文字中连续的特定符号[\r\n\t ]
883
- * 1. 首尾的替换为空字符串
884
- * 2. 其它替换为1个空格
885
- *
886
- * @example
887
- * `
888
- * a
889
- * b
890
- *
891
- * ` --> `a b
892
- *
893
- * @param node
876
+ /**
877
+ * 获取节点中的文字内容
878
+ *
879
+ * 1. 如果是文字元素,返回 value
880
+ * 2. 循环子元素列表
881
+ * 1. 如果子元素是文本,直接返回内容
882
+ * 2. 否则,如果子元素允许文本,则递归获取结果
883
+ * 3. 文字中连续的特定符号[\r\n\t ]
884
+ * 1. 首尾的替换为空字符串
885
+ * 2. 其它替换为1个空格
886
+ *
887
+ * @example
888
+ * `
889
+ * a
890
+ * b
891
+ *
892
+ * `
893
+ * -->
894
+ * `a b`
895
+ *
896
+ * @param node
894
897
  */
895
898
  getTextContent(node, elementConfig) {
896
899
  let result = '';
900
+ // 不压缩内容的节点名称列表
901
+ const noCompressTextNodeNames = ['markdown'];
897
902
  if (TemplateUtil.isText(node)) {
898
903
  result = node.value;
899
904
  } else {
@@ -908,6 +913,9 @@ class TemplateUtil {
908
913
  }
909
914
  });
910
915
  }
916
+ if (noCompressTextNodeNames.includes(node.nodeName)) {
917
+ return result;
918
+ }
911
919
  result = result.replace(/(^[\r\n\t ]+)|([\r\n\t ]+$)|[\r\n\t ]+/g, (match, p1, p2) => {
912
920
  if (p1 || p2) {
913
921
  return '';
@@ -18,11 +18,10 @@ declare class StyleMapUtil {
18
18
  * 3.4 原始位置的endColumn根据map结果,发现存在1个位置的差异,所以-1
19
19
  * 3.5 source存储来源的文件
20
20
  * @param location 生成结果中节点的位置信息
21
- * @param consumer 经过解析器获取的map信息生成的consumer对象
22
- * @param offset 当前样式代码在ux文件中的偏移量
21
+ * @param useAbsolutePath 是否生成绝对路径
23
22
  * @returns
24
23
  */
25
- transfromLocToPosition(location: CssLocation): {
24
+ transfromLocToPosition(location: CssLocation, useAbsolutePath?: boolean): {
26
25
  pos: number;
27
26
  end: number;
28
27
  startLine: number;
@@ -25,11 +25,11 @@ class StyleMapUtil {
25
25
  * 3.4 原始位置的endColumn根据map结果,发现存在1个位置的差异,所以-1
26
26
  * 3.5 source存储来源的文件
27
27
  * @param location 生成结果中节点的位置信息
28
- * @param consumer 经过解析器获取的map信息生成的consumer对象
29
- * @param offset 当前样式代码在ux文件中的偏移量
28
+ * @param useAbsolutePath 是否生成绝对路径
30
29
  * @returns
31
30
  */
32
31
  transfromLocToPosition(location) {
32
+ let useAbsolutePath = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
33
33
  const {
34
34
  projectPath
35
35
  } = this.options;
@@ -41,7 +41,7 @@ class StyleMapUtil {
41
41
  line: location.end.line,
42
42
  column: location.end.column
43
43
  });
44
- let nodeRelativePath = '';
44
+ let nodePath = '';
45
45
  if (nodeStartPosition.source?.endsWith('.ux') && nodeStartPosition.line) {
46
46
  nodeStartPosition.line = nodeStartPosition.line - 1 + this.lineOffset;
47
47
  }
@@ -54,7 +54,8 @@ class StyleMapUtil {
54
54
  if (decodeStr.startsWith('file://')) {
55
55
  decodeStr = (0, _url.fileURLToPath)(decodeStr);
56
56
  }
57
- nodeRelativePath = _path.default.relative(projectPath, decodeStr).replaceAll(_path.default.sep, _path.default.posix.sep);
57
+ // useAbsolutePath为false,返回相对路径
58
+ nodePath = (useAbsolutePath ? decodeStr : _path.default.relative(projectPath, decodeStr)).replaceAll(_path.default.sep, _path.default.posix.sep);
58
59
  }
59
60
  return {
60
61
  pos: 0,
@@ -63,7 +64,7 @@ class StyleMapUtil {
63
64
  startColumn: nodeStartPosition.column || 0,
64
65
  endLine: nodeEndPosition.line || 0,
65
66
  endColumn: nodeEndPosition.column || 0,
66
- source: nodeRelativePath
67
+ source: nodePath
67
68
  };
68
69
  }
69
70
  }
@@ -1,3 +1,4 @@
1
+ import { IPosition, Loglevel, StyleMessage } from '@aiot-toolkit/shared-utils';
1
2
  import { AtrulePrelude, CssNode, Declaration, Identifier, MediaFeature, MediaQuery, MediaQueryList, SelectorList, SyntaxParseError } from 'css-tree';
2
3
  import IOptions from '../config/IOptions';
3
4
  import { IImageFunction } from '../interface/IImageResource';
@@ -226,5 +227,6 @@ declare class StyleUtil {
226
227
  * @returns
227
228
  */
228
229
  static translateToNormalValue(value: any): any;
230
+ static handleStyleError(errorLevel: Loglevel, messages: StyleMessage[], options: IOptions, position: IPosition | undefined): void;
229
231
  }
230
232
  export default StyleUtil;