@hpcc-js/observablehq-compiler 3.7.5 → 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/cst.ts CHANGED
@@ -1,181 +1,181 @@
1
- import { ancestor, parseCell as ohqParseCell, Cell, Node, walk, AncestorVisitors } from "./observable-shim.ts";
2
- import { fixRelativeUrl, createFunction, Refs } from "./kit/index.ts";
3
-
4
- function calcRefs(cellAst: Cell, cellStr: string): Refs {
5
- if (cellAst.references === undefined) return { inputs: [], args: [], patches: [] };
6
-
7
- const dedup: { [id: string]: boolean } = {};
8
- cellAst.references.forEach((r: any) => dedup[cellStr.substring(r.start, r.end)] = true);
9
- const retVal: Refs = {
10
- inputs: Object.keys(dedup),
11
- args: Object.keys(dedup).map(r => r.split(" ").join("_")),
12
- patches: []
13
- };
14
- const pushPatch = (node: Node, newText: string) => retVal.patches.push({ start: node.start - (cellAst.body?.start ?? 0), end: node.end - (cellAst.body?.start ?? 0), newText });
15
- if (cellAst.body) {
16
- ancestor(cellAst.body, {
17
- Identifier(node) {
18
- const value = cellStr.substring(node.start, node.end);
19
- if (dedup[value]) {
20
- }
21
- },
22
- MutableExpression(node: Node) {
23
- const value = cellStr.substring(node.start, node.end);
24
- const newText = value.split(" ").join("_") + ".value";
25
- pushPatch(node, newText);
26
- },
27
- ViewExpression(node: Node) {
28
- const value = cellStr.substring(node.start, node.end);
29
- const newText = value.split(" ").join("_");
30
- pushPatch(node, newText);
31
- },
32
- ThisExpression(node: Node, ancestors: Node[]) {
33
- const value = cellStr.substring(node.start, node.end);
34
- if (value === "this" && !ancestors.find(n => n.type === "FunctionExpression")) {
35
- pushPatch(node, "((this === globalThis || this === globalThis.window)? undefined : this?.valueOf())");
36
- }
37
- }
38
- } as AncestorVisitors<any>, walk);
39
- }
40
- return retVal;
41
- }
42
-
43
- export interface ParsedCell {
44
- type: "import" | "viewof" | "mutable" | "variable" | "identifier"
45
- }
46
-
47
- export interface ParsedImportCell extends ParsedCell {
48
- type: "import"
49
- src: string;
50
- specifiers: { view: boolean, name: string, alias?: string }[];
51
- injections: { name: string, alias: string }[];
52
- }
53
-
54
- function parseImportDeclaration(cellAst: any): ParsedImportCell {
55
- return {
56
- type: "import",
57
- src: cellAst.body.source.value,
58
- specifiers: cellAst.body.specifiers?.map((spec: any) => {
59
- return {
60
- view: spec.view,
61
- name: spec.imported.name,
62
- alias: (spec.local?.name && spec.imported.name !== spec.local.name) ? spec.local.name : spec.imported.name
63
- };
64
- }) ?? [],
65
- injections: cellAst.body.injections?.map((inj: any) => {
66
- return {
67
- name: inj.imported.name,
68
- alias: inj.local?.name ?? inj.imported.name
69
- };
70
- }) ?? [],
71
- };
72
- }
73
-
74
- export interface ParsedVariable extends ParsedCell {
75
- type: "variable"
76
- id?: string,
77
- inputs: string[],
78
- func: any,
79
- }
80
-
81
- export interface ParsedViewCell extends ParsedCell {
82
- type: "viewof",
83
- variable: ParsedVariable;
84
- variableValue: ParsedVariable;
85
- }
86
-
87
- function parseViewExpression(cellStr: string, cellAst: any, refs: Refs, bodyStr?: string): ParsedViewCell {
88
- const id = cellAst.id && cellStr.substring(cellAst.id.start, cellAst.id.end);
89
- return {
90
- type: "viewof",
91
- variable: {
92
- type: "variable",
93
- id,
94
- inputs: refs.inputs,
95
- func: createFunction(refs, cellAst.async, cellAst.generator, cellAst.body.type === "BlockStatement", bodyStr)
96
- },
97
- variableValue: {
98
- type: "variable",
99
- id: cellAst?.id?.id?.name,
100
- inputs: ["Generators", id],
101
- func: (G: any, _: any) => G.input(_)
102
- }
103
- };
104
- }
105
-
106
- interface ParsedMutableCell extends ParsedCell {
107
- type: "mutable",
108
- initial: ParsedVariable;
109
- variable: ParsedVariable;
110
- variableValue: ParsedVariable;
111
- }
112
-
113
- function parseMutableExpression(cellStr: string, cellAst: any, refs: Refs, bodyStr?: string): ParsedMutableCell {
114
- const id = cellAst.id && cellStr.substring(cellAst.id.start, cellAst.id.end);
115
- const initialValueId = cellAst?.id?.id?.name;
116
- const initialId = `initial ${initialValueId}`;
117
- return {
118
- type: "mutable",
119
- initial: {
120
- type: "variable",
121
- id: initialId,
122
- inputs: refs.inputs,
123
- func: createFunction(refs, cellAst.async, cellAst.generator, cellAst.body.type === "BlockStatement", bodyStr)
124
- },
125
- variable: {
126
- type: "variable",
127
- id,
128
- inputs: ["Mutable", initialId],
129
- func: (M: any, _: any) => new M(_)
130
- },
131
- variableValue: {
132
- type: "variable",
133
- id: initialValueId,
134
- inputs: [id],
135
- func: (_: any) => _.generator
136
- }
137
- };
138
- }
139
-
140
- interface ParsedVariableCell extends ParsedCell {
141
- type: "variable",
142
- id: string,
143
- inputs: string[],
144
- func: any,
145
- }
146
-
147
- function parseVariableExpression(cellStr: string, cellAst: any, refs: Refs, bodyStr?: string): ParsedVariableCell {
148
- return {
149
- type: "variable",
150
- id: cellAst.id && cellStr.substring(cellAst.id?.start, cellAst.id?.end),
151
- inputs: refs.inputs,
152
- func: createFunction(refs, cellAst.async, cellAst.generator, cellAst.body.type === "BlockStatement", bodyStr)
153
- };
154
- }
155
-
156
- export function parseCell(cellStr: string, baseUrl: string): ParsedImportCell | ParsedViewCell | ParsedMutableCell | ParsedVariableCell {
157
- const cellAst = ohqParseCell(cellStr);
158
- let bodyStr = cellAst.body && cellStr.substring(cellAst.body.start, cellAst.body.end);
159
- switch ((cellAst.body)?.type) {
160
- case "ImportDeclaration":
161
- return parseImportDeclaration(cellAst);
162
- case "ImportExpression":
163
- switch (cellAst.body.source.type) {
164
- case "Literal":
165
- bodyStr = `import("${fixRelativeUrl("" + cellAst.body.source.value, baseUrl)}")`;
166
- break;
167
- default:
168
- console.error("Unexpected import value");
169
- }
170
- }
171
- const refs = calcRefs(cellAst, cellStr);
172
- switch (cellAst.id?.type) {
173
- case "ViewExpression":
174
- return parseViewExpression(cellStr, cellAst, refs, bodyStr);
175
- case "MutableExpression":
176
- return parseMutableExpression(cellStr, cellAst, refs, bodyStr);
177
- default:
178
- return parseVariableExpression(cellStr, cellAst, refs, bodyStr);
179
- }
180
- }
181
-
1
+ import { ancestor, parseCell as ohqParseCell, Cell, Node, walk, AncestorVisitors } from "./observable-shim.ts";
2
+ import { fixRelativeUrl, createFunction, Refs } from "./kit/index.ts";
3
+
4
+ function calcRefs(cellAst: Cell, cellStr: string): Refs {
5
+ if (cellAst.references === undefined) return { inputs: [], args: [], patches: [] };
6
+
7
+ const dedup: { [id: string]: boolean } = {};
8
+ cellAst.references.forEach((r: any) => dedup[cellStr.substring(r.start, r.end)] = true);
9
+ const retVal: Refs = {
10
+ inputs: Object.keys(dedup),
11
+ args: Object.keys(dedup).map(r => r.split(" ").join("_")),
12
+ patches: []
13
+ };
14
+ const pushPatch = (node: Node, newText: string) => retVal.patches.push({ start: node.start - (cellAst.body?.start ?? 0), end: node.end - (cellAst.body?.start ?? 0), newText });
15
+ if (cellAst.body) {
16
+ ancestor(cellAst.body, {
17
+ Identifier(node) {
18
+ const value = cellStr.substring(node.start, node.end);
19
+ if (dedup[value]) {
20
+ }
21
+ },
22
+ MutableExpression(node: Node) {
23
+ const value = cellStr.substring(node.start, node.end);
24
+ const newText = value.split(" ").join("_") + ".value";
25
+ pushPatch(node, newText);
26
+ },
27
+ ViewExpression(node: Node) {
28
+ const value = cellStr.substring(node.start, node.end);
29
+ const newText = value.split(" ").join("_");
30
+ pushPatch(node, newText);
31
+ },
32
+ ThisExpression(node: Node, ancestors: Node[]) {
33
+ const value = cellStr.substring(node.start, node.end);
34
+ if (value === "this" && !ancestors.find(n => n.type === "FunctionExpression")) {
35
+ pushPatch(node, "((this === globalThis || this === globalThis.window)? undefined : this?.valueOf())");
36
+ }
37
+ }
38
+ } as AncestorVisitors<any>, walk);
39
+ }
40
+ return retVal;
41
+ }
42
+
43
+ export interface ParsedCell {
44
+ type: "import" | "viewof" | "mutable" | "variable" | "identifier"
45
+ }
46
+
47
+ export interface ParsedImportCell extends ParsedCell {
48
+ type: "import"
49
+ src: string;
50
+ specifiers: { view: boolean, name: string, alias?: string }[];
51
+ injections: { name: string, alias: string }[];
52
+ }
53
+
54
+ function parseImportDeclaration(cellAst: any): ParsedImportCell {
55
+ return {
56
+ type: "import",
57
+ src: cellAst.body.source.value,
58
+ specifiers: cellAst.body.specifiers?.map((spec: any) => {
59
+ return {
60
+ view: spec.view,
61
+ name: spec.imported.name,
62
+ alias: (spec.local?.name && spec.imported.name !== spec.local.name) ? spec.local.name : spec.imported.name
63
+ };
64
+ }) ?? [],
65
+ injections: cellAst.body.injections?.map((inj: any) => {
66
+ return {
67
+ name: inj.imported.name,
68
+ alias: inj.local?.name ?? inj.imported.name
69
+ };
70
+ }) ?? [],
71
+ };
72
+ }
73
+
74
+ export interface ParsedVariable extends ParsedCell {
75
+ type: "variable"
76
+ id?: string,
77
+ inputs: string[],
78
+ func: any,
79
+ }
80
+
81
+ export interface ParsedViewCell extends ParsedCell {
82
+ type: "viewof",
83
+ variable: ParsedVariable;
84
+ variableValue: ParsedVariable;
85
+ }
86
+
87
+ function parseViewExpression(cellStr: string, cellAst: any, refs: Refs, bodyStr?: string): ParsedViewCell {
88
+ const id = cellAst.id && cellStr.substring(cellAst.id.start, cellAst.id.end);
89
+ return {
90
+ type: "viewof",
91
+ variable: {
92
+ type: "variable",
93
+ id,
94
+ inputs: refs.inputs,
95
+ func: createFunction(refs, cellAst.async, cellAst.generator, cellAst.body.type === "BlockStatement", bodyStr)
96
+ },
97
+ variableValue: {
98
+ type: "variable",
99
+ id: cellAst?.id?.id?.name,
100
+ inputs: ["Generators", id],
101
+ func: (G: any, _: any) => G.input(_)
102
+ }
103
+ };
104
+ }
105
+
106
+ interface ParsedMutableCell extends ParsedCell {
107
+ type: "mutable",
108
+ initial: ParsedVariable;
109
+ variable: ParsedVariable;
110
+ variableValue: ParsedVariable;
111
+ }
112
+
113
+ function parseMutableExpression(cellStr: string, cellAst: any, refs: Refs, bodyStr?: string): ParsedMutableCell {
114
+ const id = cellAst.id && cellStr.substring(cellAst.id.start, cellAst.id.end);
115
+ const initialValueId = cellAst?.id?.id?.name;
116
+ const initialId = `initial ${initialValueId}`;
117
+ return {
118
+ type: "mutable",
119
+ initial: {
120
+ type: "variable",
121
+ id: initialId,
122
+ inputs: refs.inputs,
123
+ func: createFunction(refs, cellAst.async, cellAst.generator, cellAst.body.type === "BlockStatement", bodyStr)
124
+ },
125
+ variable: {
126
+ type: "variable",
127
+ id,
128
+ inputs: ["Mutable", initialId],
129
+ func: (M: any, _: any) => new M(_)
130
+ },
131
+ variableValue: {
132
+ type: "variable",
133
+ id: initialValueId,
134
+ inputs: [id],
135
+ func: (_: any) => _.generator
136
+ }
137
+ };
138
+ }
139
+
140
+ interface ParsedVariableCell extends ParsedCell {
141
+ type: "variable",
142
+ id: string,
143
+ inputs: string[],
144
+ func: any,
145
+ }
146
+
147
+ function parseVariableExpression(cellStr: string, cellAst: any, refs: Refs, bodyStr?: string): ParsedVariableCell {
148
+ return {
149
+ type: "variable",
150
+ id: cellAst.id && cellStr.substring(cellAst.id?.start, cellAst.id?.end),
151
+ inputs: refs.inputs,
152
+ func: createFunction(refs, cellAst.async, cellAst.generator, cellAst.body.type === "BlockStatement", bodyStr)
153
+ };
154
+ }
155
+
156
+ export function parseCell(cellStr: string, baseUrl: string): ParsedImportCell | ParsedViewCell | ParsedMutableCell | ParsedVariableCell {
157
+ const cellAst = ohqParseCell(cellStr);
158
+ let bodyStr = cellAst.body && cellStr.substring(cellAst.body.start, cellAst.body.end);
159
+ switch ((cellAst.body)?.type) {
160
+ case "ImportDeclaration":
161
+ return parseImportDeclaration(cellAst);
162
+ case "ImportExpression":
163
+ switch (cellAst.body.source.type) {
164
+ case "Literal":
165
+ bodyStr = `import("${fixRelativeUrl("" + cellAst.body.source.value, baseUrl)}")`;
166
+ break;
167
+ default:
168
+ console.error("Unexpected import value");
169
+ }
170
+ }
171
+ const refs = calcRefs(cellAst, cellStr);
172
+ switch (cellAst.id?.type) {
173
+ case "ViewExpression":
174
+ return parseViewExpression(cellStr, cellAst, refs, bodyStr);
175
+ case "MutableExpression":
176
+ return parseMutableExpression(cellStr, cellAst, refs, bodyStr);
177
+ default:
178
+ return parseVariableExpression(cellStr, cellAst, refs, bodyStr);
179
+ }
180
+ }
181
+
package/src/index.node.ts CHANGED
@@ -1,7 +1,7 @@
1
- import { JSDOM } from "jsdom";
2
-
3
- const { window } = new JSDOM();
4
- globalThis.document = window.document;
5
- globalThis.DOMParser = window.DOMParser;
6
-
7
- export * from "./index.ts";
1
+ import { JSDOM } from "jsdom";
2
+
3
+ const { window } = new JSDOM();
4
+ globalThis.document = window.document;
5
+ globalThis.DOMParser = window.DOMParser;
6
+
7
+ export * from "./index.ts";
package/src/index.ts CHANGED
@@ -1,6 +1,6 @@
1
- export type { ohq } from "./observable-shim.ts";
2
-
3
- export * from "./compiler.ts";
4
- export { ojs2notebook, omd2notebook, omd2notebookKit, ojs2notebookKit, download } from "./util.ts";
5
- export * from "./kit/index.ts";
6
- export * from "./writer.ts";
1
+ export type { ohq } from "./observable-shim.ts";
2
+
3
+ export * from "./compiler.ts";
4
+ export { ojs2notebook, omd2notebook, omd2notebookKit, ojs2notebookKit, download } from "./util.ts";
5
+ export * from "./kit/index.ts";
6
+ export * from "./writer.ts";
@@ -1,60 +1,60 @@
1
-
2
- import { type Notebook, type Cell, transpile } from "@observablehq/notebook-kit";
3
- import { type Definition } from "@observablehq/notebook-kit/runtime";
4
- import { constructFunction } from "./util.ts";
5
-
6
- export { Notebook, Cell };
7
-
8
- export interface CompileCellOptions {
9
- inline?: boolean;
10
- resolveLocalImports?: boolean;
11
- includePinned?: boolean;
12
- }
13
-
14
- export function compileCell(cell: Cell, { inline = true, resolveLocalImports = false, includePinned = true }: CompileCellOptions = {}): Definition[] {
15
- const retVal: Definition[] = [];
16
- const sourceIDOffset = 1000000;
17
- try {
18
- const compiled = transpile(cell, { resolveLocalImports });
19
- retVal.push({
20
- id: cell.id,
21
- ...compiled,
22
- body: inline ? constructFunction(compiled.body, `cell_${cell.id}`) : compiled.body,
23
- });
24
- if (includePinned && cell.pinned) {
25
- const compiled = transpile({
26
- ...cell,
27
- mode: "md",
28
- value: `\
29
- \`\`\`${cell.mode}
30
- ${cell.value}
31
- \`\`\``
32
- });
33
- retVal.push({
34
- id: sourceIDOffset + cell.id,
35
- ...compiled,
36
- body: inline ? constructFunction(compiled.body, `cell_${sourceIDOffset + cell.id}`) : compiled.body,
37
- });
38
- }
39
- } catch (error) {
40
- console.error(`Error compiling cell ${cell.id}:`, error);
41
- }
42
- return retVal;
43
- }
44
-
45
- export function compileNotebook(notebook: Notebook, { inline = true, resolveLocalImports = false, includePinned = true }: CompileCellOptions = {}): Definition[] {
46
- const retVal: Definition[] = [];
47
- for (const cell of notebook.cells) {
48
- const cellDefs = compileCell(cell, { inline, resolveLocalImports, includePinned });
49
- retVal.push(...cellDefs);
50
- }
51
- return retVal;
52
- }
53
-
54
- export function resetCellIDs(notebook: Notebook, start: number = 0, increment: number = 1): Notebook {
55
- for (const cell of notebook.cells) {
56
- cell.id = start;
57
- start += increment;
58
- }
59
- return notebook;
60
- }
1
+
2
+ import { type Notebook, type Cell, transpile } from "@observablehq/notebook-kit";
3
+ import { type Definition } from "@observablehq/notebook-kit/runtime";
4
+ import { constructFunction } from "./util.ts";
5
+
6
+ export { Notebook, Cell };
7
+
8
+ export interface CompileCellOptions {
9
+ inline?: boolean;
10
+ resolveLocalImports?: boolean;
11
+ includePinned?: boolean;
12
+ }
13
+
14
+ export function compileCell(cell: Cell, { inline = true, resolveLocalImports = false, includePinned = true }: CompileCellOptions = {}): Definition[] {
15
+ const retVal: Definition[] = [];
16
+ const sourceIDOffset = 1000000;
17
+ try {
18
+ const compiled = transpile(cell, { resolveLocalImports });
19
+ retVal.push({
20
+ id: cell.id,
21
+ ...compiled,
22
+ body: inline ? constructFunction(compiled.body, `cell_${cell.id}`) : compiled.body,
23
+ });
24
+ if (includePinned && cell.pinned) {
25
+ const compiled = transpile({
26
+ ...cell,
27
+ mode: "md",
28
+ value: `\
29
+ \`\`\`${cell.mode}
30
+ ${cell.value}
31
+ \`\`\``
32
+ });
33
+ retVal.push({
34
+ id: sourceIDOffset + cell.id,
35
+ ...compiled,
36
+ body: inline ? constructFunction(compiled.body, `cell_${sourceIDOffset + cell.id}`) : compiled.body,
37
+ });
38
+ }
39
+ } catch (error) {
40
+ console.error(`Error compiling cell ${cell.id}:`, error);
41
+ }
42
+ return retVal;
43
+ }
44
+
45
+ export function compileNotebook(notebook: Notebook, { inline = true, resolveLocalImports = false, includePinned = true }: CompileCellOptions = {}): Definition[] {
46
+ const retVal: Definition[] = [];
47
+ for (const cell of notebook.cells) {
48
+ const cellDefs = compileCell(cell, { inline, resolveLocalImports, includePinned });
49
+ retVal.push(...cellDefs);
50
+ }
51
+ return retVal;
52
+ }
53
+
54
+ export function resetCellIDs(notebook: Notebook, start: number = 0, increment: number = 1): Notebook {
55
+ for (const cell of notebook.cells) {
56
+ cell.id = start;
57
+ start += increment;
58
+ }
59
+ return notebook;
60
+ }
package/src/kit/index.ts CHANGED
@@ -1,2 +1,2 @@
1
- export * from "./compiler.ts";
2
- export * from "./util.ts";
1
+ export * from "./compiler.ts";
2
+ export * from "./util.ts";