@risinghq/lienzo 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/README.md +82 -2
- package/dist/shell/assets/index-Cna8wlzF.js +4 -0
- package/dist/shell/assets/index-CoJSASvW.css +2 -0
- package/dist/shell/index.html +13 -0
- package/package.json +76 -4
- package/src/bench/bench.ts +132 -0
- package/src/bridge/page.ts +443 -0
- package/src/bridge/protocol.ts +124 -0
- package/src/bridge/record.ts +75 -0
- package/src/bridge/script.ts +24 -0
- package/src/bridge/state.ts +143 -0
- package/src/capture/capture.ts +125 -0
- package/src/capture/compare.ts +46 -0
- package/src/capture/diff.ts +16 -0
- package/src/capture/manifest.ts +60 -0
- package/src/capture/refs.ts +20 -0
- package/src/capture/replay.ts +25 -0
- package/src/capture/review.ts +78 -0
- package/src/capture/serve.ts +32 -0
- package/src/capture/snap.ts +62 -0
- package/src/capture/store.ts +28 -0
- package/src/cli.ts +287 -0
- package/src/cloud/checkout.ts +33 -0
- package/src/cloud/credentials.ts +25 -0
- package/src/cloud/link.ts +60 -0
- package/src/cloud/login.ts +85 -0
- package/src/commands/canvas.ts +46 -0
- package/src/commands/change.ts +218 -0
- package/src/commands/class.ts +66 -0
- package/src/commands/command.ts +27 -0
- package/src/commands/create.ts +95 -0
- package/src/commands/element.ts +118 -0
- package/src/commands/file.ts +96 -0
- package/src/commands/frame.ts +58 -0
- package/src/commands/override.ts +38 -0
- package/src/commands/prop.ts +18 -0
- package/src/commands/registry.ts +76 -0
- package/src/commands/rule.ts +131 -0
- package/src/commands/stage.ts +52 -0
- package/src/commands/story.ts +112 -0
- package/src/commands/style.ts +123 -0
- package/src/commands/text.ts +17 -0
- package/src/commands/token.ts +60 -0
- package/src/commands/transaction.ts +66 -0
- package/src/job/docker.ts +109 -0
- package/src/job/job.ts +125 -0
- package/src/job/server.ts +62 -0
- package/src/mirror/apply.ts +121 -0
- package/src/mirror/entry.ts +7 -0
- package/src/mirror/host.ts +68 -0
- package/src/mirror/observe.ts +271 -0
- package/src/mirror/script.ts +18 -0
- package/src/mirror/transport.ts +7 -0
- package/src/mirror/wire.ts +25 -0
- package/src/project/config.ts +98 -0
- package/src/project/design.ts +59 -0
- package/src/project/detect.ts +184 -0
- package/src/project/files.ts +30 -0
- package/src/project/git.ts +68 -0
- package/src/project/github.ts +42 -0
- package/src/project/history.ts +89 -0
- package/src/project/project.ts +106 -0
- package/src/project/spawn.ts +23 -0
- package/src/project/step.ts +15 -0
- package/src/protocol.ts +125 -0
- package/src/sandbox/forward.ts +73 -0
- package/src/sandbox/install.ts +44 -0
- package/src/sandbox/sandbox.ts +57 -0
- package/src/serve/build.ts +49 -0
- package/src/serve/canvases.ts +65 -0
- package/src/serve/dev.ts +296 -0
- package/src/serve/env.ts +16 -0
- package/src/serve/mcp.ts +65 -0
- package/src/serve/middleware.ts +172 -0
- package/src/serve/next/config.ts +68 -0
- package/src/serve/next/hook.ts +74 -0
- package/src/serve/next/loader.ts +37 -0
- package/src/serve/nuxt.ts +14 -0
- package/src/serve/plugin.ts +95 -0
- package/src/serve/preload.ts +167 -0
- package/src/serve/resend.ts +29 -0
- package/src/serve/running.ts +115 -0
- package/src/serve/screenshot.ts +26 -0
- package/src/serve/sources.ts +46 -0
- package/src/serve/uncached.ts +25 -0
- package/src/serve/vite-build.ts +15 -0
- package/src/serve/vite.ts +19 -0
- package/src/session/session.ts +96 -0
- package/src/share/ask.ts +24 -0
- package/src/share/client.ts +140 -0
- package/src/share/consent.ts +39 -0
- package/src/share/frame.ts +26 -0
- package/src/source/astro/astro.ts +10 -0
- package/src/source/astro/elements.ts +60 -0
- package/src/source/blocks.ts +42 -0
- package/src/source/css.ts +48 -0
- package/src/source/estree.ts +55 -0
- package/src/source/extract.ts +68 -0
- package/src/source/insert.ts +189 -0
- package/src/source/jsx/elements.ts +136 -0
- package/src/source/jsx/jsx.ts +5 -0
- package/src/source/language.ts +176 -0
- package/src/source/location.ts +34 -0
- package/src/source/modules.ts +41 -0
- package/src/source/overrides.ts +35 -0
- package/src/source/page.ts +69 -0
- package/src/source/patch.ts +79 -0
- package/src/source/prop.ts +34 -0
- package/src/source/refusal.ts +7 -0
- package/src/source/rules.ts +298 -0
- package/src/source/stamp.ts +28 -0
- package/src/source/story-file.ts +40 -0
- package/src/source/story.ts +162 -0
- package/src/source/structure.ts +253 -0
- package/src/source/style.ts +143 -0
- package/src/source/svelte/elements.ts +154 -0
- package/src/source/svelte/svelte.ts +11 -0
- package/src/source/tag.ts +99 -0
- package/src/source/tailwind.ts +46 -0
- package/src/source/tokens.ts +56 -0
- package/src/source/vue/elements.ts +236 -0
- package/src/source/vue/vue.ts +11 -0
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import MagicString from "magic-string";
|
|
2
|
+
import type { Rung } from "../protocol.ts";
|
|
3
|
+
import { tagAt, type ClassAttribute, type Span } from "./language.ts";
|
|
4
|
+
import type { Location } from "./location.ts";
|
|
5
|
+
import { refuse, type Refusal } from "./refusal.ts";
|
|
6
|
+
|
|
7
|
+
// `select` is the offset in `code` of the element a structure edit leaves
|
|
8
|
+
// selected, or null when it leaves none. Other edits keep the selection.
|
|
9
|
+
export type Patch = { ok: true; code: string; select?: number | null; rung?: Rung } | Refusal;
|
|
10
|
+
|
|
11
|
+
// Replaces the text between an element's tags, keeping the whitespace around it.
|
|
12
|
+
export function patchText(source: string, at: Location, text: string): Patch {
|
|
13
|
+
const tag = tagAt(source, at);
|
|
14
|
+
if (!tag.ok) return tag;
|
|
15
|
+
if (!tag.text.ok) return tag.text;
|
|
16
|
+
|
|
17
|
+
if (/[{}<>]/.test(text)) {
|
|
18
|
+
return refuse("text with { } < or > needs an expression");
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const { start, end, value: old } = tag.text;
|
|
22
|
+
const lead = /^\s*/.exec(old)?.[0] ?? "";
|
|
23
|
+
const trail = old.trim() ? (/\s*$/.exec(old)?.[0] ?? "") : "";
|
|
24
|
+
|
|
25
|
+
const out = new MagicString(source);
|
|
26
|
+
if (start === end) out.appendLeft(start, text);
|
|
27
|
+
else out.overwrite(start, end, lead + text.trim() + trail);
|
|
28
|
+
|
|
29
|
+
return { ok: true, code: out.toString() };
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function patchHelper(source: string, strings: Span[], change: (list: string) => string): Patch {
|
|
33
|
+
for (const span of strings) {
|
|
34
|
+
const list = change(span.value);
|
|
35
|
+
if (list === span.value) continue;
|
|
36
|
+
if (/["{}\\`]/.test(list)) return refuse(`"${list}" cannot be a class list`);
|
|
37
|
+
|
|
38
|
+
const out = new MagicString(source);
|
|
39
|
+
out.overwrite(span.start, span.end, `"${list}"`);
|
|
40
|
+
|
|
41
|
+
return { ok: true, code: out.toString(), rung: "helper" };
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return { ok: true, code: source, rung: "helper" };
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// Rewrites the class list of the element whose tag starts at `at`.
|
|
48
|
+
export function patchClasses(
|
|
49
|
+
source: string,
|
|
50
|
+
at: Location,
|
|
51
|
+
attribute: ClassAttribute,
|
|
52
|
+
change: (list: string) => string,
|
|
53
|
+
): Patch {
|
|
54
|
+
const tag = tagAt(source, at);
|
|
55
|
+
if (!tag.ok) return tag;
|
|
56
|
+
|
|
57
|
+
const classes = tag.classes(attribute);
|
|
58
|
+
if (!classes.ok) return classes;
|
|
59
|
+
if ("strings" in classes && classes.strings) return patchHelper(source, classes.strings, change);
|
|
60
|
+
|
|
61
|
+
const list = change(classes.value);
|
|
62
|
+
if (/["{}\\`]/.test(list)) return refuse(`"${list}" cannot be a class list`);
|
|
63
|
+
|
|
64
|
+
const out = new MagicString(source);
|
|
65
|
+
|
|
66
|
+
// An empty list has no attribute, so adding a class and removing it again
|
|
67
|
+
// gives back the source.
|
|
68
|
+
if ("insert" in classes) {
|
|
69
|
+
if (list) out.appendLeft(classes.insert, ` ${classes.name}="${list}"`);
|
|
70
|
+
} else if (list) {
|
|
71
|
+
out.overwrite(classes.start, classes.end, `"${list}"`);
|
|
72
|
+
} else {
|
|
73
|
+
const { start, end } = classes.attribute;
|
|
74
|
+
const space = /\s*$/.exec(source.slice(0, start))?.[0].length ?? 0;
|
|
75
|
+
out.remove(start - space, end);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return { ok: true, code: out.toString() };
|
|
79
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import MagicString from "magic-string";
|
|
2
|
+
import { elementAt } from "./language.ts";
|
|
3
|
+
import type { Location } from "./location.ts";
|
|
4
|
+
import type { Patch } from "./patch.ts";
|
|
5
|
+
import { refuse } from "./refusal.ts";
|
|
6
|
+
|
|
7
|
+
const NAME = /^[A-Za-z_][\w:.-]*$/;
|
|
8
|
+
const UNSAFE = /["{}\\`<>]/;
|
|
9
|
+
|
|
10
|
+
// Sets an attribute to a string the source states outright. An attribute whose
|
|
11
|
+
// value is an expression, or that a spread may also set, is not overwritten.
|
|
12
|
+
export function patchProp(source: string, at: Location, name: string, value: string): Patch {
|
|
13
|
+
if (!NAME.test(name)) return refuse(`"${name}" is not an attribute name`);
|
|
14
|
+
if (UNSAFE.test(value)) return refuse(`"${value}" needs an expression, not a quoted value`);
|
|
15
|
+
|
|
16
|
+
const found = elementAt(source, at);
|
|
17
|
+
if (!found.ok) return found;
|
|
18
|
+
const { element } = found;
|
|
19
|
+
|
|
20
|
+
const held = element.attributes.filter((attribute) => attribute.name === name);
|
|
21
|
+
const stated = held.find((attribute) => attribute.value?.literal !== undefined) ?? held[0];
|
|
22
|
+
const out = new MagicString(source);
|
|
23
|
+
|
|
24
|
+
if (!stated) {
|
|
25
|
+
if (element.spread) return refuse(`<${element.name}> spreads props that may set ${name}`);
|
|
26
|
+
out.appendLeft(element.nameEnd, ` ${name}="${value}"`);
|
|
27
|
+
} else if (stated.value?.literal === undefined) {
|
|
28
|
+
return refuse(`${name} of <${element.name}> is computed, not a literal`);
|
|
29
|
+
} else {
|
|
30
|
+
out.overwrite(stated.value.start, stated.value.end, `"${value}"`);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return { ok: true, code: out.toString() };
|
|
34
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
// A refusal carries the reason the source has no single span to change. The
|
|
2
|
+
// caller hands it to an agent, which can read the surrounding code.
|
|
3
|
+
export type Refusal = { ok: false; reason: string };
|
|
4
|
+
|
|
5
|
+
export function refuse(reason: string): Refusal {
|
|
6
|
+
return { ok: false, reason };
|
|
7
|
+
}
|
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
import MagicString from "magic-string";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import type { AtRule, ChildNode, Container, Declaration, Document, Root, Rule } from "postcss";
|
|
4
|
+
import { blocksOf } from "./blocks.ts";
|
|
5
|
+
import { cleanValue, parseCss, valueOf } from "./css.ts";
|
|
6
|
+
import type { StyleBlock } from "./language.ts";
|
|
7
|
+
import { offsetOf } from "./location.ts";
|
|
8
|
+
import type { Patch } from "./patch.ts";
|
|
9
|
+
import { refuse, type Refusal } from "./refusal.ts";
|
|
10
|
+
|
|
11
|
+
export type Property = { property: string; value: string };
|
|
12
|
+
|
|
13
|
+
// The at-rules a rule can sit in and still be edited by its selector. A rule
|
|
14
|
+
// nested in another rule matches more than its own selector says, and one in
|
|
15
|
+
// `@scope` or `@keyframes` is not matched by its selector alone.
|
|
16
|
+
const GROUPS = ["media", "supports", "container", "layer"];
|
|
17
|
+
|
|
18
|
+
// The at-rules around a rule, outermost first, each as `@media (width < 40rem)`
|
|
19
|
+
// with its spacing collapsed, joined by a space: "" for a rule outside any.
|
|
20
|
+
// Undefined when something around the rule is not a group lienzo reads
|
|
21
|
+
// through.
|
|
22
|
+
function conditionOf(rule: Rule): string | undefined {
|
|
23
|
+
const chain: string[] = [];
|
|
24
|
+
|
|
25
|
+
let node: Container | Document | undefined = rule.parent;
|
|
26
|
+
for (; node && node.type !== "root" && node.type !== "document"; node = node.parent) {
|
|
27
|
+
const group = node as AtRule;
|
|
28
|
+
if (group.type !== "atrule" || !GROUPS.includes(group.name.toLowerCase())) return undefined;
|
|
29
|
+
|
|
30
|
+
chain.unshift(`@${group.name} ${group.params}`.trim());
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return chain.join(" ").replace(/\s+/g, " ");
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// A condition as it compares: `(min-width:40rem)` is `(min-width: 40rem)`.
|
|
37
|
+
const normalCondition = (condition: string) =>
|
|
38
|
+
condition
|
|
39
|
+
.replace(/\s+/g, " ")
|
|
40
|
+
.replace(/ ?([(),:]) ?/g, "$1")
|
|
41
|
+
.trim();
|
|
42
|
+
|
|
43
|
+
// `.card`, or `.card in @media (width < 40rem)`.
|
|
44
|
+
const named = (selector: string, condition: string) =>
|
|
45
|
+
condition ? `${selector} in ${condition}` : selector;
|
|
46
|
+
|
|
47
|
+
// A selector list as it compares: the browser writes `.a, .b` and `a > b` for
|
|
48
|
+
// a source's `.a,.b` and `a>b`. A descendant space stays, so `.a .b` is not
|
|
49
|
+
// `.a.b`.
|
|
50
|
+
const normal = (selector: string) =>
|
|
51
|
+
selector
|
|
52
|
+
.replace(/\s+/g, " ")
|
|
53
|
+
.replace(/ ?([>+~,]) ?/g, "$1")
|
|
54
|
+
.trim();
|
|
55
|
+
|
|
56
|
+
// A rule, the block of the file it sits in and its condition. The rule's
|
|
57
|
+
// offsets count from the block's start.
|
|
58
|
+
type Placed = { rule: Rule; block: StyleBlock; condition: string };
|
|
59
|
+
|
|
60
|
+
// The rules of the file that lienzo edits, across its blocks.
|
|
61
|
+
function editableRules(file: string, source: string): { ok: true; rules: Placed[] } | Refusal {
|
|
62
|
+
const css = blocksOf(file, source);
|
|
63
|
+
if (!css.ok) return css;
|
|
64
|
+
|
|
65
|
+
const rules: Placed[] = [];
|
|
66
|
+
for (const block of css.blocks) {
|
|
67
|
+
parseCss(file, block.css).walkRules((rule) => {
|
|
68
|
+
const condition = conditionOf(rule);
|
|
69
|
+
if (condition !== undefined) rules.push({ rule, block, condition });
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return { ok: true, rules };
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// The one rule whose whole selector list is `selector` under exactly the
|
|
77
|
+
// at-rules `condition` names; "" names a rule outside any. A component's
|
|
78
|
+
// blocks count together, so a selector two blocks state is refused.
|
|
79
|
+
function ruleIn(
|
|
80
|
+
file: string,
|
|
81
|
+
source: string,
|
|
82
|
+
selector: string,
|
|
83
|
+
condition: string,
|
|
84
|
+
): ({ ok: true } & Placed) | Refusal {
|
|
85
|
+
const all = editableRules(file, source);
|
|
86
|
+
if (!all.ok) return all;
|
|
87
|
+
|
|
88
|
+
const wanted = normal(selector);
|
|
89
|
+
const under = normalCondition(condition);
|
|
90
|
+
const found = all.rules.filter(
|
|
91
|
+
(placed) =>
|
|
92
|
+
normal(placed.rule.selector) === wanted && normalCondition(placed.condition) === under,
|
|
93
|
+
);
|
|
94
|
+
|
|
95
|
+
const name = named(selector, condition.trim());
|
|
96
|
+
if (found.length === 0) return refuse(`${file} has no rule ${name}`);
|
|
97
|
+
if (found.length > 1) return refuse(`${file} has ${found.length} rules ${name}`);
|
|
98
|
+
|
|
99
|
+
return { ok: true, ...found[0]! };
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function declarationsOf(rule: Rule): Declaration[] {
|
|
103
|
+
return (rule.nodes ?? []).filter((node): node is Declaration => node.type === "decl");
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// Custom properties are case-sensitive; every other property is not.
|
|
107
|
+
function same(held: string, wanted: string): boolean {
|
|
108
|
+
return held.startsWith("--") ? held === wanted : held.toLowerCase() === wanted.toLowerCase();
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// The declarations of the rule, in source order.
|
|
112
|
+
export function ruleOf(
|
|
113
|
+
file: string,
|
|
114
|
+
source: string,
|
|
115
|
+
selector: string,
|
|
116
|
+
condition = "",
|
|
117
|
+
): { ok: true; declarations: Property[] } | Refusal {
|
|
118
|
+
const found = ruleIn(file, source, selector, condition);
|
|
119
|
+
if (!found.ok) return found;
|
|
120
|
+
|
|
121
|
+
const declarations = declarationsOf(found.rule).map((declaration) => ({
|
|
122
|
+
property: declaration.prop,
|
|
123
|
+
value: valueOf(declaration).value,
|
|
124
|
+
}));
|
|
125
|
+
|
|
126
|
+
return { ok: true, declarations };
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// Where a rule the browser holds was written: an absolute path, and the line
|
|
130
|
+
// and column of the rule's selector, from 1.
|
|
131
|
+
export type Origin = { file: string; line: number; column: number };
|
|
132
|
+
|
|
133
|
+
// A source a map names relative to the stylesheet, as vite-plugin-svelte
|
|
134
|
+
// writes `App.svelte`. A path from `/` or a URL is not.
|
|
135
|
+
const relative = (source: string) => !path.isAbsolute(source) && !/^[a-z][\w+.-]*:/i.test(source);
|
|
136
|
+
|
|
137
|
+
// The node at `index` in a stylesheet: the first number counts the top-level
|
|
138
|
+
// rules, and each next one the rules of the group before it. The CSSOM counts
|
|
139
|
+
// them as `cssRules` does, without comments.
|
|
140
|
+
function nodeAt(root: Root, index: number[]): ChildNode | undefined {
|
|
141
|
+
let nodes: ChildNode[] | undefined = root.nodes;
|
|
142
|
+
let node: ChildNode | undefined;
|
|
143
|
+
|
|
144
|
+
for (const at of index) {
|
|
145
|
+
node = (nodes ?? []).filter((child) => child.type !== "comment")[at];
|
|
146
|
+
nodes = node?.type === "atrule" ? node.nodes : undefined;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
return node;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const PARSED_SHEETS = 8;
|
|
153
|
+
const parsedSheets = new Map<string, Root | null>();
|
|
154
|
+
|
|
155
|
+
// An inspector read asks once per matched rule, and most of them name the same
|
|
156
|
+
// stylesheet text. Parsing it and decoding its source map is the cost, so a
|
|
157
|
+
// parse is kept for the next ask. originOf only reads the tree.
|
|
158
|
+
function parsedSheet(file: string, css: string): Root | null {
|
|
159
|
+
const key = `${file}\0${css}`;
|
|
160
|
+
const kept = parsedSheets.get(key);
|
|
161
|
+
if (kept !== undefined) return kept;
|
|
162
|
+
|
|
163
|
+
let root: Root | null;
|
|
164
|
+
try {
|
|
165
|
+
root = parseCss(file, css);
|
|
166
|
+
} catch {
|
|
167
|
+
root = null;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
parsedSheets.set(key, root);
|
|
171
|
+
if (parsedSheets.size > PARSED_SHEETS) parsedSheets.delete(parsedSheets.keys().next().value!);
|
|
172
|
+
|
|
173
|
+
return root;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// The source of the rule at `index` in a stylesheet the browser holds, as
|
|
177
|
+
// nodeAt counts it. The dev server writes the source map into the text, and
|
|
178
|
+
// `selector`, as the CSSOM states it, checks that the text is the one the
|
|
179
|
+
// index counts in. `from` is the absolute path of the file the stylesheet was
|
|
180
|
+
// served from, which a relative source in the map is resolved against.
|
|
181
|
+
export function originOf(
|
|
182
|
+
css: string,
|
|
183
|
+
index: number[],
|
|
184
|
+
selector: string,
|
|
185
|
+
from?: string,
|
|
186
|
+
): ({ ok: true } & Origin) | Refusal {
|
|
187
|
+
if (from !== undefined && !path.isAbsolute(from))
|
|
188
|
+
return refuse(`${from} is not an absolute path`);
|
|
189
|
+
|
|
190
|
+
const root = parsedSheet(from ?? "stylesheet", css);
|
|
191
|
+
if (!root) return refuse("the stylesheet does not parse");
|
|
192
|
+
|
|
193
|
+
const rule = nodeAt(root, index);
|
|
194
|
+
if (rule?.type !== "rule" || normal(rule.selector) !== normal(selector)) {
|
|
195
|
+
return refuse(`the stylesheet has no rule ${selector} at ${index.join(".")}`);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const { input, start } = rule.source!;
|
|
199
|
+
const mapped = input.map?.consumer().originalPositionFor({
|
|
200
|
+
line: start!.line,
|
|
201
|
+
column: start!.column - 1,
|
|
202
|
+
});
|
|
203
|
+
if (!mapped?.source) return refuse(`the stylesheet of ${selector} has no source map`);
|
|
204
|
+
if (from === undefined && relative(mapped.source)) {
|
|
205
|
+
return refuse(
|
|
206
|
+
`the source map of ${selector} names ${mapped.source}, relative to an unnamed file`,
|
|
207
|
+
);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
const origin = input.origin(start!.line, start!.column);
|
|
211
|
+
if (!origin || !origin.file) return refuse(`the source of ${selector} is not a file`);
|
|
212
|
+
|
|
213
|
+
return { ok: true, file: origin.file, line: origin.line, column: origin.column };
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// The selector and condition of the rule that starts at the line and column.
|
|
217
|
+
export function selectorAt(
|
|
218
|
+
file: string,
|
|
219
|
+
source: string,
|
|
220
|
+
line: number,
|
|
221
|
+
column: number,
|
|
222
|
+
): { ok: true; selector: string; condition: string } | Refusal {
|
|
223
|
+
const all = editableRules(file, source);
|
|
224
|
+
if (!all.ok) return all;
|
|
225
|
+
|
|
226
|
+
const offset = offsetOf(source, { line, column });
|
|
227
|
+
const found = all.rules.find(
|
|
228
|
+
({ rule, block }) => block.start + rule.source!.start!.offset === offset,
|
|
229
|
+
);
|
|
230
|
+
|
|
231
|
+
return found
|
|
232
|
+
? { ok: true, selector: found.rule.selector, condition: found.condition }
|
|
233
|
+
: refuse(`${file} has no rule at ${line}:${column} that lienzo edits`);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
const PROPERTY = /^(--|-?[a-zA-Z_])[\w-]*$/;
|
|
237
|
+
|
|
238
|
+
// The text that adds a declaration after the rule's last one, in its
|
|
239
|
+
// indentation, or opens the empty rule with it.
|
|
240
|
+
function addition(source: string, rule: Rule, text: string): { at: number; insert: string } {
|
|
241
|
+
const last = declarationsOf(rule).at(-1);
|
|
242
|
+
|
|
243
|
+
if (last) {
|
|
244
|
+
const end = last.source!.end!.offset;
|
|
245
|
+
const indent = last.raws.before ?? "\n";
|
|
246
|
+
|
|
247
|
+
return source[end - 1] === ";"
|
|
248
|
+
? { at: end, insert: `${indent}${text};` }
|
|
249
|
+
: { at: end, insert: `;${indent}${text}` };
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
const outer = (rule.raws.before ?? "").split("\n").at(-1) ?? "";
|
|
253
|
+
const selector = rule.raws.selector?.raw ?? rule.selector;
|
|
254
|
+
const brace = rule.source!.start!.offset + selector.length + (rule.raws.between ?? "").length;
|
|
255
|
+
const close = (rule.raws.after ?? "").includes("\n") ? "" : `\n${outer}`;
|
|
256
|
+
|
|
257
|
+
return { at: brace + 1, insert: `\n${outer} ${text};${close}` };
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
// Overwrites the value of a property in the rule, or adds the property as the
|
|
261
|
+
// rule's last declaration when the rule does not declare it.
|
|
262
|
+
export function patchRule(
|
|
263
|
+
file: string,
|
|
264
|
+
source: string,
|
|
265
|
+
selector: string,
|
|
266
|
+
property: string,
|
|
267
|
+
value: string,
|
|
268
|
+
condition = "",
|
|
269
|
+
): Patch {
|
|
270
|
+
const found = ruleIn(file, source, selector, condition);
|
|
271
|
+
if (!found.ok) return found;
|
|
272
|
+
|
|
273
|
+
const name = property.trim();
|
|
274
|
+
if (!PROPERTY.test(name)) return refuse(`"${property}" cannot be a property`);
|
|
275
|
+
|
|
276
|
+
const next = cleanValue(value);
|
|
277
|
+
if (next === undefined) return refuse(`"${value}" cannot be a value of ${name}`);
|
|
278
|
+
|
|
279
|
+
const held = declarationsOf(found.rule).filter((declaration) => same(declaration.prop, name));
|
|
280
|
+
if (held.length > 1) {
|
|
281
|
+
return refuse(
|
|
282
|
+
`${named(selector, condition.trim())} of ${file} declares ${name} ${held.length} times`,
|
|
283
|
+
);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
const out = new MagicString(source);
|
|
287
|
+
const { start, css } = found.block;
|
|
288
|
+
|
|
289
|
+
if (held[0]) {
|
|
290
|
+
const span = valueOf(held[0]);
|
|
291
|
+
out.overwrite(start + span.start, start + span.end, next);
|
|
292
|
+
} else {
|
|
293
|
+
const { at, insert } = addition(css, found.rule, `${name}: ${next}`);
|
|
294
|
+
out.appendLeft(start + at, insert);
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
return { ok: true, code: out.toString() };
|
|
298
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import MagicString, { type SourceMap } from "magic-string";
|
|
3
|
+
import { ATTRIBUTE } from "../protocol.ts";
|
|
4
|
+
import type { Language } from "./language.ts";
|
|
5
|
+
import { formatLocation, positionOf } from "./location.ts";
|
|
6
|
+
|
|
7
|
+
export type Stamped = { code: string; map: SourceMap };
|
|
8
|
+
|
|
9
|
+
// Marks each element the browser draws with the location of its tag, so a node
|
|
10
|
+
// on the page leads back to the source that made it. Components are not
|
|
11
|
+
// marked: they may not pass an unknown prop on. Returns nothing for a file with
|
|
12
|
+
// no such element, and throws on a syntax error.
|
|
13
|
+
export function stamp(language: Language, file: string, source: string): Stamped | undefined {
|
|
14
|
+
const hosts = language.elements(file, source).filter((element) => element.host);
|
|
15
|
+
if (hosts.length === 0) return undefined;
|
|
16
|
+
|
|
17
|
+
const out = new MagicString(source);
|
|
18
|
+
|
|
19
|
+
for (const host of hosts) {
|
|
20
|
+
const at = formatLocation({ file, ...positionOf(source, host.start) });
|
|
21
|
+
out.appendLeft(host.nameEnd, ` ${ATTRIBUTE}="${at}"`);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// Vite resolves a map's source beside the module, so it gets the bare name.
|
|
25
|
+
const map = out.generateMap({ hires: true, source: path.basename(file), includeContent: true });
|
|
26
|
+
|
|
27
|
+
return { code: out.toString(), map };
|
|
28
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { refuse, type Refusal } from "./refusal.ts";
|
|
3
|
+
|
|
4
|
+
export const REACT_FRAMEWORKS = ["@storybook/nextjs", "@storybook/react-vite", "@storybook/react"];
|
|
5
|
+
const EXPORT = /^export\s+(default\s+)?(?:function|const|class)\s+([A-Z]\w*)/m;
|
|
6
|
+
|
|
7
|
+
export type NewStory = { ok: true; file: string; code: string };
|
|
8
|
+
|
|
9
|
+
// A CSF 3 file beside the component, with a `Default` story that sets no args.
|
|
10
|
+
// Only React components are written, the framework whose package the project
|
|
11
|
+
// declares.
|
|
12
|
+
export function storyFile(
|
|
13
|
+
file: string,
|
|
14
|
+
source: string,
|
|
15
|
+
framework: string | undefined,
|
|
16
|
+
): NewStory | Refusal {
|
|
17
|
+
if (!framework) return refuse("the project declares no React Storybook framework");
|
|
18
|
+
|
|
19
|
+
const base = path.basename(file).replace(/\.[jt]sx$/, "");
|
|
20
|
+
if (base === path.basename(file)) return refuse(`${file} is not a JSX file`);
|
|
21
|
+
|
|
22
|
+
const found = EXPORT.exec(source);
|
|
23
|
+
if (!found) return refuse(`${file} exports no component named in capitals`);
|
|
24
|
+
|
|
25
|
+
const [, byDefault, name] = found;
|
|
26
|
+
const imported = byDefault ? name : `{ ${name} }`;
|
|
27
|
+
const code = [
|
|
28
|
+
`import type { Meta, StoryObj } from "${framework}";`,
|
|
29
|
+
`import ${imported} from "./${base}";`,
|
|
30
|
+
"",
|
|
31
|
+
`const meta = { component: ${name} } satisfies Meta<typeof ${name}>;`,
|
|
32
|
+
"",
|
|
33
|
+
"export default meta;",
|
|
34
|
+
"",
|
|
35
|
+
"export const Default: StoryObj<typeof meta> = {};",
|
|
36
|
+
"",
|
|
37
|
+
].join("\n");
|
|
38
|
+
|
|
39
|
+
return { ok: true, file: path.join(path.dirname(file), `${base}.stories.tsx`), code };
|
|
40
|
+
}
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import MagicString from "magic-string";
|
|
2
|
+
import { parseSync } from "oxc-parser";
|
|
3
|
+
import type { ArgData } from "../protocol.ts";
|
|
4
|
+
import { isNode, type Node } from "./estree.ts";
|
|
5
|
+
import type { Patch } from "./patch.ts";
|
|
6
|
+
import { refuse, type Refusal } from "./refusal.ts";
|
|
7
|
+
|
|
8
|
+
export type Arg = NonNullable<ArgData["value"]>;
|
|
9
|
+
|
|
10
|
+
type Found = { ok: true; args: Node; entries: (ArgData & { node: Node })[] } | Refusal;
|
|
11
|
+
|
|
12
|
+
const SCRIPT = /\.[cm]?[jt]sx?$/;
|
|
13
|
+
const IDENTIFIER = /^[A-Za-z_$][\w$]*$/;
|
|
14
|
+
|
|
15
|
+
// `{...} satisfies Story`, `({...})` and `{...} as Story` all hold the object.
|
|
16
|
+
function unwrapped(node: Node): Node {
|
|
17
|
+
const wrappers = ["TSSatisfiesExpression", "TSAsExpression", "ParenthesizedExpression"];
|
|
18
|
+
if (wrappers.includes(node.type) && isNode(node.expression)) return unwrapped(node.expression);
|
|
19
|
+
|
|
20
|
+
return node;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// The name a non-computed key states: `label` or `"label"`.
|
|
24
|
+
function nameOf(key: unknown): string | undefined {
|
|
25
|
+
if (!isNode(key)) return undefined;
|
|
26
|
+
if (key.type === "Identifier") return key.name as string;
|
|
27
|
+
if (key.type === "Literal" && typeof key.value === "string") return key.value;
|
|
28
|
+
|
|
29
|
+
return undefined;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function keyOf(property: Node): string | undefined {
|
|
33
|
+
return property.type === "Property" && !property.computed ? nameOf(property.key) : undefined;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// `meta.story({...})`, the call that declares a CSF 4 story.
|
|
37
|
+
function storyCall(node: Node): Node | undefined {
|
|
38
|
+
if (node.type !== "CallExpression" || !isNode(node.callee)) return undefined;
|
|
39
|
+
|
|
40
|
+
const { callee } = node;
|
|
41
|
+
const story =
|
|
42
|
+
callee.type === "MemberExpression" && !callee.computed && nameOf(callee.property) === "story";
|
|
43
|
+
const [first] = node.arguments as Node[];
|
|
44
|
+
|
|
45
|
+
return story && first ? unwrapped(first) : undefined;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function literalOf(node: Node): Arg | null {
|
|
49
|
+
const value = unwrapped(node);
|
|
50
|
+
if (value.type !== "Literal") return null;
|
|
51
|
+
if (typeof value.value === "number" && !Number.isFinite(value.value)) return null;
|
|
52
|
+
|
|
53
|
+
return ["string", "number", "boolean"].includes(typeof value.value) ? (value.value as Arg) : null;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// The object a story export declares: CSF 3's `export const Name = {...}`, or
|
|
57
|
+
// CSF 4's `export const Name = meta.story({...})`.
|
|
58
|
+
function storyObject(program: Node, name: string): Node | undefined {
|
|
59
|
+
for (const statement of program.body as Node[]) {
|
|
60
|
+
if (statement.type !== "ExportNamedDeclaration" || !isNode(statement.declaration)) continue;
|
|
61
|
+
|
|
62
|
+
for (const declarator of (statement.declaration.declarations ?? []) as Node[]) {
|
|
63
|
+
if (!isNode(declarator.id) || declarator.id.name !== name || !isNode(declarator.init))
|
|
64
|
+
continue;
|
|
65
|
+
|
|
66
|
+
const init = unwrapped(declarator.init);
|
|
67
|
+
|
|
68
|
+
return init.type === "ObjectExpression" ? init : storyCall(init);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return undefined;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function found(source: string, file: string, story: string): Found {
|
|
76
|
+
if (!SCRIPT.test(file)) return refuse(`${file} is not a JavaScript or TypeScript stories file`);
|
|
77
|
+
|
|
78
|
+
const { program, errors } = parseSync(file, source);
|
|
79
|
+
const first = errors[0];
|
|
80
|
+
if (first) throw new Error(`${file}: ${first.message}`);
|
|
81
|
+
|
|
82
|
+
const object = storyObject(program as unknown as Node, story);
|
|
83
|
+
if (!object) return refuse(`${file} exports no story ${story} declared as an object`);
|
|
84
|
+
if (object.type !== "ObjectExpression")
|
|
85
|
+
return refuse(`story ${story} is not declared as an object`);
|
|
86
|
+
|
|
87
|
+
const property = (object.properties as Node[]).find((candidate) => keyOf(candidate) === "args");
|
|
88
|
+
if (!property || !isNode(property.value)) return refuse(`story ${story} declares no args`);
|
|
89
|
+
|
|
90
|
+
const args = unwrapped(property.value);
|
|
91
|
+
if (args.type !== "ObjectExpression") return refuse(`the args of story ${story} are computed`);
|
|
92
|
+
|
|
93
|
+
const entries = (args.properties as Node[]).flatMap((node) => {
|
|
94
|
+
const name = keyOf(node);
|
|
95
|
+
return name === undefined || !isNode(node.value)
|
|
96
|
+
? []
|
|
97
|
+
: [{ name, value: literalOf(node.value), node }];
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
return { ok: true, args, entries };
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// The args a story states in its own `args` object, in source order.
|
|
104
|
+
export function storyArgs(source: string, file: string, story: string): ArgData[] | Refusal {
|
|
105
|
+
const args = found(source, file, story);
|
|
106
|
+
if (!args.ok) return args;
|
|
107
|
+
|
|
108
|
+
return args.entries.map(({ name, value }) => ({ name, value }));
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// The value as source: a string keeps the quote the old one used.
|
|
112
|
+
function sourceOf(value: Arg, old: string | undefined): string {
|
|
113
|
+
if (typeof value !== "string") return String(value);
|
|
114
|
+
if (!old?.startsWith("'")) return JSON.stringify(value);
|
|
115
|
+
|
|
116
|
+
return `'${JSON.stringify(value).slice(1, -1).replaceAll('\\"', '"').replaceAll("'", "\\'")}'`;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// Sets one arg of a story to a literal. A new arg goes last, so it overrides
|
|
120
|
+
// any spread before it.
|
|
121
|
+
export function patchArg(
|
|
122
|
+
source: string,
|
|
123
|
+
file: string,
|
|
124
|
+
story: string,
|
|
125
|
+
name: string,
|
|
126
|
+
value: Arg,
|
|
127
|
+
): Patch {
|
|
128
|
+
const args = found(source, file, story);
|
|
129
|
+
if (!args.ok) return args;
|
|
130
|
+
|
|
131
|
+
const out = new MagicString(source);
|
|
132
|
+
const entry = args.entries.findLast((candidate) => candidate.name === name);
|
|
133
|
+
|
|
134
|
+
if (entry) {
|
|
135
|
+
if (entry.value === null) return refuse(`the arg ${name} of story ${story} is computed`);
|
|
136
|
+
|
|
137
|
+
const node = entry.node.value as Node;
|
|
138
|
+
out.overwrite(node.start, node.end, sourceOf(value, source.slice(node.start, node.end)));
|
|
139
|
+
return { ok: true, code: out.toString() };
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const key = IDENTIFIER.test(name) ? name : JSON.stringify(name);
|
|
143
|
+
const added = `${key}: ${sourceOf(value, undefined)}`;
|
|
144
|
+
const last = (args.args.properties as Node[]).at(-1);
|
|
145
|
+
|
|
146
|
+
if (!last) {
|
|
147
|
+
out.overwrite(args.args.start, args.args.end, `{ ${added} }`);
|
|
148
|
+
return { ok: true, code: out.toString() };
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// A trailing comma stays trailing, after the new arg.
|
|
152
|
+
const comma = /^\s*,/.exec(source.slice(last.end, args.args.end));
|
|
153
|
+
const end = comma ? last.end + comma[0].length : last.end;
|
|
154
|
+
|
|
155
|
+
// An object written one property per line gets the new one on its own line.
|
|
156
|
+
const indent = /\n([ \t]*)$/.exec(source.slice(args.args.start, last.start))?.[1];
|
|
157
|
+
const text = indent === undefined ? ` ${added}` : `\n${indent}${added}`;
|
|
158
|
+
|
|
159
|
+
out.appendLeft(end, comma ? `${text},` : `,${text}`);
|
|
160
|
+
|
|
161
|
+
return { ok: true, code: out.toString() };
|
|
162
|
+
}
|