@skeudo/core 0.0.0-stage → 0.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 The Skeudo authors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,48 @@
1
- # Temporary Holding Version
1
+ # @skeudo/core
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ > Experimental, 0.x: the format and this API can change in any minor version. [CHANGELOG.md](https://github.com/wlezzar/skeudo/blob/main/CHANGELOG.md) lists the changes.
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, when it starts with a `data` line, a data model drawn as tables and a story of how their rows change. The format is defined in [spec/spec.md](https://github.com/wlezzar/skeudo/blob/main/spec/spec.md).
6
+
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
+
9
+ ```sh
10
+ npm install @skeudo/core
11
+ ```
12
+
13
+ ## Use
14
+
15
+ ```js
16
+ import { parse } from '@skeudo/core';
17
+
18
+ const sketch = parse(`get_user(id):
19
+ row = find(id) ::read:postgres.users
20
+ return row
21
+ `);
22
+
23
+ sketch.view; // 'code', or 'data' for a sketch whose first line is `data`
24
+ sketch.diagnostics; // [{ line, severity, message }], the warnings listed in the spec
25
+ ```
26
+
27
+ `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
+ - 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 `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
+ The exported TypeScript types describe every field.
33
+
34
+ ## Options
35
+
36
+ | Option | Default | |
37
+ | --- | --- | --- |
38
+ | `labelKinds` | `['untrusted', 'secret']` | Tag kinds whose value labels a variable, as `::untrusted:body` labels `body`. Every later use of a labeled variable is marked. |
39
+
40
+ ## Other exports
41
+
42
+ - `FENCE_LANGS`: the Markdown fence languages that hold a sketch, `skeudo` and `sketch`.
43
+ - `parseTagsOnly(text)`: the tags in a string made only of tags, such as `::read:postgres.users`, or undefined. The Markdown plugins use it for inline code.
44
+ - `parseRepo(url)` and `repoId(repo)`: the `::repo` forms.
45
+
46
+ ## License
47
+
48
+ MIT
@@ -0,0 +1,82 @@
1
+ import type { Diagnostic } from '../diagnostic.ts';
2
+ import type { ChangeKind, CodeSketch, Note, SketchLine, Tag, Variable } from './types.ts';
3
+ export interface Effect {
4
+ line: number;
5
+ direction: 'read' | 'write';
6
+ /** Full system value, such as `postgres.orders` or `*`. */
7
+ system: string;
8
+ /** System without its part: `postgres`. */
9
+ root: string;
10
+ /** Part after the first dot: `orders`. */
11
+ part?: string;
12
+ conditional: boolean;
13
+ unverified: boolean;
14
+ /** The system is `*`: decided at runtime. */
15
+ runtime: boolean;
16
+ note?: Note;
17
+ tag: Tag;
18
+ /** The line's text with its tags removed. */
19
+ context: string;
20
+ /** What the change being sketched did to the effect's line. A removed effect is one the code no longer has. */
21
+ change?: ChangeKind;
22
+ }
23
+ /** A change tag, with the effects on the lines it covers. */
24
+ export interface Change {
25
+ line: number;
26
+ kind: ChangeKind;
27
+ note?: string;
28
+ tag: Tag;
29
+ context: string;
30
+ /** True when the tag is `::new` or `::removed` on a block header, so it covers the whole block. */
31
+ block: boolean;
32
+ /** Effects on the lines the change covers, in order. */
33
+ effects: Effect[];
34
+ }
35
+ /** Systems, counted from the effects the code has: removed effects are left out. */
36
+ export interface SystemUse {
37
+ name: string;
38
+ reads: number;
39
+ writes: number;
40
+ parts: string[];
41
+ runtime: boolean;
42
+ }
43
+ export interface Failure {
44
+ line: number;
45
+ name: string;
46
+ /** Prose on the line, or the enclosing block header when the line has none. */
47
+ condition: string;
48
+ tag: Tag;
49
+ }
50
+ export interface Flag {
51
+ line: number;
52
+ kind: 'danger' | 'unsure' | 'unverified' | 'runtime';
53
+ text: string;
54
+ tag: Tag;
55
+ }
56
+ /** A labeled variable used on a line that has an effect. Removed effects are left out. */
57
+ export interface Flow {
58
+ line: number;
59
+ variable: string;
60
+ labels: string[];
61
+ effect: Effect;
62
+ }
63
+ export interface Review {
64
+ effects: Effect[];
65
+ systems: SystemUse[];
66
+ failures: Failure[];
67
+ flags: Flag[];
68
+ flows: Flow[];
69
+ /** Change tags in a sketch of a commit or pull request, in line order. */
70
+ changes: Change[];
71
+ labeled: Variable[];
72
+ /** Tags of kinds the spec doesn't define, such as `::todo` or `::lock`. */
73
+ others: {
74
+ line: number;
75
+ tag: Tag;
76
+ context: string;
77
+ }[];
78
+ diagnostics: Diagnostic[];
79
+ }
80
+ /** The line's text without its tags. */
81
+ export declare function lineContext(line: SketchLine): string;
82
+ export declare function analyze(sketch: CodeSketch): Review;
@@ -0,0 +1,128 @@
1
+ import { CHANGE_KINDS } from './parse.js';
2
+ const KNOWN = new Set(['read', 'write', 'fail', 'src', 'repo', 'doc', 'danger', 'unsure', 'untrusted', 'secret', ...CHANGE_KINDS]);
3
+ /** The line's text without its tags. */
4
+ export function lineContext(line) {
5
+ let text = line.text;
6
+ for (const t of [...line.tags].reverse())
7
+ text = text.slice(0, t.start) + text.slice(t.end);
8
+ return text.replace(/\s+/g, ' ').trim();
9
+ }
10
+ export function analyze(sketch) {
11
+ const byNumber = new Map(sketch.lines.map((l) => [l.number, l]));
12
+ const effects = [];
13
+ const failures = [];
14
+ const flags = [];
15
+ const flows = [];
16
+ const others = [];
17
+ const systems = new Map();
18
+ const changes = [];
19
+ const changeAt = new Map();
20
+ for (const line of sketch.lines) {
21
+ if (line.blank)
22
+ continue;
23
+ const context = lineContext(line);
24
+ const lineEffects = [];
25
+ const own = line.change && line.change.from === undefined ? line.tags.find((t) => CHANGE_KINDS.has(t.kind)) : undefined;
26
+ if (own && line.change) {
27
+ const change = {
28
+ line: line.number,
29
+ kind: line.change.kind,
30
+ note: line.change.note,
31
+ tag: own,
32
+ context,
33
+ block: line.header && line.change.kind !== 'changed',
34
+ effects: [],
35
+ };
36
+ changes.push(change);
37
+ changeAt.set(line.number, change);
38
+ }
39
+ const coveringChange = line.change && changeAt.get(line.change.from ?? line.number);
40
+ for (const tag of line.tags) {
41
+ if (tag.note?.danger)
42
+ flags.push({ line: line.number, kind: 'danger', text: tag.note.text, tag });
43
+ if ((tag.kind === 'read' || tag.kind === 'write') && tag.value) {
44
+ const system = tag.value;
45
+ const dot = system.indexOf('.');
46
+ const root = dot === -1 ? system : system.slice(0, dot);
47
+ const effect = {
48
+ line: line.number,
49
+ direction: tag.kind,
50
+ system,
51
+ root,
52
+ part: dot === -1 ? undefined : system.slice(dot + 1),
53
+ conditional: tag.conditional,
54
+ unverified: tag.unverified,
55
+ runtime: system === '*',
56
+ note: tag.note,
57
+ tag,
58
+ context,
59
+ change: line.change?.kind,
60
+ };
61
+ effects.push(effect);
62
+ coveringChange?.effects.push(effect);
63
+ // A removed effect is one the code no longer has: it isn't counted or followed.
64
+ if (effect.change !== 'removed') {
65
+ lineEffects.push(effect);
66
+ const use = systems.get(root) ?? { name: root, reads: 0, writes: 0, parts: [], runtime: effect.runtime };
67
+ if (tag.kind === 'read')
68
+ use.reads++;
69
+ else
70
+ use.writes++;
71
+ if (effect.part && !use.parts.includes(effect.part))
72
+ use.parts.push(effect.part);
73
+ systems.set(root, use);
74
+ }
75
+ if (effect.unverified)
76
+ flags.push({ line: line.number, kind: 'unverified', text: `${tag.kind}:${system} is unverified`, tag });
77
+ if (effect.runtime)
78
+ flags.push({ line: line.number, kind: 'runtime', text: `${tag.kind} target is decided at runtime`, tag });
79
+ }
80
+ else if (tag.kind === 'fail' && tag.value) {
81
+ let condition = context;
82
+ if (!condition && line.parent !== undefined) {
83
+ const parent = byNumber.get(line.parent);
84
+ if (parent)
85
+ condition = lineContext(parent).replace(/:$/, '');
86
+ }
87
+ failures.push({ line: line.number, name: tag.value, condition, tag });
88
+ }
89
+ else if (tag.kind === 'danger') {
90
+ if (!tag.note?.danger)
91
+ flags.push({ line: line.number, kind: 'danger', text: tag.note?.text || context, tag });
92
+ }
93
+ else if (tag.kind === 'unsure') {
94
+ flags.push({ line: line.number, kind: 'unsure', text: tag.note?.text || context, tag });
95
+ }
96
+ else if (!KNOWN.has(tag.kind)) {
97
+ others.push({ line: line.number, tag, context });
98
+ }
99
+ }
100
+ if (lineEffects.length) {
101
+ const used = new Set();
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) {
109
+ const v = sketch.variables[name];
110
+ if (!v?.labels.length)
111
+ continue;
112
+ for (const effect of lineEffects)
113
+ flows.push({ line: line.number, variable: name, labels: v.labels, effect });
114
+ }
115
+ }
116
+ }
117
+ return {
118
+ effects,
119
+ systems: [...systems.values()],
120
+ failures,
121
+ flags,
122
+ flows,
123
+ changes,
124
+ labeled: Object.values(sketch.variables).filter((v) => v.labels.length),
125
+ others,
126
+ diagnostics: sketch.diagnostics,
127
+ };
128
+ }
@@ -0,0 +1,28 @@
1
+ import type { CodeSketch, ParseOptions, Repo, SrcLocation, Tag } from './types.ts';
2
+ /** Tag kinds that mark what a commit or pull request did to a line. */
3
+ export declare const CHANGE_KINDS: ReadonlySet<string>;
4
+ /** Tag kinds that renderers hide: `::src` and `::repo` become links, and `::doc` shows on hover. */
5
+ export declare const HIDDEN_KINDS: ReadonlySet<string>;
6
+ /**
7
+ * Finds every tag in a line. A tag starts at the beginning of the text or after whitespace,
8
+ * so code such as `std::io` or `Vec::new()` stays plain text.
9
+ */
10
+ export declare function scanTags(text: string, onWarning?: (message: string) => void): Tag[];
11
+ /** Parses text made only of tags, such as inline code `::read?:sys`. Returns undefined otherwise. */
12
+ export declare function parseTagsOnly(text: string): Tag[] | undefined;
13
+ /** Parses a `::src` note: `path:start-end`, `path:line`, `path`, or a bare line or range. */
14
+ export declare function parseLocation(text: string): SrcLocation | undefined;
15
+ export declare function formatLocation(loc: SrcLocation): string;
16
+ /**
17
+ * Parses the URL in a `::repo` note: `git+<remote>@<revision>`, `git+file://<path>@<revision>`, or any
18
+ * other URL, used as a base. Returns undefined when the note isn't a URL.
19
+ */
20
+ export declare function parseRepo(text: string): Repo | undefined;
21
+ /**
22
+ * Names a repository by its host and path, as in `github.com/org/shop`, from a remote URL or the
23
+ * `git@host:path` form `git remote` prints. Two remotes with the same name are the same repository.
24
+ * Returns undefined for a local path or `file://` URL.
25
+ */
26
+ export declare function repoId(remote: string): string | undefined;
27
+ /** Parses a block of the code view. */
28
+ export declare function parseCode(source: string, options?: ParseOptions): CodeSketch;