@lcap/nasl 3.8.2-beta.9 → 3.8.3-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.
- package/ai-engineer.config.js +617 -0
- package/out/common/BaseNode.d.ts +9 -4
- package/out/common/BaseNode.js +43 -10
- package/out/common/BaseNode.js.map +1 -1
- package/out/concepts/AuthLogicForCallInterface__.js +2 -50
- package/out/concepts/AuthLogicForCallInterface__.js.map +1 -1
- package/out/concepts/BackendVariable__.js +16 -1
- package/out/concepts/BackendVariable__.js.map +1 -1
- package/out/concepts/BindAttribute__.js +12 -4
- package/out/concepts/BindAttribute__.js.map +1 -1
- package/out/concepts/BindDirective__.js +1 -1
- package/out/concepts/BindDirective__.js.map +1 -1
- package/out/concepts/BindStyle__.js +1 -1
- package/out/concepts/BindStyle__.js.map +1 -1
- package/out/concepts/BusinessComponent__.js +2 -42
- package/out/concepts/BusinessComponent__.js.map +1 -1
- package/out/concepts/BusinessLogic__.js +2 -50
- package/out/concepts/BusinessLogic__.js.map +1 -1
- package/out/concepts/CallFunction__.d.ts +1 -1
- package/out/concepts/CallFunction__.js +1 -1
- package/out/concepts/CallLogic__.js +7 -1
- package/out/concepts/CallLogic__.js.map +1 -1
- package/out/concepts/CallQueryComponent__.d.ts +1 -0
- package/out/concepts/CallQueryComponent__.js +23 -12
- package/out/concepts/CallQueryComponent__.js.map +1 -1
- package/out/concepts/Destination__.js +11 -8
- package/out/concepts/Destination__.js.map +1 -1
- package/out/concepts/EntityProperty__.js +3 -2
- package/out/concepts/EntityProperty__.js.map +1 -1
- package/out/concepts/Entity__.js +1 -1
- package/out/concepts/Entity__.js.map +1 -1
- package/out/concepts/Identifier__.d.ts +2 -0
- package/out/concepts/Identifier__.js +51 -5
- package/out/concepts/Identifier__.js.map +1 -1
- package/out/concepts/Interface__.js +1 -1
- package/out/concepts/Interface__.js.map +1 -1
- package/out/concepts/Logic__.js +33 -111
- package/out/concepts/Logic__.js.map +1 -1
- package/out/concepts/MemberExpression__.d.ts +1 -1
- package/out/concepts/MemberExpression__.js +15 -3
- package/out/concepts/MemberExpression__.js.map +1 -1
- package/out/concepts/OqlQueryComponent__.js +37 -12
- package/out/concepts/OqlQueryComponent__.js.map +1 -1
- package/out/concepts/OverriddenLogic__.js +2 -50
- package/out/concepts/OverriddenLogic__.js.map +1 -1
- package/out/concepts/Param__.js +5 -4
- package/out/concepts/Param__.js.map +1 -1
- package/out/concepts/ProcessElementV2__.js +4 -0
- package/out/concepts/ProcessElementV2__.js.map +1 -1
- package/out/concepts/ProcessElement__.js +4 -0
- package/out/concepts/ProcessElement__.js.map +1 -1
- package/out/concepts/ProcessV2__.js +4 -0
- package/out/concepts/ProcessV2__.js.map +1 -1
- package/out/concepts/Process__.js +4 -0
- package/out/concepts/Process__.js.map +1 -1
- package/out/concepts/QueryOrderByExpression__.d.ts +0 -1
- package/out/concepts/QueryOrderByExpression__.js +0 -1
- package/out/concepts/QueryOrderByExpression__.js.map +1 -1
- package/out/concepts/Structure__.js +5 -7
- package/out/concepts/Structure__.js.map +1 -1
- package/out/concepts/SubLogic__.js +5 -50
- package/out/concepts/SubLogic__.js.map +1 -1
- package/out/concepts/Variable__.js +5 -0
- package/out/concepts/Variable__.js.map +1 -1
- package/out/concepts/ViewElement__.d.ts +1 -0
- package/out/concepts/ViewElement__.js +16 -6
- package/out/concepts/ViewElement__.js.map +1 -1
- package/out/concepts/View__.js +48 -49
- package/out/concepts/View__.js.map +1 -1
- package/out/natural/genNaturalTS.d.ts +45 -10
- package/out/natural/genNaturalTS.js +57 -33
- package/out/natural/genNaturalTS.js.map +1 -1
- package/out/natural/getContext/getUILib.js +6 -3
- package/out/natural/getContext/getUILib.js.map +1 -1
- package/out/natural/getContext/index.d.ts +20 -7
- package/out/natural/getContext/index.js +143 -23
- package/out/natural/getContext/index.js.map +1 -1
- package/out/natural/getContext/naslStdlibMap.js +8 -8
- package/out/natural/getContext/naslStdlibMap.js.map +1 -1
- package/out/natural/tools.d.ts +16 -0
- package/out/natural/tools.js +110 -2
- package/out/natural/tools.js.map +1 -1
- package/out/natural/transformTS2UI.js +343 -292
- package/out/natural/transformTS2UI.js.map +1 -1
- package/out/natural/transformTSCode.d.ts +3 -5
- package/out/natural/transformTSCode.js +30 -1089
- package/out/natural/transformTSCode.js.map +1 -1
- package/out/natural/transforms/registerTransform.d.ts +7 -0
- package/out/natural/transforms/registerTransform.js +24 -0
- package/out/natural/transforms/registerTransform.js.map +1 -0
- package/out/natural/transforms/transform2LogicItem.d.ts +24 -0
- package/out/natural/transforms/transform2LogicItem.js +1240 -0
- package/out/natural/transforms/transform2LogicItem.js.map +1 -0
- package/out/natural/transforms/transform2TypeAnnotation.d.ts +2 -0
- package/out/natural/transforms/transform2TypeAnnotation.js +86 -0
- package/out/natural/transforms/transform2TypeAnnotation.js.map +1 -0
- package/out/natural/transforms/utils.d.ts +8 -0
- package/out/natural/transforms/utils.js +59 -0
- package/out/natural/transforms/utils.js.map +1 -0
- package/out/server/getLogics.js +12 -12
- package/out/server/getLogics.js.map +1 -1
- package/out/server/naslServer.d.ts +2 -2
- package/out/server/naslServer.js +48 -18
- package/out/server/naslServer.js.map +1 -1
- package/out/server/semanticData.d.ts +32 -0
- package/out/server/semanticData.js +371 -0
- package/out/server/semanticData.js.map +1 -0
- package/out/server/translator.js +5 -0
- package/out/server/translator.js.map +1 -1
- package/out/templator/block2nasl/jsx2nasl/index.d.ts +5 -0
- package/out/templator/block2nasl/jsx2nasl/index.js +15 -0
- package/out/templator/block2nasl/jsx2nasl/index.js.map +1 -0
- package/out/templator/block2nasl/jsx2nasl/transform-expression2nasl.d.ts +3 -0
- package/out/templator/block2nasl/jsx2nasl/transform-expression2nasl.js +213 -0
- package/out/templator/block2nasl/jsx2nasl/transform-expression2nasl.js.map +1 -0
- package/out/templator/block2nasl/jsx2nasl/transform-func2nasl.d.ts +14 -0
- package/out/templator/block2nasl/jsx2nasl/transform-func2nasl.js +201 -0
- package/out/templator/block2nasl/jsx2nasl/transform-func2nasl.js.map +1 -0
- package/out/templator/block2nasl/jsx2nasl/transform-tstype2nasl.d.ts +4 -0
- package/out/templator/block2nasl/jsx2nasl/transform-tstype2nasl.js +186 -0
- package/out/templator/block2nasl/jsx2nasl/transform-tstype2nasl.js.map +1 -0
- package/out/templator/block2nasl/jsx2nasl/transform-tsx2nasl.d.ts +28 -0
- package/out/templator/block2nasl/jsx2nasl/transform-tsx2nasl.js +336 -0
- package/out/templator/block2nasl/jsx2nasl/transform-tsx2nasl.js.map +1 -0
- package/out/templator/block2nasl/jsx2nasl/utils.d.ts +2 -0
- package/out/templator/block2nasl/jsx2nasl/utils.js +26 -0
- package/out/templator/block2nasl/jsx2nasl/utils.js.map +1 -0
- package/out/templator/block2nasl/transformBlock2Nasl.d.ts +10 -0
- package/out/templator/block2nasl/transformBlock2Nasl.js +122 -0
- package/out/templator/block2nasl/transformBlock2Nasl.js.map +1 -0
- package/out/templator/block2nasl/viewMergeBlock.d.ts +9 -0
- package/out/templator/block2nasl/viewMergeBlock.js +111 -0
- package/out/templator/block2nasl/viewMergeBlock.js.map +1 -0
- package/out/translator/types.d.ts +2 -0
- package/out/translator/utils.js +1 -1
- package/out/translator/utils.js.map +1 -1
- package/out/utils/index.js +0 -4
- package/out/utils/index.js.map +1 -1
- package/out/utils/language-cache/constant.d.ts +2 -1
- package/out/utils/language-cache/constant.js +1 -0
- package/out/utils/language-cache/constant.js.map +1 -1
- package/out/utils/language-cache/nasl.d.ts +1 -0
- package/out/utils/language-cache/nasl.js +9 -1
- package/out/utils/language-cache/nasl.js.map +1 -1
- package/package.json +6 -2
- package/sandbox-natural/stdlib/nasl.core.d.ts +54 -0
- package/sandbox-natural/stdlib/nasl.oql.d.ts +22 -0
- package/sandbox-natural/stdlib/nasl.ui.d.ts +78 -0
- package/sandbox-natural/stdlib/nasl.ui.h5.d.ts +1684 -1643
- package/sandbox-natural/stdlib/nasl.ui.h5.json +15746 -15045
- package/sandbox-natural/stdlib/nasl.ui.pc.d.ts +5341 -5154
- package/sandbox-natural/stdlib/nasl.ui.pc.json +31176 -30681
- package/sandbox-natural/stdlib/{nasl.util.ts → nasl.util.d.ts} +49 -43
- package/src/common/BaseNode.ts +50 -8
- package/src/concepts/AuthLogicForCallInterface__.ts +3 -56
- package/src/concepts/BackendVariable__.ts +16 -1
- package/src/concepts/BindAttribute__.ts +18 -13
- package/src/concepts/BindDirective__.ts +1 -1
- package/src/concepts/BindStyle__.ts +1 -1
- package/src/concepts/BusinessComponent__.ts +6 -51
- package/src/concepts/BusinessLogic__.ts +4 -56
- package/src/concepts/CallFunction__.ts +1 -1
- package/src/concepts/CallLogic__.ts +21 -16
- package/src/concepts/CallQueryComponent__.ts +26 -12
- package/src/concepts/Destination__.ts +14 -12
- package/src/concepts/EntityProperty__.ts +5 -5
- package/src/concepts/Entity__.ts +1 -1
- package/src/concepts/Identifier__.ts +57 -6
- package/src/concepts/Interface__.ts +1 -1
- package/src/concepts/Logic__.ts +44 -134
- package/src/concepts/MemberExpression__.ts +21 -7
- package/src/concepts/OqlQueryComponent__.ts +29 -5
- package/src/concepts/OverriddenLogic__.ts +4 -56
- package/src/concepts/Param__.ts +5 -4
- package/src/concepts/ProcessElementV2__.ts +4 -0
- package/src/concepts/ProcessElement__.ts +4 -0
- package/src/concepts/ProcessV2__.ts +5 -0
- package/src/concepts/Process__.ts +4 -0
- package/src/concepts/QueryOrderByExpression__.ts +0 -1
- package/src/concepts/Structure__.ts +5 -7
- package/src/concepts/SubLogic__.ts +6 -56
- package/src/concepts/Variable__.ts +9 -5
- package/src/concepts/ViewElement__.ts +24 -7
- package/src/concepts/View__.ts +75 -76
- package/src/natural/genNaturalTS.ts +88 -39
- package/src/natural/getContext/getUILib.ts +6 -3
- package/src/natural/getContext/index.ts +125 -23
- package/src/natural/getContext/naslStdlibMap.ts +8 -8
- package/src/natural/tools.ts +107 -2
- package/src/natural/transformTS2UI.ts +190 -137
- package/src/natural/transformTSCode.ts +29 -1055
- package/src/natural/transforms/registerTransform.ts +34 -0
- package/src/natural/transforms/transform2LogicItem.ts +1335 -0
- package/src/natural/transforms/transform2TypeAnnotation.ts +77 -0
- package/src/natural/transforms/utils.ts +25 -0
- package/src/server/getLogics.ts +12 -12
- package/src/server/naslServer.ts +54 -19
- package/src/server/semanticData.ts +447 -0
- package/src/server/translator.ts +5 -0
- package/src/templator/block2nasl/jsx2nasl/index.ts +11 -0
- package/src/templator/block2nasl/jsx2nasl/transform-expression2nasl.ts +238 -0
- package/src/templator/block2nasl/jsx2nasl/transform-func2nasl.ts +241 -0
- package/src/templator/block2nasl/jsx2nasl/transform-tstype2nasl.ts +213 -0
- package/src/templator/block2nasl/jsx2nasl/transform-tsx2nasl.ts +422 -0
- package/src/templator/block2nasl/jsx2nasl/utils.ts +19 -0
- package/src/templator/block2nasl/transformBlock2Nasl.ts +99 -0
- package/src/templator/block2nasl/viewMergeBlock.ts +150 -0
- package/src/translator/types.ts +2 -0
- package/src/translator/utils.ts +2 -2
- package/src/utils/index.ts +0 -4
- package/src/utils/language-cache/constant.ts +1 -0
- package/src/utils/language-cache/nasl.ts +6 -0
- package/test/concepts/logic/__snapshots__/toEmbeddedTS.spec.ts.snap +182 -0
- package/test/concepts/logic/constant.ts +5 -0
- package/test/concepts/logic/fixtures/variable-host-call-logic-member-expression.json +267 -0
- package/test/concepts/logic/fixtures/variable-host-call-logic-nested-member-expression copy.json +457 -0
- package/test/concepts/logic/fixtures/variable-host-call-logic-with-handle-error-member-expression.json +267 -0
- package/test/concepts/logic/toEmbeddedTS.spec.ts +15 -0
- package/test/tdd/dataQuery/QueryFieldExpression/case1/ast.json +7 -0
- package/test/tdd/dataQuery/QueryFieldExpression/case1/natural.ts.txt +1 -0
- package/test/tdd/dataQuery/QueryFieldExpression/case2/ast.json +7 -0
- package/test/tdd/dataQuery/QueryFieldExpression/case2/natural.ts.txt +1 -0
- package/test/tdd/transform/case1/ast.json +243 -0
- package/test/tdd/transform/case1/natural.ts.md +20 -0
- package/test/tdd/transform/case2/ast.json +207 -0
- package/test/tdd/transform/case2/natural.ts.md +12 -0
- package/test/tdd.test.js +10 -0
- package/test/tdd.transform.test.js +38 -0
- package/test/utils.js +40 -0
- package/sandbox-natural/stdlib/nasl.core.ts +0 -36
- package/sandbox-natural/stdlib/nasl.oql.ts +0 -14
- package/sandbox-natural/stdlib/nasl.ui.pre.d.ts +0 -90
- package/sandbox-natural/stdlib/nasl.ui.ts +0 -63
|
@@ -1,1050 +1,29 @@
|
|
|
1
1
|
/* eslint-disable global-require */
|
|
2
2
|
/* eslint-disable @typescript-eslint/no-use-before-define */
|
|
3
3
|
import * as babel from '@babel/core';
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
import * as naslTypes from '../concepts';
|
|
4
|
+
import { babelTypes, naslTypes, generate, DEBUG, throwError, tryParseTS } from './transforms/utils';
|
|
5
|
+
import { transform2LogicItem, transform2Param } from './transforms/transform2LogicItem';
|
|
7
6
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
if (DEBUG) throw new Error(message);
|
|
11
|
-
else console.error(message);
|
|
7
|
+
function isPageLogic(logicName: string) {
|
|
8
|
+
return logicName.includes('on_') || logicName.includes('viewLogic_') || logicName.includes('businessLogic_');
|
|
12
9
|
}
|
|
13
10
|
|
|
14
|
-
export function
|
|
15
|
-
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
// console.log((root as any).program.body[0].body.body.map((item) => item.trailingComments));
|
|
21
|
-
return root;
|
|
22
|
-
} catch (e) {
|
|
23
|
-
return null;
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
function flatMemberExpression(node: babelTypes.Node): Array<babelTypes.Node> {
|
|
28
|
-
if (node.type === 'MemberExpression')
|
|
29
|
-
return [...flatMemberExpression(node.object), node.property];
|
|
30
|
-
return [node];
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
function flatMembers(node: naslTypes.MemberExpression): Array<naslTypes.MemberExpression> {
|
|
34
|
-
if (node.concept === 'MemberExpression')
|
|
35
|
-
return [...flatMembers(((node as any)?.object)), node];
|
|
36
|
-
return [];
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
function flatMatchPatternsExpression(node: any): Array<babelTypes.Node> {
|
|
40
|
-
if (node.concept === 'BinaryExpression') {
|
|
41
|
-
if (node.operator === '||') {
|
|
42
|
-
return [...flatMatchPatternsExpression(node.left), ...flatMatchPatternsExpression(node.right)];
|
|
43
|
-
}
|
|
44
|
-
if (node.operator === '==') {
|
|
45
|
-
return [node.right];
|
|
46
|
-
}
|
|
47
|
-
return [node];
|
|
48
|
-
}
|
|
49
|
-
return [node];
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
function flatIfStatement(node: any): Array<babelTypes.Node> {
|
|
53
|
-
if (node.alternate.type === 'IfStatement') {
|
|
54
|
-
return [...flatIfStatement(node.alternate), node];
|
|
55
|
-
}
|
|
56
|
-
return [node];
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* 这个函数是修复 AI 翻译出错的情况,与正常的 transform 做个区分
|
|
61
|
-
* @param node
|
|
62
|
-
* @returns
|
|
63
|
-
*/
|
|
64
|
-
function fixLogicNode(node: babelTypes.Node): void {
|
|
65
|
-
if (node?.type === 'CallExpression') {
|
|
66
|
-
const calleeName = generate(node.callee as any).code;
|
|
67
|
-
if (calleeName === 'nasl.util.ForEach') {
|
|
68
|
-
const callee = flatMemberExpression(node.callee);
|
|
69
|
-
node.callee = callee[2] as babelTypes.Identifier;
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
export function transformTSCode(tsCode: string, contextLogicName: string, type?: string) {
|
|
76
|
-
const logic = new naslTypes.Logic();
|
|
77
|
-
logic.body = [];
|
|
78
|
-
|
|
79
|
-
function transformTypeAnnotation(node: babelTypes.TSType): naslTypes.TypeAnnotation {
|
|
80
|
-
if (!node) return null;
|
|
81
|
-
if (node.type === 'TSTypeReference') {
|
|
82
|
-
const typeName = (node.typeName as babelTypes.Identifier).name;
|
|
83
|
-
const primitiveType = naslTypes.primitiveTypeList.find((ta) => ta.typeName === typeName);
|
|
84
|
-
if ((node as any)?.typeNamespace) {
|
|
85
|
-
return naslTypes.TypeAnnotation.createReference(typeName, {
|
|
86
|
-
typeNamespace: (node as any).typeNamespace,
|
|
87
|
-
});
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
if (['ValidateResult', 'ValidateEvent'].includes(typeName)) {
|
|
91
|
-
return naslTypes.TypeAnnotation.createReference(typeName, {
|
|
92
|
-
typeNamespace: 'nasl.ui'
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
if (typeName === 'Any' || typeName === 'any') {
|
|
97
|
-
return undefined;
|
|
98
|
-
}
|
|
99
|
-
if (typeName === 'Integer') {
|
|
100
|
-
return naslTypes.TypeAnnotation.createPrimitive('Long');
|
|
101
|
-
}
|
|
102
|
-
if (primitiveType) {
|
|
103
|
-
return naslTypes.TypeAnnotation.createPrimitive(typeName);
|
|
104
|
-
}
|
|
105
|
-
if (['List', 'Map', 'Current'].includes(typeName) && node?.typeParameters) {
|
|
106
|
-
return naslTypes.TypeAnnotation.createGeneric(typeName, {
|
|
107
|
-
typeArguments: node?.typeParameters?.params.map(transformTypeAnnotation),
|
|
108
|
-
});
|
|
109
|
-
}
|
|
110
|
-
if (node?.typeName?.type === 'TSQualifiedName' && node?.typeParameters) {
|
|
111
|
-
const curTypeName = (node?.typeName as any)?.right?.name;
|
|
112
|
-
return naslTypes.TypeAnnotation.createGeneric(curTypeName, {
|
|
113
|
-
typeArguments: node?.typeParameters?.params.map(transformTypeAnnotation),
|
|
114
|
-
});
|
|
115
|
-
}
|
|
116
|
-
if (!node.typeParameters) {
|
|
117
|
-
if (node?.typeName?.type === 'TSQualifiedName') {
|
|
118
|
-
const allTypeName = generate(node?.typeName?.left as any)?.code;
|
|
119
|
-
// 实体、枚举、结构体
|
|
120
|
-
if (/^app.dataSources|.enums|.structures|nasl.ui/.test(allTypeName)) {
|
|
121
|
-
const curTypeName = (node?.typeName as any)?.right?.name;
|
|
122
|
-
return naslTypes.TypeAnnotation.createReference(curTypeName, {
|
|
123
|
-
typeNamespace: allTypeName,
|
|
124
|
-
});
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
return naslTypes.TypeAnnotation.createReference(typeName, {
|
|
128
|
-
typeNamespace: null,
|
|
129
|
-
});
|
|
130
|
-
}
|
|
131
|
-
throwError(`Unhandled node ${node.type}`);
|
|
132
|
-
} else if (node.type === 'TSTypeLiteral') {
|
|
133
|
-
return naslTypes.TypeAnnotation.createTypeAnonymousStructure(node.members.map((member: babelTypes.TSPropertySignature) => new naslTypes.StructureProperty({
|
|
134
|
-
name: (member.key as babelTypes.Identifier).name,
|
|
135
|
-
typeAnnotation: transformTypeAnnotation(member.typeAnnotation.typeAnnotation),
|
|
136
|
-
})));
|
|
137
|
-
} else if (node.type === 'TSBooleanKeyword') {
|
|
138
|
-
return naslTypes.TypeAnnotation.createPrimitive('Boolean');
|
|
139
|
-
} else if (node.type === 'TSStringKeyword') {
|
|
140
|
-
return naslTypes.TypeAnnotation.createPrimitive('String');
|
|
141
|
-
} else if (node.type === 'TSNumberKeyword') {
|
|
142
|
-
return naslTypes.TypeAnnotation.createPrimitive('Double');
|
|
143
|
-
} else if (node.type === 'TSUnionType') {
|
|
144
|
-
const union = naslTypes.TypeAnnotation.createPrimitive('Union');
|
|
145
|
-
union.typeArguments = [];
|
|
146
|
-
node.types.forEach((curType) => {
|
|
147
|
-
union.typeArguments.push(transformTypeAnnotation(curType));
|
|
148
|
-
});
|
|
149
|
-
return union;
|
|
11
|
+
export function transformTSCode(tsCode: string, contextLogicName?: string) {
|
|
12
|
+
if (!contextLogicName) {
|
|
13
|
+
const functionRE = /function\s+(\w+)\s*\(/;
|
|
14
|
+
const cap = tsCode.match(functionRE);
|
|
15
|
+
if (cap) {
|
|
16
|
+
contextLogicName = cap[1];
|
|
150
17
|
} else {
|
|
151
|
-
throwError(
|
|
18
|
+
throwError('无法解析函数名');
|
|
152
19
|
}
|
|
153
20
|
}
|
|
154
|
-
function transformParam(node: babelTypes.Identifier): naslTypes.Param {
|
|
155
|
-
const typeAnnotation = (node.typeAnnotation as babelTypes.TSTypeAnnotation)?.typeAnnotation;
|
|
156
|
-
const exclude = ['event'];
|
|
157
|
-
if (exclude.includes(node.name)) return null;
|
|
158
|
-
return new naslTypes.Param({
|
|
159
|
-
name: node.name,
|
|
160
|
-
typeAnnotation: typeAnnotation ? transformTypeAnnotation(typeAnnotation) : null,
|
|
161
|
-
});
|
|
162
|
-
}
|
|
163
|
-
function pushElseIf(ifNode: babelTypes.IfStatement, switchStatement: naslTypes.SwitchStatement) {
|
|
164
|
-
switchStatement.cases.push(new naslTypes.SwitchCase({
|
|
165
|
-
test: transformLogicNode(ifNode.test),
|
|
166
|
-
consequent: !ifNode.consequent ? [] : (ifNode.consequent as babelTypes.BlockStatement).body.map((item) => transformLogicNode(item)).filter((item) => !!item),
|
|
167
|
-
}));
|
|
168
|
-
if (ifNode.alternate && ifNode.alternate.type === 'IfStatement') {
|
|
169
|
-
pushElseIf(ifNode.alternate, switchStatement);
|
|
170
|
-
} else {
|
|
171
|
-
switchStatement.cases.push(new naslTypes.SwitchCase({
|
|
172
|
-
test: undefined,
|
|
173
|
-
consequent: !ifNode?.alternate ? [] : (ifNode?.alternate as babelTypes.BlockStatement)?.body.map((item) => transformLogicNode(item)).filter((item) => !!item),
|
|
174
|
-
}));
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
function handleBinaryExpression(calleeName: String, argument: any) {
|
|
179
|
-
const map: Record<'plus' | 'minus' | 'multiply' | 'divide' | 'remainder', '+' | '-' | '*' | '/' | '%'> = {
|
|
180
|
-
plus: '+',
|
|
181
|
-
minus: '-',
|
|
182
|
-
multiply: '*',
|
|
183
|
-
divide: '/',
|
|
184
|
-
remainder: '%',
|
|
185
|
-
};
|
|
186
|
-
return new naslTypes.BinaryExpression({
|
|
187
|
-
operator: map?.[calleeName as keyof typeof map],
|
|
188
|
-
left: transformLogicNode(argument?.[0]),
|
|
189
|
-
right: transformLogicNode(argument?.[1]),
|
|
190
|
-
});
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
function handleNewExpression(calleeName: String, node: any) {
|
|
194
|
-
if (calleeName === 'NewList') {
|
|
195
|
-
return new naslTypes.NewList({
|
|
196
|
-
typeAnnotation: naslTypes.TypeAnnotation.createGeneric('List', {
|
|
197
|
-
typeArguments: node?.typeParameters?.params.map(transformTypeAnnotation),
|
|
198
|
-
}),
|
|
199
|
-
items: node?.arguments?.[0]?.elements?.map((arg: any) => transformLogicNode(arg)),
|
|
200
|
-
});
|
|
201
|
-
}
|
|
202
|
-
if (calleeName === 'NewMap') {
|
|
203
|
-
const keys: any = [];
|
|
204
|
-
const values: any = [];
|
|
205
|
-
node?.arguments?.[0]?.properties.forEach((arg: any) => {
|
|
206
|
-
keys.push(transformLogicNode(arg.key));
|
|
207
|
-
values.push(transformLogicNode(arg.value));
|
|
208
|
-
});
|
|
209
|
-
return new naslTypes.NewMap({
|
|
210
|
-
typeAnnotation: naslTypes.TypeAnnotation.createGeneric('Map', {
|
|
211
|
-
typeArguments: node?.typeParameters?.params.map(transformTypeAnnotation),
|
|
212
|
-
}),
|
|
213
|
-
keys,
|
|
214
|
-
values,
|
|
215
|
-
});
|
|
216
|
-
}
|
|
217
|
-
if (calleeName === 'NewEntity') {
|
|
218
|
-
const properties: any = [];
|
|
219
|
-
const rights: any = [];
|
|
220
|
-
const assignmentLines: any = [];
|
|
221
|
-
node?.arguments?.[0]?.properties.forEach((arg: any, index: number) => {
|
|
222
|
-
properties.push(transformLogicNode(arg.key));
|
|
223
|
-
if (arg.value.type === 'MemberExpression') {
|
|
224
|
-
const expression = flatMemberExpression(arg.value);
|
|
225
|
-
const members = flatMembers(transformLogicNode(arg.value) as any);
|
|
226
|
-
rights.push(new naslTypes.SelectMembers({
|
|
227
|
-
expression: transformLogicNode(expression[0]),
|
|
228
|
-
members,
|
|
229
|
-
}));
|
|
230
|
-
if (arg?.value) {
|
|
231
|
-
assignmentLines.push(new naslTypes.AssignmentLine({
|
|
232
|
-
leftIndex: [0, index],
|
|
233
|
-
rightIndex: [index, members.length - 1],
|
|
234
|
-
}));
|
|
235
|
-
}
|
|
236
|
-
} else {
|
|
237
|
-
rights.push(new naslTypes.SelectMembers({
|
|
238
|
-
expression: transformLogicNode(arg.value)
|
|
239
|
-
}));
|
|
240
|
-
if (arg?.value) {
|
|
241
|
-
assignmentLines.push(new naslTypes.AssignmentLine({
|
|
242
|
-
leftIndex: [0, index],
|
|
243
|
-
rightIndex: [index],
|
|
244
|
-
}));
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
});
|
|
248
|
-
const parameters = node?.typeParameters?.params?.[0] || {};
|
|
249
|
-
if (parameters && parameters?.type === 'TSTypeReference' && parameters?.typeName?.type === 'TSQualifiedName') {
|
|
250
|
-
const { left, right } = parameters?.typeName || {};
|
|
251
|
-
const typeNamespace = generate(left as any)?.code;
|
|
252
|
-
const typeName = right?.name;
|
|
253
|
-
return new naslTypes.NewComposite({
|
|
254
|
-
typeAnnotation: naslTypes.TypeAnnotation.createReference(typeName, {
|
|
255
|
-
typeNamespace,
|
|
256
|
-
typeName
|
|
257
|
-
}),
|
|
258
|
-
properties,
|
|
259
|
-
rights,
|
|
260
|
-
assignmentLines
|
|
261
|
-
});
|
|
262
|
-
}
|
|
263
|
-
const typeName = parameters?.typeName?.name;
|
|
264
|
-
return new naslTypes.NewComposite({
|
|
265
|
-
typeAnnotation: naslTypes.TypeAnnotation.createReference(typeName, {
|
|
266
|
-
typeNamespace: 'app.dataSources.defaultDS.entities',
|
|
267
|
-
typeName
|
|
268
|
-
}),
|
|
269
|
-
properties,
|
|
270
|
-
rights,
|
|
271
|
-
assignmentLines
|
|
272
|
-
});
|
|
273
|
-
}
|
|
274
|
-
if (calleeName === 'NewStructure') {
|
|
275
|
-
const properties: any = [];
|
|
276
|
-
const rights: any = [];
|
|
277
|
-
const assignmentLines: any = [];
|
|
278
|
-
node?.arguments?.[0]?.properties.forEach((arg: any, index: number) => {
|
|
279
|
-
properties.push(transformLogicNode(arg.key));
|
|
280
|
-
if (arg.value.type === 'MemberExpression') {
|
|
281
|
-
const expression = flatMemberExpression(arg.value);
|
|
282
|
-
const members = flatMembers(transformLogicNode(arg.value) as any);
|
|
283
|
-
rights.push(new naslTypes.SelectMembers({
|
|
284
|
-
expression: transformLogicNode(expression[0]),
|
|
285
|
-
members,
|
|
286
|
-
}));
|
|
287
|
-
if (arg?.value) {
|
|
288
|
-
assignmentLines.push(new naslTypes.AssignmentLine({
|
|
289
|
-
leftIndex: [0, index],
|
|
290
|
-
rightIndex: [index, members.length - 1],
|
|
291
|
-
}));
|
|
292
|
-
}
|
|
293
|
-
} else {
|
|
294
|
-
rights.push(new naslTypes.SelectMembers({
|
|
295
|
-
expression: transformLogicNode(arg.value)
|
|
296
|
-
}));
|
|
297
|
-
if (arg?.value) {
|
|
298
|
-
assignmentLines.push(new naslTypes.AssignmentLine({
|
|
299
|
-
leftIndex: [0, index],
|
|
300
|
-
rightIndex: [index],
|
|
301
|
-
}));
|
|
302
|
-
}
|
|
303
|
-
}
|
|
304
|
-
});
|
|
305
|
-
const parameters = node?.typeParameters?.params?.[0] || {};
|
|
306
|
-
if (parameters && parameters?.type === 'TSTypeReference' && parameters?.typeName?.type === 'TSQualifiedName') {
|
|
307
|
-
const { left, right } = parameters?.typeName || {};
|
|
308
|
-
const typeNamespace = generate(left as any)?.code;
|
|
309
|
-
const typeName = right?.name;
|
|
310
|
-
return new naslTypes.NewComposite({
|
|
311
|
-
typeAnnotation: naslTypes.TypeAnnotation.createReference(typeName, {
|
|
312
|
-
typeNamespace,
|
|
313
|
-
typeName
|
|
314
|
-
}),
|
|
315
|
-
properties,
|
|
316
|
-
rights,
|
|
317
|
-
assignmentLines
|
|
318
|
-
});
|
|
319
|
-
}
|
|
320
|
-
const typeName = parameters?.typeName?.name;
|
|
321
|
-
return new naslTypes.NewComposite({
|
|
322
|
-
typeAnnotation: naslTypes.TypeAnnotation.createReference(typeName, {
|
|
323
|
-
typeNamespace: 'app.structures',
|
|
324
|
-
typeName
|
|
325
|
-
}),
|
|
326
|
-
properties,
|
|
327
|
-
rights,
|
|
328
|
-
assignmentLines
|
|
329
|
-
});
|
|
330
|
-
}
|
|
331
|
-
if (calleeName === 'NewAnonymousStructure') {
|
|
332
|
-
const properties: any = [];
|
|
333
|
-
const rights: any = [];
|
|
334
|
-
const structureProperties: any = [];
|
|
335
|
-
const assignmentLines: any = [];
|
|
336
|
-
node?.arguments?.[0]?.properties?.forEach((arg: any, index: number) => {
|
|
337
|
-
properties.push(transformLogicNode(arg.key));
|
|
338
|
-
if (arg.value.type === 'MemberExpression') {
|
|
339
|
-
const expression = flatMemberExpression(arg.value);
|
|
340
|
-
const members = flatMembers(transformLogicNode(arg.value) as any);
|
|
341
|
-
rights.push(new naslTypes.SelectMembers({
|
|
342
|
-
expression: transformLogicNode(expression[0]),
|
|
343
|
-
members,
|
|
344
|
-
}));
|
|
345
|
-
if (arg?.value) {
|
|
346
|
-
assignmentLines.push(new naslTypes.AssignmentLine({
|
|
347
|
-
leftIndex: [0, index],
|
|
348
|
-
rightIndex: [index, members.length - 1],
|
|
349
|
-
}));
|
|
350
|
-
}
|
|
351
|
-
} else {
|
|
352
|
-
rights.push(new naslTypes.SelectMembers({
|
|
353
|
-
expression: transformLogicNode(arg.value)
|
|
354
|
-
}));
|
|
355
|
-
if (arg?.value) {
|
|
356
|
-
assignmentLines.push(new naslTypes.AssignmentLine({
|
|
357
|
-
leftIndex: [0, index],
|
|
358
|
-
rightIndex: [index],
|
|
359
|
-
}));
|
|
360
|
-
}
|
|
361
|
-
}
|
|
362
|
-
structureProperties.push(new naslTypes.StructureProperty({
|
|
363
|
-
name: arg?.key?.name,
|
|
364
|
-
typeAnnotation: null
|
|
365
|
-
}));
|
|
366
|
-
});
|
|
367
|
-
return new naslTypes.NewComposite({
|
|
368
|
-
typeAnnotation: naslTypes.TypeAnnotation.createTypeAnonymousStructure(structureProperties),
|
|
369
|
-
properties,
|
|
370
|
-
rights,
|
|
371
|
-
assignmentLines
|
|
372
|
-
});
|
|
373
|
-
}
|
|
374
|
-
return node;
|
|
375
|
-
}
|
|
376
|
-
|
|
377
|
-
function isPageLogic(logicName: string) {
|
|
378
|
-
return logicName.includes('on_') || logicName.includes('viewLogic_') || logicName.includes('businessLogic_');
|
|
379
|
-
}
|
|
380
21
|
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
let newNode;
|
|
387
|
-
declList?.forEach((decl) => {
|
|
388
|
-
const variableName = (decl.id as babelTypes.Identifier).name;
|
|
389
|
-
if (!logic.variables.find((variable) => variable.name === variableName)) {
|
|
390
|
-
const varTypeAnnotation = ((decl.id as babelTypes.Identifier).typeAnnotation as babelTypes.TSTypeAnnotation)?.typeAnnotation;
|
|
391
|
-
logic.variables.push(new naslTypes.Variable({
|
|
392
|
-
name: variableName,
|
|
393
|
-
typeAnnotation: varTypeAnnotation && transformTypeAnnotation(varTypeAnnotation),
|
|
394
|
-
}));
|
|
395
|
-
}
|
|
396
|
-
if (decl.init) {
|
|
397
|
-
if (decl.init.type !== 'ArrayExpression' && decl.init.type !== 'ObjectExpression') {
|
|
398
|
-
const calleeName = generate((decl?.init as any)?.callee).code;
|
|
399
|
-
if (type === 'expression') {
|
|
400
|
-
return transformLogicNode(decl.init);
|
|
401
|
-
}
|
|
402
|
-
const varTypeAnnotation = ((decl?.id as babelTypes.Identifier)?.typeAnnotation as babelTypes.TSTypeAnnotation)?.typeAnnotation;
|
|
403
|
-
if (calleeName === 'nasl.oql.query' && varTypeAnnotation) {
|
|
404
|
-
(decl.init as any).typeAnnotations = varTypeAnnotation;
|
|
405
|
-
}
|
|
406
|
-
newNode = new naslTypes.Assignment({
|
|
407
|
-
left: new naslTypes.Identifier({
|
|
408
|
-
name: variableName,
|
|
409
|
-
}),
|
|
410
|
-
right: transformLogicNode(decl.init),
|
|
411
|
-
});
|
|
412
|
-
}
|
|
413
|
-
}
|
|
414
|
-
});
|
|
415
|
-
if (newNode) return newNode;
|
|
416
|
-
} else if (node.type === 'CallExpression' && node.callee.type === 'CallExpression') {
|
|
417
|
-
// (function match() {})(...) 的情况
|
|
418
|
-
} else if (node.type === 'CallExpression') {
|
|
419
|
-
const callee = flatMemberExpression(node.callee);
|
|
420
|
-
const calleeName = generate(node.callee as any).code;
|
|
421
|
-
// console.log('lemon ~~~ node--2', node, callee, calleeName);
|
|
422
|
-
if (/^\$refs\./.test(calleeName)) {
|
|
423
|
-
return new naslTypes.CallLogic({
|
|
424
|
-
calleeNamespace: `elements.${(callee[1] as babelTypes.Identifier).name}.logics`,
|
|
425
|
-
calleeName: (callee[2] as babelTypes.Identifier).name,
|
|
426
|
-
arguments: node?.arguments?.map((arg) => new naslTypes.Argument({
|
|
427
|
-
expression: transformLogicNode(arg),
|
|
428
|
-
})) as naslTypes.Argument[],
|
|
429
|
-
});
|
|
430
|
-
}
|
|
431
|
-
if (callee.length === 2 && callee[0].type === 'Identifier' && callee[0].name.endsWith('Entity')) {
|
|
432
|
-
const entityName = callee[0].name.replace(/Entity$/, '');
|
|
433
|
-
return new naslTypes.CallLogic({
|
|
434
|
-
calleeNamespace: `app.dataSources.defaultDS.entities.${entityName}.logics`,
|
|
435
|
-
calleeName: (callee[1] as babelTypes.Identifier).name,
|
|
436
|
-
arguments: node?.arguments?.map((arg) => new naslTypes.Argument({
|
|
437
|
-
expression: transformLogicNode(arg),
|
|
438
|
-
})) as naslTypes.Argument[],
|
|
439
|
-
});
|
|
440
|
-
}
|
|
441
|
-
if (calleeName === 'nasl.oql.query') {
|
|
442
|
-
if (!isPageLogic(contextLogicName)) {
|
|
443
|
-
// const _typeAnnotation = node?.typeParameters?.params?.length > 0 ? naslTypes.TypeAnnotation.createGeneric('List', {
|
|
444
|
-
// typeArguments: node?.typeParameters?.params.map((curType: any) => transformTypeAnnotation({ ...curType })),
|
|
445
|
-
// }) : null;
|
|
446
|
-
let code = generate(node.arguments[0] as any).code.trim().slice(1, -1);
|
|
447
|
-
code = code?.replace(/app\.enums\.(\w+)\[(\d+)\]/g, "app.enums.$1.$2").replace(/app\.enums\.(\w+)\['(\w+)'\]/g, "app.enums.$1.$2");
|
|
448
|
-
return new naslTypes.OqlQueryComponent({
|
|
449
|
-
dataSource: 'app.dataSources.defaultDS',
|
|
450
|
-
code,
|
|
451
|
-
typeAnnotation: transformTypeAnnotation((node as any)?.typeAnnotations)
|
|
452
|
-
});
|
|
453
|
-
}
|
|
454
|
-
}
|
|
455
|
-
if (calleeName === 'nasl.util.consoleLog' || calleeName === 'console.log') {
|
|
456
|
-
if (isPageLogic(contextLogicName)) {
|
|
457
|
-
return new naslTypes.CallLogic({
|
|
458
|
-
calleeNamespace: 'nasl.util',
|
|
459
|
-
calleeName: 'consoleLog',
|
|
460
|
-
shortcut: true,
|
|
461
|
-
arguments: node?.arguments?.map((arg) => new naslTypes.Argument({
|
|
462
|
-
expression: transformLogicNode(arg),
|
|
463
|
-
})) as naslTypes.Argument[],
|
|
464
|
-
});
|
|
465
|
-
}
|
|
466
|
-
return new naslTypes.CallLogic({
|
|
467
|
-
calleeNamespace: 'nasl.logging',
|
|
468
|
-
calleeName: 'INFO',
|
|
469
|
-
shortcut: true,
|
|
470
|
-
arguments: node?.arguments?.map((arg) => new naslTypes.Argument({
|
|
471
|
-
expression: transformLogicNode(arg),
|
|
472
|
-
})) as naslTypes.Argument[],
|
|
473
|
-
});
|
|
474
|
-
}
|
|
475
|
-
if (calleeName.startsWith('nasl.logging.')) {
|
|
476
|
-
return new naslTypes.CallLogic({
|
|
477
|
-
calleeNamespace: 'nasl.logging',
|
|
478
|
-
calleeName: (callee[2] as babelTypes.Identifier).name,
|
|
479
|
-
shortcut: true,
|
|
480
|
-
arguments: node.arguments.map((arg) => new naslTypes.Argument({
|
|
481
|
-
expression: transformLogicNode(arg),
|
|
482
|
-
})) as naslTypes.Argument[],
|
|
483
|
-
});
|
|
484
|
-
}
|
|
485
|
-
if (calleeName === 'nasl.util.jsonSerialize' || calleeName === 'nasl.util.jsonDeserialize') {
|
|
486
|
-
const curCalleeName = (node.callee as any).property.name;
|
|
487
|
-
return new naslTypes.CallLogic({
|
|
488
|
-
calleeNamespace: 'nasl.util',
|
|
489
|
-
calleeName: curCalleeName,
|
|
490
|
-
shortcut: true,
|
|
491
|
-
arguments: node?.arguments?.map((arg) => new naslTypes.Argument({
|
|
492
|
-
expression: transformLogicNode(arg),
|
|
493
|
-
})) as naslTypes.Argument[],
|
|
494
|
-
typeArguments: node?.typeParameters?.params.map((curType: any) => transformTypeAnnotation({ ...curType })),
|
|
495
|
-
});
|
|
496
|
-
};
|
|
497
|
-
if (calleeName.startsWith('nasl.util.')) {
|
|
498
|
-
let curCalleeName: string;
|
|
499
|
-
if (callee.length === 3)
|
|
500
|
-
curCalleeName = (callee[callee.length - 1] as babelTypes.Identifier).name;
|
|
501
|
-
else {
|
|
502
|
-
let _callee = (callee[0] as any).callee;
|
|
503
|
-
while (_callee.type === 'CallExpression')
|
|
504
|
-
_callee = _callee.callee;
|
|
505
|
-
curCalleeName = _callee.property.name;
|
|
506
|
-
}
|
|
507
|
-
const argument = callee.length === 3 ? node.arguments : (callee[0] as any).arguments as any;
|
|
508
|
-
|
|
509
|
-
if (['plus', 'minus', 'multiply', 'divide', 'remainder'].includes(curCalleeName)) {
|
|
510
|
-
return handleBinaryExpression(curCalleeName, argument);
|
|
511
|
-
};
|
|
512
|
-
|
|
513
|
-
if (['NewList', 'NewMap', 'NewEntity', 'NewStructure', 'NewAnonymousStructure'].includes(curCalleeName)) {
|
|
514
|
-
return handleNewExpression(curCalleeName, node);
|
|
515
|
-
}
|
|
516
|
-
|
|
517
|
-
// let typeNamespace: string;
|
|
518
|
-
// if (argument?.[argument.length - 1]?.type === 'ObjectExpression') {
|
|
519
|
-
// typeNamespace = argument.splice(-1)[0].properties[0].value.value;
|
|
520
|
-
// }
|
|
521
|
-
const nodeElement = new naslTypes.CallFunction({
|
|
522
|
-
calleeNamespace: 'nasl.util',
|
|
523
|
-
calleeName: curCalleeName,
|
|
524
|
-
arguments: argument.map((arg: any, index: number) => new naslTypes.Argument({
|
|
525
|
-
expression: transformLogicNode(arg),
|
|
526
|
-
keyword: `str${index + 1}`,
|
|
527
|
-
})) as naslTypes.Argument[],
|
|
528
|
-
typeArguments: node?.typeParameters?.params.map(transformTypeAnnotation),
|
|
529
|
-
// typeArguments: node?.typeParameters?.params.map((curType: any) => transformTypeAnnotation({ ...curType, typeNamespace })),
|
|
530
|
-
});
|
|
531
|
-
// console.log('lemon ~ callFunction ~ 3:', nodeElement);
|
|
532
|
-
return nodeElement;
|
|
533
|
-
}
|
|
534
|
-
if (callee.length === 3 && calleeName.startsWith('nasl.ui.')) {
|
|
535
|
-
if ((callee[2] as babelTypes.Identifier).name === 'destination') {
|
|
536
|
-
const namespace = (node.arguments[0] as babelTypes.StringLiteral).value;
|
|
537
|
-
const nsArr = namespace.split('.').map((item) => item.replace(/_view$/, ''));
|
|
538
|
-
const pageType = nsArr.shift();
|
|
539
|
-
const viewName = nsArr.pop();
|
|
540
|
-
return new naslTypes.Destination({
|
|
541
|
-
viewNamespace: `app.frontendTypes.${pageType}.frontends.${pageType}.views${nsArr?.map((item) => `.${item}.views`)?.join('')}`, // viewNamespace
|
|
542
|
-
viewName, // viewName
|
|
543
|
-
|
|
544
|
-
arguments: node.arguments.slice(1).map((arg) => new naslTypes.Argument({
|
|
545
|
-
expression: transformLogicNode(arg),
|
|
546
|
-
})) as naslTypes.Argument[],
|
|
547
|
-
});
|
|
548
|
-
}
|
|
549
|
-
return new naslTypes.CallLogic({
|
|
550
|
-
calleeNamespace: 'nasl.ui',
|
|
551
|
-
calleeName: (callee[2] as babelTypes.Identifier).name,
|
|
552
|
-
shortcut: true,
|
|
553
|
-
/**
|
|
554
|
-
* 这个函数是修复 AI 翻译出错的情况,与正常的 transform 做个区分
|
|
555
|
-
* @param node
|
|
556
|
-
* @returns
|
|
557
|
-
*/
|
|
558
|
-
arguments: node?.arguments?.map((arg) => new naslTypes.Argument({
|
|
559
|
-
expression: transformLogicNode(arg),
|
|
560
|
-
})) as naslTypes.Argument[],
|
|
561
|
-
});
|
|
562
|
-
}
|
|
563
|
-
if (calleeName.endsWith('.map')) {
|
|
564
|
-
return new naslTypes.CallFunction({
|
|
565
|
-
calleeNamespace: 'nasl.util',
|
|
566
|
-
calleeName: 'ListTransform',
|
|
567
|
-
arguments: [new naslTypes.Argument({
|
|
568
|
-
expression: transformLogicNode((node.callee as babelTypes.MemberExpression).object),
|
|
569
|
-
}), ...node?.arguments?.map((arg: any) => new naslTypes.Argument({
|
|
570
|
-
expression: transformLogicNode(arg),
|
|
571
|
-
}))],
|
|
572
|
-
});
|
|
573
|
-
}
|
|
574
|
-
if (calleeName === 'PAGINATE') {
|
|
575
|
-
const newCallee = node?.arguments || [];
|
|
576
|
-
return new naslTypes.Paginate({
|
|
577
|
-
list: newCallee?.[0] && transformLogicNode(newCallee?.[0]),
|
|
578
|
-
page: newCallee?.[1] && transformLogicNode(newCallee?.[1]),
|
|
579
|
-
size: newCallee?.[2] && transformLogicNode(newCallee?.[2]),
|
|
580
|
-
});
|
|
581
|
-
}
|
|
582
|
-
if (calleeName === 'ForEach') {
|
|
583
|
-
const newCallee = node.arguments;
|
|
584
|
-
const arrowFunction = node.arguments[3] as babelTypes.ArrowFunctionExpression;
|
|
585
|
-
const nodeElement = new naslTypes.ForEachStatement({
|
|
586
|
-
each: transformLogicNode(newCallee[0]),
|
|
587
|
-
start: transformLogicNode(newCallee[1]),
|
|
588
|
-
end: transformLogicNode(newCallee[2]),
|
|
589
|
-
item: transformParam(arrowFunction.params[0] as babelTypes.Identifier),
|
|
590
|
-
index: arrowFunction.params[1] ? transformParam(arrowFunction.params[1] as babelTypes.Identifier) : new naslTypes.Param({
|
|
591
|
-
name: 'index', typeAnnotation: naslTypes.TypeAnnotation.createPrimitive('Long'),
|
|
592
|
-
}),
|
|
593
|
-
body: (arrowFunction.body as babelTypes.BlockStatement).body.map((item) => transformLogicNode(item)).filter((item) => !!item),
|
|
594
|
-
});
|
|
595
|
-
return nodeElement;
|
|
596
|
-
}
|
|
597
|
-
if (callee.length === 2) {
|
|
598
|
-
const curCalleeName = (node.callee as any).property.name;
|
|
599
|
-
if (curCalleeName === 'forEach') {
|
|
600
|
-
const newCallee = (node.callee as any).object;
|
|
601
|
-
const arrowFunction = node.arguments[0] as babelTypes.ArrowFunctionExpression;
|
|
602
|
-
const nodeElement = new naslTypes.ForEachStatement({
|
|
603
|
-
each: transformLogicNode(newCallee.callee.object),
|
|
604
|
-
start: transformLogicNode(newCallee?.arguments?.[0]),
|
|
605
|
-
end: transformLogicNode(newCallee?.arguments?.[1]),
|
|
606
|
-
item: transformParam(arrowFunction.params[0] as babelTypes.Identifier),
|
|
607
|
-
index: arrowFunction.params[1] ? transformParam(arrowFunction.params[1] as babelTypes.Identifier) : new naslTypes.Param({
|
|
608
|
-
name: 'index', typeAnnotation: naslTypes.TypeAnnotation.createPrimitive('Long'),
|
|
609
|
-
}),
|
|
610
|
-
body: (arrowFunction.body as babelTypes.BlockStatement).body.map((item) => transformLogicNode(item)).filter((item) => !!item),
|
|
611
|
-
});
|
|
612
|
-
return nodeElement;
|
|
613
|
-
}
|
|
614
|
-
} else if (['plus', 'minus', 'multiply', 'divide', 'remainder'].includes(calleeName)) {
|
|
615
|
-
return handleBinaryExpression(calleeName, node?.arguments);
|
|
616
|
-
} else if (calleeName === 'alert') {
|
|
617
|
-
return new naslTypes.CallLogic({
|
|
618
|
-
calleeNamespace: 'nasl.ui',
|
|
619
|
-
calleeName: 'showMessage',
|
|
620
|
-
shortcut: true,
|
|
621
|
-
arguments: node?.arguments?.map((arg) => new naslTypes.Argument({
|
|
622
|
-
expression: transformLogicNode(arg),
|
|
623
|
-
})) as naslTypes.Argument[],
|
|
624
|
-
});
|
|
625
|
-
} else if (/^app.dataSources|app.logics/.test(calleeName)) {
|
|
626
|
-
if (calleeName?.includes('.entities.')) {
|
|
627
|
-
const calleeNamespace = generate((node?.callee as any)?.object?.object)?.code;
|
|
628
|
-
const entityName = (node?.callee as any)?.object?.property?.name?.replace(/Entity$/, '');
|
|
629
|
-
return new naslTypes.CallLogic({
|
|
630
|
-
calleeNamespace: `${calleeNamespace}.${entityName}.logics`,
|
|
631
|
-
calleeName: (node?.callee as any)?.property?.name,
|
|
632
|
-
arguments: node?.arguments?.map((arg) => new naslTypes.Argument({
|
|
633
|
-
expression: transformLogicNode(arg),
|
|
634
|
-
})) as naslTypes.Argument[],
|
|
635
|
-
});
|
|
636
|
-
}
|
|
637
|
-
|
|
638
|
-
return new naslTypes.CallLogic({
|
|
639
|
-
calleeNamespace: generate((node.callee as any).object).code,
|
|
640
|
-
calleeName: (callee[callee.length - 1] as babelTypes.Identifier).name,
|
|
641
|
-
arguments: node?.arguments?.map((arg) => new naslTypes.Argument({
|
|
642
|
-
expression: transformLogicNode(arg),
|
|
643
|
-
})) as naslTypes.Argument[],
|
|
644
|
-
});
|
|
645
|
-
} else if (callee.length === 1) {
|
|
646
|
-
if (calleeName.startsWith('viewLogic_')) {
|
|
647
|
-
return new naslTypes.CallLogic({
|
|
648
|
-
calleeNamespace: '',
|
|
649
|
-
calleeName: calleeName.slice('viewLogic_'.length),
|
|
650
|
-
arguments: node?.arguments?.map((arg) => new naslTypes.Argument({
|
|
651
|
-
expression: transformLogicNode(arg),
|
|
652
|
-
})) as naslTypes.Argument[],
|
|
653
|
-
});
|
|
654
|
-
}
|
|
655
|
-
if (calleeName === 'FROM') {
|
|
656
|
-
// TODO
|
|
657
|
-
return new naslTypes.CallLogic({
|
|
658
|
-
calleeNamespace: `app.logics`,
|
|
659
|
-
calleeName,
|
|
660
|
-
arguments: [],
|
|
661
|
-
});
|
|
662
|
-
}
|
|
663
|
-
return new naslTypes.CallLogic({
|
|
664
|
-
calleeNamespace: `app.logics`,
|
|
665
|
-
calleeName,
|
|
666
|
-
arguments: node?.arguments?.map((arg) => new naslTypes.Argument({
|
|
667
|
-
expression: transformLogicNode(arg),
|
|
668
|
-
})) as naslTypes.Argument[],
|
|
669
|
-
});
|
|
670
|
-
} else if (/interfaces/.test(calleeName)) {
|
|
671
|
-
return new naslTypes.CallInterface({
|
|
672
|
-
calleeNamespace: generate((node.callee as any).object).code,
|
|
673
|
-
calleeName: (callee[callee.length - 1] as babelTypes.Identifier).name,
|
|
674
|
-
arguments: node?.arguments?.map((arg: any) => arg?.properties?.map((item: any) => new naslTypes.Argument({
|
|
675
|
-
keyword: item?.key?.name,
|
|
676
|
-
expression: transformLogicNode(item?.value),
|
|
677
|
-
}))).flat() as naslTypes.Argument[],
|
|
678
|
-
});
|
|
679
|
-
} else if (/connector./.test(calleeName)) {
|
|
680
|
-
let flatCalleeKey = generate(node?.callee as any)?.code;
|
|
681
|
-
if (flatCalleeKey?.startsWith('connector.')) {
|
|
682
|
-
flatCalleeKey = flatCalleeKey?.replace('connector.', '');
|
|
683
|
-
}
|
|
684
|
-
const [, calleeConnectionName, calleeNamespace, calleeFunName] = flatCalleeKey?.match(/^(\w+)\.([\w.]+)\.(\w+)$/);
|
|
685
|
-
return new naslTypes.CallConnector({
|
|
686
|
-
calleeConnectionName,
|
|
687
|
-
calleeNamespace,
|
|
688
|
-
calleeName: calleeFunName,
|
|
689
|
-
arguments: node?.arguments?.map((arg) => new naslTypes.Argument({
|
|
690
|
-
expression: transformLogicNode(arg),
|
|
691
|
-
})) as naslTypes.Argument[],
|
|
692
|
-
});
|
|
693
|
-
} else if (/extensions./.test(calleeName)) {
|
|
694
|
-
return new naslTypes.CallLogic({
|
|
695
|
-
calleeNamespace: generate((node?.callee as any)?.object)?.code,
|
|
696
|
-
calleeName: (callee?.[callee?.length - 1] as babelTypes.Identifier)?.name,
|
|
697
|
-
handleError: true,
|
|
698
|
-
arguments: node?.arguments?.map((arg) => new naslTypes.Argument({
|
|
699
|
-
expression: transformLogicNode(arg),
|
|
700
|
-
})) as naslTypes.Argument[],
|
|
701
|
-
});
|
|
702
|
-
}
|
|
703
|
-
// else if (/destination/.test(calleeName)) {
|
|
704
|
-
// const newCallee = flatMemberExpression((node.callee as any).object);
|
|
705
|
-
// return new naslTypes.Destination({
|
|
706
|
-
// viewNamespace: generate((node.callee as any).object.object).code,
|
|
707
|
-
// viewName: (newCallee[newCallee.length - 1] as babelTypes.Identifier).name,
|
|
708
|
-
// target: (node.arguments[0] as any).properties[1].value.value,
|
|
709
|
-
// anchor: new naslTypes.Anchor({
|
|
710
|
-
// expression: transformLogicNode((node.arguments[0] as any).properties[0].value),
|
|
711
|
-
// }),
|
|
712
|
-
// });
|
|
713
|
-
// }
|
|
714
|
-
else if (node.callee.type === 'ArrowFunctionExpression') {
|
|
715
|
-
// NOTE: 适用于 if else
|
|
716
|
-
const newCallee = (node.callee.body as any).body;
|
|
717
|
-
if (newCallee[newCallee.length - 1].argument?.callee?.name === '__MatchExpressionFuntion') {
|
|
718
|
-
const match = new naslTypes.Match({
|
|
719
|
-
expression: transformLogicNode(newCallee[0]),
|
|
720
|
-
isExpression: type === 'expression',
|
|
721
|
-
cases: flatIfStatement(newCallee[1].body.body[0]).map((_case: any) => new naslTypes.MatchCase({
|
|
722
|
-
patterns: !_case.test ? [] : flatMatchPatternsExpression(transformLogicNode(_case.test)) as any,
|
|
723
|
-
body: _case.consequent.body.filter((item: any) => item.type !== 'BreakStatement').map((item: any) => transformLogicNode(item)).filter((item: any) => !!item),
|
|
724
|
-
})),
|
|
725
|
-
});
|
|
726
|
-
if (!match.cases.length || match.cases[match.cases.length - 1].patterns.length) {
|
|
727
|
-
match.cases.push(new naslTypes.MatchCase({
|
|
728
|
-
patterns: [],
|
|
729
|
-
body: [],
|
|
730
|
-
}));
|
|
731
|
-
}
|
|
732
|
-
match.cases.forEach((case_: any) => {
|
|
733
|
-
if (case_.body.length && (case_.body[0].concept === 'BooleanLiteral' || case_.body[0].concept === 'StringLiteral')) {
|
|
734
|
-
case_.isMatchedTypeEnumable = true;
|
|
735
|
-
}
|
|
736
|
-
if (case_.patterns.length && case_.patterns[0].concept === 'MemberExpression') {
|
|
737
|
-
case_.isMatchedTypeEnumable = true;
|
|
738
|
-
case_.patterns[0] = new naslTypes.MemberExpression({
|
|
739
|
-
object: {
|
|
740
|
-
concept: 'Identifier',
|
|
741
|
-
name: case_.patterns[0].object.property.name,
|
|
742
|
-
folded: false,
|
|
743
|
-
namespace: 'app.enums'
|
|
744
|
-
} as any,
|
|
745
|
-
property: {
|
|
746
|
-
concept: 'Identifier',
|
|
747
|
-
name: case_.patterns[0].property.value,
|
|
748
|
-
folded: false,
|
|
749
|
-
} as any,
|
|
750
|
-
});
|
|
751
|
-
}
|
|
752
|
-
});
|
|
753
|
-
return match;
|
|
754
|
-
}
|
|
755
|
-
if (newCallee[newCallee.length - 1].argument?.callee?.name === '__SwitchExpressionFuntion') {
|
|
756
|
-
const switchStatement = transformLogicNode(newCallee[0].body.body[0]);
|
|
757
|
-
return switchStatement;
|
|
758
|
-
}
|
|
759
|
-
throwError(`Unhandled ArrowFunctionExpression node ${node.type}`);
|
|
760
|
-
} else {
|
|
761
|
-
throwError(`Unhandled node ${callee.map((item: babelTypes.Identifier) => item.name).join('.')} ${node.type}`);
|
|
762
|
-
}
|
|
763
|
-
} else if (node.type === 'IfStatement') {
|
|
764
|
-
if (node.alternate && node.alternate.type === 'IfStatement') {
|
|
765
|
-
const switchStatement = new naslTypes.SwitchStatement({
|
|
766
|
-
cases: [],
|
|
767
|
-
});
|
|
768
|
-
pushElseIf(node, switchStatement);
|
|
769
|
-
return switchStatement;
|
|
770
|
-
}
|
|
771
|
-
let consequentIndex: number;
|
|
772
|
-
let alternateIndex: number;
|
|
773
|
-
const naslNode = new naslTypes.IfStatement({
|
|
774
|
-
test: transformLogicNode(node.test),
|
|
775
|
-
consequent: !node.consequent ? [] : (node.consequent as babelTypes.BlockStatement).body.map((item: any, index: number) => {
|
|
776
|
-
if (item?.type === 'ReturnStatement') {
|
|
777
|
-
item.ends = true;
|
|
778
|
-
if (item?.argument) {
|
|
779
|
-
consequentIndex = index;
|
|
780
|
-
}
|
|
781
|
-
}
|
|
782
|
-
return transformLogicNode(item);
|
|
783
|
-
}).filter((item) => !!item),
|
|
784
|
-
alternate: !node.alternate ? [] : (node.alternate as babelTypes.BlockStatement).body.map((item: any, index: number) => {
|
|
785
|
-
if (item?.type === 'ReturnStatement') {
|
|
786
|
-
item.ends = true;
|
|
787
|
-
if (item?.argument) {
|
|
788
|
-
alternateIndex = index;
|
|
789
|
-
}
|
|
790
|
-
}
|
|
791
|
-
return transformLogicNode(item);
|
|
792
|
-
}).filter((item) => !!item),
|
|
793
|
-
}) as any;
|
|
794
|
-
if (consequentIndex !== undefined) {
|
|
795
|
-
naslNode?.consequent?.splice(consequentIndex + 1, 0, new naslTypes.End());
|
|
796
|
-
}
|
|
797
|
-
if (alternateIndex !== undefined) {
|
|
798
|
-
naslNode?.alternate?.splice(alternateIndex + 1, 0, new naslTypes.End());
|
|
799
|
-
}
|
|
800
|
-
return naslNode;
|
|
801
|
-
} else if (node.type === 'WhileStatement') {
|
|
802
|
-
return new naslTypes.WhileStatement({
|
|
803
|
-
test: transformLogicNode(node.test),
|
|
804
|
-
body: (node.body as babelTypes.BlockStatement).body.map((item) => transformLogicNode(item)).filter((item) => !!item),
|
|
805
|
-
});
|
|
806
|
-
} else if (node.type === 'SwitchStatement') {
|
|
807
|
-
const match = new naslTypes.Match({
|
|
808
|
-
expression: transformLogicNode(node.discriminant),
|
|
809
|
-
isExpression: type === 'expression',
|
|
810
|
-
cases: node.cases.map((_case) => new naslTypes.MatchCase({
|
|
811
|
-
patterns: !_case.test ? [] : [transformLogicNode(_case.test)],
|
|
812
|
-
body: _case.consequent.filter((item) => item.type !== 'BreakStatement').map((item) => transformLogicNode(item)).filter((item) => !!item),
|
|
813
|
-
})),
|
|
814
|
-
});
|
|
815
|
-
if (!match.cases.length || match.cases[match.cases.length - 1].patterns.length) {
|
|
816
|
-
match.cases.push(new naslTypes.MatchCase({
|
|
817
|
-
patterns: [],
|
|
818
|
-
body: [],
|
|
819
|
-
}));
|
|
820
|
-
}
|
|
821
|
-
match.cases.forEach((case_: any) => {
|
|
822
|
-
if (case_.body.length && (case_.body[0].concept === 'BooleanLiteral' || case_.body[0].concept === 'StringLiteral')) {
|
|
823
|
-
case_.isMatchedTypeEnumable = true;
|
|
824
|
-
}
|
|
825
|
-
if (case_.patterns.length && case_.patterns[0].concept === 'MemberExpression') {
|
|
826
|
-
case_.isMatchedTypeEnumable = true;
|
|
827
|
-
case_.patterns[0].object.namespace = 'app.enums';
|
|
828
|
-
}
|
|
829
|
-
});
|
|
830
|
-
return match;
|
|
831
|
-
} else if (node.type === 'MemberExpression') {
|
|
832
|
-
const callee = flatMemberExpression(node);
|
|
833
|
-
const calleeName = generate(node)?.code;
|
|
834
|
-
if (calleeName?.includes('app.enums')) {
|
|
835
|
-
const name = (callee?.[3] as babelTypes.StringLiteral)?.value || (callee?.[3] as babelTypes.Identifier)?.name || (callee?.[3] as babelTypes.NumericLiteral)?.value;
|
|
836
|
-
return new naslTypes.MemberExpression({
|
|
837
|
-
object: new naslTypes.Identifier({
|
|
838
|
-
namespace: 'app.enums',
|
|
839
|
-
name: (callee?.[2] as babelTypes.Identifier)?.name,
|
|
840
|
-
}),
|
|
841
|
-
property: new naslTypes.Identifier({
|
|
842
|
-
name: String(name),
|
|
843
|
-
}),
|
|
844
|
-
});
|
|
845
|
-
}
|
|
846
|
-
if (node.property.type === 'NumericLiteral') {
|
|
847
|
-
return new naslTypes.CallFunction({
|
|
848
|
-
calleeNamespace: 'nasl.util',
|
|
849
|
-
calleeName: 'Get',
|
|
850
|
-
arguments: [
|
|
851
|
-
new naslTypes.Argument({
|
|
852
|
-
expression: transformLogicNode(node.object),
|
|
853
|
-
}),
|
|
854
|
-
new naslTypes.Argument({
|
|
855
|
-
expression: transformLogicNode(node.property),
|
|
856
|
-
}),
|
|
857
|
-
],
|
|
858
|
-
});
|
|
859
|
-
}
|
|
860
|
-
return new naslTypes.MemberExpression({
|
|
861
|
-
object: transformLogicNode(node.object),
|
|
862
|
-
property: transformLogicNode(node.property),
|
|
863
|
-
});
|
|
864
|
-
} else if (node.type === 'Identifier') {
|
|
865
|
-
return new naslTypes.Identifier({
|
|
866
|
-
name: node.name,
|
|
867
|
-
});
|
|
868
|
-
} else if (node.type === 'ExpressionStatement') {
|
|
869
|
-
return transformLogicNode(node.expression);
|
|
870
|
-
} else if (node.type === 'AssignmentExpression') {
|
|
871
|
-
if (node.operator === '+=' || node.operator === '-=') {
|
|
872
|
-
if (type === 'expression') {
|
|
873
|
-
return new naslTypes.BinaryExpression({
|
|
874
|
-
operator: node.operator[0] as '+' | '-',
|
|
875
|
-
left: transformLogicNode(node.left),
|
|
876
|
-
right: transformLogicNode(node.right),
|
|
877
|
-
});
|
|
878
|
-
}
|
|
879
|
-
return new naslTypes.Assignment({
|
|
880
|
-
left: transformLogicNode(node.left),
|
|
881
|
-
right: new naslTypes.BinaryExpression({
|
|
882
|
-
operator: node.operator[0] as '+' | '-',
|
|
883
|
-
left: transformLogicNode(node.left),
|
|
884
|
-
right: transformLogicNode(node.right),
|
|
885
|
-
}),
|
|
886
|
-
});
|
|
887
|
-
}
|
|
888
|
-
if (type === 'expression') {
|
|
889
|
-
return transformLogicNode(node.right);
|
|
890
|
-
}
|
|
891
|
-
return new naslTypes.Assignment({
|
|
892
|
-
left: transformLogicNode(node.left),
|
|
893
|
-
right: transformLogicNode(node.right),
|
|
894
|
-
});
|
|
895
|
-
} else if (node.type === 'NewExpression') {
|
|
896
|
-
const calleeName = generate(node.callee as any).code;
|
|
897
|
-
if (['Boolean', 'Integer', 'Double', 'Decimal', 'Long'].includes(calleeName)) {
|
|
898
|
-
return transformLogicNode(node.arguments[0], naslTypes.TypeAnnotation.createPrimitive(calleeName));
|
|
899
|
-
}
|
|
900
|
-
throwError(`Unhandled node ${node.type}`);
|
|
901
|
-
} else if (node.type === 'ArrowFunctionExpression') {
|
|
902
|
-
return new naslTypes.AnonymousFunction({
|
|
903
|
-
params: node.params.map((param: babelTypes.Identifier) => transformParam(param)),
|
|
904
|
-
body: transformLogicNode(node.body),
|
|
905
|
-
}) as any as naslTypes.LogicItem;
|
|
906
|
-
} else if (node.type === 'BooleanLiteral') {
|
|
907
|
-
return new naslTypes.BooleanLiteral({
|
|
908
|
-
value: String(node.value),
|
|
909
|
-
});
|
|
910
|
-
} else if (node.type === 'StringLiteral') {
|
|
911
|
-
return new naslTypes.StringLiteral({
|
|
912
|
-
value: node.value,
|
|
913
|
-
});
|
|
914
|
-
} else if (node.type === 'NumericLiteral') {
|
|
915
|
-
const literal = new naslTypes.NumericLiteral({
|
|
916
|
-
value: String(node.value),
|
|
917
|
-
typeAnnotation: typeAnnotation || naslTypes.TypeAnnotation.createPrimitive('Integer'),
|
|
918
|
-
});
|
|
919
|
-
literal.typeAnnotation.typeName = literal.changeType(String(node.value));
|
|
920
|
-
return literal;
|
|
921
|
-
} else if (node.type === 'NullLiteral') {
|
|
922
|
-
return new naslTypes.NullLiteral();
|
|
923
|
-
} else if (node.type === 'TemplateLiteral') {
|
|
924
|
-
const expressions = [...node.expressions as babelTypes.Expression[], ...node.quasis.map((element) => ({
|
|
925
|
-
type: 'StringLiteral',
|
|
926
|
-
value: element.value.raw,
|
|
927
|
-
start: element.start,
|
|
928
|
-
end: element.end,
|
|
929
|
-
})) as babelTypes.StringLiteral[]];
|
|
930
|
-
expressions.sort((a, b) => a.start - b.start);
|
|
931
|
-
|
|
932
|
-
return new naslTypes.StringInterpolation({
|
|
933
|
-
expressions: expressions.map((item) => transformLogicNode(item)),
|
|
934
|
-
});
|
|
935
|
-
} else if (node.type === 'BinaryExpression' || node.type === 'LogicalExpression') {
|
|
936
|
-
if (node.operator === '===')
|
|
937
|
-
node.operator = '==';
|
|
938
|
-
if (node.operator === '!==')
|
|
939
|
-
node.operator = '!=';
|
|
940
|
-
if (['+', '-', '*', '/', '%', '==', '!=', '>', '<', '>=', '<=', '&&', '||', 'startwith', 'endwith', 'like', 'in'].includes(node.operator)) {
|
|
941
|
-
return new naslTypes.BinaryExpression({
|
|
942
|
-
operator: node.operator as any,
|
|
943
|
-
left: transformLogicNode(node.left),
|
|
944
|
-
right: transformLogicNode(node.right),
|
|
945
|
-
});
|
|
946
|
-
}
|
|
947
|
-
throwError(`Unhandled node ${node.type}`);
|
|
948
|
-
} else if (node.type === 'UnaryExpression') {
|
|
949
|
-
if (['!'].includes(node.operator)) {
|
|
950
|
-
return new naslTypes.UnaryExpression({
|
|
951
|
-
operator: node.operator as any,
|
|
952
|
-
argument: transformLogicNode(node.argument),
|
|
953
|
-
});
|
|
954
|
-
}
|
|
955
|
-
if (['-'].includes(node.operator) && node.argument.type === 'NumericLiteral') {
|
|
956
|
-
return transformLogicNode({
|
|
957
|
-
type: 'NumericLiteral',
|
|
958
|
-
value: -node.argument.value,
|
|
959
|
-
} as babelTypes.NumericLiteral);
|
|
960
|
-
}
|
|
961
|
-
throwError(`Unhandled node ${node.type}`);
|
|
962
|
-
} else if (node.type === 'UpdateExpression') {
|
|
963
|
-
if (node.operator === '++' || node.operator === '--') {
|
|
964
|
-
return new naslTypes.Assignment({
|
|
965
|
-
left: transformLogicNode(node.argument),
|
|
966
|
-
right: new naslTypes.BinaryExpression({
|
|
967
|
-
operator: node.operator[0] as '+' | '-',
|
|
968
|
-
left: transformLogicNode(node.argument),
|
|
969
|
-
right: new naslTypes.NumericLiteral({
|
|
970
|
-
value: '1',
|
|
971
|
-
typeAnnotation: naslTypes.TypeAnnotation.createPrimitive('Integer'),
|
|
972
|
-
}),
|
|
973
|
-
}),
|
|
974
|
-
});
|
|
975
|
-
}
|
|
976
|
-
} else if (node.type === 'ReturnStatement') {
|
|
977
|
-
if (node.argument) {
|
|
978
|
-
if (type === 'expression') {
|
|
979
|
-
return transformLogicNode(node.argument);
|
|
980
|
-
}
|
|
981
|
-
if (!logic.returns.length) {
|
|
982
|
-
logic.returns.push(new naslTypes.Return({
|
|
983
|
-
name: 'result',
|
|
984
|
-
}));
|
|
985
|
-
}
|
|
986
|
-
return new naslTypes.Assignment({
|
|
987
|
-
left: new naslTypes.Identifier({
|
|
988
|
-
name: logic.returns[0].name,
|
|
989
|
-
}),
|
|
990
|
-
right: transformLogicNode(node.argument),
|
|
991
|
-
});
|
|
992
|
-
}
|
|
993
|
-
if ((node as any)?.ends) {
|
|
994
|
-
return new naslTypes.End();
|
|
995
|
-
}
|
|
996
|
-
} else if (node.type === 'EmptyStatement') {
|
|
997
|
-
return node.leadingComments && new naslTypes.Comment({
|
|
998
|
-
value: String(node.leadingComments[0].value),
|
|
999
|
-
});
|
|
1000
|
-
} else if (node.type === 'BlockStatement') {
|
|
1001
|
-
return transformLogicNode(node?.body?.[0]);
|
|
1002
|
-
} else if (node.type === 'ObjectExpression' && node?.properties?.length > 0) {
|
|
1003
|
-
const properties: any = [];
|
|
1004
|
-
const rights: any = [];
|
|
1005
|
-
const structureProperties: any = [];
|
|
1006
|
-
const assignmentLines: any = [];
|
|
1007
|
-
node?.properties?.forEach((arg: any, index: number) => {
|
|
1008
|
-
properties.push(transformLogicNode(arg.key));
|
|
1009
|
-
if (arg.value.type === 'MemberExpression') {
|
|
1010
|
-
const expression = flatMemberExpression(arg.value);
|
|
1011
|
-
const members = flatMembers(transformLogicNode(arg.value) as any);
|
|
1012
|
-
rights.push(new naslTypes.SelectMembers({
|
|
1013
|
-
expression: transformLogicNode(expression[0]),
|
|
1014
|
-
members,
|
|
1015
|
-
}));
|
|
1016
|
-
if (arg?.value) {
|
|
1017
|
-
assignmentLines.push(new naslTypes.AssignmentLine({
|
|
1018
|
-
leftIndex: [0, index],
|
|
1019
|
-
rightIndex: [index, members.length - 1],
|
|
1020
|
-
}));
|
|
1021
|
-
}
|
|
1022
|
-
} else {
|
|
1023
|
-
rights.push(new naslTypes.SelectMembers({
|
|
1024
|
-
expression: transformLogicNode(arg?.value)
|
|
1025
|
-
}));
|
|
1026
|
-
if (arg?.value) {
|
|
1027
|
-
assignmentLines.push(new naslTypes.AssignmentLine({
|
|
1028
|
-
leftIndex: [0, index],
|
|
1029
|
-
rightIndex: [index],
|
|
1030
|
-
}));
|
|
1031
|
-
}
|
|
1032
|
-
}
|
|
1033
|
-
structureProperties.push(new naslTypes.StructureProperty({
|
|
1034
|
-
name: arg?.key?.name,
|
|
1035
|
-
typeAnnotation: null
|
|
1036
|
-
}));
|
|
1037
|
-
});
|
|
1038
|
-
return new naslTypes.NewComposite({
|
|
1039
|
-
typeAnnotation: naslTypes.TypeAnnotation.createTypeAnonymousStructure(structureProperties),
|
|
1040
|
-
properties,
|
|
1041
|
-
rights,
|
|
1042
|
-
assignmentLines
|
|
1043
|
-
});
|
|
1044
|
-
} else {
|
|
1045
|
-
throwError(`Unhandled node ${node.type}`);
|
|
1046
|
-
}
|
|
1047
|
-
}
|
|
22
|
+
const logic = new naslTypes.Logic();
|
|
23
|
+
logic.name = contextLogicName;
|
|
24
|
+
logic.body = [
|
|
25
|
+
new naslTypes.Start(),
|
|
26
|
+
];
|
|
1048
27
|
|
|
1049
28
|
const root = babel.parseSync(tsCode, {
|
|
1050
29
|
filename: 'result.ts',
|
|
@@ -1073,28 +52,23 @@ export function transformTSCode(tsCode: string, contextLogicName: string, type?:
|
|
|
1073
52
|
statements = statements.filter((stat) => stat.start > currentPositionComment.start);
|
|
1074
53
|
}
|
|
1075
54
|
|
|
1076
|
-
// traverse<babelTypes.Node>((current) => {
|
|
1077
|
-
// if (!current.node.type)
|
|
1078
|
-
// console.log(current.key, current.node);
|
|
1079
|
-
// // console.log(current.node.type);
|
|
1080
|
-
// }, { node: {
|
|
1081
|
-
// type: 'BlockStatement',
|
|
1082
|
-
// body: statements,
|
|
1083
|
-
// } as babelTypes.BlockStatement }, {
|
|
1084
|
-
// mode: 'anyObject',
|
|
1085
|
-
// depthFirst: true,
|
|
1086
|
-
// excludedKeySet: new Set(['loc']),
|
|
1087
|
-
// });
|
|
1088
|
-
|
|
1089
55
|
parameters.forEach((node) => {
|
|
1090
|
-
const param =
|
|
56
|
+
const param = transform2Param(node);
|
|
1091
57
|
param && logic.params.push(param);
|
|
1092
58
|
});
|
|
1093
59
|
statements.forEach((node) => {
|
|
1094
|
-
const item =
|
|
60
|
+
const item = transform2LogicItem(node, {
|
|
61
|
+
logic,
|
|
62
|
+
isPageLogic: isPageLogic(contextLogicName),
|
|
63
|
+
});
|
|
1095
64
|
item && logic.body.push(item);
|
|
1096
65
|
});
|
|
1097
66
|
|
|
67
|
+
if (logic.returns[0]?.name) {
|
|
68
|
+
const index = logic.variables.findIndex((variable) => variable.name === logic.returns[0].name);
|
|
69
|
+
if (index >= 0)
|
|
70
|
+
logic.variables.splice(index, 1);
|
|
71
|
+
}
|
|
1098
72
|
// console.log(JSON.stringify(logic));
|
|
1099
73
|
// try {
|
|
1100
74
|
// console.log(logic.toNaturalTS());
|
|
@@ -1105,14 +79,14 @@ export function transformTSCode(tsCode: string, contextLogicName: string, type?:
|
|
|
1105
79
|
return logic;
|
|
1106
80
|
}
|
|
1107
81
|
|
|
1108
|
-
export function tryTransformTSCode(tsCode: string, contextLogicName: string
|
|
82
|
+
export function tryTransformTSCode(tsCode: string, contextLogicName: string): naslTypes.Logic {
|
|
1109
83
|
if (tsCode.includes('```')) {
|
|
1110
84
|
const cap = tsCode.match(/```.*\n([\s\S]+?)```/);
|
|
1111
85
|
if (cap)
|
|
1112
86
|
tsCode = cap[1].trim();
|
|
1113
87
|
}
|
|
1114
88
|
|
|
1115
|
-
return transformTSCode(tsCode, contextLogicName
|
|
89
|
+
return transformTSCode(tsCode, contextLogicName);
|
|
1116
90
|
}
|
|
1117
91
|
|
|
1118
92
|
export function transformTSCodeEx(answer: any, activeLogic: any, context: any) {
|
|
@@ -1148,7 +122,7 @@ export function transformTSCodeEx(answer: any, activeLogic: any, context: any) {
|
|
|
1148
122
|
export function transformTSCodeToExpression(answer: any, context: any) {
|
|
1149
123
|
let logic;
|
|
1150
124
|
try {
|
|
1151
|
-
logic = tryTransformTSCode(answer, ''
|
|
125
|
+
logic = tryTransformTSCode(answer, '');
|
|
1152
126
|
} catch (error) {
|
|
1153
127
|
throw new Error(error);
|
|
1154
128
|
}
|