@hpcc-js/observablehq-compiler 1.5.0 → 1.5.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/LICENSE +43 -43
- package/README.md +105 -105
- package/bin/ojscc.mjs +73 -73
- package/dist/index.esm.js +49 -49
- package/dist/index.esm.js.map +1 -1
- package/dist/index.esm.min.js.map +1 -1
- package/dist/index.js +49 -49
- package/dist/index.js.map +1 -1
- package/dist/index.min.js.map +1 -1
- package/package.json +3 -3
- package/src/__package__.ts +3 -3
- package/src/__tests__/File Attachments.ts +894 -894
- package/src/__tests__/Introduction to Imports.ts +748 -748
- package/src/__tests__/Observable TimeChart.ts +771 -771
- package/src/__tests__/index.ts +13 -13
- package/src/__tests__/m1.mjs +3 -3
- package/src/__tests__/node.ts +199 -199
- package/src/compiler.md +234 -234
- package/src/compiler.ts +311 -311
- package/src/cst.ts +178 -178
- package/src/index.css +459 -459
- package/src/index.ts +6 -6
- package/src/util.md +113 -113
- package/src/util.ts +175 -175
- package/src/writer.ts +80 -80
package/src/__tests__/index.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
// @ts-ignore
|
|
2
|
-
import fetch, { Blob, blobFrom, blobFromSync, File, fileFrom, fileFromSync, FormData, Headers, Request, Response } from "node-fetch";
|
|
3
|
-
if (!globalThis.fetch) {
|
|
4
|
-
// @ts-ignore
|
|
5
|
-
globalThis.fetch = fetch;
|
|
6
|
-
// @ts-ignore
|
|
7
|
-
globalThis.Headers = Headers;
|
|
8
|
-
// @ts-ignore
|
|
9
|
-
globalThis.Request = Request;
|
|
10
|
-
// @ts-ignore
|
|
11
|
-
globalThis.Response = Response;
|
|
12
|
-
}
|
|
13
|
-
export * from "./node";
|
|
1
|
+
// @ts-ignore
|
|
2
|
+
import fetch, { Blob, blobFrom, blobFromSync, File, fileFrom, fileFromSync, FormData, Headers, Request, Response } from "node-fetch";
|
|
3
|
+
if (!globalThis.fetch) {
|
|
4
|
+
// @ts-ignore
|
|
5
|
+
globalThis.fetch = fetch;
|
|
6
|
+
// @ts-ignore
|
|
7
|
+
globalThis.Headers = Headers;
|
|
8
|
+
// @ts-ignore
|
|
9
|
+
globalThis.Request = Request;
|
|
10
|
+
// @ts-ignore
|
|
11
|
+
globalThis.Response = Response;
|
|
12
|
+
}
|
|
13
|
+
export * from "./node";
|
package/src/__tests__/m1.mjs
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export function f(a, b) {
|
|
2
|
-
return a * b;
|
|
3
|
-
}
|
|
1
|
+
export function f(a, b) {
|
|
2
|
+
return a * b;
|
|
3
|
+
}
|
package/src/__tests__/node.ts
CHANGED
|
@@ -1,199 +1,199 @@
|
|
|
1
|
-
import { Library, Runtime, Inspector } from "@observablehq/runtime";
|
|
2
|
-
import { expect } from "chai";
|
|
3
|
-
import { compile, download, ohq, ojs2notebook } from "../index";
|
|
4
|
-
import { Writer } from "../writer";
|
|
5
|
-
import { fa } from "./File Attachments";
|
|
6
|
-
import { imports } from "./Introduction to Imports";
|
|
7
|
-
import { timechart } from "./Observable TimeChart";
|
|
8
|
-
|
|
9
|
-
const placeholder = globalThis?.document?.getElementById("placeholder");
|
|
10
|
-
|
|
11
|
-
const ojs = `
|
|
12
|
-
42;
|
|
13
|
-
a = 1
|
|
14
|
-
b = 2
|
|
15
|
-
c = a + b
|
|
16
|
-
d = {
|
|
17
|
-
yield 1;
|
|
18
|
-
yield 2;
|
|
19
|
-
yield 3;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
viewof e = {
|
|
23
|
-
let output = {};
|
|
24
|
-
let listeners = [];
|
|
25
|
-
output.value = 10;
|
|
26
|
-
output.addEventListener = (listener) => listeners.push(listener);;
|
|
27
|
-
output.removeEventListener = (listener) => {
|
|
28
|
-
listeners = listeners.filter(l => l !== listener);
|
|
29
|
-
};
|
|
30
|
-
return output;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
tenTimes = {
|
|
34
|
-
for (let i = 0; i < 10; i++) {
|
|
35
|
-
yield Promises.delay(100);
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
f = {
|
|
40
|
-
tenTimes;
|
|
41
|
-
return (this || 0) + 1;
|
|
42
|
-
}
|
|
43
|
-
`;
|
|
44
|
-
|
|
45
|
-
describe("ojs", function () {
|
|
46
|
-
it("quickstart", async function () {
|
|
47
|
-
this.timeout(10000);
|
|
48
|
-
|
|
49
|
-
const notebook = await download("https://observablehq.com/@observablehq/summary-table");
|
|
50
|
-
const compiledNB = await compile(notebook);
|
|
51
|
-
|
|
52
|
-
const library = new Library();
|
|
53
|
-
const runtime = new Runtime(library);
|
|
54
|
-
|
|
55
|
-
compiledNB(runtime, name => {
|
|
56
|
-
const div = globalThis?.document.createElement("div");
|
|
57
|
-
placeholder.appendChild(div);
|
|
58
|
-
return new Inspector(div);
|
|
59
|
-
});
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
it("simple", async function () {
|
|
63
|
-
this.timeout(10000);
|
|
64
|
-
|
|
65
|
-
const notebook = ojs2notebook(ojs);
|
|
66
|
-
const define = await compile(notebook);
|
|
67
|
-
|
|
68
|
-
const library = new Library();
|
|
69
|
-
const runtime = new Runtime(library);
|
|
70
|
-
const main: ohq.Module = define(runtime, name => {
|
|
71
|
-
if (placeholder) {
|
|
72
|
-
const div = globalThis?.document.createElement("div");
|
|
73
|
-
placeholder.appendChild(div);
|
|
74
|
-
return new Inspector(div);
|
|
75
|
-
}
|
|
76
|
-
return {
|
|
77
|
-
pending() { },
|
|
78
|
-
fulfilled(value) { console.info("fulfilled", name, value); },
|
|
79
|
-
rejected(error) { console.error("rejected", name, error); },
|
|
80
|
-
};
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
expect(await main.value("d")).to.equal(1);
|
|
84
|
-
expect(await main.value("d")).to.equal(2);
|
|
85
|
-
expect(await main.value("d")).to.equal(3);
|
|
86
|
-
expect(await main.value("d")).to.equal(3);
|
|
87
|
-
expect(await main.value("d")).to.equal(3);
|
|
88
|
-
|
|
89
|
-
expect(await main.value("c")).to.equal(3);
|
|
90
|
-
expect(await main.value("a")).to.equal(1);
|
|
91
|
-
expect(await main.value("b")).to.equal(2);
|
|
92
|
-
|
|
93
|
-
expect(await main.value("e")).to.equal(10);
|
|
94
|
-
const viewOfE = await main.value("viewof e");
|
|
95
|
-
expect(viewOfE).to.have.property("value");
|
|
96
|
-
expect(viewOfE).to.have.property("addEventListener");
|
|
97
|
-
expect(viewOfE).to.have.property("removeEventListener");
|
|
98
|
-
|
|
99
|
-
await main.value("tenTimes");
|
|
100
|
-
|
|
101
|
-
for (const cellID in define.cells) {
|
|
102
|
-
define.delete(cellID);
|
|
103
|
-
break;
|
|
104
|
-
}
|
|
105
|
-
});
|
|
106
|
-
|
|
107
|
-
it("compiler", async function () {
|
|
108
|
-
const writer = new Writer();
|
|
109
|
-
const define = await compile(imports as any);
|
|
110
|
-
define.write(writer);
|
|
111
|
-
console.info(writer.toString());
|
|
112
|
-
});
|
|
113
|
-
|
|
114
|
-
it("File Attachements", async function () {
|
|
115
|
-
|
|
116
|
-
const define = await compile(fa as any);
|
|
117
|
-
|
|
118
|
-
const library = new Library();
|
|
119
|
-
const runtime = new Runtime(library);
|
|
120
|
-
/*const main: ohq.Module =*/ define(runtime, name => {
|
|
121
|
-
if (placeholder) {
|
|
122
|
-
const div = globalThis?.document.createElement("div");
|
|
123
|
-
placeholder.appendChild(div);
|
|
124
|
-
return new Inspector(div);
|
|
125
|
-
}
|
|
126
|
-
return {
|
|
127
|
-
pending() { console.info("pending", name); },
|
|
128
|
-
fulfilled(value) { console.info("fulfilled", name, value); },
|
|
129
|
-
rejected(error) { console.error("rejected", name, error); },
|
|
130
|
-
};
|
|
131
|
-
});
|
|
132
|
-
|
|
133
|
-
});
|
|
134
|
-
|
|
135
|
-
it("esm imports", async function () {
|
|
136
|
-
this.timeout(10000);
|
|
137
|
-
|
|
138
|
-
const define = await compile(`\
|
|
139
|
-
m1 = import("../src/__tests__/m1.mjs");
|
|
140
|
-
x = m1.f(5, 7);
|
|
141
|
-
`);
|
|
142
|
-
|
|
143
|
-
const library = new Library();
|
|
144
|
-
const runtime = new Runtime(library);
|
|
145
|
-
const main: ohq.Module = define(runtime, name => {
|
|
146
|
-
return {
|
|
147
|
-
pending() { },
|
|
148
|
-
fulfilled(value) { console.info("fulfilled", name, value); },
|
|
149
|
-
rejected(error) { console.error("rejected", name, error); },
|
|
150
|
-
};
|
|
151
|
-
});
|
|
152
|
-
|
|
153
|
-
expect(await main.value("x")).to.equal(35);
|
|
154
|
-
|
|
155
|
-
});
|
|
156
|
-
|
|
157
|
-
it("Introduction to Imports", async function () {
|
|
158
|
-
|
|
159
|
-
const define = await compile(imports as any);
|
|
160
|
-
|
|
161
|
-
const library = new Library();
|
|
162
|
-
const runtime = new Runtime(library);
|
|
163
|
-
/*const main: ohq.Module =*/ define(runtime, name => {
|
|
164
|
-
if (placeholder) {
|
|
165
|
-
const div = globalThis?.document.createElement("div");
|
|
166
|
-
placeholder.appendChild(div);
|
|
167
|
-
return new Inspector(div);
|
|
168
|
-
}
|
|
169
|
-
return {
|
|
170
|
-
pending() { console.info("pending", name); },
|
|
171
|
-
fulfilled(value) { console.info("fulfilled", name, value); },
|
|
172
|
-
rejected(error) { console.error("rejected", name, error); },
|
|
173
|
-
};
|
|
174
|
-
});
|
|
175
|
-
|
|
176
|
-
});
|
|
177
|
-
|
|
178
|
-
it("Observable TimeChart", async function () {
|
|
179
|
-
|
|
180
|
-
const define2 = await compile(timechart as any);
|
|
181
|
-
|
|
182
|
-
const library = new Library();
|
|
183
|
-
const runtime = new Runtime(library);
|
|
184
|
-
/*const main: ohq.Module =*/ define2(runtime, name => {
|
|
185
|
-
if (placeholder) {
|
|
186
|
-
const div = globalThis?.document.createElement("div");
|
|
187
|
-
placeholder.appendChild(div);
|
|
188
|
-
return new Inspector(div);
|
|
189
|
-
}
|
|
190
|
-
return {
|
|
191
|
-
pending() { console.info("pending", name); },
|
|
192
|
-
fulfilled(value) { console.info("fulfilled", name, value); },
|
|
193
|
-
rejected(error) { console.error("rejected", name, error); },
|
|
194
|
-
};
|
|
195
|
-
});
|
|
196
|
-
|
|
197
|
-
});
|
|
198
|
-
});
|
|
199
|
-
|
|
1
|
+
import { Library, Runtime, Inspector } from "@observablehq/runtime";
|
|
2
|
+
import { expect } from "chai";
|
|
3
|
+
import { compile, download, ohq, ojs2notebook } from "../index";
|
|
4
|
+
import { Writer } from "../writer";
|
|
5
|
+
import { fa } from "./File Attachments";
|
|
6
|
+
import { imports } from "./Introduction to Imports";
|
|
7
|
+
import { timechart } from "./Observable TimeChart";
|
|
8
|
+
|
|
9
|
+
const placeholder = globalThis?.document?.getElementById("placeholder");
|
|
10
|
+
|
|
11
|
+
const ojs = `
|
|
12
|
+
42;
|
|
13
|
+
a = 1
|
|
14
|
+
b = 2
|
|
15
|
+
c = a + b
|
|
16
|
+
d = {
|
|
17
|
+
yield 1;
|
|
18
|
+
yield 2;
|
|
19
|
+
yield 3;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
viewof e = {
|
|
23
|
+
let output = {};
|
|
24
|
+
let listeners = [];
|
|
25
|
+
output.value = 10;
|
|
26
|
+
output.addEventListener = (listener) => listeners.push(listener);;
|
|
27
|
+
output.removeEventListener = (listener) => {
|
|
28
|
+
listeners = listeners.filter(l => l !== listener);
|
|
29
|
+
};
|
|
30
|
+
return output;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
tenTimes = {
|
|
34
|
+
for (let i = 0; i < 10; i++) {
|
|
35
|
+
yield Promises.delay(100);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
f = {
|
|
40
|
+
tenTimes;
|
|
41
|
+
return (this || 0) + 1;
|
|
42
|
+
}
|
|
43
|
+
`;
|
|
44
|
+
|
|
45
|
+
describe("ojs", function () {
|
|
46
|
+
it("quickstart", async function () {
|
|
47
|
+
this.timeout(10000);
|
|
48
|
+
|
|
49
|
+
const notebook = await download("https://observablehq.com/@observablehq/summary-table");
|
|
50
|
+
const compiledNB = await compile(notebook);
|
|
51
|
+
|
|
52
|
+
const library = new Library();
|
|
53
|
+
const runtime = new Runtime(library);
|
|
54
|
+
|
|
55
|
+
compiledNB(runtime, name => {
|
|
56
|
+
const div = globalThis?.document.createElement("div");
|
|
57
|
+
placeholder.appendChild(div);
|
|
58
|
+
return new Inspector(div);
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it("simple", async function () {
|
|
63
|
+
this.timeout(10000);
|
|
64
|
+
|
|
65
|
+
const notebook = ojs2notebook(ojs);
|
|
66
|
+
const define = await compile(notebook);
|
|
67
|
+
|
|
68
|
+
const library = new Library();
|
|
69
|
+
const runtime = new Runtime(library);
|
|
70
|
+
const main: ohq.Module = define(runtime, name => {
|
|
71
|
+
if (placeholder) {
|
|
72
|
+
const div = globalThis?.document.createElement("div");
|
|
73
|
+
placeholder.appendChild(div);
|
|
74
|
+
return new Inspector(div);
|
|
75
|
+
}
|
|
76
|
+
return {
|
|
77
|
+
pending() { },
|
|
78
|
+
fulfilled(value) { console.info("fulfilled", name, value); },
|
|
79
|
+
rejected(error) { console.error("rejected", name, error); },
|
|
80
|
+
};
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
expect(await main.value("d")).to.equal(1);
|
|
84
|
+
expect(await main.value("d")).to.equal(2);
|
|
85
|
+
expect(await main.value("d")).to.equal(3);
|
|
86
|
+
expect(await main.value("d")).to.equal(3);
|
|
87
|
+
expect(await main.value("d")).to.equal(3);
|
|
88
|
+
|
|
89
|
+
expect(await main.value("c")).to.equal(3);
|
|
90
|
+
expect(await main.value("a")).to.equal(1);
|
|
91
|
+
expect(await main.value("b")).to.equal(2);
|
|
92
|
+
|
|
93
|
+
expect(await main.value("e")).to.equal(10);
|
|
94
|
+
const viewOfE = await main.value("viewof e");
|
|
95
|
+
expect(viewOfE).to.have.property("value");
|
|
96
|
+
expect(viewOfE).to.have.property("addEventListener");
|
|
97
|
+
expect(viewOfE).to.have.property("removeEventListener");
|
|
98
|
+
|
|
99
|
+
await main.value("tenTimes");
|
|
100
|
+
|
|
101
|
+
for (const cellID in define.cells) {
|
|
102
|
+
define.delete(cellID);
|
|
103
|
+
break;
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it("compiler", async function () {
|
|
108
|
+
const writer = new Writer();
|
|
109
|
+
const define = await compile(imports as any);
|
|
110
|
+
define.write(writer);
|
|
111
|
+
console.info(writer.toString());
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it("File Attachements", async function () {
|
|
115
|
+
|
|
116
|
+
const define = await compile(fa as any);
|
|
117
|
+
|
|
118
|
+
const library = new Library();
|
|
119
|
+
const runtime = new Runtime(library);
|
|
120
|
+
/*const main: ohq.Module =*/ define(runtime, name => {
|
|
121
|
+
if (placeholder) {
|
|
122
|
+
const div = globalThis?.document.createElement("div");
|
|
123
|
+
placeholder.appendChild(div);
|
|
124
|
+
return new Inspector(div);
|
|
125
|
+
}
|
|
126
|
+
return {
|
|
127
|
+
pending() { console.info("pending", name); },
|
|
128
|
+
fulfilled(value) { console.info("fulfilled", name, value); },
|
|
129
|
+
rejected(error) { console.error("rejected", name, error); },
|
|
130
|
+
};
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it("esm imports", async function () {
|
|
136
|
+
this.timeout(10000);
|
|
137
|
+
|
|
138
|
+
const define = await compile(`\
|
|
139
|
+
m1 = import("../src/__tests__/m1.mjs");
|
|
140
|
+
x = m1.f(5, 7);
|
|
141
|
+
`);
|
|
142
|
+
|
|
143
|
+
const library = new Library();
|
|
144
|
+
const runtime = new Runtime(library);
|
|
145
|
+
const main: ohq.Module = define(runtime, name => {
|
|
146
|
+
return {
|
|
147
|
+
pending() { },
|
|
148
|
+
fulfilled(value) { console.info("fulfilled", name, value); },
|
|
149
|
+
rejected(error) { console.error("rejected", name, error); },
|
|
150
|
+
};
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
expect(await main.value("x")).to.equal(35);
|
|
154
|
+
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
it("Introduction to Imports", async function () {
|
|
158
|
+
|
|
159
|
+
const define = await compile(imports as any);
|
|
160
|
+
|
|
161
|
+
const library = new Library();
|
|
162
|
+
const runtime = new Runtime(library);
|
|
163
|
+
/*const main: ohq.Module =*/ define(runtime, name => {
|
|
164
|
+
if (placeholder) {
|
|
165
|
+
const div = globalThis?.document.createElement("div");
|
|
166
|
+
placeholder.appendChild(div);
|
|
167
|
+
return new Inspector(div);
|
|
168
|
+
}
|
|
169
|
+
return {
|
|
170
|
+
pending() { console.info("pending", name); },
|
|
171
|
+
fulfilled(value) { console.info("fulfilled", name, value); },
|
|
172
|
+
rejected(error) { console.error("rejected", name, error); },
|
|
173
|
+
};
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
it("Observable TimeChart", async function () {
|
|
179
|
+
|
|
180
|
+
const define2 = await compile(timechart as any);
|
|
181
|
+
|
|
182
|
+
const library = new Library();
|
|
183
|
+
const runtime = new Runtime(library);
|
|
184
|
+
/*const main: ohq.Module =*/ define2(runtime, name => {
|
|
185
|
+
if (placeholder) {
|
|
186
|
+
const div = globalThis?.document.createElement("div");
|
|
187
|
+
placeholder.appendChild(div);
|
|
188
|
+
return new Inspector(div);
|
|
189
|
+
}
|
|
190
|
+
return {
|
|
191
|
+
pending() { console.info("pending", name); },
|
|
192
|
+
fulfilled(value) { console.info("fulfilled", name, value); },
|
|
193
|
+
rejected(error) { console.error("rejected", name, error); },
|
|
194
|
+
};
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
});
|
|
198
|
+
});
|
|
199
|
+
|