@skeudo/core 0.1.0 → 0.2.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 +6 -5
- package/dist/code/analyze.d.ts +5 -0
- package/dist/code/analyze.js +16 -9
- package/dist/code/fold.d.ts +39 -0
- package/dist/code/fold.js +48 -0
- package/dist/code/parse.d.ts +10 -2
- package/dist/code/parse.js +53 -6
- package/dist/code/types.d.ts +3 -1
- package/dist/data/parse.d.ts +2 -2
- package/dist/data/parse.js +5 -5
- package/dist/header.d.ts +9 -1
- package/dist/header.js +14 -3
- package/dist/index.d.ts +3 -2
- package/dist/index.js +3 -2
- package/dist/views.d.ts +7 -4
- package/dist/views.js +33 -9
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
# @skeudo/core
|
|
2
2
|
|
|
3
|
-
>
|
|
3
|
+
> **Alpha.** The format and this API will change in ways that break existing sketches and code. Until 1.0, breaking changes come in minor versions (0.1 to 0.2), never in patches, and the [release notes](https://github.com/wlezzar/skeudo/releases) list each one.
|
|
4
4
|
|
|
5
|
-
The parser for [Skeudo](https://github.com/wlezzar/skeudo), small text formats for explaining code to a reader. A sketch is pseudocode for code review where every outside effect is tagged, or
|
|
5
|
+
The parser for [Skeudo](https://github.com/wlezzar/skeudo), small text formats for explaining code to a reader. A sketch is pseudocode for code review where every outside effect is tagged, or a data model drawn as tables and a story of how their rows change, and its first line says which: `view: code` or `view: data`. The format is defined in [spec/spec.md](https://github.com/wlezzar/skeudo/blob/main/spec/spec.md).
|
|
6
6
|
|
|
7
7
|
Most people want a renderer instead: [`@skeudo/html`](https://www.npmjs.com/package/@skeudo/html), [`markdown-it-skeudo`](https://www.npmjs.com/package/markdown-it-skeudo), [`remark-skeudo`](https://www.npmjs.com/package/remark-skeudo) or the [`skeudo`](https://www.npmjs.com/package/skeudo) CLI. Use this package to read sketches in your own tools.
|
|
8
8
|
|
|
@@ -15,18 +15,19 @@ npm install @skeudo/core
|
|
|
15
15
|
```js
|
|
16
16
|
import { parse } from '@skeudo/core';
|
|
17
17
|
|
|
18
|
-
const sketch = parse(`
|
|
18
|
+
const sketch = parse(`view: code
|
|
19
|
+
get_user(id):
|
|
19
20
|
row = find(id) ::read:postgres.users
|
|
20
21
|
return row
|
|
21
22
|
`);
|
|
22
23
|
|
|
23
|
-
sketch.view; // 'code', or 'data' for a sketch whose first line is `data`
|
|
24
|
+
sketch.view; // 'code', or 'data' for a sketch whose first line is `view: data`
|
|
24
25
|
sketch.diagnostics; // [{ line, severity, message }], the warnings listed in the spec
|
|
25
26
|
```
|
|
26
27
|
|
|
27
28
|
`parse` never throws. A problem becomes a warning on its line, and line numbers count from the sketch's first line. The result is a `Sketch`, a union told apart by `view`:
|
|
28
29
|
|
|
29
|
-
- A `CodeSketch` has its `lines`, each with its code segments and tags, the `variables` it defines and uses, its `signature`, and the repository it names with `::repo`. `analyze(sketch)` lists what a reviewer looks at: the effects, the systems each one touches, failures and the flows of labeled values.
|
|
30
|
+
- A `CodeSketch` has its `lines`, each with its code segments and tags, the `variables` it defines and uses, its `signature`, and the repository it names with `::repo`. `analyze(sketch)` lists what a reviewer looks at: the effects, the systems each one touches, failures and the flows of labeled values. `rollup(sketch, line)` sums up a block for its header, as a folded header shows it.
|
|
30
31
|
- A `DataSketch` has its `tables` (and folders), `groups` and story `steps`. `replayData(sketch)` plays the story and returns the rows at each frame, and the rows each read finds.
|
|
31
32
|
|
|
32
33
|
The exported TypeScript types describe every field.
|
package/dist/code/analyze.d.ts
CHANGED
|
@@ -79,4 +79,9 @@ export interface Review {
|
|
|
79
79
|
}
|
|
80
80
|
/** The line's text without its tags. */
|
|
81
81
|
export declare function lineContext(line: SketchLine): string;
|
|
82
|
+
/**
|
|
83
|
+
* The tracked variables a line uses, in order: every use but a definition, and the variable a call chain
|
|
84
|
+
* starts with. Labeled data reaches an effect when the line with the effect uses the labeled variable.
|
|
85
|
+
*/
|
|
86
|
+
export declare function usedVariables(line: SketchLine): string[];
|
|
82
87
|
export declare function analyze(sketch: CodeSketch): Review;
|
package/dist/code/analyze.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { CHANGE_KINDS } from './parse.js';
|
|
2
|
-
const KNOWN = new Set(['read', 'write', 'fail', 'src', 'repo', 'doc', 'danger', 'unsure', 'untrusted', 'secret', ...CHANGE_KINDS]);
|
|
2
|
+
const KNOWN = new Set(['read', 'write', 'fail', 'src', 'repo', 'doc', 'fold', 'danger', 'unsure', 'untrusted', 'secret', ...CHANGE_KINDS]);
|
|
3
3
|
/** The line's text without its tags. */
|
|
4
4
|
export function lineContext(line) {
|
|
5
5
|
let text = line.text;
|
|
@@ -7,6 +7,20 @@ export function lineContext(line) {
|
|
|
7
7
|
text = text.slice(0, t.start) + text.slice(t.end);
|
|
8
8
|
return text.replace(/\s+/g, ' ').trim();
|
|
9
9
|
}
|
|
10
|
+
/**
|
|
11
|
+
* The tracked variables a line uses, in order: every use but a definition, and the variable a call chain
|
|
12
|
+
* starts with. Labeled data reaches an effect when the line with the effect uses the labeled variable.
|
|
13
|
+
*/
|
|
14
|
+
export function usedVariables(line) {
|
|
15
|
+
const used = new Set();
|
|
16
|
+
for (const seg of line.segments) {
|
|
17
|
+
if (seg.type === 'var' && !seg.definition)
|
|
18
|
+
used.add(seg.name);
|
|
19
|
+
if (seg.type === 'call' && seg.head)
|
|
20
|
+
used.add(seg.head);
|
|
21
|
+
}
|
|
22
|
+
return [...used];
|
|
23
|
+
}
|
|
10
24
|
export function analyze(sketch) {
|
|
11
25
|
const byNumber = new Map(sketch.lines.map((l) => [l.number, l]));
|
|
12
26
|
const effects = [];
|
|
@@ -98,14 +112,7 @@ export function analyze(sketch) {
|
|
|
98
112
|
}
|
|
99
113
|
}
|
|
100
114
|
if (lineEffects.length) {
|
|
101
|
-
const
|
|
102
|
-
for (const seg of line.segments) {
|
|
103
|
-
if (seg.type === 'var' && !seg.definition)
|
|
104
|
-
used.add(seg.name);
|
|
105
|
-
if (seg.type === 'call' && seg.head)
|
|
106
|
-
used.add(seg.head);
|
|
107
|
-
}
|
|
108
|
-
for (const name of used) {
|
|
115
|
+
for (const name of usedVariables(line)) {
|
|
109
116
|
const v = sketch.variables[name];
|
|
110
117
|
if (!v?.labels.length)
|
|
111
118
|
continue;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { Diagnostic } from '../diagnostic.ts';
|
|
2
|
+
import type { ChangeKind, CodeSketch, SketchLine, Tag } from './types.ts';
|
|
3
|
+
/** Tags of one kind, modifiers and value from the lines of a block, shown as one pill on its folded header. */
|
|
4
|
+
export interface RolledTag {
|
|
5
|
+
/** The first of these tags, in line order. */
|
|
6
|
+
tag: Tag;
|
|
7
|
+
/** The line of each of these tags, in order, so a line with two of them is listed twice. */
|
|
8
|
+
lines: number[];
|
|
9
|
+
/** The danger notes of these tags, in order: their `[!…]` notes, or the notes of `::danger` tags. */
|
|
10
|
+
dangers: string[];
|
|
11
|
+
/** True when they are all on removed lines, so they are effects the code no longer has. */
|
|
12
|
+
removed: boolean;
|
|
13
|
+
/**
|
|
14
|
+
* The labeled variables that reach these effects, in order: those used on a line that carries one. Only
|
|
15
|
+
* reads and writes have them, and not removed ones, as in analyze().
|
|
16
|
+
*/
|
|
17
|
+
flows: string[];
|
|
18
|
+
}
|
|
19
|
+
/** What a folded block header shows of the lines under it. */
|
|
20
|
+
export interface Rollup {
|
|
21
|
+
/** Line number of the header. */
|
|
22
|
+
header: number;
|
|
23
|
+
/** How many lines fold under the header, leaving out blank lines and lines renderers drop. */
|
|
24
|
+
lines: number;
|
|
25
|
+
/** The tags of the block's lines, in order of first appearance. */
|
|
26
|
+
tags: RolledTag[];
|
|
27
|
+
/** The changes on the block's lines, in order of first appearance. */
|
|
28
|
+
changes: ChangeKind[];
|
|
29
|
+
/** Warnings on the block's lines. */
|
|
30
|
+
diagnostics: Diagnostic[];
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Sums up a block for its header, for when the block is folded. Every tag a reviewer must see rolls up:
|
|
34
|
+
* effects, failures, dangers, doubts, change tags and kinds the spec doesn't define. Labels stay on their
|
|
35
|
+
* variables, but the labeled variables that reach an effect are named on its pill, since the line that
|
|
36
|
+
* shows it is folded away. The tags renderers hide stay hidden. Tags of the same kind, modifiers and value
|
|
37
|
+
* share a pill. Their notes are dropped, except dangers, since a note says how and the pill only says what.
|
|
38
|
+
*/
|
|
39
|
+
export declare function rollup(sketch: CodeSketch, header: SketchLine): Rollup;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { usedVariables } from './analyze.js';
|
|
2
|
+
import { blockLines, CHANGE_KINDS, HIDDEN_KINDS } from './parse.js';
|
|
3
|
+
/**
|
|
4
|
+
* Sums up a block for its header, for when the block is folded. Every tag a reviewer must see rolls up:
|
|
5
|
+
* effects, failures, dangers, doubts, change tags and kinds the spec doesn't define. Labels stay on their
|
|
6
|
+
* variables, but the labeled variables that reach an effect are named on its pill, since the line that
|
|
7
|
+
* shows it is folded away. The tags renderers hide stay hidden. Tags of the same kind, modifiers and value
|
|
8
|
+
* share a pill. Their notes are dropped, except dangers, since a note says how and the pill only says what.
|
|
9
|
+
*/
|
|
10
|
+
export function rollup(sketch, header) {
|
|
11
|
+
const inside = blockLines(sketch.lines, header).filter((l) => !l.blank);
|
|
12
|
+
const numbers = new Set(inside.map((l) => l.number));
|
|
13
|
+
const tags = new Map();
|
|
14
|
+
const changes = [];
|
|
15
|
+
for (const line of inside) {
|
|
16
|
+
if (line.change && !changes.includes(line.change.kind))
|
|
17
|
+
changes.push(line.change.kind);
|
|
18
|
+
const labeled = usedVariables(line).filter((name) => sketch.variables[name]?.labels.length);
|
|
19
|
+
for (const tag of line.tags) {
|
|
20
|
+
if (HIDDEN_KINDS.has(tag.kind))
|
|
21
|
+
continue;
|
|
22
|
+
if (tag.value && sketch.variables[tag.value]?.labels.includes(tag.kind))
|
|
23
|
+
continue;
|
|
24
|
+
// A removed line's effects are ones the code no longer has, so they don't share a pill with current ones.
|
|
25
|
+
const removed = line.change?.kind === 'removed' && !CHANGE_KINDS.has(tag.kind);
|
|
26
|
+
const key = [tag.kind, tag.conditional, tag.unverified, tag.value ?? '', removed].join('\u0000');
|
|
27
|
+
let rolled = tags.get(key);
|
|
28
|
+
if (!rolled)
|
|
29
|
+
tags.set(key, (rolled = { tag, lines: [], dangers: [], removed, flows: [] }));
|
|
30
|
+
rolled.lines.push(line.number);
|
|
31
|
+
if ((tag.kind === 'read' || tag.kind === 'write') && tag.value && !removed) {
|
|
32
|
+
for (const name of labeled)
|
|
33
|
+
if (!rolled.flows.includes(name))
|
|
34
|
+
rolled.flows.push(name);
|
|
35
|
+
}
|
|
36
|
+
const danger = tag.note && (tag.note.danger || tag.kind === 'danger') ? tag.note.text : undefined;
|
|
37
|
+
if (danger && !rolled.dangers.includes(danger))
|
|
38
|
+
rolled.dangers.push(danger);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return {
|
|
42
|
+
header: header.number,
|
|
43
|
+
lines: inside.filter((l) => !l.hidden).length,
|
|
44
|
+
tags: [...tags.values()],
|
|
45
|
+
changes,
|
|
46
|
+
diagnostics: sketch.diagnostics.filter((d) => numbers.has(d.line)),
|
|
47
|
+
};
|
|
48
|
+
}
|
package/dist/code/parse.d.ts
CHANGED
|
@@ -1,8 +1,16 @@
|
|
|
1
|
-
import type { CodeSketch, ParseOptions, Repo, SrcLocation, Tag } from './types.ts';
|
|
1
|
+
import type { CodeSketch, ParseOptions, Repo, SketchLine, SrcLocation, Tag } from './types.ts';
|
|
2
2
|
/** Tag kinds that mark what a commit or pull request did to a line. */
|
|
3
3
|
export declare const CHANGE_KINDS: ReadonlySet<string>;
|
|
4
|
-
/**
|
|
4
|
+
/**
|
|
5
|
+
* Tag kinds that renderers hide: `::src` and `::repo` become links, `::doc` shows on hover, and `::fold`
|
|
6
|
+
* folds its block.
|
|
7
|
+
*/
|
|
5
8
|
export declare const HIDDEN_KINDS: ReadonlySet<string>;
|
|
9
|
+
/**
|
|
10
|
+
* The lines of a header's block, in order: every line after it until a line that is no deeper, with the
|
|
11
|
+
* blank lines between them but not the blank lines after the block's last line.
|
|
12
|
+
*/
|
|
13
|
+
export declare function blockLines(lines: SketchLine[], header: SketchLine): SketchLine[];
|
|
6
14
|
/**
|
|
7
15
|
* Finds every tag in a line. A tag starts at the beginning of the text or after whitespace,
|
|
8
16
|
* so code such as `std::io` or `Vec::new()` stays plain text.
|
package/dist/code/parse.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { readHeader, splitLines } from '../header.js';
|
|
2
2
|
// Grammar pieces (see spec: Grammar).
|
|
3
3
|
const NAME = String.raw `[\p{L}\p{N}_][\p{L}\p{N}_-]*`;
|
|
4
4
|
const TAG_RE = new RegExp(String.raw `::(${NAME})([?~]*)(?::(${NAME}(?:\.${NAME})*|\*))?(?:\[(!?)([^\]\n]*)\])?`, 'uy');
|
|
@@ -13,8 +13,27 @@ const WORD_RE = /^[\p{L}][\p{L}\p{N}_'’-]*/u;
|
|
|
13
13
|
const DEFAULT_LABEL_KINDS = ['untrusted', 'secret'];
|
|
14
14
|
/** Tag kinds that mark what a commit or pull request did to a line. */
|
|
15
15
|
export const CHANGE_KINDS = new Set(['new', 'changed', 'removed']);
|
|
16
|
-
/**
|
|
17
|
-
|
|
16
|
+
/**
|
|
17
|
+
* Tag kinds that renderers hide: `::src` and `::repo` become links, `::doc` shows on hover, and `::fold`
|
|
18
|
+
* folds its block.
|
|
19
|
+
*/
|
|
20
|
+
export const HIDDEN_KINDS = new Set(['src', 'repo', 'doc', 'fold']);
|
|
21
|
+
/**
|
|
22
|
+
* The lines of a header's block, in order: every line after it until a line that is no deeper, with the
|
|
23
|
+
* blank lines between them but not the blank lines after the block's last line.
|
|
24
|
+
*/
|
|
25
|
+
export function blockLines(lines, header) {
|
|
26
|
+
const start = lines.indexOf(header) + 1;
|
|
27
|
+
let end = start;
|
|
28
|
+
for (let i = start; i < lines.length; i++) {
|
|
29
|
+
if (lines[i].blank)
|
|
30
|
+
continue;
|
|
31
|
+
if (lines[i].depth <= header.depth)
|
|
32
|
+
break;
|
|
33
|
+
end = i + 1;
|
|
34
|
+
}
|
|
35
|
+
return lines.slice(start, end);
|
|
36
|
+
}
|
|
18
37
|
/**
|
|
19
38
|
* Finds every tag in a line. A tag starts at the beginning of the text or after whitespace,
|
|
20
39
|
* so code such as `std::io` or `Vec::new()` stays plain text.
|
|
@@ -207,9 +226,10 @@ export function parseCode(source, options = {}) {
|
|
|
207
226
|
const rawLines = splitLines(source);
|
|
208
227
|
while (rawLines.length && rawLines[rawLines.length - 1].trim() === '')
|
|
209
228
|
rawLines.pop();
|
|
210
|
-
//
|
|
211
|
-
|
|
212
|
-
|
|
229
|
+
// The header only names the view. It stays a line, left blank, so line numbers still count it. A first
|
|
230
|
+
// line naming no view, such as `view: dat`, stays as it is, so the warning parse() puts on it shows.
|
|
231
|
+
const header = readHeader(rawLines);
|
|
232
|
+
if (header?.name === 'code')
|
|
213
233
|
rawLines[header.index] = '';
|
|
214
234
|
const indents = [];
|
|
215
235
|
const lines = rawLines.map((raw, idx) => {
|
|
@@ -228,6 +248,7 @@ export function parseCode(source, options = {}) {
|
|
|
228
248
|
segments: [],
|
|
229
249
|
trailingStart: text.length,
|
|
230
250
|
hidden: false,
|
|
251
|
+
fold: false,
|
|
231
252
|
};
|
|
232
253
|
});
|
|
233
254
|
// --- Indentation and blocks -------------------------------------------------
|
|
@@ -444,6 +465,32 @@ export function parseCode(source, options = {}) {
|
|
|
444
465
|
line.change = { kind: outer.kind, from: outer.from ?? parent.number };
|
|
445
466
|
}
|
|
446
467
|
}
|
|
468
|
+
// --- Folds ----------------------------------------------------------------------
|
|
469
|
+
// A folded line shows the effects of the lines under it, so an effect tagged on both would count twice.
|
|
470
|
+
for (const line of body) {
|
|
471
|
+
if (!line.tags.some((t) => t.kind === 'fold'))
|
|
472
|
+
continue;
|
|
473
|
+
if (line.signature) {
|
|
474
|
+
warn(line.number, `The signature can't fold. Put ::fold on a block inside the sketch.`);
|
|
475
|
+
continue;
|
|
476
|
+
}
|
|
477
|
+
if (!line.header) {
|
|
478
|
+
warn(line.number, `There is nothing under this line to fold. Put ::fold on a line with indented lines under it.`);
|
|
479
|
+
continue;
|
|
480
|
+
}
|
|
481
|
+
line.fold = true;
|
|
482
|
+
const inside = blockLines(lines, line);
|
|
483
|
+
const seen = new Set();
|
|
484
|
+
for (const t of line.tags) {
|
|
485
|
+
if ((t.kind !== 'read' && t.kind !== 'write') || !t.value || seen.has(`${t.kind}:${t.value}`))
|
|
486
|
+
continue;
|
|
487
|
+
seen.add(`${t.kind}:${t.value}`);
|
|
488
|
+
if (inside.some((l) => l.tags.some((u) => u.kind === t.kind && u.value === t.value))) {
|
|
489
|
+
const verb = t.kind === 'read' ? 'read' : 'written';
|
|
490
|
+
warn(line.number, `${t.value} is ${verb} inside this block too. Tag an effect once, where it happens; the folded line shows it.`);
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
}
|
|
447
494
|
// --- Segments -------------------------------------------------------------------
|
|
448
495
|
const varNames = new Set(Object.keys(variables));
|
|
449
496
|
for (const line of body)
|
package/dist/code/types.d.ts
CHANGED
|
@@ -130,8 +130,10 @@ export interface SketchLine {
|
|
|
130
130
|
change?: LineChange;
|
|
131
131
|
/** Explanation of the line, from its `::doc[text]` tags, one per line of text. On a block header it explains the block. */
|
|
132
132
|
doc?: string;
|
|
133
|
-
/** True when the line holds nothing but `::src`, `::repo` and `::
|
|
133
|
+
/** True when the line holds nothing but `::src`, `::repo`, `::doc` and `::fold` tags, so a renderer drops it. */
|
|
134
134
|
hidden: boolean;
|
|
135
|
+
/** True when the line is a block header tagged `::fold`, so renderers start its block folded. */
|
|
136
|
+
fold: boolean;
|
|
135
137
|
}
|
|
136
138
|
export interface Variable {
|
|
137
139
|
name: string;
|
package/dist/data/parse.d.ts
CHANGED
|
@@ -4,7 +4,7 @@ import { type DataSketch } from './types.ts';
|
|
|
4
4
|
* `table name(col, col -> table)` or `folder name(col)`, or start a step. Indented lines under any of them
|
|
5
5
|
* are its explanation, except under a step. There, a line starting with `>` starts a substep, and lines
|
|
6
6
|
* starting with `+`, `~` or `-` and a space are changes and lines starting with `?` are reads, of the
|
|
7
|
-
* substep or step above them. A blank line splits an explanation into paragraphs.
|
|
8
|
-
* first line is skipped. The diagnostics include what playing the story finds (checkData).
|
|
7
|
+
* substep or step above them. A blank line splits an explanation into paragraphs. The `view: data` header
|
|
8
|
+
* on the first line is skipped. The diagnostics include what playing the story finds (checkData).
|
|
9
9
|
*/
|
|
10
10
|
export declare function parseData(source: string): DataSketch;
|
package/dist/data/parse.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { readHeader, splitLines } from '../header.js';
|
|
2
2
|
import { checkData } from './replay.js';
|
|
3
3
|
import { DATA_KINDS, } from './types.js';
|
|
4
4
|
const NAME = String.raw `[\p{L}\p{N}_.-]+`;
|
|
@@ -25,8 +25,8 @@ const VALUE = new RegExp(String.raw `^(${NAME})\s*:\s*(.*)$`, 'u');
|
|
|
25
25
|
* `table name(col, col -> table)` or `folder name(col)`, or start a step. Indented lines under any of them
|
|
26
26
|
* are its explanation, except under a step. There, a line starting with `>` starts a substep, and lines
|
|
27
27
|
* starting with `+`, `~` or `-` and a space are changes and lines starting with `?` are reads, of the
|
|
28
|
-
* substep or step above them. A blank line splits an explanation into paragraphs.
|
|
29
|
-
* first line is skipped. The diagnostics include what playing the story finds (checkData).
|
|
28
|
+
* substep or step above them. A blank line splits an explanation into paragraphs. The `view: data` header
|
|
29
|
+
* on the first line is skipped. The diagnostics include what playing the story finds (checkData).
|
|
30
30
|
*/
|
|
31
31
|
export function parseData(source) {
|
|
32
32
|
const lines = splitLines(source);
|
|
@@ -44,8 +44,8 @@ export function parseData(source) {
|
|
|
44
44
|
let group;
|
|
45
45
|
/** Whether the line before continues the same paragraph of explanation. */
|
|
46
46
|
let paragraph = false;
|
|
47
|
-
const header =
|
|
48
|
-
const start = header?.
|
|
47
|
+
const header = readHeader(lines);
|
|
48
|
+
const start = header?.name === 'data' ? header.index + 1 : 0;
|
|
49
49
|
for (let i = start; i < lines.length; i++) {
|
|
50
50
|
const raw = lines[i];
|
|
51
51
|
const line = i + 1;
|
package/dist/header.d.ts
CHANGED
|
@@ -1,7 +1,15 @@
|
|
|
1
1
|
/** Splits a block into lines, whatever its line endings. */
|
|
2
2
|
export declare const splitLines: (source: string) => string[];
|
|
3
|
-
/** A block's first non-blank line, trimmed, and its index.
|
|
3
|
+
/** A block's first non-blank line, trimmed, and its index. */
|
|
4
4
|
export declare function firstLine(lines: readonly string[]): {
|
|
5
5
|
text: string;
|
|
6
6
|
index: number;
|
|
7
7
|
} | undefined;
|
|
8
|
+
/**
|
|
9
|
+
* `view:` and a name, alone on a block's first non-blank line. It is a header when the name is a view's.
|
|
10
|
+
* views.ts warns when it isn't. Undefined when the first line isn't of this form.
|
|
11
|
+
*/
|
|
12
|
+
export declare function readHeader(lines: readonly string[]): {
|
|
13
|
+
name: string;
|
|
14
|
+
index: number;
|
|
15
|
+
} | undefined;
|
package/dist/header.js
CHANGED
|
@@ -1,11 +1,22 @@
|
|
|
1
1
|
/*
|
|
2
|
-
* A block
|
|
3
|
-
*
|
|
2
|
+
* A block names its view on its first non-blank line, as `view: data`. The header stays a line of the
|
|
3
|
+
* block, so line numbers match its text. Picking the view, and warning when the header is missing or names
|
|
4
|
+
* no view, happens in views.ts. Each view's parser skips its own header line, and nothing else.
|
|
4
5
|
*/
|
|
5
6
|
/** Splits a block into lines, whatever its line endings. */
|
|
6
7
|
export const splitLines = (source) => source.replace(/\r\n?/g, '\n').split('\n');
|
|
7
|
-
/** A block's first non-blank line, trimmed, and its index.
|
|
8
|
+
/** A block's first non-blank line, trimmed, and its index. */
|
|
8
9
|
export function firstLine(lines) {
|
|
9
10
|
const index = lines.findIndex((l) => l.trim() !== '');
|
|
10
11
|
return index === -1 ? undefined : { text: lines[index].trim(), index };
|
|
11
12
|
}
|
|
13
|
+
const HEADER = /^view:\s*(\S+)$/;
|
|
14
|
+
/**
|
|
15
|
+
* `view:` and a name, alone on a block's first non-blank line. It is a header when the name is a view's.
|
|
16
|
+
* views.ts warns when it isn't. Undefined when the first line isn't of this form.
|
|
17
|
+
*/
|
|
18
|
+
export function readHeader(lines) {
|
|
19
|
+
const first = firstLine(lines);
|
|
20
|
+
const name = first && HEADER.exec(first.text)?.[1];
|
|
21
|
+
return name ? { name, index: first.index } : undefined;
|
|
22
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
export * from './diagnostic.ts';
|
|
2
2
|
export { VIEWS, codeView, dataView, viewOf, parse, FENCE_LANGS, type Sketch, type View, type ViewName } from './views.ts';
|
|
3
3
|
export * from './code/types.ts';
|
|
4
|
-
export { parseCode, scanTags, parseTagsOnly, parseLocation, formatLocation, parseRepo, repoId, CHANGE_KINDS, HIDDEN_KINDS } from './code/parse.ts';
|
|
5
|
-
export {
|
|
4
|
+
export { parseCode, scanTags, parseTagsOnly, parseLocation, formatLocation, parseRepo, repoId, blockLines, CHANGE_KINDS, HIDDEN_KINDS } from './code/parse.ts';
|
|
5
|
+
export { rollup, type Rollup, type RolledTag } from './code/fold.ts';
|
|
6
|
+
export { analyze, lineContext, usedVariables } from './code/analyze.ts';
|
|
6
7
|
export type { Effect, SystemUse, Failure, Flag, Flow, Change, Review } from './code/analyze.ts';
|
|
7
8
|
export * from './data/types.ts';
|
|
8
9
|
export { parseData } from './data/parse.ts';
|
package/dist/index.js
CHANGED
|
@@ -2,8 +2,9 @@ export * from './diagnostic.js';
|
|
|
2
2
|
export { VIEWS, codeView, dataView, viewOf, parse, FENCE_LANGS } from './views.js';
|
|
3
3
|
// The code view.
|
|
4
4
|
export * from './code/types.js';
|
|
5
|
-
export { parseCode, scanTags, parseTagsOnly, parseLocation, formatLocation, parseRepo, repoId, CHANGE_KINDS, HIDDEN_KINDS } from './code/parse.js';
|
|
6
|
-
export {
|
|
5
|
+
export { parseCode, scanTags, parseTagsOnly, parseLocation, formatLocation, parseRepo, repoId, blockLines, CHANGE_KINDS, HIDDEN_KINDS } from './code/parse.js';
|
|
6
|
+
export { rollup } from './code/fold.js';
|
|
7
|
+
export { analyze, lineContext, usedVariables } from './code/analyze.js';
|
|
7
8
|
// The data view.
|
|
8
9
|
export * from './data/types.js';
|
|
9
10
|
export { parseData } from './data/parse.js';
|
package/dist/views.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import type { CodeSketch, ParseOptions } from './code/types.ts';
|
|
|
2
2
|
import type { DataSketch } from './data/types.ts';
|
|
3
3
|
/** A parsed block of any view, told apart by `view`. */
|
|
4
4
|
export type Sketch = CodeSketch | DataSketch;
|
|
5
|
-
/** The name of a view, such as `code`. A block names its view
|
|
5
|
+
/** The name of a view, such as `code`. A block names its view on its first line, as `view: code`. */
|
|
6
6
|
export type ViewName = Sketch['view'];
|
|
7
7
|
/** One kind of Skeudo block. */
|
|
8
8
|
export interface View<S extends Sketch = Sketch> {
|
|
@@ -14,11 +14,14 @@ export interface View<S extends Sketch = Sketch> {
|
|
|
14
14
|
export declare const codeView: View<CodeSketch>;
|
|
15
15
|
/** Tables, example rows, and a story of how the rows change. See spec/data.md. */
|
|
16
16
|
export declare const dataView: View<DataSketch>;
|
|
17
|
-
/** Every view. The first is the
|
|
17
|
+
/** Every view. The first is the fallback, for a block that doesn't name one. */
|
|
18
18
|
export declare const VIEWS: readonly View[];
|
|
19
|
-
/** The view a block is written in: the one its
|
|
19
|
+
/** The view a block is written in: the one its header names, or code when it has none or names no view. */
|
|
20
20
|
export declare function viewOf(source: string): View;
|
|
21
|
-
/**
|
|
21
|
+
/**
|
|
22
|
+
* Parses a block in its view. A block without a header, or whose first line names no view, is parsed as
|
|
23
|
+
* code with a warning, since a sketch always renders. A view's own parser, such as parseCode, doesn't warn.
|
|
24
|
+
*/
|
|
22
25
|
export declare function parse(source: string, options?: ParseOptions): Sketch;
|
|
23
26
|
/** Fence languages that hold a Skeudo block: skeudo, and sketch, the format's working name, which older documents use. */
|
|
24
27
|
export declare const FENCE_LANGS: readonly string[];
|
package/dist/views.js
CHANGED
|
@@ -1,26 +1,50 @@
|
|
|
1
1
|
/*
|
|
2
2
|
* Skeudo has several views, like Mermaid's diagram types: each is a kind of block with its own grammar,
|
|
3
3
|
* parser and renderer. They share one fence and one file extension, and a block names its view on its
|
|
4
|
-
* first line (header.ts). Tools that host blocks, such as the Markdown plugins and the
|
|
5
|
-
* this registry and never name a view, so adding a view means adding its folder and an
|
|
4
|
+
* first line, as `view: data` (header.ts). Tools that host blocks, such as the Markdown plugins and the
|
|
5
|
+
* CLI, go through this registry and never name a view, so adding a view means adding its folder and an
|
|
6
|
+
* entry here.
|
|
6
7
|
*/
|
|
7
8
|
import { parseCode } from './code/parse.js';
|
|
8
9
|
import { parseData } from './data/parse.js';
|
|
9
|
-
import { firstLine, splitLines } from './header.js';
|
|
10
|
+
import { firstLine, readHeader, splitLines } from './header.js';
|
|
10
11
|
/** Pseudocode for code review, with every outside effect tagged. See spec/code.md. */
|
|
11
12
|
export const codeView = { name: 'code', parse: parseCode };
|
|
12
13
|
/** Tables, example rows, and a story of how the rows change. See spec/data.md. */
|
|
13
14
|
export const dataView = { name: 'data', parse: parseData };
|
|
14
|
-
/** Every view. The first is the
|
|
15
|
+
/** Every view. The first is the fallback, for a block that doesn't name one. */
|
|
15
16
|
export const VIEWS = [codeView, dataView];
|
|
16
|
-
/** The view a block is written in: the one its
|
|
17
|
+
/** The view a block is written in: the one its header names, or code when it has none or names no view. */
|
|
17
18
|
export function viewOf(source) {
|
|
18
|
-
const
|
|
19
|
-
return VIEWS.find((v) => v.name ===
|
|
19
|
+
const name = readHeader(splitLines(source))?.name;
|
|
20
|
+
return VIEWS.find((v) => v.name === name) ?? VIEWS[0];
|
|
20
21
|
}
|
|
21
|
-
/**
|
|
22
|
+
/**
|
|
23
|
+
* Parses a block in its view. A block without a header, or whose first line names no view, is parsed as
|
|
24
|
+
* code with a warning, since a sketch always renders. A view's own parser, such as parseCode, doesn't warn.
|
|
25
|
+
*/
|
|
22
26
|
export function parse(source, options = {}) {
|
|
23
|
-
|
|
27
|
+
const lines = splitLines(source);
|
|
28
|
+
const header = readHeader(lines);
|
|
29
|
+
const view = VIEWS.find((v) => v.name === header?.name);
|
|
30
|
+
const sketch = (view ?? VIEWS[0]).parse(source, options);
|
|
31
|
+
if (!view) {
|
|
32
|
+
const warning = headerWarning(lines, header);
|
|
33
|
+
// Put first and sorted, so it leads the other warnings on its line.
|
|
34
|
+
if (warning)
|
|
35
|
+
sketch.diagnostics = [warning, ...sketch.diagnostics].sort((a, b) => a.line - b.line);
|
|
36
|
+
}
|
|
37
|
+
return sketch;
|
|
38
|
+
}
|
|
39
|
+
/** The warning for a block whose header is missing or names no view, on its first line. A blank block gets none. */
|
|
40
|
+
function headerWarning(lines, header) {
|
|
41
|
+
const warning = (index, message) => ({ line: index + 1, severity: 'warning', message });
|
|
42
|
+
if (header)
|
|
43
|
+
return warning(header.index, `No view named ${header.name}. The views are ${list(VIEWS.map((v) => v.name))}.`);
|
|
44
|
+
const first = firstLine(lines);
|
|
45
|
+
return first && warning(first.index, 'A sketch should start with its view, as in view: code.');
|
|
24
46
|
}
|
|
47
|
+
/** `a`, `a and b`, `a, b and c`. */
|
|
48
|
+
const list = (words) => (words.length < 2 ? words.join('') : `${words.slice(0, -1).join(', ')} and ${words.at(-1)}`);
|
|
25
49
|
/** Fence languages that hold a Skeudo block: skeudo, and sketch, the format's working name, which older documents use. */
|
|
26
50
|
export const FENCE_LANGS = ['skeudo', 'sketch'];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@skeudo/core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Parses Skeudo sketches, small text formats for explaining code: pseudocode with every outside effect tagged, and data models drawn as tables with a story of how their rows change.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"skeudo",
|