motoko 3.1.3 → 3.2.1

Sign up to get free protection for your applications and to get access to all the features.
package/src/ast.ts CHANGED
@@ -16,6 +16,7 @@ export interface Source {
16
16
  }
17
17
 
18
18
  export interface Node extends Partial<Source> {
19
+ parent?: Node | undefined;
19
20
  name: string;
20
21
  type?: string;
21
22
  doc?: string;
@@ -23,13 +24,18 @@ export interface Node extends Partial<Source> {
23
24
  args?: AST[];
24
25
  }
25
26
 
26
- export function simplifyAST(ast: CompilerNode): Node;
27
- export function simplifyAST(ast: CompilerAST[]): AST[];
28
- export function simplifyAST<T extends CompilerAST>(ast: T): T;
29
-
30
- export function simplifyAST(ast: CompilerAST): AST {
27
+ export function simplifyAST(ast: CompilerNode, parent?: Node | undefined): Node;
28
+ export function simplifyAST(
29
+ ast: CompilerAST[],
30
+ parent?: Node | undefined,
31
+ ): AST[];
32
+ export function simplifyAST<T extends CompilerAST>(
33
+ ast: T,
34
+ parent?: Node | undefined,
35
+ ): T;
36
+ export function simplifyAST(ast: CompilerAST, parent?: Node | undefined): AST {
31
37
  if (Array.isArray(ast)) {
32
- return ast.map((a) => simplifyAST(a));
38
+ return ast.map((a) => simplifyAST(a, parent));
33
39
  }
34
40
  if (typeof ast !== 'object') {
35
41
  return ast;
@@ -40,13 +46,12 @@ export function simplifyAST(ast: CompilerAST): AST {
40
46
  CompilerSpan,
41
47
  CompilerAST,
42
48
  ];
43
- const node: Node = {
44
- ...(typeof subAst === 'string'
49
+ const node: Node =
50
+ typeof subAst === 'string'
45
51
  ? { name: subAst }
46
- : simplifyAST(subAst as any as CompilerNode)),
47
- start: [+start.args[1], +start.args[2]],
48
- end: [+end.args[1], +end.args[2]],
49
- };
52
+ : simplifyAST(subAst as any as CompilerNode, parent);
53
+ node.start = [+start.args[1], +start.args[2]];
54
+ node.end = [+end.args[1], +end.args[2]];
50
55
  const file = start.args[0];
51
56
  if (file) {
52
57
  node.file = file;
@@ -58,7 +63,7 @@ export function simplifyAST(ast: CompilerAST): AST {
58
63
  return {
59
64
  ...(typeof typeAst === 'string'
60
65
  ? { name: typeAst }
61
- : simplifyAST(typeAst)),
66
+ : simplifyAST(typeAst, parent)),
62
67
  type,
63
68
  };
64
69
  }
@@ -67,12 +72,17 @@ export function simplifyAST(ast: CompilerAST): AST {
67
72
  return {
68
73
  ...(typeof docAst === 'string'
69
74
  ? { name: docAst }
70
- : simplifyAST(docAst)),
75
+ : simplifyAST(docAst, parent)),
71
76
  doc,
72
77
  };
73
78
  }
74
- return {
79
+ const node: Node = {
75
80
  name: ast.name,
76
- args: simplifyAST(ast.args),
77
81
  };
82
+ Object.defineProperty(node, 'parent', {
83
+ value: parent,
84
+ enumerable: false,
85
+ });
86
+ node.args = simplifyAST(ast.args, node);
87
+ return node;
78
88
  }