@lcap/nasl 3.8.2-beta.8 → 3.8.3-beta.1
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.js +5 -3
- package/out/common/BaseNode.js.map +1 -1
- package/out/common/ComponentAPI.d.ts +9 -0
- package/out/concepts/AuthLogicForCallInterface__.js +21 -6
- 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 +24 -8
- package/out/concepts/BusinessComponent__.js.map +1 -1
- package/out/concepts/BusinessLogic__.js +21 -6
- package/out/concepts/BusinessLogic__.js.map +1 -1
- package/out/concepts/CallInterface__.js +2 -1
- package/out/concepts/CallInterface__.js.map +1 -1
- package/out/concepts/CallLogic__.d.ts +1 -0
- package/out/concepts/CallLogic__.js +32 -11
- 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/Interface__.js +1 -1
- package/out/concepts/Interface__.js.map +1 -1
- package/out/concepts/Logic__.js +49 -17
- package/out/concepts/Logic__.js.map +1 -1
- package/out/concepts/MemberExpression__.js +19 -13
- 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 +21 -6
- 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/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/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 +17 -7
- package/out/concepts/ViewElement__.js.map +1 -1
- package/out/concepts/View__.js +70 -14
- package/out/concepts/View__.js.map +1 -1
- package/out/concepts/basics/stdlib/nasl.processV2.js +78 -0
- package/out/concepts/basics/stdlib/nasl.processV2.js.map +1 -1
- package/out/concepts/basics/stdlib/nasl.ui.js +40 -40
- package/out/concepts/basics/stdlib/nasl.ui.js.map +1 -1
- package/out/generator/genBundleFiles.js +1 -1
- package/out/generator/genBundleFiles.js.map +1 -1
- package/out/generator/release-body/utils.js +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 +361 -304
- 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/getMemberIdentifier.js +11 -17
- package/out/server/getMemberIdentifier.js.map +1 -1
- package/out/server/naslServer.d.ts +2 -2
- package/out/server/naslServer.js +38 -18
- package/out/server/naslServer.js.map +1 -1
- 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 +110 -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/stdlib/nasl.processV2.ts +19 -0
- package/sandbox/stdlib/nasl.ui.ts +8 -8
- 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 +5 -3
- package/src/common/ComponentAPI.ts +7 -0
- package/src/concepts/AuthLogicForCallInterface__.ts +23 -6
- 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 +26 -8
- package/src/concepts/BusinessLogic__.ts +23 -6
- package/src/concepts/CallInterface__.ts +2 -2
- package/src/concepts/CallLogic__.ts +50 -24
- 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/Interface__.ts +1 -1
- package/src/concepts/Logic__.ts +62 -33
- package/src/concepts/MemberExpression__.ts +20 -13
- package/src/concepts/OqlQueryComponent__.ts +29 -5
- package/src/concepts/OverriddenLogic__.ts +23 -6
- package/src/concepts/Param__.ts +5 -4
- package/src/concepts/QueryOrderByExpression__.ts +0 -1
- package/src/concepts/Structure__.ts +5 -7
- package/src/concepts/Variable__.ts +9 -5
- package/src/concepts/ViewElement__.ts +25 -9
- package/src/concepts/View__.ts +97 -37
- package/src/concepts/basics/stdlib/nasl.processV2.ts +79 -0
- package/src/concepts/basics/stdlib/nasl.ui.ts +40 -40
- package/src/generator/genBundleFiles.ts +1 -1
- package/src/generator/release-body/utils.ts +1 -1
- 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 +205 -147
- 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/getMemberIdentifier.ts +14 -14
- package/src/server/naslServer.ts +39 -19
- 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 +149 -0
- package/src/translator/types.ts +2 -0
- package/src/translator/utils.ts +1 -1
- 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/member-expression/__snapshots__/toJS.spec.ts.snap +1 -1
- package/test/concepts/member-expression/__snapshots__/toVue.spec.ts.snap +1 -1
- 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
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
import {
|
|
3
|
+
FunctionExpression,
|
|
4
|
+
FunctionDeclaration,
|
|
5
|
+
Identifier,
|
|
6
|
+
Pattern,
|
|
7
|
+
RestElement,
|
|
8
|
+
Statement,
|
|
9
|
+
Noop,
|
|
10
|
+
TypeAnnotation,
|
|
11
|
+
TSTypeAnnotation,
|
|
12
|
+
VariableDeclarator,
|
|
13
|
+
} from '@babel/types';
|
|
14
|
+
import { getNodeCode, normalizeArray } from './utils';
|
|
15
|
+
import transformExpression2Nasl from './transform-expression2nasl';
|
|
16
|
+
import transformTypeAnnotation from './transform-tstype2nasl';
|
|
17
|
+
|
|
18
|
+
const getTypeAnnotation = (typeAnnotation: TypeAnnotation | TSTypeAnnotation | Noop | null | undefined) => {
|
|
19
|
+
if (typeAnnotation && typeAnnotation.type === 'TSTypeAnnotation') {
|
|
20
|
+
return transformTypeAnnotation(typeAnnotation.typeAnnotation);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return null;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const getRestElementTypeAnnotation = (param: RestElement, typeNames: string[] = []) => {
|
|
27
|
+
if (!param.typeAnnotation || param.typeAnnotation.type !== 'TSTypeAnnotation') {
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
if (param.typeAnnotation.typeAnnotation.type === 'TSTypeReference'
|
|
32
|
+
&& (
|
|
33
|
+
(
|
|
34
|
+
param.typeAnnotation.typeAnnotation.typeName.type === 'TSQualifiedName'
|
|
35
|
+
&& param.typeAnnotation.typeAnnotation.typeName.right.name === 'List'
|
|
36
|
+
) || (
|
|
37
|
+
param.typeAnnotation.typeAnnotation.typeName.type === 'Identifier'
|
|
38
|
+
&& param.typeAnnotation.typeAnnotation.typeName.name === 'Array'
|
|
39
|
+
)
|
|
40
|
+
)
|
|
41
|
+
&& param.typeAnnotation.typeAnnotation.typeParameters
|
|
42
|
+
&& param.typeAnnotation.typeAnnotation.typeParameters.params
|
|
43
|
+
&& param.typeAnnotation.typeAnnotation.typeParameters.params.length > 0
|
|
44
|
+
) {
|
|
45
|
+
return transformTypeAnnotation(param.typeAnnotation.typeAnnotation.typeParameters.params[0], typeNames);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (param.typeAnnotation.typeAnnotation.type === 'TSArrayType') {
|
|
49
|
+
return transformTypeAnnotation(param.typeAnnotation.typeAnnotation.elementType, typeNames);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return transformTypeAnnotation(param.typeAnnotation.typeAnnotation, typeNames);
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
export function transformParam2Nasl(paramAST: Identifier | Pattern | RestElement) {
|
|
56
|
+
switch (paramAST.type) {
|
|
57
|
+
case 'Identifier':
|
|
58
|
+
return {
|
|
59
|
+
concept: 'Param',
|
|
60
|
+
name: paramAST.name,
|
|
61
|
+
description: '',
|
|
62
|
+
typeAnnotation: getTypeAnnotation(paramAST.typeAnnotation),
|
|
63
|
+
};
|
|
64
|
+
case 'RestElement':
|
|
65
|
+
if (paramAST.argument.type !== 'Identifier') {
|
|
66
|
+
throw new Error(`解析函数参数失败,不支持该参数类型 ${getNodeCode(paramAST)}`);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return {
|
|
70
|
+
concept: 'Param',
|
|
71
|
+
name: paramAST.argument.name,
|
|
72
|
+
description: '',
|
|
73
|
+
spread: true,
|
|
74
|
+
typeAnnotation: getRestElementTypeAnnotation(paramAST),
|
|
75
|
+
};
|
|
76
|
+
case 'AssignmentPattern':
|
|
77
|
+
if (paramAST.left.type !== 'Identifier') {
|
|
78
|
+
throw new Error(`解析函数参数失败,不支持该参数类型 ${getNodeCode(paramAST)}`);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return {
|
|
82
|
+
concept: 'Param',
|
|
83
|
+
name: paramAST.left.name,
|
|
84
|
+
description: '',
|
|
85
|
+
typeAnnotation: getTypeAnnotation(paramAST.left.typeAnnotation),
|
|
86
|
+
defaultValue: {
|
|
87
|
+
concept: 'DefaultValue',
|
|
88
|
+
expression: transformExpression2Nasl(paramAST.right),
|
|
89
|
+
playground: [],
|
|
90
|
+
},
|
|
91
|
+
};
|
|
92
|
+
default:
|
|
93
|
+
throw new Error(`解析函数参数失败,不支持该参数类型 ${getNodeCode(paramAST)}`);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export function transformStatement(statement: Statement) {
|
|
98
|
+
if (statement.type === 'BlockStatement') {
|
|
99
|
+
return statement.body.map(transformStatement);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (statement.type === 'IfStatement') {
|
|
103
|
+
return {
|
|
104
|
+
concept: 'IfStatement',
|
|
105
|
+
test: transformExpression2Nasl(statement.test),
|
|
106
|
+
consequent: normalizeArray(transformStatement(statement.consequent)),
|
|
107
|
+
alternate: statement.alternate ? normalizeArray(transformStatement(statement.alternate)) : [],
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if (statement.type === 'SwitchStatement' && statement.discriminant) {
|
|
112
|
+
const testLeft = transformExpression2Nasl(statement.discriminant);
|
|
113
|
+
return {
|
|
114
|
+
concept: 'SwitchStatement',
|
|
115
|
+
cases: statement.cases.map((switchCaseAST) => ({
|
|
116
|
+
concept: 'SwitchCase',
|
|
117
|
+
test: {
|
|
118
|
+
concept: 'BinaryExpression',
|
|
119
|
+
left: testLeft,
|
|
120
|
+
right: switchCaseAST.test ? transformExpression2Nasl(switchCaseAST.test) : null,
|
|
121
|
+
operator: '==',
|
|
122
|
+
},
|
|
123
|
+
consequent: switchCaseAST.consequent.map((st) => transformStatement(st)).flat(),
|
|
124
|
+
})),
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
if (statement.type === 'ForInStatement') {
|
|
129
|
+
if (statement.left.type !== 'VariableDeclaration') {
|
|
130
|
+
throw new Error('解析 for in 语句失败, 左侧仅允许定义变量,例如 var xxx in list');
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
return {
|
|
134
|
+
concept: 'ForEachStatement',
|
|
135
|
+
each: transformExpression2Nasl(statement.right),
|
|
136
|
+
item: {
|
|
137
|
+
concept: 'Param',
|
|
138
|
+
name: statement.left.declarations[0].id && statement.left.declarations[0].id.type === 'Identifier' ? statement.left.declarations[0].id.name : 'item',
|
|
139
|
+
description: '',
|
|
140
|
+
},
|
|
141
|
+
index: {
|
|
142
|
+
concept: 'Param',
|
|
143
|
+
name: statement.left.declarations[1] && statement.left.declarations[1].id && statement.left.declarations[1].id.type === 'Identifier' ? statement.left.declarations[1].id.name : 'index',
|
|
144
|
+
description: '',
|
|
145
|
+
},
|
|
146
|
+
start: {
|
|
147
|
+
concept: 'NumericLiteral',
|
|
148
|
+
value: 0,
|
|
149
|
+
},
|
|
150
|
+
body: normalizeArray(transformStatement(statement.body)),
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
if (statement.type === 'WhileStatement') {
|
|
155
|
+
return {
|
|
156
|
+
concept: 'WhileStatement',
|
|
157
|
+
test: transformExpression2Nasl(statement.test),
|
|
158
|
+
body: normalizeArray(transformStatement(statement.body)),
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
if (statement.type === 'ExpressionStatement') {
|
|
163
|
+
return transformExpression2Nasl(statement.expression);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
if (statement.type === 'ReturnStatement') {
|
|
167
|
+
return {
|
|
168
|
+
concept: 'End',
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
throw new Error(`解析语句失败, Nasl 不支持该语法, ${getNodeCode(statement)}`);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export function transformVariable(ast: VariableDeclarator) {
|
|
176
|
+
if (ast.id.type !== 'Identifier') {
|
|
177
|
+
throw new Error(`解析变量定义失败, ${getNodeCode(ast)}`);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const variable: any = {
|
|
181
|
+
concept: 'Variable',
|
|
182
|
+
name: ast.id.name,
|
|
183
|
+
description: '',
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
if (ast.id.typeAnnotation && ast.id.typeAnnotation.type === 'TSTypeAnnotation') {
|
|
187
|
+
variable.typeAnnotation = transformTypeAnnotation(ast.id.typeAnnotation.typeAnnotation);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
if (ast.init) {
|
|
191
|
+
variable.defaultValue = transformExpression2Nasl(ast.init);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
return variable;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
export function transformFunc2Nasl(funcAst: FunctionExpression | FunctionDeclaration) {
|
|
198
|
+
if (!funcAst.id) {
|
|
199
|
+
throw new Error('解析函数失败,未定义函数名称');
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
const variables: any[] = [];
|
|
203
|
+
const statements: Statement[] = funcAst.body.body.filter((s) => {
|
|
204
|
+
if (s.type !== 'VariableDeclaration') {
|
|
205
|
+
return true;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
s.declarations.forEach((decla) => {
|
|
209
|
+
if (decla.id && decla.id.type === 'Identifier') {
|
|
210
|
+
variables.push(transformVariable(decla));
|
|
211
|
+
}
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
return false;
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
const Logic = {
|
|
218
|
+
concept: 'Logic',
|
|
219
|
+
name: funcAst.id.name,
|
|
220
|
+
variables,
|
|
221
|
+
params: funcAst.params.map(transformParam2Nasl),
|
|
222
|
+
returns: [],
|
|
223
|
+
playground: [],
|
|
224
|
+
body: [
|
|
225
|
+
{
|
|
226
|
+
concept: 'Start',
|
|
227
|
+
},
|
|
228
|
+
...statements.map(transformStatement),
|
|
229
|
+
],
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
if (Logic.body[Logic.body.length - 1].concept !== 'End') {
|
|
233
|
+
Logic.body.push({
|
|
234
|
+
concept: 'End',
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
return Logic;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
export default transformFunc2Nasl;
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
/* eslint-disable no-nested-ternary, no-use-before-define */
|
|
2
|
+
// @ts-nocheck
|
|
3
|
+
import * as babelTypes from '@babel/types';
|
|
4
|
+
import type {
|
|
5
|
+
StructureProperty,
|
|
6
|
+
TypeAnnotation,
|
|
7
|
+
} from '@nasl/types/nasl.ui.ast';
|
|
8
|
+
import { getNodeCode } from './utils';
|
|
9
|
+
|
|
10
|
+
const isPrimitive = (name: string) => ['String', 'Integer', 'Decimal', 'Boolean', 'Date', 'Time', 'DateTime', 'Binary', 'Any'].includes(name);
|
|
11
|
+
const getTypeKind = (typeParameters, primitive) => (typeParameters && typeParameters.params && typeParameters.params.length ? 'generic' : primitive ? 'primitive' : 'reference');
|
|
12
|
+
|
|
13
|
+
function exitNotSupportType(node: babelTypes.TSType) {
|
|
14
|
+
throw new Error(`解析Ts 类型异常,未支持 TS 类型 ${getNodeCode(node)}, 请使用nasl/types`);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function transformTSTypeReference(node: babelTypes.TSTypeReference, typeNames): TypeAnnotation | undefined {
|
|
18
|
+
const { typeName, typeParameters } = node;
|
|
19
|
+
// 不支持 Promise
|
|
20
|
+
if (typeName.type === 'Identifier' && typeName.name === 'Promise') {
|
|
21
|
+
if (typeParameters && typeParameters.params && typeParameters.params.length > 0) {
|
|
22
|
+
return transformTypeAnnotation(typeParameters.params[0], typeNames);
|
|
23
|
+
}
|
|
24
|
+
} else if (typeName.type === 'TSQualifiedName') {
|
|
25
|
+
const { left, right } = typeName;
|
|
26
|
+
if (right.name === 'Any') {
|
|
27
|
+
exitNotSupportType(node);
|
|
28
|
+
}
|
|
29
|
+
const primitive = isPrimitive(right.name);
|
|
30
|
+
const namespace = getNodeCode(left);
|
|
31
|
+
const typeArguments: TypeAnnotation[] = [];
|
|
32
|
+
|
|
33
|
+
if (typeParameters && typeParameters.params) {
|
|
34
|
+
typeParameters.params.forEach((param) => {
|
|
35
|
+
const annotation = transformTypeAnnotation(param, typeNames);
|
|
36
|
+
if (annotation) {
|
|
37
|
+
typeArguments.push(annotation);
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return {
|
|
43
|
+
typeKind: getTypeKind(typeParameters, primitive) as any,
|
|
44
|
+
typeName: namespace === 'nasl.core' && right.name === 'Integer' ? 'Long' : right.name,
|
|
45
|
+
typeNamespace: namespace,
|
|
46
|
+
concept: 'TypeAnnotation',
|
|
47
|
+
inferred: false,
|
|
48
|
+
ruleMap: new Map(),
|
|
49
|
+
typeArguments,
|
|
50
|
+
};
|
|
51
|
+
} else if (typeName.type === 'Identifier') {
|
|
52
|
+
const primitive = isPrimitive(typeName.name);
|
|
53
|
+
|
|
54
|
+
if (['String', 'Number', 'Array', 'Object', 'Boolean', 'Symbol', 'Function', 'Date'].indexOf(typeName.name) !== -1) {
|
|
55
|
+
exitNotSupportType(node);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if (typeNames.indexOf(typeName.name) !== -1) {
|
|
59
|
+
return {
|
|
60
|
+
concept: 'TypeAnnotation',
|
|
61
|
+
typeKind: 'typeParam',
|
|
62
|
+
typeName: typeName.name,
|
|
63
|
+
typeNamespace: '',
|
|
64
|
+
inferred: false,
|
|
65
|
+
ruleMap: new Map(),
|
|
66
|
+
typeArguments: [],
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const NaslTypes = ['Current', 'CurrentDynamic'];
|
|
71
|
+
|
|
72
|
+
if (!NaslTypes.includes(typeName.name)) {
|
|
73
|
+
exitNotSupportType(node);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const typeArguments: TypeAnnotation[] = [];
|
|
77
|
+
|
|
78
|
+
if (typeParameters && typeParameters.params) {
|
|
79
|
+
typeParameters.params.forEach((param) => {
|
|
80
|
+
const annotation = transformTypeAnnotation(param, typeNames);
|
|
81
|
+
if (annotation) {
|
|
82
|
+
typeArguments.push(annotation);
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
return {
|
|
88
|
+
typeKind: getTypeKind(typeParameters, primitive) as any,
|
|
89
|
+
typeName: typeName.name,
|
|
90
|
+
typeNamespace: NaslTypes.includes(typeName.name) ? 'nasl.ui' : '',
|
|
91
|
+
concept: 'TypeAnnotation',
|
|
92
|
+
inferred: false,
|
|
93
|
+
ruleMap: new Map(),
|
|
94
|
+
typeArguments,
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return undefined;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function transformTSUnionType(node: babelTypes.TSUnionType, typeNames): TypeAnnotation {
|
|
102
|
+
const typeArguments = node.types.map((t) => transformTypeAnnotation(t, typeNames)).filter((v) => !!v) as TypeAnnotation[];
|
|
103
|
+
|
|
104
|
+
return {
|
|
105
|
+
typeKind: 'union',
|
|
106
|
+
typeName: '',
|
|
107
|
+
typeNamespace: '',
|
|
108
|
+
concept: 'TypeAnnotation',
|
|
109
|
+
inferred: false,
|
|
110
|
+
ruleMap: new Map(),
|
|
111
|
+
typeArguments,
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function transformTSFunctionType(node: babelTypes.TSFunctionType, typeNames): TypeAnnotation {
|
|
116
|
+
const typeArguments: TypeAnnotation[] = [];
|
|
117
|
+
const returnType: TypeAnnotation[] = [];
|
|
118
|
+
|
|
119
|
+
if (node.parameters && node.parameters.length > 0) {
|
|
120
|
+
node.parameters.forEach((parameter) => {
|
|
121
|
+
if (parameter.type === 'Identifier' && parameter.typeAnnotation && parameter.typeAnnotation.type === 'TSTypeAnnotation') {
|
|
122
|
+
if (parameter.typeAnnotation.typeAnnotation.type === 'TSFunctionType') {
|
|
123
|
+
throw new Error('解析Ts 类型异常,未支持 TS 类型多层嵌套 function 类型');
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const typeAnnotation = transformTypeAnnotation(parameter.typeAnnotation.typeAnnotation, typeNames);
|
|
127
|
+
if (typeAnnotation) {
|
|
128
|
+
typeArguments.push(typeAnnotation);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
if (node.typeAnnotation && node.typeAnnotation.type === 'TSTypeAnnotation') {
|
|
135
|
+
const tsType = node.typeAnnotation.typeAnnotation as babelTypes.TSType;
|
|
136
|
+
if (tsType.type !== 'TSTypeReference' || tsType.typeName.type !== 'Identifier' || tsType.typeName.name !== 'Promise') {
|
|
137
|
+
throw new Error('解析Ts 类型异常,用函数作为参数,返回值类型强制需要为 Promise!');
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const typeAnnotation = transformTypeAnnotation(node.typeAnnotation.typeAnnotation, typeNames);
|
|
141
|
+
if (typeAnnotation) {
|
|
142
|
+
returnType.push(typeAnnotation);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
return {
|
|
147
|
+
concept: 'TypeAnnotation',
|
|
148
|
+
typeKind: 'function',
|
|
149
|
+
typeName: '',
|
|
150
|
+
typeNamespace: '',
|
|
151
|
+
inferred: false,
|
|
152
|
+
ruleMap: new Map(),
|
|
153
|
+
typeArguments,
|
|
154
|
+
returnType,
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function transformTSTypeLiteral(node: babelTypes.TSTypeLiteral, typeNames: string[]): TypeAnnotation {
|
|
159
|
+
const properties: StructureProperty[] = [];
|
|
160
|
+
node.members.forEach((n) => {
|
|
161
|
+
if (n.type === 'TSPropertySignature' && n.key.type === 'Identifier' && n.typeAnnotation && n.typeAnnotation.type === 'TSTypeAnnotation') {
|
|
162
|
+
const typeAnnotation = transformTypeAnnotation(n.typeAnnotation.typeAnnotation, typeNames);
|
|
163
|
+
|
|
164
|
+
if (typeAnnotation) {
|
|
165
|
+
properties.push({
|
|
166
|
+
concept: 'StructureProperty',
|
|
167
|
+
name: n.key.name,
|
|
168
|
+
jsonName: n.key.name,
|
|
169
|
+
label: '',
|
|
170
|
+
description: '',
|
|
171
|
+
defaultValue: {
|
|
172
|
+
concept: 'DefaultValue',
|
|
173
|
+
expression: {
|
|
174
|
+
concept: 'NullLiteral',
|
|
175
|
+
},
|
|
176
|
+
playground: [],
|
|
177
|
+
},
|
|
178
|
+
typeAnnotation,
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
return {
|
|
185
|
+
concept: 'TypeAnnotation',
|
|
186
|
+
typeKind: 'anonymousStructure',
|
|
187
|
+
typeName: '',
|
|
188
|
+
typeNamespace: '',
|
|
189
|
+
inferred: false,
|
|
190
|
+
ruleMap: new Map(),
|
|
191
|
+
properties,
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
// eslint-disable-next-line consistent-return
|
|
195
|
+
function transformTypeAnnotation(node: babelTypes.TSType, typeNames: string[] = []): TypeAnnotation | undefined {
|
|
196
|
+
switch (node.type) {
|
|
197
|
+
case 'TSTypeReference':
|
|
198
|
+
return transformTSTypeReference(node, typeNames);
|
|
199
|
+
case 'TSUnionType':
|
|
200
|
+
return transformTSUnionType(node, typeNames);
|
|
201
|
+
case 'TSFunctionType':
|
|
202
|
+
return transformTSFunctionType(node, typeNames);
|
|
203
|
+
case 'TSTypeLiteral':
|
|
204
|
+
return transformTSTypeLiteral(node, typeNames);
|
|
205
|
+
case 'TSVoidKeyword':
|
|
206
|
+
return undefined;
|
|
207
|
+
default:
|
|
208
|
+
exitNotSupportType(node);
|
|
209
|
+
return undefined;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
export default transformTypeAnnotation;
|