motoko 3.0.0-beta3 → 3.0.0-beta5

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/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;
@@ -28,15 +28,11 @@ export type Diagnostic = {
28
28
 
29
29
  export type WasmMode = 'ic' | 'wasi';
30
30
 
31
- export default function wrapMotoko(compiler: Compiler, version: string) {
31
+ export default function wrapMotoko(compiler: Compiler) {
32
+ const version = compiler.version || '(unknown)';
32
33
  const debug = require('debug')(`motoko:${version}`);
33
34
 
34
35
  const invoke = (key: string, unwrap: boolean, args: any[]) => {
35
- if (!compiler) {
36
- throw new Error(
37
- 'Please load a Motoko compiler before running this function',
38
- );
39
- }
40
36
  if (typeof compiler[key] !== 'function') {
41
37
  throw new Error(`Unknown compiler function: '${key}'`);
42
38
  }
@@ -68,6 +64,26 @@ export default function wrapMotoko(compiler: Compiler, version: string) {
68
64
  return result.code;
69
65
  };
70
66
 
67
+ // Function signatures for `mo.parseMotokoTyped()`
68
+ type ParseMotokoTypedResult = { ast: Node; type: Node };
69
+ function parseMotokoTyped(paths: string): ParseMotokoTypedResult;
70
+ function parseMotokoTyped(paths: string[]): ParseMotokoTypedResult[];
71
+ function parseMotokoTyped(
72
+ paths: string | string[],
73
+ ): ParseMotokoTypedResult | ParseMotokoTypedResult[] {
74
+ if (typeof paths === 'string') {
75
+ return mo.parseMotokoTyped([paths])[0];
76
+ }
77
+ return invoke('parseMotokoTyped', true, [paths]).map(
78
+ ({ ast, typ }: { ast: CompilerNode; typ: CompilerNode }) => {
79
+ return {
80
+ ast: simplifyAST(ast),
81
+ type: simplifyAST(typ),
82
+ };
83
+ },
84
+ );
85
+ }
86
+
71
87
  const mo = {
72
88
  version,
73
89
  compiler,
@@ -94,11 +110,11 @@ export default function wrapMotoko(compiler: Compiler, version: string) {
94
110
  list(directory: string): string[] {
95
111
  return invoke('readDir', false, [directory]);
96
112
  },
97
- async fetchPackage(name:string, info: string | PackageInfo) {
98
- if(!info){
113
+ async fetchPackage(name: string, info: string | PackageInfo) {
114
+ if (!info) {
99
115
  throw new Error('Please specify both a name and source');
100
116
  }
101
- return fetchPackage(name,info);
117
+ return fetchPackage(name, info);
102
118
  },
103
119
  async installPackages(packages: Record<string, string | PackageInfo>) {
104
120
  return installPackages(mo, packages);
@@ -123,8 +139,9 @@ export default function wrapMotoko(compiler: Compiler, version: string) {
123
139
  validatePackage(pkg: Package) {
124
140
  validatePackage(pkg);
125
141
  },
126
- setAliases(aliases: Record<string, string>) {
127
- debug('aliases', aliases);
142
+ setAliases(directory: string, aliases: Record<string, string>) {
143
+ debug('aliases', directory, aliases);
144
+ invoke('setCandidPath', false, [directory]);
128
145
  invoke('setActorAliases', false, [Object.entries(aliases)]);
129
146
  },
130
147
  setMetadata(values: string) {
@@ -151,20 +168,14 @@ export default function wrapMotoko(compiler: Compiler, version: string) {
151
168
  }
152
169
  return invoke('compileWasm', true, [mode, path]);
153
170
  },
171
+ parseCandid(content: string): object {
172
+ return invoke('parseCandid', true, [content]);
173
+ },
154
174
  parseMotoko(content: string): Node {
155
175
  const ast = invoke('parseMotoko', true, [content]);
156
176
  return simplifyAST(ast);
157
177
  },
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
- },
178
+ parseMotokoTyped,
168
179
  resolveMain(directory: string = ''): string | undefined {
169
180
  return resolveMain(mo, directory);
170
181
  },
@@ -1,4 +1,4 @@
1
1
  import wrapMotoko from '..';
2
2
  const { Motoko } = require('../../versions/latest/moc_interpreter.min');
3
3
 
4
- export default wrapMotoko(Motoko, 'latest/interpreter');
4
+ export default wrapMotoko(Motoko);
@@ -1,4 +1,4 @@
1
1
  import wrapMotoko from '..';
2
2
  const { Motoko } = require('../../versions/latest/moc.min');
3
3
 
4
- export default wrapMotoko(Motoko, 'latest');
4
+ export default wrapMotoko(Motoko);