motoko 3.0.0-beta2 → 3.0.0-beta4

Sign up to get free protection for your applications and to get access to all the features.
package/src/file.ts CHANGED
@@ -21,6 +21,9 @@ export const file = (mo: Motoko, path: string) => {
21
21
  get path(): string {
22
22
  return path;
23
23
  },
24
+ set path(newPath) {
25
+ path = newPath;
26
+ },
24
27
  // file(subPath) {
25
28
  // subPath = getValidPath(subPath);
26
29
  // return exports.file(`${path}/${subPath}`);
@@ -57,14 +60,14 @@ export const file = (mo: Motoko, path: string) => {
57
60
  wasm(mode: WasmMode) {
58
61
  return mo.wasm(path, mode);
59
62
  },
63
+ parseCandid() {
64
+ return mo.parseCandid(result.read());
65
+ },
60
66
  parseMotoko() {
61
67
  return mo.parseMotoko(result.read());
62
68
  },
63
69
  parseMotokoTyped() {
64
- return mo.parseMotokoTyped(result.read());
65
- },
66
- parseCandid() {
67
- return mo.parseCandid(result.read());
70
+ return mo.parseMotokoTyped([path]);
68
71
  },
69
72
  };
70
73
  return result;
package/src/index.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Node, simplifyAST } from './ast';
1
+ import { Node, simplifyAST, CompilerAST, CompilerNode } from './ast';
2
2
  import { file } from './file';
3
3
  import {
4
4
  fetchPackage,
@@ -11,9 +11,9 @@ import { resolveMain, resolveLib } from './utils/resolveEntryPoint';
11
11
 
12
12
  export type Motoko = ReturnType<typeof wrapMotoko>;
13
13
 
14
- type Compiler = any; // TODO
14
+ type Compiler = any; // TODO: generate from `js_of_ocaml`?
15
15
 
16
- // TODO
16
+ // TODO: compatibility with the VS Code or Monaco `Diagnostic` type
17
17
  export type Diagnostic = {
18
18
  code?: string | number | { target: any; value: string | number };
19
19
  message: string;
@@ -68,6 +68,26 @@ export default function wrapMotoko(compiler: Compiler, version: string) {
68
68
  return result.code;
69
69
  };
70
70
 
71
+ // Function signatures for `mo.parseMotokoTyped()`
72
+ type ParseMotokoTypedResult = { ast: Node; type: Node };
73
+ function parseMotokoTyped(paths: string): ParseMotokoTypedResult;
74
+ function parseMotokoTyped(paths: string[]): ParseMotokoTypedResult[];
75
+ function parseMotokoTyped(
76
+ paths: string | string[],
77
+ ): ParseMotokoTypedResult | ParseMotokoTypedResult[] {
78
+ if (typeof paths === 'string') {
79
+ return mo.parseMotokoTyped([paths])[0];
80
+ }
81
+ return invoke('parseMotokoTyped', true, [paths]).map(
82
+ ({ ast, typ }: { ast: CompilerNode; typ: CompilerNode }) => {
83
+ return {
84
+ ast: simplifyAST(ast),
85
+ type: simplifyAST(typ),
86
+ };
87
+ },
88
+ );
89
+ }
90
+
71
91
  const mo = {
72
92
  version,
73
93
  compiler,
@@ -94,11 +114,11 @@ export default function wrapMotoko(compiler: Compiler, version: string) {
94
114
  list(directory: string): string[] {
95
115
  return invoke('readDir', false, [directory]);
96
116
  },
97
- async fetchPackage(name:string, info: string | PackageInfo) {
98
- if(!info){
117
+ async fetchPackage(name: string, info: string | PackageInfo) {
118
+ if (!info) {
99
119
  throw new Error('Please specify both a name and source');
100
120
  }
101
- return fetchPackage(name,info);
121
+ return fetchPackage(name, info);
102
122
  },
103
123
  async installPackages(packages: Record<string, string | PackageInfo>) {
104
124
  return installPackages(mo, packages);
@@ -151,20 +171,14 @@ export default function wrapMotoko(compiler: Compiler, version: string) {
151
171
  }
152
172
  return invoke('compileWasm', true, [mode, path]);
153
173
  },
174
+ parseCandid(content: string): object {
175
+ return invoke('parseCandid', true, [content]);
176
+ },
154
177
  parseMotoko(content: string): Node {
155
178
  const ast = invoke('parseMotoko', true, [content]);
156
179
  return simplifyAST(ast);
157
180
  },
158
- parseMotokoTyped(content: string): { ast: Node; type: Node } {
159
- const { ast, typ } = invoke('parseMotokoTyped', true, [content]);
160
- return {
161
- ast: simplifyAST(ast),
162
- type: simplifyAST(typ),
163
- };
164
- },
165
- parseCandid(content: string): object {
166
- return invoke('parseCandid', true, [content]);
167
- },
181
+ parseMotokoTyped,
168
182
  resolveMain(directory: string = ''): string | undefined {
169
183
  return resolveMain(mo, directory);
170
184
  },