@malloy-publisher/sdk 0.4.0 → 0.5.0
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/README.md +19 -0
- package/dist/{DocumentStorageProvider-CyOJ0hqf.es.js → DocumentStorage-B04sZ5Ie.es.js} +2307 -2334
- package/dist/{DocumentStorageProvider-DGPQEd_l.cjs.js → DocumentStorage-B_MMJ0Ot.cjs.js} +20 -20
- package/dist/{RenderedResult-BnPzJ9Jf.es.js → RenderedResult-BB7eokcf.es.js} +100 -80
- package/dist/RenderedResult-DVuGhatg.cjs.js +135 -0
- package/dist/ServerProvider-D-jjzfE5.cjs.js +1 -0
- package/dist/{ServerProvider-BWOEKg0O.es.js → ServerProvider-Do-GBvpn.es.js} +492 -481
- package/dist/builder/index.cjs.js +57 -52
- package/dist/builder/index.es.js +4421 -3738
- package/dist/client/api.d.ts +14 -1
- package/dist/client/index.cjs.js +1 -1
- package/dist/client/index.es.js +1 -1
- package/dist/components/Dashboard/TileCard.d.ts +7 -3
- package/dist/components/Dashboard/telemetry.d.ts +20 -8
- package/dist/components/DashboardBuilder/DashboardBuilder.d.ts +16 -4
- package/dist/components/DashboardBuilder/DashboardEditor.d.ts +31 -8
- package/dist/components/DashboardBuilder/controls.d.ts +8 -6
- package/dist/components/DashboardBuilder/document.d.ts +19 -5
- package/dist/components/DashboardBuilder/malloyText.d.ts +0 -30
- package/dist/components/DashboardBuilder/malloyTree.d.ts +232 -0
- package/dist/components/DashboardBuilder/preview.d.ts +17 -0
- package/dist/components/DashboardBuilder/readDocument.d.ts +23 -28
- package/dist/components/DashboardBuilder/spliceDocument.d.ts +12 -0
- package/dist/components/DocumentStorage/DocumentStorage.d.ts +69 -5
- package/dist/components/DocumentStorage/index.d.ts +1 -0
- package/dist/index.cjs.js +9 -9
- package/dist/index.es.js +508 -506
- package/dist/theme/resolveTheme.d.ts +1 -1
- package/dist/theme/types.d.ts +12 -2
- package/package.json +1 -1
- package/src/components/Dashboard/TileCard.tsx +8 -4
- package/src/components/Dashboard/telemetry.ts +20 -8
- package/src/components/DashboardBuilder/BuilderToolbar.tsx +4 -5
- package/src/components/DashboardBuilder/DashboardBuilder.tsx +21 -3
- package/src/components/DashboardBuilder/DashboardEditor.authoritative.spec.tsx +706 -0
- package/src/components/DashboardBuilder/DashboardEditor.resourceUri.spec.tsx +356 -0
- package/src/components/DashboardBuilder/DashboardEditor.tsx +468 -103
- package/src/components/DashboardBuilder/FilterDialog.tsx +19 -6
- package/src/components/DashboardBuilder/TileFrame.tsx +11 -2
- package/src/components/DashboardBuilder/TileMenu.tsx +6 -0
- package/src/components/DashboardBuilder/__test__/inventory.ts +367 -0
- package/src/components/DashboardBuilder/controls.spec.ts +53 -3
- package/src/components/DashboardBuilder/controls.ts +9 -7
- package/src/components/DashboardBuilder/document.ts +17 -5
- package/src/components/DashboardBuilder/invariants.spec.ts +297 -0
- package/src/components/DashboardBuilder/malloyText.spec.ts +27 -66
- package/src/components/DashboardBuilder/malloyText.ts +4 -109
- package/src/components/DashboardBuilder/malloyTree.spec.ts +362 -0
- package/src/components/DashboardBuilder/malloyTree.ts +954 -0
- package/src/components/DashboardBuilder/preview.spec.ts +18 -0
- package/src/components/DashboardBuilder/preview.ts +24 -6
- package/src/components/DashboardBuilder/readDocument.spec.ts +312 -21
- package/src/components/DashboardBuilder/readDocument.ts +144 -165
- package/src/components/DashboardBuilder/scannerShapes.spec.ts +550 -0
- package/src/components/DashboardBuilder/spliceDocument.spec.ts +955 -21
- package/src/components/DashboardBuilder/spliceDocument.ts +509 -119
- package/src/components/DashboardBuilder/statementExtent.spec.ts +606 -0
- package/src/components/DocumentStorage/BrowserDocumentStorage.ts +7 -6
- package/src/components/DocumentStorage/DocumentStorage.ts +78 -5
- package/src/components/DocumentStorage/index.ts +1 -0
- package/src/components/Package/Package.tsx +12 -7
- package/src/components/RenderedResult/RenderedResult.tsx +21 -1
- package/src/components/given/GivensPanel.tsx +15 -3
- package/src/theme/buildTableCssVars.spec.ts +3 -0
- package/src/theme/buildTableCssVars.ts +6 -0
- package/src/theme/defaults.ts +16 -12
- package/src/theme/resolveTheme.spec.ts +14 -7
- package/src/theme/resolveTheme.ts +14 -8
- package/src/theme/types.ts +12 -2
- package/tsconfig.spec.json +5 -1
- package/dist/RenderedResult-Dp22kZsQ.cjs.js +0 -115
- package/dist/ServerProvider-Cc489A3f.cjs.js +0 -1
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
// Copyright (c) Credible Data Inc.
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
import { describe, expect, it } from "bun:test";
|
|
5
|
+
import type { DashboardDocument } from "./document";
|
|
6
|
+
import { readDashboardDocument, readFailed } from "./readDocument";
|
|
7
|
+
import {
|
|
8
|
+
spliceDashboardDocument,
|
|
9
|
+
spliceFailed,
|
|
10
|
+
syntaxErrors,
|
|
11
|
+
} from "./spliceDocument";
|
|
12
|
+
import { openDocument } from "./testing/fixtures";
|
|
13
|
+
import {
|
|
14
|
+
NOTHING_MOVED,
|
|
15
|
+
whatMoved,
|
|
16
|
+
type InventoryDiff,
|
|
17
|
+
} from "./__test__/inventory";
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* ONE INVARIANT for the writer, in place of one spec per corruption: an edit
|
|
21
|
+
* changes the declaration it names, and nothing else.
|
|
22
|
+
*
|
|
23
|
+
* The sibling specs pin SHAPES -- this list, this refinement, this brace on
|
|
24
|
+
* this line -- and each of them was written after a defect shipped. Three of
|
|
25
|
+
* those defects returned `ok`, which is what makes the shape-by-shape approach
|
|
26
|
+
* lose: the two gates in `spliceDocument` cannot see a comment or an unmodelled
|
|
27
|
+
* tag go, so the next sibling of the same shape ships too. This file asks the
|
|
28
|
+
* question once, of a fresh parse of the output, through `__test__/inventory`.
|
|
29
|
+
*
|
|
30
|
+
* EVERY ROW DECLARES ITS OUTCOME. A reasoned refusal is a legitimate answer on
|
|
31
|
+
* a real file and the corpus sweep below treats it as one; here it is not, and
|
|
32
|
+
* a row that is meant to write and refuses fails exactly as loudly as one that
|
|
33
|
+
* is meant to refuse and writes. Without that the gate is hollow: reverting the
|
|
34
|
+
* two-line `given:` fix turns a success into a reasoned refusal, and a sweep
|
|
35
|
+
* that accepted refusals would stay green over the very defect it exists for.
|
|
36
|
+
*/
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* One file carrying every shape at once, because each `it` re-reads it and the
|
|
40
|
+
* only real constraint is that the shapes coexist syntactically.
|
|
41
|
+
*
|
|
42
|
+
* What is in here, and why each is load-bearing:
|
|
43
|
+
* - FOUR declared tiles: one is removed, so it cannot also be the one under a
|
|
44
|
+
* filter edit, and the reader needs one left over.
|
|
45
|
+
* - `kpis`: a `where:` whose every clause is the builder's, with a BLOCK
|
|
46
|
+
* comment inside its span -- the shape that deleted a comment and reported
|
|
47
|
+
* success, in the spelling the comment index could not see.
|
|
48
|
+
* - `kpis` and `counts`: `# colspan` above a `--` line and above a multi-line
|
|
49
|
+
* `/* ... *\/`, both of which are comments to Malloy. The reader takes tags
|
|
50
|
+
* from the parser and sees the tag; a writer that reads the block as text
|
|
51
|
+
* does not, and writes a second copy below.
|
|
52
|
+
* - `trend`: a refinement that collapses, with a comment beside it, and above
|
|
53
|
+
* it a dimension that SHARES ITS LINE with a block comment. A locator that
|
|
54
|
+
* asks "does this line hold a comment" rather than "does it hold code" walks
|
|
55
|
+
* straight past that dimension and takes its `# label` for the tile's.
|
|
56
|
+
* - `counts`: a trailing comment on the LAST statement of the body, which is
|
|
57
|
+
* where an appended binding slides underneath one, and a `#` line written
|
|
58
|
+
* inside its block comment that WOULD be a modelled tag anywhere else,
|
|
59
|
+
* which here is prose: read as a tag it becomes a label the tile does not
|
|
60
|
+
* have, and the writer then writes that label out for real.
|
|
61
|
+
* - `spare`: a tile to remove that carries a comment and a tag, and shares its
|
|
62
|
+
* line with a view that is not a tile of this page.
|
|
63
|
+
* - `SINCE`: a `given:` declared across two lines.
|
|
64
|
+
* - `run:`: a top-level statement the document does not model at all.
|
|
65
|
+
*/
|
|
66
|
+
const PROBE = `##! experimental.givens
|
|
67
|
+
|
|
68
|
+
##" The probe dashboard.
|
|
69
|
+
## artifact { title="Probe" tiles=["a -> kpis", "a -> trend", "a -> counts", "a -> spare"] } dashboard { columns=12 }
|
|
70
|
+
import "../m.malloy"
|
|
71
|
+
|
|
72
|
+
# label="Category"
|
|
73
|
+
given: CATEGORY :: filter<string> is f''
|
|
74
|
+
|
|
75
|
+
# label="Brand"
|
|
76
|
+
given: BRAND :: filter<string> is f''
|
|
77
|
+
|
|
78
|
+
given: SINCE :: filter<string> is
|
|
79
|
+
f''
|
|
80
|
+
|
|
81
|
+
source: a is one extend {
|
|
82
|
+
view: base is { aggregate: n is count() }
|
|
83
|
+
|
|
84
|
+
// Why the KPI strip leads: revenue is the number people ask about first.
|
|
85
|
+
# colspan=6
|
|
86
|
+
-- six across, so it sits beside the trend
|
|
87
|
+
# big_value
|
|
88
|
+
view: kpis is {
|
|
89
|
+
aggregate: revenue is count()
|
|
90
|
+
where: cat ~ $CATEGORY,
|
|
91
|
+
/* both halves of the same question */
|
|
92
|
+
brand ~ $BRAND
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
# label="A helper, not a tile"
|
|
96
|
+
/* a helper, not a tile */ dimension: helper is 1
|
|
97
|
+
# colspan=6
|
|
98
|
+
view: trend is base // the trend, on the right
|
|
99
|
+
+ { where: sold_on ~ $SINCE }
|
|
100
|
+
|
|
101
|
+
# colspan=12
|
|
102
|
+
/* twelve across: the breakdown reads as a table.
|
|
103
|
+
It was tagged
|
|
104
|
+
# label="Counts"
|
|
105
|
+
until somebody decided the title said it twice. */
|
|
106
|
+
view: counts is {
|
|
107
|
+
group_by: cat
|
|
108
|
+
aggregate: n is count() // keep with measure
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// A note the author wrote about the spare.
|
|
112
|
+
# colspan=12
|
|
113
|
+
view: spare is base view: sidekick is base
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
run: a -> kpis
|
|
117
|
+
`;
|
|
118
|
+
|
|
119
|
+
const tile = (d: DashboardDocument, name: string) =>
|
|
120
|
+
d.tiles.find((t) => t.name === name)!;
|
|
121
|
+
|
|
122
|
+
interface Row {
|
|
123
|
+
what: string;
|
|
124
|
+
edit: (d: DashboardDocument) => void;
|
|
125
|
+
/**
|
|
126
|
+
* What the writer must do. A mismatch EITHER WAY fails: see the file's
|
|
127
|
+
* header for why a refusal cannot be allowed to stand in for a write.
|
|
128
|
+
*/
|
|
129
|
+
outcome: "succeeds" | "refuses";
|
|
130
|
+
/** A substring of the refusal, matched loosely so wording stays editable. */
|
|
131
|
+
because?: string;
|
|
132
|
+
/** Everything the inventory may report as moved. Absent fields must be empty. */
|
|
133
|
+
moved?: Partial<InventoryDiff>;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const ROWS: Row[] = [
|
|
137
|
+
{
|
|
138
|
+
what: "asks for nothing",
|
|
139
|
+
edit: () => {},
|
|
140
|
+
outcome: "succeeds",
|
|
141
|
+
},
|
|
142
|
+
{
|
|
143
|
+
what: "reorders the tiles",
|
|
144
|
+
edit: (d) => {
|
|
145
|
+
d.tiles = [d.tiles[3], d.tiles[0], d.tiles[1], d.tiles[2]];
|
|
146
|
+
},
|
|
147
|
+
outcome: "succeeds",
|
|
148
|
+
},
|
|
149
|
+
{
|
|
150
|
+
// The `#` tags this rewrites sit above a `--` line, so a writer that
|
|
151
|
+
// cannot see them adds a second `# colspan` rather than patching the one
|
|
152
|
+
// that is there -- and the reader, which takes tags from the parser,
|
|
153
|
+
// reads the lower one back and the round-trip gate is satisfied.
|
|
154
|
+
what: "retags a tile whose tag block holds a `--` comment",
|
|
155
|
+
edit: (d) => {
|
|
156
|
+
tile(d, "kpis").colspan = 3;
|
|
157
|
+
},
|
|
158
|
+
outcome: "succeeds",
|
|
159
|
+
moved: { tagText: ["~view:a.kpis"] },
|
|
160
|
+
},
|
|
161
|
+
{
|
|
162
|
+
// The same shape as the row above, in the spelling that also hid a tag
|
|
163
|
+
// from a walk that had just learned about `--`: a `/* ... */`, and one
|
|
164
|
+
// that runs over more than one line. That comment holds a `# label=` line,
|
|
165
|
+
// which here is prose: walking further up to find the real tag must not
|
|
166
|
+
// turn a line inside a comment into an annotation -- to rewrite in place
|
|
167
|
+
// on the writer's side, or to report as a tag on the reader's.
|
|
168
|
+
what: "retags a tile whose tag block holds a block comment",
|
|
169
|
+
edit: (d) => {
|
|
170
|
+
tile(d, "counts").colspan = 6;
|
|
171
|
+
},
|
|
172
|
+
outcome: "succeeds",
|
|
173
|
+
moved: { tagText: ["~view:a.counts"] },
|
|
174
|
+
},
|
|
175
|
+
{
|
|
176
|
+
// A line that is a comment AND a declaration at once. Read as wholly
|
|
177
|
+
// comment, it is walked straight past, and the `# label` above it -- the
|
|
178
|
+
// dimension's -- is taken for part of the tile's tag block and rewritten
|
|
179
|
+
// to match a tile that never had a label. Nothing downstream can tell:
|
|
180
|
+
// the reader takes tags from the parser and never attributed it to the
|
|
181
|
+
// tile in the first place.
|
|
182
|
+
what: "retags a tile below a declaration sharing its line with a comment",
|
|
183
|
+
edit: (d) => {
|
|
184
|
+
tile(d, "trend").colspan = 4;
|
|
185
|
+
},
|
|
186
|
+
outcome: "succeeds",
|
|
187
|
+
moved: { tagText: ["~view:a.trend"] },
|
|
188
|
+
},
|
|
189
|
+
{
|
|
190
|
+
what: "relabels a control",
|
|
191
|
+
edit: (d) => {
|
|
192
|
+
d.localGivens!.find((g) => g.name === "CATEGORY")!.label = "Product";
|
|
193
|
+
},
|
|
194
|
+
outcome: "succeeds",
|
|
195
|
+
moved: { tagText: ["~given:CATEGORY"] },
|
|
196
|
+
},
|
|
197
|
+
{
|
|
198
|
+
// The anchor is the last statement's end and the comment is past it, so
|
|
199
|
+
// an insertion lands between them unless the anchor is widened. The
|
|
200
|
+
// comment then explains a `where:` its author never wrote, and stays in
|
|
201
|
+
// the file, so nothing downstream can tell.
|
|
202
|
+
what: "binds a control onto a body whose last statement carries a comment",
|
|
203
|
+
edit: (d) => {
|
|
204
|
+
tile(d, "counts").filters = [{ field: "cat", given: "CATEGORY" }];
|
|
205
|
+
},
|
|
206
|
+
outcome: "succeeds",
|
|
207
|
+
moved: { declarations: ["~view:a.counts"] },
|
|
208
|
+
},
|
|
209
|
+
{
|
|
210
|
+
what: "adds a binding to a refinement a comment sits beside",
|
|
211
|
+
edit: (d) => {
|
|
212
|
+
tile(d, "trend").filters = [
|
|
213
|
+
{ field: "sold_on", given: "SINCE" },
|
|
214
|
+
{ field: "brand", given: "BRAND" },
|
|
215
|
+
];
|
|
216
|
+
},
|
|
217
|
+
outcome: "succeeds",
|
|
218
|
+
moved: { declarations: ["~view:a.trend"] },
|
|
219
|
+
},
|
|
220
|
+
{
|
|
221
|
+
// The comment above the removed tile is LEFT WHERE IT WAS, which is the
|
|
222
|
+
// documented rule: the file cannot say whether it belonged to the tile,
|
|
223
|
+
// the row or the page. With the tile gone it reads as the block above the
|
|
224
|
+
// view that shared its line, and that is the whole of what moved.
|
|
225
|
+
what: "removes a tile that shares its line with a view that is not a tile",
|
|
226
|
+
edit: (d) => {
|
|
227
|
+
d.tiles = d.tiles.filter((t) => t.name !== "spare");
|
|
228
|
+
},
|
|
229
|
+
outcome: "succeeds",
|
|
230
|
+
moved: {
|
|
231
|
+
declarations: ["-view:a.spare"],
|
|
232
|
+
block: ["~view:a.sidekick"],
|
|
233
|
+
},
|
|
234
|
+
},
|
|
235
|
+
{
|
|
236
|
+
what: "removes a control declared across two lines",
|
|
237
|
+
edit: (d) => {
|
|
238
|
+
d.localGivens = d.localGivens!.filter((g) => g.name !== "SINCE");
|
|
239
|
+
},
|
|
240
|
+
outcome: "succeeds",
|
|
241
|
+
moved: { declarations: ["-given:SINCE"] },
|
|
242
|
+
},
|
|
243
|
+
{
|
|
244
|
+
what: "is asked to unbind a tile whose whole `where:` holds a comment",
|
|
245
|
+
edit: (d) => {
|
|
246
|
+
delete tile(d, "kpis").filters;
|
|
247
|
+
},
|
|
248
|
+
outcome: "refuses",
|
|
249
|
+
because: "/* both halves of the same question */",
|
|
250
|
+
},
|
|
251
|
+
{
|
|
252
|
+
what: "is asked to drop one clause across a comment",
|
|
253
|
+
edit: (d) => {
|
|
254
|
+
tile(d, "kpis").filters = [{ field: "cat", given: "CATEGORY" }];
|
|
255
|
+
},
|
|
256
|
+
outcome: "refuses",
|
|
257
|
+
because: "/* both halves of the same question */",
|
|
258
|
+
},
|
|
259
|
+
{
|
|
260
|
+
what: "is asked to collapse a refinement a comment sits beside",
|
|
261
|
+
edit: (d) => {
|
|
262
|
+
delete tile(d, "trend").filters;
|
|
263
|
+
},
|
|
264
|
+
outcome: "refuses",
|
|
265
|
+
because: "// the trend, on the right",
|
|
266
|
+
},
|
|
267
|
+
];
|
|
268
|
+
|
|
269
|
+
describe("an edit changes the declaration it names, and nothing else", () => {
|
|
270
|
+
for (const row of ROWS) {
|
|
271
|
+
it(`${row.what} (${row.outcome})`, async () => {
|
|
272
|
+
const next = structuredClone(await openDocument(PROBE));
|
|
273
|
+
row.edit(next);
|
|
274
|
+
const result = await spliceDashboardDocument(PROBE, next);
|
|
275
|
+
|
|
276
|
+
if (row.outcome === "refuses") {
|
|
277
|
+
if (!spliceFailed(result))
|
|
278
|
+
throw new Error(
|
|
279
|
+
"expected a refusal; the writer wrote instead:\n" +
|
|
280
|
+
result.source,
|
|
281
|
+
);
|
|
282
|
+
expect(result.reason).toContain(row.because!);
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
if (spliceFailed(result))
|
|
286
|
+
throw new Error(`expected a write; refused: ${result.reason}`);
|
|
287
|
+
|
|
288
|
+
expect(await syntaxErrors(result.source)).toEqual([]);
|
|
289
|
+
const reread = await readDashboardDocument(result.source);
|
|
290
|
+
if (readFailed(reread)) throw new Error(reread.reason);
|
|
291
|
+
expect(await whatMoved(PROBE, result.source)).toEqual({
|
|
292
|
+
...NOTHING_MOVED,
|
|
293
|
+
...row.moved,
|
|
294
|
+
});
|
|
295
|
+
});
|
|
296
|
+
}
|
|
297
|
+
});
|
|
@@ -2,72 +2,14 @@
|
|
|
2
2
|
// SPDX-License-Identifier: MIT
|
|
3
3
|
|
|
4
4
|
import { describe, expect, it } from "bun:test";
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
const LINES = `##! experimental.givens
|
|
14
|
-
## artifact { title="T" tiles=["a -> x"] }
|
|
15
|
-
import "../m.malloy"
|
|
16
|
-
|
|
17
|
-
given: CATEGORY :: filter<string> is f''
|
|
18
|
-
given:
|
|
19
|
-
SINCE :: date is @2023-01-01
|
|
20
|
-
MIN :: number is 0
|
|
21
|
-
|
|
22
|
-
source: a is one extend {
|
|
23
|
-
# drill { to=self }
|
|
24
|
-
dimension: cat is products.category
|
|
25
|
-
view: x is vx + { limit: 5, where: cat ~ $CATEGORY }
|
|
26
|
-
view: y is {
|
|
27
|
-
group_by: cat
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
source: b is two extend {
|
|
32
|
-
view: z is vz
|
|
33
|
-
}`.split("\n");
|
|
34
|
-
|
|
35
|
-
describe("the text a dashboard file is read as", () => {
|
|
36
|
-
it("finds each source's own declarations and where a body ends", () => {
|
|
37
|
-
expect([...declarationsUnder(LINES, "a", "view")]).toEqual([
|
|
38
|
-
["x", { line: 12, rest: "vx + { limit: 5, where: cat ~ $CATEGORY }" }],
|
|
39
|
-
["y", { line: 13, rest: "{" }],
|
|
40
|
-
]);
|
|
41
|
-
expect([...declarationsUnder(LINES, "a", "dimension")]).toEqual([
|
|
42
|
-
["cat", { line: 11, rest: "products.category" }],
|
|
43
|
-
]);
|
|
44
|
-
expect([...declarationsUnder(LINES, "b", "view").keys()]).toEqual(["z"]);
|
|
45
|
-
expect(declarationLine(LINES, "source", "b")).toBe(18);
|
|
46
|
-
expect(declarationLine(LINES, "view", "missing")).toBe(-1);
|
|
47
|
-
expect(artifactLine(LINES)).toBe(1);
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
it("reads givens in both spellings, remembering a block's header", () => {
|
|
51
|
-
expect([...givenDeclarations(LINES)]).toEqual([
|
|
52
|
-
[
|
|
53
|
-
"CATEGORY",
|
|
54
|
-
{ line: 4, declaration: "CATEGORY :: filter<string> is f''" },
|
|
55
|
-
],
|
|
56
|
-
[
|
|
57
|
-
"SINCE",
|
|
58
|
-
{
|
|
59
|
-
line: 6,
|
|
60
|
-
blockHeader: 5,
|
|
61
|
-
declaration: "SINCE :: date is @2023-01-01",
|
|
62
|
-
},
|
|
63
|
-
],
|
|
64
|
-
[
|
|
65
|
-
"MIN",
|
|
66
|
-
{ line: 7, blockHeader: 5, declaration: "MIN :: number is 0" },
|
|
67
|
-
],
|
|
68
|
-
]);
|
|
69
|
-
});
|
|
70
|
-
|
|
5
|
+
import { artifactLine, isIdentifier, tileSteps } from "./malloyText";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* What is left here reads the two grammars that are not Malloy's. The shapes
|
|
9
|
+
* the deleted scanners used to cover are pinned end to end in
|
|
10
|
+
* `scannerShapes.spec.ts` instead, so they outlive the scanners.
|
|
11
|
+
*/
|
|
12
|
+
describe("the tiles=[…] grammar", () => {
|
|
71
13
|
it("splits a tile expression into its steps", () => {
|
|
72
14
|
expect(tileSteps("orders -> by_brand")).toEqual({
|
|
73
15
|
source: "orders",
|
|
@@ -81,4 +23,23 @@ describe("the text a dashboard file is read as", () => {
|
|
|
81
23
|
expect(tileSteps("{ group_by: x } -> y -> z")).toBeUndefined();
|
|
82
24
|
expect(tileSteps(undefined)).toBeUndefined();
|
|
83
25
|
});
|
|
26
|
+
|
|
27
|
+
it("accepts only a bare identifier as a name", () => {
|
|
28
|
+
expect(isIdentifier("by_brand")).toBe(true);
|
|
29
|
+
expect(isIdentifier("a.b")).toBe(false);
|
|
30
|
+
expect(isIdentifier("")).toBe(false);
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
describe("the model-level ## lines", () => {
|
|
35
|
+
it("finds the artifact tag, and nothing else", () => {
|
|
36
|
+
const lines = [
|
|
37
|
+
"##! experimental.givens",
|
|
38
|
+
'##" prose',
|
|
39
|
+
'## artifact { title="T" tiles=["a -> x"] }',
|
|
40
|
+
"source: a is b",
|
|
41
|
+
];
|
|
42
|
+
expect(artifactLine(lines)).toBe(2);
|
|
43
|
+
expect(artifactLine(["source: a is b"])).toBe(-1);
|
|
44
|
+
});
|
|
84
45
|
});
|
|
@@ -2,127 +2,22 @@
|
|
|
2
2
|
// SPDX-License-Identifier: MIT
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
* The
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
* Text, not the parser's symbol tree, because the tree is unreliable inside a
|
|
9
|
-
* source — measured, a refinement spelled `+ { limit: 5, where: … }` makes it
|
|
10
|
-
* report the refined view's base as a child and drop the next declaration —
|
|
11
|
-
* and does not cover `given:` at all. A `<keyword>: <name> is` line under a
|
|
12
|
-
* `source: <name> is` line is unambiguous, and Malloy has no nested sources
|
|
13
|
-
* to confuse it. Both sides reading the same lines the same way is also what
|
|
14
|
-
* lets the writer patch exactly what the reader read.
|
|
5
|
+
* The two small grammars that are NOT Malloy's, and so are not the parser's to
|
|
6
|
+
* answer: a `tiles=[…]` entry inside an annotation, and the model-level `##`
|
|
7
|
+
* lines. Everything about Malloy itself is located in `malloyTree`.
|
|
15
8
|
*/
|
|
9
|
+
|
|
16
10
|
const IDENT = "[A-Za-z_][A-Za-z0-9_]*";
|
|
17
11
|
|
|
18
12
|
export const isIdentifier = (text: string) =>
|
|
19
13
|
new RegExp(`^${IDENT}$`).test(text);
|
|
20
14
|
|
|
21
|
-
/** A one-line `<keyword>: <name> is <rest>` declaration. */
|
|
22
|
-
export interface DeclarationAt {
|
|
23
|
-
line: number;
|
|
24
|
-
/** Everything after `is`, trimmed: a base view, an expression, or `{`. */
|
|
25
|
-
rest: string;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* The `<keyword>:` declarations under `source: <owner>`, in file order. The
|
|
30
|
-
* body of a source ends at the next top-level declaration or `##` line.
|
|
31
|
-
*/
|
|
32
|
-
export function declarationsUnder(
|
|
33
|
-
lines: string[],
|
|
34
|
-
owner: string,
|
|
35
|
-
keyword: "view" | "dimension",
|
|
36
|
-
): Map<string, DeclarationAt> {
|
|
37
|
-
const found = new Map<string, DeclarationAt>();
|
|
38
|
-
const declaration = new RegExp(
|
|
39
|
-
`^\\s*${keyword}:\\s*(${IDENT})\\s+is\\b\\s*(.*)$`,
|
|
40
|
-
);
|
|
41
|
-
let current: string | undefined;
|
|
42
|
-
for (let line = 0; line < lines.length; line++) {
|
|
43
|
-
const text = lines[line];
|
|
44
|
-
const source = new RegExp(`^\\s*source:\\s*(${IDENT})\\s+is\\b`).exec(
|
|
45
|
-
text,
|
|
46
|
-
);
|
|
47
|
-
if (source) {
|
|
48
|
-
current = source[1];
|
|
49
|
-
continue;
|
|
50
|
-
}
|
|
51
|
-
if (/^\s*(query|run|import|given)\b/.test(text) || text.startsWith("##"))
|
|
52
|
-
current = undefined;
|
|
53
|
-
if (current !== owner) continue;
|
|
54
|
-
const m = declaration.exec(text);
|
|
55
|
-
if (m) found.set(m[1], { line, rest: m[2].trim() });
|
|
56
|
-
}
|
|
57
|
-
return found;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/** The line declaring `<keyword>: <name> is`, or -1. */
|
|
61
|
-
export function declarationLine(
|
|
62
|
-
lines: string[],
|
|
63
|
-
keyword: "view" | "source",
|
|
64
|
-
name: string,
|
|
65
|
-
): number {
|
|
66
|
-
const re = new RegExp(`^\\s*${keyword}:\\s*${name}\\s+is\\b`);
|
|
67
|
-
return lines.findIndex((l) => re.test(l));
|
|
68
|
-
}
|
|
69
|
-
|
|
70
15
|
/** The line carrying the model-level `## artifact` tag, or -1. */
|
|
71
16
|
export const artifactLine = (lines: string[]) =>
|
|
72
17
|
lines.findIndex(
|
|
73
18
|
(l) => l.trimStart().startsWith("##") && l.includes("artifact"),
|
|
74
19
|
);
|
|
75
20
|
|
|
76
|
-
/** A `given:` declaration: its line, and the block header when it is in one. */
|
|
77
|
-
export interface GivenAt {
|
|
78
|
-
line: number;
|
|
79
|
-
blockHeader?: number;
|
|
80
|
-
/** `NAME :: type is default`, as written. */
|
|
81
|
-
declaration: string;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* Every `given:` declaration, by name, in both spellings Malloy accepts:
|
|
86
|
-
*
|
|
87
|
-
* given: CATEGORY :: filter<string> is f'' // one per line
|
|
88
|
-
*
|
|
89
|
-
* given: // a block
|
|
90
|
-
* CATEGORY :: filter<string> is f''
|
|
91
|
-
* SINCE :: date is @2023-01-01
|
|
92
|
-
*/
|
|
93
|
-
export function givenDeclarations(lines: string[]): Map<string, GivenAt> {
|
|
94
|
-
const found = new Map<string, GivenAt>();
|
|
95
|
-
const nameOf = (declaration: string) =>
|
|
96
|
-
/^([A-Z_][A-Z0-9_]*)\s*::/.exec(declaration)?.[1];
|
|
97
|
-
const add = (line: number, declaration: string, blockHeader?: number) => {
|
|
98
|
-
const text = declaration.trim();
|
|
99
|
-
const name = nameOf(text);
|
|
100
|
-
if (name)
|
|
101
|
-
found.set(name, {
|
|
102
|
-
line,
|
|
103
|
-
declaration: text,
|
|
104
|
-
...(blockHeader === undefined ? {} : { blockHeader }),
|
|
105
|
-
});
|
|
106
|
-
};
|
|
107
|
-
for (let i = 0; i < lines.length; i++) {
|
|
108
|
-
const text = lines[i].trim();
|
|
109
|
-
if (text === "given:") {
|
|
110
|
-
for (let j = i + 1; j < lines.length; j++) {
|
|
111
|
-
const inner = lines[j].trim();
|
|
112
|
-
if (
|
|
113
|
-
inner === "" ||
|
|
114
|
-
/^(source|query|import|run|given|##)/.test(inner)
|
|
115
|
-
)
|
|
116
|
-
break;
|
|
117
|
-
add(j, inner, i);
|
|
118
|
-
}
|
|
119
|
-
} else if (text.startsWith("given:")) {
|
|
120
|
-
add(i, text.slice("given:".length));
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
return found;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
21
|
/** A tile expression's steps: `orders -> by_brand + { limit: 2 }`. */
|
|
127
22
|
export interface TileSteps {
|
|
128
23
|
source: string;
|