@aiot-toolkit/parser 2.0.5-beta.9 → 2.0.5-widget-provider-beta.2

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.
Files changed (109) hide show
  1. package/README.md +11 -11
  2. package/lib/index.d.ts +1 -4
  3. package/lib/index.js +0 -27
  4. package/lib/utils/ParserUtil.js +15 -15
  5. package/lib/ux/config/ExtensionConfig.js +2 -2
  6. package/lib/ux/config/FeatureConfig.js +19 -19
  7. package/lib/ux/config/vela/ElementConfig.js +8 -0
  8. package/lib/ux/config/vela/StyleAttributeConfig.js +87 -87
  9. package/lib/ux/enum/StyleSelectorType.js +6 -6
  10. package/lib/ux/enum/TemplateNodeType.js +2 -2
  11. package/lib/ux/enum/android/StyleSelectorName.js +2 -2
  12. package/lib/ux/parser/ScriptParser.js +2 -2
  13. package/lib/ux/parser/StyleParser.js +62 -62
  14. package/lib/ux/parser/TemplateValueParser.js +49 -49
  15. package/lib/ux/parser/UxParser.js +11 -11
  16. package/lib/ux/translate/android/StyleToTypescript.js +51 -51
  17. package/lib/ux/translate/android/TemplateToTypescript.js +51 -51
  18. package/lib/ux/translate/android/UxToTypescript.js +14 -14
  19. package/lib/ux/translate/android/attributeTranslate/ForTranslate.js +17 -17
  20. package/lib/ux/translate/android/attributeTranslate/IfTranslate.js +9 -9
  21. package/lib/ux/translate/vela/ScriptToTypescript.js +13 -12
  22. package/lib/ux/translate/vela/StyleToTypescript.js +10 -10
  23. package/lib/ux/translate/vela/TemplateToTypescript.js +74 -74
  24. package/lib/ux/translate/vela/TranslateCache.js +2 -2
  25. package/lib/ux/translate/vela/UxToTypescript.js +6 -6
  26. package/lib/ux/translate/vela/VelaContext.js +2 -2
  27. package/lib/ux/translate/vela/plugins/e2e.js +61 -61
  28. package/lib/ux/translate/vela/protobuf/BinaryPlugin.js +172 -172
  29. package/lib/ux/translate/vela/protobuf/protobuf.js +235 -235
  30. package/lib/ux/translate/vela/protobuf/protobufControl.js +159 -159
  31. package/lib/ux/translate/vela/protobuf/vdom_pb.js +2374 -2374
  32. package/lib/ux/translate/vela/runtime/velaTestLibrary.js +104 -104
  33. package/lib/ux/translate/vela/utils/AttributeConfig.js +15 -15
  34. package/lib/ux/translate/vela/utils/ExtendedBoxStyle.js +38 -38
  35. package/lib/ux/translate/vela/utils/SourceMapUtil.d.ts +4 -0
  36. package/lib/ux/translate/vela/utils/SourceMapUtil.js +36 -30
  37. package/lib/ux/translate/vela/utils/TemplateUtil.d.ts +10 -8
  38. package/lib/ux/translate/vela/utils/TemplateUtil.js +206 -191
  39. package/lib/ux/translate/vela/wrap/CbTranslate.js +8 -8
  40. package/lib/ux/translate/vela/wrap/CfTranslate.js +14 -14
  41. package/lib/ux/translate/vela/wrap/CiTranslate.js +11 -11
  42. package/lib/ux/utils/BabelUtil.js +35 -35
  43. package/lib/ux/utils/ElementConfigUtil.js +24 -24
  44. package/lib/ux/utils/StyleMapUtil.js +26 -16
  45. package/lib/ux/utils/StyleUtil.js +165 -165
  46. package/lib/ux/utils/UxUtil.js +32 -32
  47. package/lib/ux/validate/ElementValidate.js +2 -2
  48. package/lib/ux/validate/UxFileValidate.js +2 -2
  49. package/package.json +4 -5
  50. package/lib/xts/XtsParser.d.ts +0 -20
  51. package/lib/xts/XtsParser.js +0 -86
  52. package/lib/xts/XtsToTypescript.d.ts +0 -69
  53. package/lib/xts/XtsToTypescript.js +0 -894
  54. package/lib/xts/config/CommonAttributeList.d.ts +0 -6
  55. package/lib/xts/config/CommonAttributeList.js +0 -17
  56. package/lib/xts/config/classCount.json +0 -38
  57. package/lib/xts/config/commonAttribute.json +0 -84
  58. package/lib/xts/config/componentList.d.ts +0 -56
  59. package/lib/xts/config/componentList.js +0 -9
  60. package/lib/xts/config/predefinedComponent.json +0 -91
  61. package/lib/xts/config/render.json +0 -31
  62. package/lib/xts/data/ElementType.d.ts +0 -6
  63. package/lib/xts/data/ElementType.js +0 -5
  64. package/lib/xts/data/element/CommentElement.d.ts +0 -16
  65. package/lib/xts/data/element/CommentElement.js +0 -26
  66. package/lib/xts/data/element/ElementBase.d.ts +0 -14
  67. package/lib/xts/data/element/ElementBase.js +0 -20
  68. package/lib/xts/data/element/ForElement.d.ts +0 -58
  69. package/lib/xts/data/element/ForElement.js +0 -81
  70. package/lib/xts/data/element/IfElement.d.ts +0 -55
  71. package/lib/xts/data/element/IfElement.js +0 -87
  72. package/lib/xts/data/element/NodeElement.d.ts +0 -39
  73. package/lib/xts/data/element/NodeElement.js +0 -86
  74. package/lib/xts/enum/AttributeType.d.ts +0 -31
  75. package/lib/xts/enum/AttributeType.js +0 -53
  76. package/lib/xts/enum/Framework.d.ts +0 -98
  77. package/lib/xts/enum/Framework.js +0 -107
  78. package/lib/xts/interface/IContext.d.ts +0 -11
  79. package/lib/xts/interface/IContext.js +0 -1
  80. package/lib/xts/interface/IElement.d.ts +0 -17
  81. package/lib/xts/interface/IElement.js +0 -5
  82. package/lib/xts/interface/IExpressNode.d.ts +0 -20
  83. package/lib/xts/interface/IExpressNode.js +0 -1
  84. package/lib/xts/interface/config/ICommon.d.ts +0 -23
  85. package/lib/xts/interface/config/ICommon.js +0 -5
  86. package/lib/xts/interface/config/IRenderNode.d.ts +0 -25
  87. package/lib/xts/interface/config/IRenderNode.js +0 -1
  88. package/lib/xts/interface/validate/IAttibuteValidate.d.ts +0 -8
  89. package/lib/xts/interface/validate/IAttibuteValidate.js +0 -1
  90. package/lib/xts/interface/validate/IComponentValidate.d.ts +0 -9
  91. package/lib/xts/interface/validate/IComponentValidate.js +0 -1
  92. package/lib/xts/interface/validate/IRenderValidate.d.ts +0 -10
  93. package/lib/xts/interface/validate/IRenderValidate.js +0 -1
  94. package/lib/xts/interface/validate/ISourceFileValidate.d.ts +0 -7
  95. package/lib/xts/interface/validate/ISourceFileValidate.js +0 -1
  96. package/lib/xts/utils/AttributeUtil.d.ts +0 -73
  97. package/lib/xts/utils/AttributeUtil.js +0 -206
  98. package/lib/xts/utils/TypeUtil.d.ts +0 -60
  99. package/lib/xts/utils/TypeUtil.js +0 -97
  100. package/lib/xts/utils/TypescriptUtil.d.ts +0 -42
  101. package/lib/xts/utils/TypescriptUtil.js +0 -128
  102. package/lib/xts/validate/AttibuteValidate.d.ts +0 -18
  103. package/lib/xts/validate/AttibuteValidate.js +0 -97
  104. package/lib/xts/validate/ComponentValidate.d.ts +0 -19
  105. package/lib/xts/validate/ComponentValidate.js +0 -90
  106. package/lib/xts/validate/RenderValidate.d.ts +0 -63
  107. package/lib/xts/validate/RenderValidate.js +0 -239
  108. package/lib/xts/validate/SourceFileValidate.d.ts +0 -10
  109. package/lib/xts/validate/SourceFileValidate.js +0 -43
@@ -1,97 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
- var _sharedUtils = require("@aiot-toolkit/shared-utils");
8
- var _CommonAttributeList = _interopRequireDefault(require("../config/CommonAttributeList"));
9
- var _predefinedComponent = _interopRequireDefault(require("../config/predefinedComponent.json"));
10
- var _AttributeUtil = _interopRequireDefault(require("../utils/AttributeUtil"));
11
- var _TypescriptUtil = _interopRequireDefault(require("../utils/TypescriptUtil"));
12
- function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
13
- /**
14
- * AttibuteValidate
15
- */
16
- class AttibuteValidate {
17
- TRANSLATE_TYPE = {
18
- StringLiteral: 'string',
19
- NumbericLiteral: 'number',
20
- ObjectLiteralExpression: 'object'
21
- };
22
- validate(element, context, componentName) {
23
- const {
24
- sourceFile,
25
- onLog,
26
- offsetList
27
- } = context;
28
- // 属性节点值
29
- const elemName = element.name.getText();
30
- const elemValue = element.value.length > 0 ? element.value[0].getText() : '';
31
- const elemType = element.value[0];
32
- // 获取属性列表
33
- const attributeList = [..._CommonAttributeList.default];
34
- // 查看组件是否有特殊属性
35
- const componentGroups = _predefinedComponent.default.componentGroups;
36
- const compIndex = componentGroups.findIndex(comp => comp.name === componentName);
37
- const specialAttrList = compIndex !== -1 ? componentGroups[compIndex]?.attributeGroups : [];
38
- if (specialAttrList.length > 0) {
39
- attributeList.push(...specialAttrList);
40
- }
41
- // 检查属性是否正确
42
- const attrIndex = attributeList.findIndex(attr => attr.name === elemName);
43
- if (attrIndex !== -1) {
44
- const attr = attributeList[attrIndex];
45
- // 检查属性值\属性类型是否正确
46
- if (attr.type) {
47
- const errorStr = this.checkElemType(elemType, attr.type);
48
- errorStr && onLog({
49
- message: `Attribute ${elemName} ${errorStr}`,
50
- level: _sharedUtils.Loglevel.ERROR,
51
- position: _TypescriptUtil.default.getNodePostion(elemType, sourceFile, offsetList)
52
- });
53
- } else {
54
- // 判断elemValue,有值则报错,属性值不能为空
55
- elemValue && onLog({
56
- message: `Attribute ${elemName} value should be null`,
57
- level: _sharedUtils.Loglevel.ERROR,
58
- position: _TypescriptUtil.default.getNodePostion(element.name, sourceFile, offsetList)
59
- });
60
- }
61
- } else {
62
- // 组件上没有该属性
63
- onLog({
64
- message: `Component ${componentName} has no ${elemName} attibute`,
65
- level: _sharedUtils.Loglevel.ERROR,
66
- position: _TypescriptUtil.default.getNodePostion(element.name, sourceFile, offsetList)
67
- });
68
- }
69
- return [];
70
- }
71
- isISubTypeArray(instance) {
72
- return Array.isArray(instance);
73
- }
74
- /**
75
- * 检查属性类型是否正确
76
- * @param elemType
77
- * @param attrType
78
- */
79
- checkElemType(elemType, attrType) {
80
- if (this.isISubTypeArray(attrType)) {
81
- // 1. 静态数据时进行数据类型校验
82
- if (!_AttributeUtil.default.isDynamicValue(elemType)) {
83
- const typeList = attrType.map(item => item.subType);
84
- const type = this.TRANSLATE_TYPE[elemType.getKindName()];
85
- if (typeList.findIndex(item => item === type) === -1) {
86
- // 属性值应该为typeList中的一种
87
- return `value should be one of [${typeList.join(`,`)}]`;
88
- }
89
- }
90
- } else {
91
- // 属性的type类型应该为IType[]
92
- return 'type should be IType[]';
93
- }
94
- return '';
95
- }
96
- }
97
- var _default = exports.default = AttibuteValidate;
@@ -1,19 +0,0 @@
1
- import { ExpressionStatement } from 'ts-morph';
2
- import IContext from '../interface/IContext';
3
- import IComponentValidate from '../interface/validate/IComponentValidate';
4
- /**
5
- * 组件校验
6
- * ComponentValidate
7
- */
8
- declare class ComponentValidate implements IComponentValidate {
9
- private TRANSLATE_TYPE;
10
- componentValidate(element: ExpressionStatement, context: IContext): Error[];
11
- /**
12
- * 自定义组件校验
13
- * @param element
14
- * @param sourceFile
15
- * @returns
16
- */
17
- customComponentValidate(element: ExpressionStatement, context: IContext): Error[];
18
- }
19
- export default ComponentValidate;
@@ -1,90 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
- var _sharedUtils = require("@aiot-toolkit/shared-utils");
8
- var _componentList = _interopRequireDefault(require("../config/componentList"));
9
- var _AttributeUtil = _interopRequireDefault(require("../utils/AttributeUtil"));
10
- var _TypescriptUtil = _interopRequireDefault(require("../utils/TypescriptUtil"));
11
- var _AttibuteValidate = _interopRequireDefault(require("./AttibuteValidate"));
12
- function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
13
- /**
14
- * 组件校验
15
- * ComponentValidate
16
- */
17
- class ComponentValidate {
18
- TRANSLATE_TYPE = {
19
- StringLiteral: 'string',
20
- NumbericLiteral: 'number',
21
- ObjectLiteralExpression: 'object'
22
- };
23
- componentValidate(element, context) {
24
- const {
25
- sourceFile,
26
- onLog,
27
- offsetList
28
- } = context;
29
- const expressionList = _TypescriptUtil.default.splitExpression(element).flat();
30
- if (expressionList.length > 0) {
31
- const expressionNode = expressionList[0];
32
- const expressionName = expressionNode.name?.getText();
33
- const expressionValue = expressionNode.value[0];
34
- const compIndex = _componentList.default.findIndex(comp => comp.name === expressionName);
35
- if (compIndex !== -1) {
36
- const componentNode = _componentList.default[compIndex];
37
- // 检查组件值
38
- if (expressionValue) {
39
- const subType0 = componentNode.type ? componentNode.type[0].subType : '';
40
- // 组件规定不能传值时传值,报错
41
- !subType0 && onLog({
42
- message: `Component ${expressionName} has no value attibute`,
43
- level: _sharedUtils.Loglevel.ERROR,
44
- position: _TypescriptUtil.default.getNodePostion(element, sourceFile, offsetList)
45
- });
46
-
47
- // 组件值为静态数据时,进行类型检查
48
- if (!_AttributeUtil.default.isDynamicValue(expressionValue) && subType0 !== this.TRANSLATE_TYPE[expressionValue.getKindName()]) {
49
- onLog({
50
- message: `The static type of value of Component ${expressionName} must be ${subType0}`,
51
- level: _sharedUtils.Loglevel.ERROR,
52
- position: _TypescriptUtil.default.getNodePostion(element, sourceFile, offsetList)
53
- });
54
- }
55
- }
56
- // 遍历属性,进行属性校验
57
- for (let i = 1; i < expressionList.length; i++) {
58
- new _AttibuteValidate.default().validate(expressionList[i], context, expressionName);
59
- }
60
- } else {
61
- // 检查自定义组件
62
- this.customComponentValidate(element, context);
63
- }
64
- }
65
- return [];
66
- }
67
- /**
68
- * 自定义组件校验
69
- * @param element
70
- * @param sourceFile
71
- * @returns
72
- */
73
- customComponentValidate(element, context) {
74
- const {
75
- onLog,
76
- sourceFile,
77
- offsetList
78
- } = context;
79
- const expressionList = _TypescriptUtil.default.splitExpression(element).flat();
80
- if (expressionList.length > 1) {
81
- onLog({
82
- message: `Custom Component ${expressionList[0].name?.getText()} are not allowed to have attributes`,
83
- level: _sharedUtils.Loglevel.WARN,
84
- position: _TypescriptUtil.default.getNodePostion(element, sourceFile, offsetList)
85
- });
86
- }
87
- return [];
88
- }
89
- }
90
- var _default = exports.default = ComponentValidate;
@@ -1,63 +0,0 @@
1
- import { ExpressionStatement, Identifier, Node, PropertyAccessExpression, ts } from 'ts-morph';
2
- import IContext from '../interface/IContext';
3
- import IRenderValidate from '../interface/validate/IRenderValidate';
4
- /**
5
- * 校验render内容
6
- * RenderValidate
7
- */
8
- declare class RenderValidate implements IRenderValidate {
9
- /**
10
- * 检查节点表达式
11
- * @param elementList
12
- * @param startIndex
13
- * @param sourceFile
14
- * @returns
15
- */
16
- validateNodeElement(elementList: Node<ts.Node>[], startIndex: number, context: IContext): Error[];
17
- /**
18
- * 检查使用的@renderer函数
19
- * a. 获取函数调用的函数名
20
- * b. 找到特殊类,匹配方法名
21
- * b1. 未匹配到则报错,没有在class中找到该方法
22
- * b2. 匹配到则判断修饰符,有修饰符则通过,没有则报错,方法应该用@renderer修饰
23
- * @param sourceFile
24
- * @param element
25
- * @returns
26
- */
27
- validateRenderer(context: IContext, element: ExpressionStatement): Error[];
28
- /**
29
- * 检查ForEach结构
30
- * 1. 检查ForEach配置规则
31
- * a. 不存在配置信息报错
32
- * 2. 存在配置,循环参数列表argumentList,列表项argumentList[i]
33
- * a. 从rules中寻找j, rules[j].paramIndex === i
34
- * b. 不存在则报错,没有对应的参数配置
35
- * c. 存在则检查argumentList[i]===undefined
36
- * c1. 不等则检查argumentList[i]的kind 是否等于 rules[j].type,不相等就报错,相等就通过
37
- * c2. 相等则判断isRequired,true则报错, 否则通过
38
- * @returns
39
- */
40
- validateForEach(context: IContext, element: Node<ts.Node>, argumentList: Node<ts.Node>[]): Error[];
41
- /**
42
- * 从表达式语句中获取函数调用表达式的属性访问表达式
43
- * ExpressionStatement--->this.squareText("A")
44
- * CallExpression--->this.squareText("A")
45
- * PropertyAccessExpression--->this.squareText
46
- * @param node
47
- * @returns
48
- */
49
- getExpressionOfCallExpression(node: ExpressionStatement): PropertyAccessExpression | Identifier | undefined;
50
- /**
51
- * 检查表达式是否为this.function
52
- * @param node
53
- * @returns
54
- */
55
- checkNodeIsThisExpression(node: ExpressionStatement): boolean;
56
- /**
57
- * 检查节点是否为ForEach
58
- * @param node
59
- * @returns
60
- */
61
- checkNodeIsForEach(node: Node<ts.Node>): boolean;
62
- }
63
- export default RenderValidate;
@@ -1,239 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
- var _sharedUtils = require("@aiot-toolkit/shared-utils");
8
- var _tsMorph = require("ts-morph");
9
- var _render = _interopRequireDefault(require("../config/render.json"));
10
- var _TypescriptUtil = _interopRequireDefault(require("../utils/TypescriptUtil"));
11
- var _ComponentValidate = _interopRequireDefault(require("./ComponentValidate"));
12
- function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
13
- /**
14
- * 校验render内容
15
- * RenderValidate
16
- */
17
- class RenderValidate {
18
- /**
19
- * 检查节点表达式
20
- * @param elementList
21
- * @param startIndex
22
- * @param sourceFile
23
- * @returns
24
- */
25
- validateNodeElement(elementList, startIndex, context) {
26
- const {
27
- sourceFile,
28
- offsetList,
29
- onLog
30
- } = context;
31
- const node1 = elementList[startIndex];
32
- // 1. 校验node1,必需为表达式
33
- if (node1 instanceof _tsMorph.ExpressionStatement) {
34
- // 2. 检查node1是否为this.renderer函数
35
- if (this.checkNodeIsThisExpression(node1)) {
36
- this.validateRenderer(context, node1);
37
- } else {
38
- // 3. 组件校验
39
- new _ComponentValidate.default().componentValidate(node1, context);
40
- }
41
- } else {
42
- onLog({
43
- message: `entry node must be expression`,
44
- level: _sharedUtils.Loglevel.ERROR,
45
- position: _TypescriptUtil.default.getNodePostion(node1, sourceFile, offsetList)
46
- });
47
- }
48
- // 4. startIndex + 2存在节点
49
- if (elementList.length > startIndex + 3) {
50
- const node2 = elementList[startIndex + 1];
51
- const node3 = elementList[startIndex + 2];
52
- if (node2.getKind() === _tsMorph.SyntaxKind.Block && node3.getKind() === _tsMorph.SyntaxKind.DotToken) {
53
- const node4 = elementList[startIndex + 3];
54
- if (node4 instanceof _tsMorph.ExpressionStatement) {
55
- // 5. 组件校验
56
- new _ComponentValidate.default().componentValidate(node4, context);
57
- } else {
58
- onLog({
59
- message: 'after docToken must be expression',
60
- level: _sharedUtils.Loglevel.ERROR,
61
- position: _TypescriptUtil.default.getNodePostion(node4, sourceFile, offsetList)
62
- });
63
- }
64
- }
65
- }
66
- return [];
67
- }
68
- /**
69
- * 检查使用的@renderer函数
70
- * a. 获取函数调用的函数名
71
- * b. 找到特殊类,匹配方法名
72
- * b1. 未匹配到则报错,没有在class中找到该方法
73
- * b2. 匹配到则判断修饰符,有修饰符则通过,没有则报错,方法应该用@renderer修饰
74
- * @param sourceFile
75
- * @param element
76
- * @returns
77
- */
78
- validateRenderer(context, element) {
79
- // 必须在特殊类中找到方法,且方法修饰符列表中存在@Renderer
80
- const {
81
- sourceFile,
82
- onLog,
83
- offsetList
84
- } = context;
85
- const propertyAccessExpression = this.getExpressionOfCallExpression(element);
86
- if (propertyAccessExpression instanceof _tsMorph.PropertyAccessExpression) {
87
- const rendererFuncName = propertyAccessExpression.getName();
88
- const classList = sourceFile.getClasses();
89
- const specialClass = classList.find(cItem => cItem.getDecorator('parentClass'));
90
- if (specialClass) {
91
- const spClassMethod = specialClass.getMethod(rendererFuncName);
92
- if (spClassMethod) {
93
- const decorator = spClassMethod.getDecorator('Renderer');
94
- decorator === undefined && onLog({
95
- message: `@Renderer modifier dose not exist`,
96
- level: _sharedUtils.Loglevel.ERROR,
97
- position: _TypescriptUtil.default.getNodePostion(element, sourceFile, offsetList)
98
- });
99
- } else {
100
- // 方法名找不到
101
- onLog({
102
- message: `${rendererFuncName} function dose not exist`,
103
- level: _sharedUtils.Loglevel.ERROR,
104
- position: _TypescriptUtil.default.getNodePostion(element, sourceFile, offsetList)
105
- });
106
- }
107
- }
108
- } else {
109
- onLog({
110
- message: `${element.getText()} error`,
111
- level: _sharedUtils.Loglevel.ERROR,
112
- position: _TypescriptUtil.default.getNodePostion(element, sourceFile, offsetList)
113
- });
114
- }
115
- return [];
116
- }
117
- /**
118
- * 检查ForEach结构
119
- * 1. 检查ForEach配置规则
120
- * a. 不存在配置信息报错
121
- * 2. 存在配置,循环参数列表argumentList,列表项argumentList[i]
122
- * a. 从rules中寻找j, rules[j].paramIndex === i
123
- * b. 不存在则报错,没有对应的参数配置
124
- * c. 存在则检查argumentList[i]===undefined
125
- * c1. 不等则检查argumentList[i]的kind 是否等于 rules[j].type,不相等就报错,相等就通过
126
- * c2. 相等则判断isRequired,true则报错, 否则通过
127
- * @returns
128
- */
129
- validateForEach(context, element, argumentList) {
130
- const {
131
- onLog,
132
- sourceFile,
133
- offsetList
134
- } = context;
135
- const forEachIndex = _render.default.findIndex(item => new RegExp(item.name).test('ForEach'));
136
- if (forEachIndex !== -1) {
137
- const rules = _render.default[forEachIndex].rule;
138
- argumentList.forEach((arguNode, index) => {
139
- const ruleIndex = rules.findIndex(item => item.paramIndex === index);
140
- if (ruleIndex !== -1) {
141
- const rule = rules[ruleIndex];
142
- const arguNodeKind = arguNode !== undefined ? arguNode.getKindName() : '';
143
- const arguIsRequired = rule.isRequired === undefined ? true : rule.isRequired;
144
- // 实参为无效参数
145
- if (!arguNodeKind) {
146
- // 若为必需参数,则实参必须传值
147
- arguIsRequired && onLog({
148
- message: `The ${index + 1}th parameter in ForEach() must exist`,
149
- level: _sharedUtils.Loglevel.ERROR,
150
- position: _TypescriptUtil.default.getNodePostion(element, sourceFile, offsetList)
151
- });
152
- }
153
- // 实参为有效参数
154
- if (arguNodeKind && arguNodeKind !== rule.type) {
155
- // 实参类型错误
156
- onLog({
157
- message: `The ${index + 1}th parameter in ForEach() must be ${rule.type}`,
158
- level: _sharedUtils.Loglevel.ERROR,
159
- position: _TypescriptUtil.default.getNodePostion(arguNode, sourceFile, offsetList)
160
- });
161
- }
162
- } else {
163
- onLog({
164
- message: `ForEach() has no corresponding ${index + 1}th parameter configuration`,
165
- level: _sharedUtils.Loglevel.ERROR,
166
- position: _TypescriptUtil.default.getNodePostion(element, sourceFile, offsetList)
167
- });
168
- }
169
- });
170
- if (rules.length > argumentList.length) {
171
- const rule = rules.find(item => item.paramIndex === argumentList.length);
172
- rule === undefined && onLog({
173
- message: `The config rules.length is ${rules.length}, but not find paramIndex`,
174
- level: _sharedUtils.Loglevel.ERROR,
175
- position: _TypescriptUtil.default.getNodePostion(element, sourceFile, offsetList)
176
- });
177
- const ruleIsRequired = rule?.isRequired === undefined ? true : rule.isRequired;
178
- ruleIsRequired && onLog({
179
- message: `The ${argumentList.length + 1}th parameter in ForEach() must exist`,
180
- level: _sharedUtils.Loglevel.ERROR,
181
- position: _TypescriptUtil.default.getNodePostion(element, sourceFile, offsetList)
182
- });
183
- }
184
- } else {
185
- onLog({
186
- message: `There is no configuration content about ForEach()`,
187
- level: _sharedUtils.Loglevel.ERROR,
188
- position: _TypescriptUtil.default.getNodePostion(element, sourceFile, offsetList)
189
- });
190
- }
191
- return [];
192
- }
193
- /**
194
- * 从表达式语句中获取函数调用表达式的属性访问表达式
195
- * ExpressionStatement--->this.squareText("A")
196
- * CallExpression--->this.squareText("A")
197
- * PropertyAccessExpression--->this.squareText
198
- * @param node
199
- * @returns
200
- */
201
- getExpressionOfCallExpression(node) {
202
- const callExpression = node.getExpression();
203
- if (callExpression instanceof _tsMorph.CallExpression) {
204
- const expression = callExpression.getExpression();
205
- if (expression instanceof _tsMorph.PropertyAccessExpression || expression instanceof _tsMorph.Identifier) {
206
- return expression;
207
- }
208
- }
209
- return undefined;
210
- }
211
- /**
212
- * 检查表达式是否为this.function
213
- * @param node
214
- * @returns
215
- */
216
- checkNodeIsThisExpression(node) {
217
- const propertyAccessExpression = this.getExpressionOfCallExpression(node);
218
- if (propertyAccessExpression instanceof _tsMorph.PropertyAccessExpression) {
219
- const expression = propertyAccessExpression.getExpression();
220
- return expression.getKind() === _tsMorph.SyntaxKind.ThisKeyword;
221
- }
222
- return false;
223
- }
224
- /**
225
- * 检查节点是否为ForEach
226
- * @param node
227
- * @returns
228
- */
229
- checkNodeIsForEach(node) {
230
- if (node instanceof _tsMorph.ExpressionStatement) {
231
- const identifier = this.getExpressionOfCallExpression(node);
232
- if (identifier instanceof _tsMorph.Identifier) {
233
- return identifier.getText() === 'ForEach';
234
- }
235
- }
236
- return false;
237
- }
238
- }
239
- var _default = exports.default = RenderValidate;
@@ -1,10 +0,0 @@
1
- import IContext from '../interface/IContext';
2
- import ISourceFileValidate from '../interface/validate/ISourceFileValidate';
3
- /**
4
- * 校验文件中的特殊类
5
- * ClassCountValidate
6
- */
7
- declare class SourceFileValidate implements ISourceFileValidate {
8
- validate(context: IContext, fileName: string): Error[];
9
- }
10
- export default SourceFileValidate;
@@ -1,43 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
- var _sharedUtils = require("@aiot-toolkit/shared-utils");
8
- var _classCount = _interopRequireDefault(require("../config/classCount.json"));
9
- function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
10
- /**
11
- * 校验文件中的特殊类
12
- * ClassCountValidate
13
- */
14
- class SourceFileValidate {
15
- validate(context, fileName) {
16
- const {
17
- sourceFile,
18
- onLog
19
- } = context;
20
- const ruleIndex = _classCount.default.findIndex(item => new RegExp(item.name).test(fileName));
21
- const rules = _classCount.default[ruleIndex].rule;
22
- rules.map(rule => {
23
- if (rule.classDecorator) {
24
- const spDecorator = rule.classDecorator;
25
- // 检查所有类中,特殊类是不是只出现了规则限制的次数
26
- const classNodes = sourceFile.getClasses();
27
- let countSpClass = 0;
28
- for (const classNode of classNodes) {
29
- const classDec = classNode.getDecorator(spDecorator);
30
- classDec?.getText() === rule.value && countSpClass++;
31
- if (countSpClass < rule.min || countSpClass > rule.max) {
32
- classDec && onLog({
33
- message: `${rule.keyword} modifier appears too many times`,
34
- level: _sharedUtils.Loglevel.ERROR
35
- });
36
- }
37
- }
38
- }
39
- });
40
- return [];
41
- }
42
- }
43
- var _default = exports.default = SourceFileValidate;