@lcap/nasl 3.8.2-alpha.4 → 3.8.2-alpha.5

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.
@@ -24,7 +24,7 @@ function tryJSONParse(str: string) {
24
24
 
25
25
  try {
26
26
  result = JSON.parse(str);
27
- } catch (e) {}
27
+ } catch (e) { }
28
28
 
29
29
  return result;
30
30
  }
@@ -208,7 +208,7 @@ export class Variable extends BaseNode {
208
208
  get __isVirtual() {
209
209
  const processV2 = this.getAncestor('ProcessV2') as ProcessV2;
210
210
  const process = this.getAncestor('Process') as Process;
211
- const isDisabled = !!this.getAncestor('HistoryProcessV2') || ['enabled', 'history'].includes(processV2?.status)
211
+ const isDisabled = !!this.getAncestor('HistoryProcessV2') || ['enabled', 'history'].includes(processV2?.status);
212
212
  return isDisabled || process?.__isVirtual || false;
213
213
  }
214
214
 
@@ -305,6 +305,9 @@ export class Variable extends BaseNode {
305
305
  @withSourceMap()
306
306
  toNaturalTS(state = createCompilerState()): string {
307
307
  let code = this.name;
308
+ if (state.needNamespace) {
309
+ code = `${this.getNamespace()}.${code}`;
310
+ }
308
311
  if (this.typeAnnotation) {
309
312
  code += ': ';
310
313
  code += this.typeAnnotation.toNaturalTS(shiftState(state, code));
@@ -363,9 +366,8 @@ export class Variable extends BaseNode {
363
366
  * @param name 一般不用传,用于 rename
364
367
  */
365
368
  getEmbeddedFilePath(name = this.name) {
366
- return `/embedded/${this.getAncestor('App')?.name}/frontendTypes/${
367
- this.getAncestor('FrontendType')?.name
368
- }/frontends/${this.getAncestor('Frontend')?.name}/variables/${name}.ts`;
369
+ return `/embedded/${this.getAncestor('App')?.name}/frontendTypes/${this.getAncestor('FrontendType')?.name
370
+ }/frontends/${this.getAncestor('Frontend')?.name}/variables/${name}.ts`;
369
371
  }
370
372
 
371
373
  *toEmbeddedTSFile(): EmbeddedTSFileGenerator {
@@ -14,6 +14,7 @@ import {
14
14
  getDependencies,
15
15
  getFrontendVariables,
16
16
  getServerVariables,
17
+ getVariablesCode,
17
18
  getFrontendViews,
18
19
  getCurrentNodeContext,
19
20
  getCurrentNodeContextForUI,
@@ -233,9 +234,9 @@ export const genNaturalTSContextFromJSONForLogic = (
233
234
 
234
235
  if (logicType === 'view_logic') {
235
236
  code += '\n### [10] 页面上下文\n';
236
- if (frontendVariables) {
237
+ if (frontendVariables?.data?.length) {
237
238
  code += '\n#### [10-0] 前端全局变量\n';
238
- code += frontendVariables;
239
+ code += getVariablesCode(frontendVariables);
239
240
  }
240
241
 
241
242
  if (views?.data?.length) {
@@ -244,7 +245,9 @@ export const genNaturalTSContextFromJSONForLogic = (
244
245
  }
245
246
  } else {
246
247
  code += '\n### [10] 全局变量\n';
247
- code += backendVariables;
248
+ if (backendVariables?.data?.length) {
249
+ code += getVariablesCode(backendVariables);
250
+ }
248
251
  }
249
252
 
250
253
  code += '\n### 当前逻辑上下文\n';
@@ -439,11 +442,10 @@ export const genNaturalTSContextFromJSONForUI = (
439
442
  }
440
443
 
441
444
  code += '\n### [10] 页面上下文\n';
442
- if (frontendVariables) {
445
+ if (frontendVariables?.data?.length) {
443
446
  code += '\n#### [10-0] 前端全局变量\n';
444
- code += frontendVariables;
447
+ code += getVariablesCode(frontendVariables);
445
448
  }
446
-
447
449
  if (views?.data?.length) {
448
450
  code += '\n#### [10-1] 前端页面列表\n';
449
451
  code += getFrontendViewsCode(views);
@@ -544,22 +544,37 @@ export const getDependenciesCode = (dependencies: any, codeType: 'detail' | 'sho
544
544
 
545
545
  export function getFrontendVariables(frontend: Frontend, state: TranslatorState) {
546
546
  let code = '';
547
+ const data = [] as Snippet[];
548
+ const namespace = frontend?.variables?.[0]?.getNamespace() || '';
547
549
  frontend?.variables?.forEach((variable) => {
548
- const tempCode = `let ${variable?.toNaturalTS(shiftState(state, code, { inline: true }))};\n`;
550
+ const tempCode = `let ${variable?.toNaturalTS(shiftState(state, code, { inline: true, needNamespace: true, }))};\n`;
551
+ data.push({ description: variable?.description || '', code: tempCode });
549
552
  code += tempCode;
550
553
  });
551
- return { code: wrapTSBlock(code), frontendVariables: code };
554
+ return { namespace, frontendVariables: { namespace, data } };
552
555
  }
553
556
 
554
557
  export function getServerVariables(app: App, state: TranslatorState) {
555
558
  let code = '';
559
+ const data = [] as Snippet[];
560
+ const namespace = 'app.backend.variables';
556
561
  app.backend?.variables?.forEach((variable) => {
557
- const tempCode = `let ${variable?.toNaturalTS(shiftState(state, code, { inline: true }))};\n`;
562
+ const tempCode = `let ${variable?.toNaturalTS(shiftState(state, code, { inline: true, needNamespace: true, }))};\n`;
563
+ data.push({ description: variable?.description || '', code: tempCode });
558
564
  code += tempCode;
559
565
  });
560
- return { code: wrapTSBlock(code), backendVariables: code };
566
+ return { namespace, backendVariables: { namespace, data } };
561
567
  }
562
568
 
569
+ export const getVariablesCode = (variables: any) => {
570
+ let code = '';
571
+ variables?.data?.forEach((item: any) => {
572
+ code += item.description ? `/* ${item.description} */ \n` : '';
573
+ code += item.code;
574
+ });
575
+ return wrapTSBlock(code);
576
+ };
577
+
563
578
  export function getChildrenViews(view: View, currentNode: BaseNode, convertChildren: boolean, prefix: string) {
564
579
  const data = [] as Snippet[];
565
580
  if (view?.children?.length > 0) {
@@ -697,6 +697,14 @@ function transformNode2Expression(node: babelTypes.Node, type?: string): any {
697
697
  return switchStatement;
698
698
  }
699
699
  throwError(`Unhandled ArrowFunctionExpression node ${node.type}`);
700
+ } else if (/nasl.auth./.test(calleeName)) {
701
+ return new naslTypes.CallLogic({
702
+ calleeNamespace: 'nasl.auth',
703
+ calleeName: (callee[callee.length - 1] as babelTypes.Identifier).name,
704
+ arguments: node?.arguments?.map((arg) => new naslTypes.Argument({
705
+ expression: transformNode2Expression(arg, type),
706
+ })) as naslTypes.Argument[],
707
+ });
700
708
  } else {
701
709
  throwError(`Unhandled node ${callee.map((item: babelTypes.Identifier) => item.name).join('.')} ${node.type}`);
702
710
  }
@@ -783,6 +791,25 @@ function transformNode2Expression(node: babelTypes.Node, type?: string): any {
783
791
  }),
784
792
  });
785
793
  }
794
+ if (calleeName?.includes('nasl.auth.')) {
795
+ return new naslTypes.MemberExpression({
796
+ object: new naslTypes.Identifier({
797
+ namespace: 'nasl.auth',
798
+ name: (callee?.[2] as babelTypes.Identifier)?.name,
799
+ }),
800
+ property: new naslTypes.Identifier({
801
+ name: (callee?.[3] as babelTypes.Identifier)?.name,
802
+ }),
803
+ });
804
+ }
805
+ const regex = /^(app\..+?)\.variables\.(.+)$/;
806
+ if (regex.test(calleeName)) {
807
+ const [, namespace, variableName] = calleeName.match(regex);
808
+ return new naslTypes.Identifier({
809
+ name: String(variableName),
810
+ namespace: `${namespace}.variables`
811
+ });
812
+ }
786
813
  if (node.property.type === 'NumericLiteral') {
787
814
  return new naslTypes.CallFunction({
788
815
  calleeNamespace: 'nasl.util',
@@ -699,19 +699,7 @@ export function transformTSCode(tsCode: string, contextLogicName: string, type?:
699
699
  expression: transformLogicNode(arg),
700
700
  })) as naslTypes.Argument[],
701
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') {
702
+ } else if (node.callee.type === 'ArrowFunctionExpression') {
715
703
  // NOTE: 适用于 if else
716
704
  const newCallee = (node.callee.body as any).body;
717
705
  if (newCallee[newCallee.length - 1].argument?.callee?.name === '__MatchExpressionFuntion') {
@@ -757,6 +745,14 @@ export function transformTSCode(tsCode: string, contextLogicName: string, type?:
757
745
  return switchStatement;
758
746
  }
759
747
  throwError(`Unhandled ArrowFunctionExpression node ${node.type}`);
748
+ } else if (/nasl.auth./.test(calleeName)) {
749
+ return new naslTypes.CallLogic({
750
+ calleeNamespace: 'nasl.auth',
751
+ calleeName: (callee[callee.length - 1] as babelTypes.Identifier).name,
752
+ arguments: node?.arguments?.map((arg) => new naslTypes.Argument({
753
+ expression: transformLogicNode(arg),
754
+ })) as naslTypes.Argument[],
755
+ });
760
756
  } else {
761
757
  throwError(`Unhandled node ${callee.map((item: babelTypes.Identifier) => item.name).join('.')} ${node.type}`);
762
758
  }
@@ -843,6 +839,25 @@ export function transformTSCode(tsCode: string, contextLogicName: string, type?:
843
839
  }),
844
840
  });
845
841
  }
842
+ if (calleeName?.includes('nasl.auth.')) {
843
+ return new naslTypes.MemberExpression({
844
+ object: new naslTypes.Identifier({
845
+ namespace: 'nasl.auth',
846
+ name: (callee?.[2] as babelTypes.Identifier)?.name,
847
+ }),
848
+ property: new naslTypes.Identifier({
849
+ name: (callee?.[3] as babelTypes.Identifier)?.name,
850
+ }),
851
+ });
852
+ }
853
+ const regex = /^(app\..+?)\.variables\.(.+)$/;
854
+ if (regex.test(calleeName)) {
855
+ const [, namespace, variableName] = calleeName.match(regex);
856
+ return new naslTypes.Identifier({
857
+ name: String(variableName),
858
+ namespace: `${namespace}.variables`
859
+ });
860
+ }
846
861
  if (node.property.type === 'NumericLiteral') {
847
862
  return new naslTypes.CallFunction({
848
863
  calleeNamespace: 'nasl.util',