@hpcc-js/observablehq-compiler 3.7.6 → 3.7.8

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/writer.ts CHANGED
@@ -1,83 +1,83 @@
1
- import { ohq } from "./observable-shim.ts";
2
- import { ParsedImportCell, ParsedVariable } from "./cst.ts";
3
-
4
- export class Writer {
5
-
6
- protected _files: ohq.File[] = [];
7
- protected _imports: string[] = [];
8
- protected _functions: string[] = [];
9
- protected _defines: string[] = [];
10
- protected _defineUid = 0;
11
- protected _functionUid = 0;
12
-
13
- constructor() {
14
- }
15
-
16
- toString() {
17
- return `\
18
- ${this._imports.join("\n")}
19
-
20
- ${this._functions.join("\n").split("\n) {").join("){")}
21
-
22
- export default function define(runtime, observer) {
23
- const main = runtime.module();
24
-
25
- function toString() { return this.url; }
26
- const fileAttachments = new Map([
27
- ${this._files.map(f => `["${f.name}", { url: new URL("${f.url}"), mimeType: ${JSON.stringify(f.mime_type)}, toString }]`).join(",\n ")}
28
- ]);
29
- main.builtin("FileAttachment", runtime.fileAttachments(name => fileAttachments.get(name)));
30
-
31
- ${this._defines.join("\n ")}
32
-
33
- return main;
34
- }\n`;
35
- }
36
-
37
- files(files: ohq.File[]) {
38
- this._files = [...this._files, ...files];
39
- }
40
-
41
- import(url: string) {
42
- this._imports.push(`import define${++this._defineUid} from "${url}"; `);
43
- }
44
-
45
- importDefine(imp: Partial<ParsedImportCell>) {
46
- const impInjections = imp.injections ?? [];
47
- const injections = impInjections.map(inj => {
48
- return inj.name === inj.alias ?
49
- `"${inj.name}"` :
50
- `{name: "${inj.name}", alias: "${inj.alias}"}`;
51
- });
52
- const derive = impInjections.length ? `.derive([${injections.join(", ")}], main)` : "";
53
- this._defines.push(`const child${this._defineUid} = runtime.module(define${this._defineUid})${derive};`);
54
- const impSpecifiers = imp.specifiers ?? [];
55
- impSpecifiers.forEach(s => {
56
- this._defines.push(`main.import("${s.name}"${s.alias && s.alias !== s.name ? `, "${s.alias}"` : ""}, child${this._defineUid}); `);
57
- });
58
- }
59
-
60
- function(variable: Partial<ParsedVariable>) {
61
- let id = variable.id ?? `${++this._functionUid}`;
62
- const idParts = id.split(" ");
63
- id = `_${idParts[idParts.length - 1]}`;
64
- this._functions.push(`${variable.func?.toString()?.replace("anonymous", `${id}`)}`);
65
- return id;
66
- }
67
-
68
- define(variable: Partial<ParsedVariable>, observable = true, inlineFunc = false, funcId?: string) {
69
- funcId = funcId ?? variable.id;
70
- const observe = observable ? `.variable(observer(${variable.id ? JSON.stringify(variable.id) : ""}))` : "";
71
- const id = variable.id ? `${JSON.stringify(variable.id)}, ` : "";
72
- const variableInputs = variable.inputs ?? [];
73
- const inputs = variableInputs.length ? `[${variableInputs.map(i => JSON.stringify(i)).join(", ")}], ` : "";
74
- const func = inlineFunc ?
75
- variable.func?.toString() :
76
- funcId;
77
- this._defines.push(`main${observe}.define(${id}${inputs}${func});`);
78
- }
79
-
80
- error(msg: string) {
81
- }
82
- }
83
-
1
+ import { ohq } from "./observable-shim.ts";
2
+ import { ParsedImportCell, ParsedVariable } from "./cst.ts";
3
+
4
+ export class Writer {
5
+
6
+ protected _files: ohq.File[] = [];
7
+ protected _imports: string[] = [];
8
+ protected _functions: string[] = [];
9
+ protected _defines: string[] = [];
10
+ protected _defineUid = 0;
11
+ protected _functionUid = 0;
12
+
13
+ constructor() {
14
+ }
15
+
16
+ toString() {
17
+ return `\
18
+ ${this._imports.join("\n")}
19
+
20
+ ${this._functions.join("\n").split("\n) {").join("){")}
21
+
22
+ export default function define(runtime, observer) {
23
+ const main = runtime.module();
24
+
25
+ function toString() { return this.url; }
26
+ const fileAttachments = new Map([
27
+ ${this._files.map(f => `["${f.name}", { url: new URL("${f.url}"), mimeType: ${JSON.stringify(f.mime_type)}, toString }]`).join(",\n ")}
28
+ ]);
29
+ main.builtin("FileAttachment", runtime.fileAttachments(name => fileAttachments.get(name)));
30
+
31
+ ${this._defines.join("\n ")}
32
+
33
+ return main;
34
+ }\n`;
35
+ }
36
+
37
+ files(files: ohq.File[]) {
38
+ this._files = [...this._files, ...files];
39
+ }
40
+
41
+ import(url: string) {
42
+ this._imports.push(`import define${++this._defineUid} from "${url}"; `);
43
+ }
44
+
45
+ importDefine(imp: Partial<ParsedImportCell>) {
46
+ const impInjections = imp.injections ?? [];
47
+ const injections = impInjections.map(inj => {
48
+ return inj.name === inj.alias ?
49
+ `"${inj.name}"` :
50
+ `{name: "${inj.name}", alias: "${inj.alias}"}`;
51
+ });
52
+ const derive = impInjections.length ? `.derive([${injections.join(", ")}], main)` : "";
53
+ this._defines.push(`const child${this._defineUid} = runtime.module(define${this._defineUid})${derive};`);
54
+ const impSpecifiers = imp.specifiers ?? [];
55
+ impSpecifiers.forEach(s => {
56
+ this._defines.push(`main.import("${s.name}"${s.alias && s.alias !== s.name ? `, "${s.alias}"` : ""}, child${this._defineUid}); `);
57
+ });
58
+ }
59
+
60
+ function(variable: Partial<ParsedVariable>) {
61
+ let id = variable.id ?? `${++this._functionUid}`;
62
+ const idParts = id.split(" ");
63
+ id = `_${idParts[idParts.length - 1]}`;
64
+ this._functions.push(`${variable.func?.toString()?.replace("anonymous", `${id}`)}`);
65
+ return id;
66
+ }
67
+
68
+ define(variable: Partial<ParsedVariable>, observable = true, inlineFunc = false, funcId?: string) {
69
+ funcId = funcId ?? variable.id;
70
+ const observe = observable ? `.variable(observer(${variable.id ? JSON.stringify(variable.id) : ""}))` : "";
71
+ const id = variable.id ? `${JSON.stringify(variable.id)}, ` : "";
72
+ const variableInputs = variable.inputs ?? [];
73
+ const inputs = variableInputs.length ? `[${variableInputs.map(i => JSON.stringify(i)).join(", ")}], ` : "";
74
+ const func = inlineFunc ?
75
+ variable.func?.toString() :
76
+ funcId;
77
+ this._defines.push(`main${observe}.define(${id}${inputs}${func});`);
78
+ }
79
+
80
+ error(msg: string) {
81
+ }
82
+ }
83
+