@lcap/nasl-translator 3.9.0-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/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # Nasl Translator
2
+
3
+ Nasl 代码翻译模块
@@ -0,0 +1,2 @@
1
+ export declare const statementMap: string[];
2
+ //# sourceMappingURL=constant.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constant.d.ts","sourceRoot":"","sources":["../src/constant.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,YAAY,UAcxB,CAAC"}
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.statementMap = void 0;
4
+ exports.statementMap = [
5
+ "Assignment",
6
+ "BatchAssignment",
7
+ "IfStatement",
8
+ "ForEachStatement",
9
+ "SwitchStatement",
10
+ "WhileStatement",
11
+ "Match",
12
+ "Comment",
13
+ "CallLogic",
14
+ "CallInterface",
15
+ "Destination",
16
+ "JSBlock",
17
+ 'JavaLogic'
18
+ ];
19
+ //# sourceMappingURL=constant.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constant.js","sourceRoot":"","sources":["../src/constant.ts"],"names":[],"mappings":";;;AAAa,QAAA,YAAY,GAAG;IAC3B,YAAY;IACZ,iBAAiB;IACjB,aAAa;IACb,kBAAkB;IAClB,iBAAiB;IACjB,gBAAgB;IAChB,OAAO;IACP,SAAS;IACT,WAAW;IACX,eAAe;IACf,aAAa;IACb,SAAS;IACT,WAAW;CACX,CAAC"}
@@ -0,0 +1,20 @@
1
+ /**
2
+ * 翻译方法修饰器
3
+ *
4
+ * 只为 toNaturalTS 用
5
+ * @description 旧的翻译方法修饰器,现在用`withSourceMapGenerator`
6
+ */
7
+ export declare function withSourceMap(): (target: any, key: string | number | symbol, descriptor: PropertyDescriptor) => PropertyDescriptor;
8
+ /**
9
+ * 翻译方法修饰器
10
+ *
11
+ * @description 翻译方法修饰器,会将翻译方法包装为生成器
12
+ */
13
+ export declare function withSourceMapGenerator(target: any, key: string, descriptor: PropertyDescriptor): PropertyDescriptor;
14
+ /**
15
+ * 生成器方法修饰器
16
+ *
17
+ * @description 翻译方法修饰器,只有停顿,没有源码映射表
18
+ */
19
+ export declare function withGenerator(target: any, key: string, descriptor: PropertyDescriptor): PropertyDescriptor;
20
+ //# sourceMappingURL=decorator.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"decorator.d.ts","sourceRoot":"","sources":["../src/decorator.ts"],"names":[],"mappings":"AAIA;;;;;GAKG;AACH,wBAAgB,aAAa,aACU,GAAG,OAAO,MAAM,GAAG,MAAM,GAAG,MAAM,cAAc,kBAAkB,wBAuDxG;AAED;;;;GAIG;AACH,wBAAgB,sBAAsB,CAAC,MAAM,EAAE,GAAG,EAAE,GAAG,EAAE,MAAM,EAAE,UAAU,EAAE,kBAAkB,sBAuD9F;AAED;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,MAAM,EAAE,GAAG,EAAE,GAAG,EAAE,MAAM,EAAE,UAAU,EAAE,kBAAkB,sBAWrF"}
@@ -0,0 +1,138 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.withGenerator = exports.withSourceMapGenerator = exports.withSourceMap = void 0;
4
+ const constant_1 = require("./constant");
5
+ const utils_1 = require("./utils");
6
+ /**
7
+ * 翻译方法修饰器
8
+ *
9
+ * 只为 toNaturalTS 用
10
+ * @description 旧的翻译方法修饰器,现在用`withSourceMapGenerator`
11
+ */
12
+ function withSourceMap() {
13
+ return function handleAction(target, key, descriptor) {
14
+ const oldMethod = descriptor.value;
15
+ const isNatural = oldMethod.name === 'toNaturalTS';
16
+ descriptor.value = function (state, ...args) {
17
+ // 重新生成ts代码就先把类型清除掉
18
+ // 注意排查错误的时候,调用了这个方法,就会把节点上缓存的__TypeAnnotation清除
19
+ try {
20
+ this.__isCorrectTypeAnnotation = false;
21
+ }
22
+ catch (err) { }
23
+ if (!state) {
24
+ // 没有状态不生成 SourceMap
25
+ return oldMethod.call(this, state, ...args);
26
+ }
27
+ let before = '';
28
+ if (!state.inline) {
29
+ before = (0, utils_1.indent)((state?.tabSize || 0));
30
+ state = (0, utils_1.shiftState)(state, before, { rename: state.rename });
31
+ }
32
+ else {
33
+ // state = shiftState(state, before, {
34
+ // inline: undefined,
35
+ // });
36
+ }
37
+ if (isNatural && state.focusedNodePath) {
38
+ const needAddComment = this.nodePath === state.focusedNodePath;
39
+ // if (!needAddComment) {
40
+ // const parts = state.focusedNodePath.split('.');
41
+ // const lastPart = parts.pop();
42
+ // }
43
+ if (needAddComment) {
44
+ const beforeComment = `/* 当前位置 */\n${before}`;
45
+ before += beforeComment;
46
+ state = (0, utils_1.shiftState)(state, beforeComment, { rename: state.rename });
47
+ }
48
+ }
49
+ // 调用真实方法, 可能有剩余参数
50
+ const code = target && this.nodePath === state.blankNodePath ? (this.concept === 'Argument' && args[1] ? `__BLANK__` : '__BLANK__') : oldMethod.call(this, state, ...args);
51
+ state.sourceMap?.set(this, {
52
+ start: state.position,
53
+ end: (0, utils_1.shiftPosition)(state.position, code),
54
+ });
55
+ if (isNatural && state.nodePathComment && constant_1.statementMap?.includes(this.concept) && code) {
56
+ before += `// nodePath: '${this.nodePath}' \n${(0, utils_1.indent)((state?.tabSize || 0))}`;
57
+ }
58
+ return before + code;
59
+ };
60
+ return descriptor;
61
+ };
62
+ }
63
+ exports.withSourceMap = withSourceMap;
64
+ /**
65
+ * 翻译方法修饰器
66
+ *
67
+ * @description 翻译方法修饰器,会将翻译方法包装为生成器
68
+ */
69
+ function withSourceMapGenerator(target, key, descriptor) {
70
+ const oldMethod = descriptor.value;
71
+ const newMethod = function* withSourceMapGenerator(state, ...args) {
72
+ // 重新生成ts代码就先把类型清除掉
73
+ // 注意排查错误的时候,调用了这个方法,就会把节点上缓存的__TypeAnnotation清除
74
+ try {
75
+ this.__isCorrectTypeAnnotation = false;
76
+ }
77
+ catch (err) {
78
+ // ..
79
+ }
80
+ // 没有状态不生成 SourceMap
81
+ if (!state) {
82
+ yield;
83
+ const code = yield* oldMethod.call(this, state, ...args);
84
+ yield;
85
+ return code;
86
+ }
87
+ let before = '';
88
+ if (!state.inline) {
89
+ before = (0, utils_1.indent)((state?.tabSize || 0));
90
+ state = (0, utils_1.shiftState)(state, before);
91
+ }
92
+ else {
93
+ state = (0, utils_1.shiftState)(state, before, {
94
+ inline: undefined,
95
+ });
96
+ }
97
+ yield;
98
+ const code = yield* oldMethod.call(this, state, ...args);
99
+ yield;
100
+ const start = {
101
+ ...state.position
102
+ };
103
+ if (this.concept === 'BusinessComponent') {
104
+ const lines = code.split('\n');
105
+ const lineOffset = lines.findIndex((item) => item.startsWith('export class '));
106
+ if (lineOffset) {
107
+ start.line = start.line + lineOffset + 1;
108
+ start.offset = 13;
109
+ }
110
+ }
111
+ state.sourceMap?.set(this, {
112
+ start,
113
+ end: (0, utils_1.shiftPosition)(state.position, code),
114
+ });
115
+ return before + code;
116
+ };
117
+ descriptor.value = newMethod;
118
+ return descriptor;
119
+ }
120
+ exports.withSourceMapGenerator = withSourceMapGenerator;
121
+ /**
122
+ * 生成器方法修饰器
123
+ *
124
+ * @description 翻译方法修饰器,只有停顿,没有源码映射表
125
+ */
126
+ function withGenerator(target, key, descriptor) {
127
+ const oldMethod = descriptor.value;
128
+ const newMethod = function* (state, ...args) {
129
+ yield;
130
+ const code = yield* oldMethod.call(this, state, ...args);
131
+ yield;
132
+ return code;
133
+ };
134
+ descriptor.value = newMethod;
135
+ return descriptor;
136
+ }
137
+ exports.withGenerator = withGenerator;
138
+ //# sourceMappingURL=decorator.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"decorator.js","sourceRoot":"","sources":["../src/decorator.ts"],"names":[],"mappings":";;;AACA,yCAA0C;AAC1C,mCAA4D;AAE5D;;;;;GAKG;AACH,SAAgB,aAAa;IAC3B,OAAO,SAAS,YAAY,CAAC,MAAW,EAAE,GAA6B,EAAE,UAA8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I,GAAG,UAAU,GAAG,CAAC,CAAC;gBACzC,KAAK,CAAC,MAAM,GAAG,EAAE,CAAC;YACpB,CAAC;QACH,CAAC;QACD,KAAK,CAAC,SAAS,EAAE,GAAG,CAAC,IAAI,EAAE;YACzB,KAAK;YACL,GAAG,EAAE,IAAA,qBAAa,EAAC,KAAK,CAAC,QAAQ,EAAE,IAAI,CAAC;SACzC,CAAC,CAAC;QAEH,OAAO,MAAM,GAAG,IAAI,CAAC;IACvB,CAAC,CAAC;IAEF,UAAU,CAAC,KAAK,GAAG,SAAS,CAAC;IAE7B,OAAO,UAAU,CAAC;AACpB,CAAC;AAvDD,wDAuDC;AAED;;;;GAIG;AACH,SAAgB,aAAa,CAAC,MAAW,EAAE,GAAW,EAAE,UAA8B;IACpF,MAAM,SAAS,GAAG,UAAU,CAAC,KAAoC,CAAC;IAClE,MAAM,SAAS,GAAgC,QAAQ,CAAC,EAA0B,KAAiC,EAAE,GAAG,IAAQ;QAC9H,KAAK,CAAC;QACN,MAAM,IAAI,GAAG,KAAK,CAAC,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,EAAE,KAAK,EAAE,GAAG,IAAI,CAAC,CAAC;QACzD,KAAK,CAAC;QACN,OAAO,IAAI,CAAC;IACd,CAAC,CAAC;IAEF,UAAU,CAAC,KAAK,GAAG,SAAS,CAAC;IAC7B,OAAO,UAAU,CAAC;AACpB,CAAC;AAXD,sCAWC"}
package/out/index.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ export * from './utils';
2
+ export * from './decorator';
3
+ import { Translator } from '@lcap/nasl-types';
4
+ export type TranslatorState = Translator.TranslatorState;
5
+ export type TranslatorGenerator = Translator.TranslatorGenerator;
6
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC;AACxB,cAAc,aAAa,CAAC;AAE5B,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAE9C,MAAM,MAAM,eAAe,GAAG,UAAU,CAAC,eAAe,CAAC;AACzD,MAAM,MAAM,mBAAmB,GAAG,UAAU,CAAC,mBAAmB,CAAC"}
package/out/index.js ADDED
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./utils"), exports);
18
+ __exportStar(require("./decorator"), exports);
19
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,0CAAwB;AACxB,8CAA4B"}
package/out/utils.d.ts ADDED
@@ -0,0 +1,26 @@
1
+ import type { Translator, Concept } from '@lcap/nasl-types';
2
+ export declare function lsp2tspNumber(num: number): number;
3
+ export declare const indent: (tabSize: number) => string;
4
+ export declare const padRemainLeft: (content: string, space: string) => string;
5
+ /**
6
+ * 创建编译器状态
7
+ */
8
+ export declare function createCompilerState<Node extends Concept.BaseNode = Concept.BaseNode>(startCode?: string, options?: Partial<Translator.TranslatorState<Node>>): Translator.TranslatorState<Node>;
9
+ /**
10
+ * 根据代码移动位置
11
+ */
12
+ export declare function shiftPosition(pos: Translator.Position, code?: string): Translator.Position;
13
+ /**
14
+ * 根据代码移动状态
15
+ */
16
+ export declare function shiftState<Node extends Concept.BaseNode = Concept.BaseNode>(state: Translator.TranslatorState<Node>, code?: string, options?: Partial<Translator.TranslatorState<Node>>): Translator.TranslatorState<Node>;
17
+ export declare function isSameRange(rangeA: Translator.Range, rangeB: Translator.Range): boolean;
18
+ export declare function createDecoratorCode<T extends Concept.BaseNode>(state: Translator.TranslatorState, node: T, properties: Array<keyof T>, options?: {
19
+ '@'?: boolean;
20
+ concept?: boolean;
21
+ key?: boolean;
22
+ }): string;
23
+ export declare function createIdentifierCode(name: string): string;
24
+ export declare function textualNamespace(name: string): string;
25
+ export declare function createDescriptionComment<T extends Concept.BaseNode>(state: Translator.TranslatorState, node: T, properties: Array<keyof T>): string;
26
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAE5D,wBAAgB,aAAa,CAAC,GAAG,EAAE,MAAM,UAExC;AAED,eAAO,MAAM,MAAM,YAAa,MAAM,WAA4B,CAAC;AACnE,eAAO,MAAM,aAAa,YAAa,MAAM,SAAS,MAAM,WAA2F,CAAC;AAExJ;;GAEG;AACH,wBAAgB,mBAAmB,CACjC,IAAI,SAAS,OAAO,CAAC,QAAQ,GAAG,OAAO,CAAC,QAAQ,EAChD,SAAS,GAAE,MAAW,EAAE,OAAO,CAAC,EAAE,OAAO,CAAC,UAAU,CAAC,eAAe,CAAC,IAAI,CAAC,CAAC,oCAgB5E;AAED;;GAEG;AACH,wBAAgB,aAAa,CAAC,GAAG,EAAE,UAAU,CAAC,QAAQ,EAAE,IAAI,GAAE,MAAW,GAAG,UAAU,CAAC,QAAQ,CAgB9F;AAED;;GAEG;AACH,wBAAgB,UAAU,CAAC,IAAI,SAAS,OAAO,CAAC,QAAQ,GAAG,OAAO,CAAC,QAAQ,EACzE,KAAK,EAAE,UAAU,CAAC,eAAe,CAAC,IAAI,CAAC,EACvC,IAAI,CAAC,EAAE,MAAM,EACb,OAAO,CAAC,EAAE,OAAO,CAAC,UAAU,CAAC,eAAe,CAAC,IAAI,CAAC,CAAC,GAClD,UAAU,CAAC,eAAe,CAAC,IAAI,CAAC,CAKlC;AAED,wBAAgB,WAAW,CAAC,MAAM,EAAE,UAAU,CAAC,KAAK,EAAE,MAAM,EAAE,UAAU,CAAC,KAAK,WAE7E;AAED,wBAAgB,mBAAmB,CAAC,CAAC,SAAS,OAAO,CAAC,QAAQ,EAAE,KAAK,EAAE,UAAU,CAAC,eAAe,EAAE,IAAI,EAAE,CAAC,EAAE,UAAU,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC,EAAE,OAAO,CAAC,EAAE;IAChJ,GAAG,CAAC,EAAE,OAAO,CAAC;IACd,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,GAAG,CAAC,EAAE,OAAO,CAAC;CACf,UAsCA;AAED,wBAAgB,oBAAoB,CAAC,IAAI,EAAE,MAAM,UAKhD;AAED,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,UAE5C;AAED,wBAAgB,wBAAwB,CAAC,CAAC,SAAS,OAAO,CAAC,QAAQ,EAAE,KAAK,EAAE,UAAU,CAAC,eAAe,EAAE,IAAI,EAAE,CAAC,EAAE,UAAU,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC,UAG1I"}
package/out/utils.js ADDED
@@ -0,0 +1,122 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createDescriptionComment = exports.textualNamespace = exports.createIdentifierCode = exports.createDecoratorCode = exports.isSameRange = exports.shiftState = exports.shiftPosition = exports.createCompilerState = exports.padRemainLeft = exports.indent = exports.lsp2tspNumber = void 0;
4
+ function lsp2tspNumber(num) {
5
+ return num + 1;
6
+ }
7
+ exports.lsp2tspNumber = lsp2tspNumber;
8
+ const indent = (tabSize) => ' '.repeat(tabSize * 4);
9
+ exports.indent = indent;
10
+ const padRemainLeft = (content, space) => content.split('\n').map((line, index) => (index === 0 ? '' : space) + line).join('\n');
11
+ exports.padRemainLeft = padRemainLeft;
12
+ /**
13
+ * 创建编译器状态
14
+ */
15
+ function createCompilerState(startCode = '', options) {
16
+ const state = {
17
+ position: {
18
+ line: 0,
19
+ character: 0,
20
+ offset: 0,
21
+ },
22
+ tabSize: 0,
23
+ ...options,
24
+ };
25
+ if (!state.sourceMap) {
26
+ state.sourceMap = new Map();
27
+ }
28
+ return shiftState(state, startCode);
29
+ }
30
+ exports.createCompilerState = createCompilerState;
31
+ /**
32
+ * 根据代码移动位置
33
+ */
34
+ function shiftPosition(pos, code = '') {
35
+ const res = { ...pos };
36
+ if (code) {
37
+ if (typeof code !== 'string')
38
+ console.error('code is not a string:', code);
39
+ const arr = code.split('\n');
40
+ res.offset += code.length;
41
+ res.line += arr.length - 1;
42
+ if (arr.length === 1) {
43
+ res.character += arr[arr.length - 1].length;
44
+ }
45
+ else {
46
+ res.character = arr[arr.length - 1].length;
47
+ }
48
+ }
49
+ return Object.freeze(res);
50
+ }
51
+ exports.shiftPosition = shiftPosition;
52
+ /**
53
+ * 根据代码移动状态
54
+ */
55
+ function shiftState(state, code, options) {
56
+ state = { ...state };
57
+ state.position = shiftPosition(state.position, code);
58
+ Object.assign(state, { rename: undefined }, options); // 优先 options
59
+ return state;
60
+ }
61
+ exports.shiftState = shiftState;
62
+ function isSameRange(rangeA, rangeB) {
63
+ return rangeA.start.offset === rangeB.start.offset && rangeA.end.offset === rangeB.end.offset;
64
+ }
65
+ exports.isSameRange = isSameRange;
66
+ function createDecoratorCode(state, node, properties, options) {
67
+ options = {
68
+ '@': true,
69
+ concept: false,
70
+ key: true,
71
+ ...options,
72
+ };
73
+ let code = `${options['@'] ? '@' : ''}${options.concept ? node.concept : ''}(`;
74
+ for (const prop of properties) {
75
+ const value = node[prop];
76
+ if (value === undefined || value === null)
77
+ continue;
78
+ if (properties.length > 1)
79
+ code += `\n${(0, exports.indent)((state.tabSize ?? 0) + 1)}`;
80
+ // if (value === true)
81
+ // code += `${String(prop)}`;
82
+ // else
83
+ if (options.key)
84
+ code += `${String(prop)} = `;
85
+ if (value.toTextualNASL)
86
+ code += value.toTextualNASL(shiftState(state, code, {
87
+ tabSize: (state.tabSize ?? 0) + 1,
88
+ inline: true,
89
+ }));
90
+ else if (typeof value !== 'object')
91
+ code += JSON.stringify(value);
92
+ else {
93
+ code += `[ ${Object.keys(value).map((key) => `${JSON.stringify(key)} -> ${JSON.stringify(value[key])}`).join(', ')} ]`;
94
+ }
95
+ if (properties.length > 1)
96
+ code += `,`;
97
+ }
98
+ if (code === '@(')
99
+ return '';
100
+ if (properties.length > 1)
101
+ code += `\n${(0, exports.indent)(state.tabSize ?? 0)}`;
102
+ code += `)\n${(0, exports.indent)(state.tabSize ?? 0)}`;
103
+ return code;
104
+ }
105
+ exports.createDecoratorCode = createDecoratorCode;
106
+ function createIdentifierCode(name) {
107
+ if (/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(name))
108
+ return name;
109
+ else
110
+ return `\`${name}\``;
111
+ }
112
+ exports.createIdentifierCode = createIdentifierCode;
113
+ function textualNamespace(name) {
114
+ return name.replace(/\./g, '::');
115
+ }
116
+ exports.textualNamespace = textualNamespace;
117
+ function createDescriptionComment(state, node, properties) {
118
+ const content = properties.map((prop) => node[prop]).filter((value) => !!value).join(' ');
119
+ return content.trim() ? `/** ${content} */\n${(0, exports.indent)(state.tabSize ?? 0)}` : '';
120
+ }
121
+ exports.createDescriptionComment = createDescriptionComment;
122
+ //# sourceMappingURL=utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":";;;AAEA,SAAgB,aAAa,CAAC,GAAW;IACvC,OAAO,GAAG,GAAG,CAAC,CAAC;AACjB,CAAC;AAFD,sCAEC;AAEM,MAAM,MAAM,GAAG,CAAC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,35 @@
1
+ {
2
+ "name": "@lcap/nasl-translator",
3
+ "description": "NetEase Application Specific Language",
4
+ "version": "3.9.0-beta.1",
5
+ "author": "Forrest <rainforest92@126.com>",
6
+ "main": "./out",
7
+ "types": "./out",
8
+ "module": "./src",
9
+ "source": "./src",
10
+ "license": "MIT",
11
+ "files": [
12
+ "out",
13
+ "README.md",
14
+ "package.json"
15
+ ],
16
+ "dependencies": {
17
+ "@lcap/nasl-test-toolkit": "1.0.0",
18
+ "@lcap/nasl-tsconfig": "1.0.1",
19
+ "@lcap/nasl-types": "3.9.0-beta.1",
20
+ "@lcap/nasl-utils": "3.9.0-beta.1"
21
+ },
22
+ "devDependencies": {
23
+ "@types/node": "18.11.9",
24
+ "rimraf": "3.0.2",
25
+ "typescript": "5.4.4"
26
+ },
27
+ "scripts": {
28
+ "clear": "rimraf ./out",
29
+ "watch": "npm run clear && npm run build -- -w",
30
+ "build": "npm run clear && tsc -p ./tsconfig.build.json",
31
+ "test": "pnpm run --filter @lcap/nasl-test-toolkit test --config $PWD/.mocharc.js",
32
+ "test:ci": "pnpm run --filter @lcap/nasl-test-toolkit test:ci --config $PWD/.mocharc.js",
33
+ "test:update": "npm run test -- --update"
34
+ }
35
+ }