@hpcc-js/observablehq-compiler 1.1.1 → 1.1.2

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.
Files changed (48) hide show
  1. package/LICENSE +43 -0
  2. package/README.md +105 -105
  3. package/bin/ojscc.mjs +74 -74
  4. package/dist/index.esm.js +554 -554
  5. package/dist/index.esm.js.map +1 -1
  6. package/dist/index.esm.min.js.map +1 -1
  7. package/dist/index.js +6700 -6696
  8. package/dist/index.js.map +1 -1
  9. package/dist/index.min.js +3 -3
  10. package/dist/index.min.js.map +1 -1
  11. package/package.json +3 -2
  12. package/src/__package__.ts +3 -3
  13. package/src/__tests__/File Attachments.ts +894 -894
  14. package/src/__tests__/Introduction to Imports.ts +748 -748
  15. package/src/__tests__/Observable TimeChart.ts +771 -771
  16. package/src/__tests__/index.ts +13 -13
  17. package/src/__tests__/node.ts +177 -177
  18. package/src/__tests__/tsconfig.json +20 -20
  19. package/src/compiler.md +234 -234
  20. package/src/compiler.ts +264 -264
  21. package/src/cst.ts +172 -172
  22. package/src/index.css +459 -459
  23. package/src/index.ts +7 -7
  24. package/src/util.md +113 -113
  25. package/src/util.ts +153 -153
  26. package/src/writer.ts +80 -80
  27. package/types/__package__.d.ts +3 -3
  28. package/types/__tests__/File Attachments.d.ts +109 -109
  29. package/types/__tests__/Introduction to Imports.d.ts +119 -119
  30. package/types/__tests__/Observable TimeChart.d.ts +110 -110
  31. package/types/__tests__/index.d.ts +1 -1
  32. package/types/__tests__/node.d.ts +1 -1
  33. package/types/compiler.d.ts +87 -87
  34. package/types/cst.d.ts +41 -41
  35. package/types/index.d.ts +5 -5
  36. package/types/util.d.ts +25 -25
  37. package/types/writer.d.ts +18 -18
  38. package/types-3.4/__package__.d.ts +4 -0
  39. package/types-3.4/__tests__/File Attachments.d.ts +110 -0
  40. package/types-3.4/__tests__/Introduction to Imports.d.ts +120 -0
  41. package/types-3.4/__tests__/Observable TimeChart.d.ts +111 -0
  42. package/types-3.4/__tests__/index.d.ts +2 -0
  43. package/types-3.4/__tests__/node.d.ts +2 -0
  44. package/types-3.4/compiler.d.ts +91 -0
  45. package/types-3.4/cst.d.ts +42 -0
  46. package/types-3.4/index.d.ts +6 -0
  47. package/types-3.4/util.d.ts +26 -0
  48. package/types-3.4/writer.d.ts +19 -0
package/src/cst.ts CHANGED
@@ -1,172 +1,172 @@
1
- import { parseCell as ohqParseCell, ancestor, walk } from "@hpcc-js/observable-shim";
2
- import { createFunction, Refs } from "./util";
3
-
4
- function calcRefs(cellAst, cellStr): Refs {
5
- if (cellAst.references === undefined) return { inputs: [], args: [], patches: [] };
6
-
7
- const dedup = {};
8
- cellAst.references.forEach(r => 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, newText) => retVal.patches.push({ start: node.start - cellAst.body.start, end: node.end - cellAst.body.start, 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) {
23
- const value = cellStr.substring(node.start, node.end);
24
- const newText = value.split(" ").join("_") + ".value";
25
- pushPatch(node, newText);
26
- },
27
- ViewExpression(node) {
28
- const value = cellStr.substring(node.start, node.end);
29
- const newText = value.split(" ").join("_");
30
- pushPatch(node, newText);
31
- },
32
- ThisExpression(node, ancestors: acorn.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
- }, 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 parseImportExpression(cellAst): ParsedImportCell {
55
- return {
56
- type: "import",
57
- src: cellAst.body.source.value,
58
- specifiers: cellAst.body.specifiers?.map(spec => {
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 => {
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, 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, _) => 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, 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, _) => new M(_)
130
- },
131
- variableValue: {
132
- type: "variable",
133
- id: initialValueId,
134
- inputs: [id],
135
- func: _ => _.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, 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): ParsedImportCell | ParsedViewCell | ParsedMutableCell | ParsedVariableCell {
157
- const cellAst = ohqParseCell(cellStr);
158
- if ((cellAst.body)?.type == "ImportDeclaration") {
159
- return parseImportExpression(cellAst);
160
- }
161
- const refs = calcRefs(cellAst, cellStr);
162
-
163
- const bodyStr = cellAst.body && cellStr.substring(cellAst.body.start, cellAst.body.end);
164
- switch (cellAst.id?.type) {
165
- case "ViewExpression":
166
- return parseViewExpression(cellStr, cellAst, refs, bodyStr);
167
- case "MutableExpression":
168
- return parseMutableExpression(cellStr, cellAst, refs, bodyStr);
169
- default:
170
- return parseVariableExpression(cellStr, cellAst, refs, bodyStr);
171
- }
172
- }
1
+ import { parseCell as ohqParseCell, ancestor, walk } from "@hpcc-js/observable-shim";
2
+ import { createFunction, Refs } from "./util";
3
+
4
+ function calcRefs(cellAst, cellStr): Refs {
5
+ if (cellAst.references === undefined) return { inputs: [], args: [], patches: [] };
6
+
7
+ const dedup = {};
8
+ cellAst.references.forEach(r => 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, newText) => retVal.patches.push({ start: node.start - cellAst.body.start, end: node.end - cellAst.body.start, 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) {
23
+ const value = cellStr.substring(node.start, node.end);
24
+ const newText = value.split(" ").join("_") + ".value";
25
+ pushPatch(node, newText);
26
+ },
27
+ ViewExpression(node) {
28
+ const value = cellStr.substring(node.start, node.end);
29
+ const newText = value.split(" ").join("_");
30
+ pushPatch(node, newText);
31
+ },
32
+ ThisExpression(node, ancestors: acorn.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
+ }, 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 parseImportExpression(cellAst): ParsedImportCell {
55
+ return {
56
+ type: "import",
57
+ src: cellAst.body.source.value,
58
+ specifiers: cellAst.body.specifiers?.map(spec => {
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 => {
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, 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, _) => 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, 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, _) => new M(_)
130
+ },
131
+ variableValue: {
132
+ type: "variable",
133
+ id: initialValueId,
134
+ inputs: [id],
135
+ func: _ => _.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, 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): ParsedImportCell | ParsedViewCell | ParsedMutableCell | ParsedVariableCell {
157
+ const cellAst = ohqParseCell(cellStr);
158
+ if ((cellAst.body)?.type == "ImportDeclaration") {
159
+ return parseImportExpression(cellAst);
160
+ }
161
+ const refs = calcRefs(cellAst, cellStr);
162
+
163
+ const bodyStr = cellAst.body && cellStr.substring(cellAst.body.start, cellAst.body.end);
164
+ switch (cellAst.id?.type) {
165
+ case "ViewExpression":
166
+ return parseViewExpression(cellStr, cellAst, refs, bodyStr);
167
+ case "MutableExpression":
168
+ return parseMutableExpression(cellStr, cellAst, refs, bodyStr);
169
+ default:
170
+ return parseVariableExpression(cellStr, cellAst, refs, bodyStr);
171
+ }
172
+ }