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