@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.
Files changed (122) hide show
  1. package/README.md +82 -2
  2. package/dist/shell/assets/index-Cna8wlzF.js +4 -0
  3. package/dist/shell/assets/index-CoJSASvW.css +2 -0
  4. package/dist/shell/index.html +13 -0
  5. package/package.json +76 -4
  6. package/src/bench/bench.ts +132 -0
  7. package/src/bridge/page.ts +443 -0
  8. package/src/bridge/protocol.ts +124 -0
  9. package/src/bridge/record.ts +75 -0
  10. package/src/bridge/script.ts +24 -0
  11. package/src/bridge/state.ts +143 -0
  12. package/src/capture/capture.ts +125 -0
  13. package/src/capture/compare.ts +46 -0
  14. package/src/capture/diff.ts +16 -0
  15. package/src/capture/manifest.ts +60 -0
  16. package/src/capture/refs.ts +20 -0
  17. package/src/capture/replay.ts +25 -0
  18. package/src/capture/review.ts +78 -0
  19. package/src/capture/serve.ts +32 -0
  20. package/src/capture/snap.ts +62 -0
  21. package/src/capture/store.ts +28 -0
  22. package/src/cli.ts +287 -0
  23. package/src/cloud/checkout.ts +33 -0
  24. package/src/cloud/credentials.ts +25 -0
  25. package/src/cloud/link.ts +60 -0
  26. package/src/cloud/login.ts +85 -0
  27. package/src/commands/canvas.ts +46 -0
  28. package/src/commands/change.ts +218 -0
  29. package/src/commands/class.ts +66 -0
  30. package/src/commands/command.ts +27 -0
  31. package/src/commands/create.ts +95 -0
  32. package/src/commands/element.ts +118 -0
  33. package/src/commands/file.ts +96 -0
  34. package/src/commands/frame.ts +58 -0
  35. package/src/commands/override.ts +38 -0
  36. package/src/commands/prop.ts +18 -0
  37. package/src/commands/registry.ts +76 -0
  38. package/src/commands/rule.ts +131 -0
  39. package/src/commands/stage.ts +52 -0
  40. package/src/commands/story.ts +112 -0
  41. package/src/commands/style.ts +123 -0
  42. package/src/commands/text.ts +17 -0
  43. package/src/commands/token.ts +60 -0
  44. package/src/commands/transaction.ts +66 -0
  45. package/src/job/docker.ts +109 -0
  46. package/src/job/job.ts +125 -0
  47. package/src/job/server.ts +62 -0
  48. package/src/mirror/apply.ts +121 -0
  49. package/src/mirror/entry.ts +7 -0
  50. package/src/mirror/host.ts +68 -0
  51. package/src/mirror/observe.ts +271 -0
  52. package/src/mirror/script.ts +18 -0
  53. package/src/mirror/transport.ts +7 -0
  54. package/src/mirror/wire.ts +25 -0
  55. package/src/project/config.ts +98 -0
  56. package/src/project/design.ts +59 -0
  57. package/src/project/detect.ts +184 -0
  58. package/src/project/files.ts +30 -0
  59. package/src/project/git.ts +68 -0
  60. package/src/project/github.ts +42 -0
  61. package/src/project/history.ts +89 -0
  62. package/src/project/project.ts +106 -0
  63. package/src/project/spawn.ts +23 -0
  64. package/src/project/step.ts +15 -0
  65. package/src/protocol.ts +125 -0
  66. package/src/sandbox/forward.ts +73 -0
  67. package/src/sandbox/install.ts +44 -0
  68. package/src/sandbox/sandbox.ts +57 -0
  69. package/src/serve/build.ts +49 -0
  70. package/src/serve/canvases.ts +65 -0
  71. package/src/serve/dev.ts +296 -0
  72. package/src/serve/env.ts +16 -0
  73. package/src/serve/mcp.ts +65 -0
  74. package/src/serve/middleware.ts +172 -0
  75. package/src/serve/next/config.ts +68 -0
  76. package/src/serve/next/hook.ts +74 -0
  77. package/src/serve/next/loader.ts +37 -0
  78. package/src/serve/nuxt.ts +14 -0
  79. package/src/serve/plugin.ts +95 -0
  80. package/src/serve/preload.ts +167 -0
  81. package/src/serve/resend.ts +29 -0
  82. package/src/serve/running.ts +115 -0
  83. package/src/serve/screenshot.ts +26 -0
  84. package/src/serve/sources.ts +46 -0
  85. package/src/serve/uncached.ts +25 -0
  86. package/src/serve/vite-build.ts +15 -0
  87. package/src/serve/vite.ts +19 -0
  88. package/src/session/session.ts +96 -0
  89. package/src/share/ask.ts +24 -0
  90. package/src/share/client.ts +140 -0
  91. package/src/share/consent.ts +39 -0
  92. package/src/share/frame.ts +26 -0
  93. package/src/source/astro/astro.ts +10 -0
  94. package/src/source/astro/elements.ts +60 -0
  95. package/src/source/blocks.ts +42 -0
  96. package/src/source/css.ts +48 -0
  97. package/src/source/estree.ts +55 -0
  98. package/src/source/extract.ts +68 -0
  99. package/src/source/insert.ts +189 -0
  100. package/src/source/jsx/elements.ts +136 -0
  101. package/src/source/jsx/jsx.ts +5 -0
  102. package/src/source/language.ts +176 -0
  103. package/src/source/location.ts +34 -0
  104. package/src/source/modules.ts +41 -0
  105. package/src/source/overrides.ts +35 -0
  106. package/src/source/page.ts +69 -0
  107. package/src/source/patch.ts +79 -0
  108. package/src/source/prop.ts +34 -0
  109. package/src/source/refusal.ts +7 -0
  110. package/src/source/rules.ts +298 -0
  111. package/src/source/stamp.ts +28 -0
  112. package/src/source/story-file.ts +40 -0
  113. package/src/source/story.ts +162 -0
  114. package/src/source/structure.ts +253 -0
  115. package/src/source/style.ts +143 -0
  116. package/src/source/svelte/elements.ts +154 -0
  117. package/src/source/svelte/svelte.ts +11 -0
  118. package/src/source/tag.ts +99 -0
  119. package/src/source/tailwind.ts +46 -0
  120. package/src/source/tokens.ts +56 -0
  121. package/src/source/vue/elements.ts +236 -0
  122. package/src/source/vue/vue.ts +11 -0
@@ -0,0 +1,253 @@
1
+ import { elementAt, type Element } from "./language.ts";
2
+ import type { Location } from "./location.ts";
3
+ import type { Patch } from "./patch.ts";
4
+ import { refuse, type Refusal } from "./refusal.ts";
5
+
6
+ // The lines an element fills when nothing else stands on them, and their
7
+ // indent. Such an element is removed with its lines, and a copy of it gets
8
+ // lines of its own.
9
+ type Lines = { start: number; end: number; indent: string };
10
+
11
+ export function linesOf(source: string, element: Element): Lines | undefined {
12
+ const start = source.lastIndexOf("\n", element.start - 1) + 1;
13
+ const indent = source.slice(start, element.start);
14
+ if (!/^[ \t]*$/.test(indent)) return undefined;
15
+
16
+ const newline = source.indexOf("\n", element.end);
17
+ const end = newline < 0 ? source.length : newline;
18
+ if (!/^[ \t]*$/.test(source.slice(element.end, end))) return undefined;
19
+
20
+ return { start, end: newline < 0 ? end : end + 1, indent };
21
+ }
22
+
23
+ // The element at the location, when it stands among siblings, so adding or
24
+ // taking one leaves the code whole.
25
+ export function childAt(
26
+ source: string,
27
+ at: Location,
28
+ ): { ok: true; element: Element; elements: Element[] } | Refusal {
29
+ const found = elementAt(source, at);
30
+ if (!found.ok) return found;
31
+
32
+ const fixed = fixedIn(found.element);
33
+ if (fixed) return fixed;
34
+
35
+ return { ok: true, element: found.element, elements: found.elements };
36
+ }
37
+
38
+ // Why an element cannot gain or lose a neighbour, if it cannot.
39
+ function fixedIn(element: Element): Refusal | undefined {
40
+ const tag = `<${element.name}>`;
41
+
42
+ if (element.container === undefined) {
43
+ return refuse(`${tag} is a value in code, not one of a list of children`);
44
+ }
45
+ if (element.chained) {
46
+ return refuse(`${tag} is part of a v-if chain, whose order the template fixes`);
47
+ }
48
+
49
+ return undefined;
50
+ }
51
+
52
+ export function removeElement(source: string, at: Location): Patch {
53
+ const found = childAt(source, at);
54
+ if (!found.ok) return found;
55
+
56
+ const { element } = found;
57
+ const { start, end } = linesOf(source, element) ?? element;
58
+
59
+ return { ok: true, code: source.slice(0, start) + source.slice(end), select: null };
60
+ }
61
+
62
+ // Where a move puts the element: next to a sibling `to`, or one place earlier
63
+ // or later among its siblings.
64
+ export type Place =
65
+ | { side: "before" | "after"; to: Location }
66
+ | { side: "earlier" | "later"; to?: undefined };
67
+
68
+ // Moves the element among the siblings it stands with, and selects it. A
69
+ // move into another list could take it out of the scope its code reads, as
70
+ // out of a loop, so it stays within its list.
71
+ export function moveElement(source: string, at: Location, place: Place): Patch {
72
+ const found = childAt(source, at);
73
+ if (!found.ok) return found;
74
+
75
+ const { element, elements } = found;
76
+ const target = targetOf(source, at.file, element, elements, place);
77
+ if (!target.ok) return target;
78
+ if (target.element.start === element.start) {
79
+ return { ok: true, code: source, select: element.start };
80
+ }
81
+
82
+ return move(source, element, elements, target.element, target.side);
83
+ }
84
+
85
+ type Target = { ok: true; element: Element; side: "before" | "after" };
86
+
87
+ function targetOf(
88
+ source: string,
89
+ file: string,
90
+ element: Element,
91
+ elements: Element[],
92
+ place: Place,
93
+ ): Target | Refusal {
94
+ const siblings = elements.filter((each) => each.container === element.container);
95
+ const tag = `<${element.name}>`;
96
+
97
+ if (place.to === undefined) {
98
+ const index = siblings.indexOf(element);
99
+ const neighbour = siblings[place.side === "earlier" ? index - 1 : index + 1];
100
+ if (!neighbour) {
101
+ return refuse(
102
+ `${tag} is already the ${place.side === "earlier" ? "first" : "last"} of its siblings`,
103
+ );
104
+ }
105
+
106
+ const fixed = fixedIn(neighbour);
107
+ if (fixed) return fixed;
108
+
109
+ return { ok: true, element: neighbour, side: place.side === "earlier" ? "before" : "after" };
110
+ }
111
+
112
+ if (place.to.file !== file) {
113
+ return refuse(`${tag} moves within its file; ${place.to.file} is another`);
114
+ }
115
+
116
+ const to = elementAt(source, place.to);
117
+ if (!to.ok) return to;
118
+
119
+ const fixed = fixedIn(to.element);
120
+ if (fixed) return fixed;
121
+
122
+ if (to.element.container !== element.container) {
123
+ return refuse(
124
+ `<${to.element.name}> is not a sibling of ${tag}; an element moves within its list`,
125
+ );
126
+ }
127
+
128
+ return { ok: true, element: to.element, side: place.side };
129
+ }
130
+
131
+ // Tags whose text the page shows as written, so a new indent changes it.
132
+ const VERBATIM = new Set(["pre", "textarea"]);
133
+
134
+ // The element's text at the indent of the lines it moves to. A line indented
135
+ // less than the tag, a template literal, or a verbatim tag inside would change
136
+ // what the code says, so they refuse.
137
+ export function reindent(
138
+ text: string,
139
+ from: string,
140
+ to: string,
141
+ element: Element,
142
+ elements: Element[],
143
+ ): { ok: true; text: string } | Refusal {
144
+ if (from === to || !text.includes("\n")) return { ok: true, text };
145
+
146
+ const tag = `<${element.name}>`;
147
+ const verbatim = elements.find(
148
+ (each) => each.start > element.start && each.end <= element.end && VERBATIM.has(each.name),
149
+ );
150
+ if (verbatim) {
151
+ return refuse(`${tag} holds a <${verbatim.name}>, whose text a new indent would change`);
152
+ }
153
+ if (text.includes("`")) {
154
+ return refuse(`${tag} holds a template literal, whose text a new indent would change`);
155
+ }
156
+
157
+ const [first, ...rest] = text.split("\n");
158
+ const shifted = rest.map((line) => {
159
+ if (!line.trim()) return line;
160
+ return line.startsWith(from) ? to + line.slice(from.length) : undefined;
161
+ });
162
+ if (shifted.includes(undefined)) return refuse(`${tag} has a line indented less than its tag`);
163
+
164
+ return { ok: true, text: [first, ...shifted].join("\n") };
165
+ }
166
+
167
+ // The blanks that part an element from what shares its line, and the span an
168
+ // element takes out with them: those after it, or else those before it. The
169
+ // line's indent is not one of them.
170
+ function spaceOf(source: string, element: Element): { start: number; end: number; gap: string } {
171
+ const after = /^[ \t]*/.exec(source.slice(element.end))![0];
172
+ const next = source[element.end + after.length];
173
+ if (after && next !== undefined && next !== "\n") {
174
+ return { start: element.start, end: element.end + after.length, gap: after };
175
+ }
176
+
177
+ const before = /[ \t]*$/.exec(source.slice(0, element.start))![0];
178
+ const previous = source[element.start - before.length - 1];
179
+ if (before && previous !== undefined && previous !== "\n") {
180
+ return { start: element.start - before.length, end: element.end, gap: before };
181
+ }
182
+
183
+ return { start: element.start, end: element.end, gap: "" };
184
+ }
185
+
186
+ function indentOf(source: string, offset: number): string {
187
+ const start = source.lastIndexOf("\n", offset - 1) + 1;
188
+
189
+ return /^[ \t]*/.exec(source.slice(start, offset))![0];
190
+ }
191
+
192
+ // Takes the element out and puts it next to the target: on lines of its own
193
+ // when the target has them, or else on the target's line, parted from it as
194
+ // the target is from its neighbour.
195
+ function move(
196
+ source: string,
197
+ element: Element,
198
+ elements: Element[],
199
+ target: Element,
200
+ side: "before" | "after",
201
+ ): Patch {
202
+ const from = linesOf(source, element);
203
+ const onto = linesOf(source, target);
204
+ const cut = from ?? spaceOf(source, element);
205
+
206
+ let text = source.slice(element.start, element.end);
207
+ let gap = spaceOf(source, target).gap;
208
+
209
+ if (onto) {
210
+ const indent = from?.indent ?? indentOf(source, element.start);
211
+ const shifted = reindent(text, indent, onto.indent, element, elements);
212
+ if (!shifted.ok) return shifted;
213
+
214
+ text = shifted.text;
215
+ gap = `\n${onto.indent}`;
216
+ }
217
+
218
+ const at = side === "before" ? target.start : target.end;
219
+ const insert = side === "before" ? text + gap : gap + text;
220
+ const lead = side === "before" ? 0 : gap.length;
221
+
222
+ // Siblings never overlap, so the insert lies wholly before or after the cut.
223
+ if (at <= cut.start) {
224
+ return {
225
+ ok: true,
226
+ code: source.slice(0, at) + insert + source.slice(at, cut.start) + source.slice(cut.end),
227
+ select: at + lead,
228
+ };
229
+ }
230
+
231
+ return {
232
+ ok: true,
233
+ code: source.slice(0, cut.start) + source.slice(cut.end, at) + insert + source.slice(at),
234
+ select: at - (cut.end - cut.start) + lead,
235
+ };
236
+ }
237
+
238
+ // The copy goes right after the element and is selected.
239
+ export function duplicateElement(source: string, at: Location): Patch {
240
+ const found = childAt(source, at);
241
+ if (!found.ok) return found;
242
+
243
+ const { element } = found;
244
+ const copy = source.slice(element.start, element.end);
245
+ const lines = linesOf(source, element);
246
+ const gap = lines ? `\n${lines.indent}` : "";
247
+
248
+ return {
249
+ ok: true,
250
+ code: source.slice(0, element.end) + gap + copy + source.slice(element.end),
251
+ select: element.end + gap.length,
252
+ };
253
+ }
@@ -0,0 +1,143 @@
1
+ import type { __unstable__loadDesignSystem } from "@tailwindcss/node";
2
+ import type { Container } from "postcss";
3
+ import type { Breakpoint, StyleDeclaration } from "../protocol.ts";
4
+ import { classesOf } from "./tailwind.ts";
5
+ import { parseCss } from "./css.ts";
6
+ import { refuse, type Refusal } from "./refusal.ts";
7
+
8
+ // Tailwind as one stylesheet configures it: theme, utilities and variants.
9
+ export type DesignSystem = Awaited<ReturnType<typeof __unstable__loadDesignSystem>>;
10
+
11
+ // A CSS property name, custom properties included.
12
+ const PROPERTY = /^(--)?[a-zA-Z][\w-]*$/;
13
+
14
+ // The root font size Tailwind's rem values assume.
15
+ const REM = 16;
16
+
17
+ // A class as its variants and its utility: `md:hover:p-4` is `md:hover` and
18
+ // `p-4`, `p-4` is "" and `p-4`. Undefined when Tailwind does not know it.
19
+ function split(
20
+ design: DesignSystem,
21
+ name: string,
22
+ ): { variant: string; utility: string } | undefined {
23
+ const candidate = design.parseCandidate(name)[0];
24
+ if (!candidate) return undefined;
25
+
26
+ const utility = design.printCandidate({ ...candidate, variants: [] });
27
+ const variant =
28
+ name.length > utility.length ? name.slice(0, name.length - utility.length - 1) : "";
29
+
30
+ return { variant, utility };
31
+ }
32
+
33
+ // Tailwind writes a theme value as `var(--spacing)`; the theme states it.
34
+ function resolved(design: DesignSystem, value: string): string {
35
+ return value.replace(/var\((--[\w-]+)\)/g, (whole, name: string) => {
36
+ return design.resolveThemeValue(name) ?? whole;
37
+ });
38
+ }
39
+
40
+ // Inside an at-rule of the utility's own: `@property`, which registers
41
+ // Tailwind's variables, or `@media`, as `container` sets its `max-width` per
42
+ // breakpoint.
43
+ function nested(node: Container | undefined): boolean {
44
+ if (!node || node.type === "root") return false;
45
+
46
+ return node.type === "atrule" || nested(node.parent as Container | undefined);
47
+ }
48
+
49
+ // Each property a class list sets, with the class and its variant, in the
50
+ // list's order. The utility is read without its variants, so a declaration
51
+ // holds wherever the variant does. Tailwind's internal `--tw-*` variables and
52
+ // what a utility sets only on some screens are left out.
53
+ export function declarationsOf(design: DesignSystem, list: string): StyleDeclaration[] {
54
+ const found: StyleDeclaration[] = [];
55
+
56
+ for (const name of classesOf(list)) {
57
+ const parts = split(design, name);
58
+ const css = parts && design.candidatesToCss([parts.utility])[0];
59
+ if (!parts || !css) continue;
60
+
61
+ const { variant } = parts;
62
+
63
+ parseCss(name, css).walkDecls((declaration) => {
64
+ if (declaration.prop.startsWith("--tw-") || nested(declaration.parent)) return;
65
+
66
+ found.push({
67
+ property: declaration.prop,
68
+ value: resolved(design, declaration.value),
69
+ variant,
70
+ class: name,
71
+ });
72
+ });
73
+ }
74
+
75
+ return found;
76
+ }
77
+
78
+ // Tailwind reads `_` in an arbitrary value as a space, and `\_` as `_`.
79
+ function arbitrary(value: string): string {
80
+ return value.trim().replaceAll("_", "\\_").replace(/\s+/g, "_");
81
+ }
82
+
83
+ // The class that sets one property under a variant, as the project's theme
84
+ // names it: `padding: 16px` is `p-4`, `color: #0f172a` is `text-brand` when
85
+ // the theme holds that colour. A value no utility names stays an arbitrary
86
+ // property, `[scroll-snap-type:x]`.
87
+ export function classFor(
88
+ design: DesignSystem,
89
+ property: string,
90
+ value: string,
91
+ variant: string,
92
+ ): { ok: true; name: string } | Refusal {
93
+ const name = property.trim();
94
+ if (!PROPERTY.test(name)) return refuse(`"${property}" cannot be a property`);
95
+ if (!value.trim() || /[;{}]/.test(value))
96
+ return refuse(`"${value}" cannot be a value of ${name}`);
97
+
98
+ const written = `${variant ? `${variant}:` : ""}[${name}:${arbitrary(value)}]`;
99
+ if (!design.candidatesToCss([written])[0]) {
100
+ const where = variant ? ` under the variant ${variant}` : "";
101
+ return refuse(`Tailwind cannot write ${name}: ${value.trim()}${where}`);
102
+ }
103
+
104
+ return { ok: true, name: design.canonicalizeCandidates([written], { rem: REM })[0] ?? written };
105
+ }
106
+
107
+ // The list without the classes that set a property under a variant.
108
+ export function withoutProperty(
109
+ design: DesignSystem,
110
+ list: string,
111
+ property: string,
112
+ variant: string,
113
+ ): string {
114
+ const setting = new Set(
115
+ declarationsOf(design, list)
116
+ .filter((declaration) => declaration.property === property && declaration.variant === variant)
117
+ .map((declaration) => declaration.class),
118
+ );
119
+
120
+ return classesOf(list)
121
+ .filter((name) => !setting.has(name))
122
+ .join(" ");
123
+ }
124
+
125
+ function pixels(length: string): number | undefined {
126
+ const match = /^([\d.]+)(px|rem)$/.exec(length.trim());
127
+ if (!match) return undefined;
128
+
129
+ return Number(match[1]) * (match[2] === "rem" ? REM : 1);
130
+ }
131
+
132
+ // The theme's breakpoints, narrowest first. One stated in a unit other than
133
+ // px or rem is left out: no frame width compares to it.
134
+ export function breakpointsOf(design: DesignSystem): Breakpoint[] {
135
+ const found: Breakpoint[] = [];
136
+
137
+ for (const [name, value] of design.theme.namespace("--breakpoint")) {
138
+ const width = pixels(value);
139
+ if (name !== null && width !== undefined) found.push({ name, width });
140
+ }
141
+
142
+ return found.sort((a, b) => a.width - b.width);
143
+ }
@@ -0,0 +1,154 @@
1
+ import { parse as parseSvelte } from "svelte/compiler";
2
+ import { defaultImportsOf, isNode, memberOf, walk, type Node } from "../estree.ts";
3
+ import type { Attribute, Element, StyleBlock } from "../language.ts";
4
+
5
+ // The style blocks `preprocess` in svelte/compiler hands a style
6
+ // preprocessor, with the same expression.
7
+ const STYLE =
8
+ /<!--[^]*?-->|<style((?:\s+[^=>'"/\s]+=(?:"[^"]*"|'[^']*'|[^>\s]+)|\s+[^=>'"/\s]+)*\s*)(?:\/>|>([\S\s]*?)<\/style>)/g;
9
+
10
+ const CLOSE_STYLE = "</style>";
11
+
12
+ // Tags the compiler takes out of the page, or keeps as they are.
13
+ const UNDRAWN = new Set(["script", "style"]);
14
+
15
+ // `parse` reads a style as CSS and fails on Sass or Less, which a preprocessor
16
+ // turns into CSS later. No element lives in a style, so its text becomes
17
+ // spaces and every offset stays.
18
+ function blankStyles(source: string): string {
19
+ return source.replace(STYLE, (block, _attributes, content?: string) => {
20
+ if (!content) return block;
21
+
22
+ const head = block.slice(0, block.length - CLOSE_STYLE.length - content.length);
23
+ return head + content.replace(/[^\n]/g, " ") + CLOSE_STYLE;
24
+ });
25
+ }
26
+
27
+ type Root = {
28
+ fragment: Node;
29
+ instance?: { content: Node };
30
+ module?: { content: Node };
31
+ css?: { attributes: Node[]; content: { start: number; end: number } } | null;
32
+ };
33
+
34
+ function parse(source: string): Root {
35
+ return parseSvelte(blankStyles(source), { modern: true }) as unknown as Root;
36
+ }
37
+
38
+ // The offset after the `>` that ends the opening tag. Only spaces and a `/`
39
+ // sit between the last attribute and it.
40
+ function openingEnd(source: string, nameEnd: number, parts: Node[]): number {
41
+ const last = Math.max(nameEnd, ...parts.map((part) => part.end));
42
+
43
+ return source.indexOf(">", last) + 1;
44
+ }
45
+
46
+ // The offset of the closing tag, when the element has one. A void element and
47
+ // one the parser closed for its parent have none.
48
+ function closingStart(source: string, node: Node, name: string): number | undefined {
49
+ const closing = `</${name}>`;
50
+
51
+ return source.endsWith(closing, node.end) ? node.end - closing.length : undefined;
52
+ }
53
+
54
+ function attribute(source: string, node: Node): Attribute | undefined {
55
+ if (node.type !== "Attribute") return undefined;
56
+
57
+ const name = node.name as string;
58
+ const whole = { name, start: node.start, end: node.end };
59
+ const value = node.value;
60
+ if (value === true) return whole;
61
+
62
+ let start = source.indexOf("=", node.start + name.length) + 1;
63
+ while (/\s/.test(source[start] ?? "")) start++;
64
+
65
+ const parts = (Array.isArray(value) ? value : [value]) as Node[];
66
+ const stated = parts.every((part) => part.type === "Text");
67
+ const literal = stated ? parts.map((part) => part.data as string).join("") : undefined;
68
+
69
+ const single = isNode(value) && value.type === "ExpressionTag" && isNode(value.expression);
70
+ const member = single ? memberOf(value.expression as Node) : undefined;
71
+
72
+ return { ...whole, value: { start, end: node.end, literal, member } };
73
+ }
74
+
75
+ function element(source: string, node: Node, container?: number): Element {
76
+ const name = node.name as string;
77
+ const nameEnd = node.start + 1 + name.length;
78
+ const parts = node.attributes as Node[];
79
+
80
+ const fragment = node.fragment as { nodes: Node[] } | undefined;
81
+ const children = fragment?.nodes ?? [];
82
+ const closing = closingStart(source, node, name);
83
+ const onlyText = closing !== undefined && children.every((child) => child.type === "Text");
84
+
85
+ return {
86
+ name,
87
+ start: node.start,
88
+ nameEnd,
89
+ end: node.end,
90
+ container,
91
+ host: node.type === "RegularElement" && !UNDRAWN.has(name),
92
+ attributes: parts.flatMap((part) => attribute(source, part) ?? []),
93
+ spread: parts.some((part) => part.type === "SpreadAttribute"),
94
+ text: onlyText ? { start: openingEnd(source, nameEnd, parts), end: closing } : undefined,
95
+ };
96
+ }
97
+
98
+ function isElement(node: Node): boolean {
99
+ return typeof node.name === "string" && Array.isArray(node.attributes);
100
+ }
101
+
102
+ // Every element, component and `<svelte:...>` tag of the markup, in source
103
+ // order. Throws on a syntax error.
104
+ export function elements(_file: string, source: string): Element[] {
105
+ const found: Element[] = [];
106
+ const containers = new Map<Node, number>();
107
+ let lists = 0;
108
+
109
+ // A fragment is every list of siblings: the markup's top level, an
110
+ // element's children and each branch of a block.
111
+ walk(parse(source).fragment, (node) => {
112
+ if (node.type === "Fragment") {
113
+ const list = lists++;
114
+ for (const child of node.nodes as Node[]) containers.set(child, list);
115
+ }
116
+
117
+ if (isElement(node)) found.push(element(source, node, containers.get(node)));
118
+ });
119
+
120
+ return found;
121
+ }
122
+
123
+ // The default imports of the instance script and the module script, which
124
+ // the markup both sees.
125
+ export function defaultImports(_file: string, source: string): Map<string, string> {
126
+ const { instance, module } = parse(source);
127
+ const found = new Map<string, string>();
128
+
129
+ if (module) defaultImportsOf(module.content, found);
130
+ if (instance) defaultImportsOf(instance.content, found);
131
+
132
+ return found;
133
+ }
134
+
135
+ // The value of an attribute as the source writes it, without quotes.
136
+ function written(source: string, node: Node): string {
137
+ const value = node.value as true | Node | Node[];
138
+ if (value === true) return "";
139
+
140
+ const parts = Array.isArray(value) ? value : [value];
141
+ return source.slice(parts[0]!.start, parts.at(-1)!.end);
142
+ }
143
+
144
+ // The top-level `<style>`, which the compiler scopes to the component. A
145
+ // `<style>` inside an element is markup the page gets as written.
146
+ export function styles(_file: string, source: string): StyleBlock[] {
147
+ const css = parse(source).css;
148
+ if (!css) return [];
149
+
150
+ const { start, end } = css.content;
151
+ const lang = css.attributes.find((node) => node.type === "Attribute" && node.name === "lang");
152
+
153
+ return [{ start, css: source.slice(start, end), lang: lang && written(source, lang) }];
154
+ }
@@ -0,0 +1,11 @@
1
+ import type { Language } from "../language.ts";
2
+ import { defaultImports, elements, styles } from "./elements.ts";
3
+
4
+ // Svelte components in `.svelte` files.
5
+ export const svelte: Language = {
6
+ extensions: [".svelte"],
7
+ classAttribute: "class",
8
+ elements,
9
+ defaultImports,
10
+ styles,
11
+ };
@@ -0,0 +1,99 @@
1
+ import type { Absent, ClassAttribute, Element, Listed, Member, Tag } from "./language.ts";
2
+ import { refuse, type Refusal } from "./refusal.ts";
3
+
4
+ // Elements that never have content, in every language.
5
+ export const VOID = new Set([
6
+ "area",
7
+ "base",
8
+ "br",
9
+ "col",
10
+ "embed",
11
+ "hr",
12
+ "img",
13
+ "input",
14
+ "link",
15
+ "meta",
16
+ "source",
17
+ "track",
18
+ "wbr",
19
+ ]);
20
+
21
+ function classes(element: Element, attribute: ClassAttribute): Listed | Absent | Refusal {
22
+ // A literal wins over a computed value of the same attribute, as a `class`
23
+ // beside a `:class` merges in Vue rather than overriding it.
24
+ const held = element.attributes.filter((candidate) => candidate.name === attribute);
25
+ const stated = held.find((candidate) => candidate.value?.literal !== undefined) ?? held[0];
26
+
27
+ if (!stated) {
28
+ // A spread may carry the class, and which one wins depends on its order.
29
+ if (element.spread) return refuse(`<${element.name}> spreads props that may set ${attribute}`);
30
+
31
+ return { ok: true, value: "", insert: element.nameEnd, name: attribute };
32
+ }
33
+
34
+ const value = stated.value;
35
+ const first = value?.strings?.[0];
36
+ if (value && first && value.literal === undefined) {
37
+ return {
38
+ ok: true,
39
+ ...first,
40
+ attribute: { start: stated.start, end: stated.end },
41
+ strings: value.strings,
42
+ };
43
+ }
44
+
45
+ if (value?.literal === undefined) {
46
+ return refuse(`${attribute} of <${element.name}> is computed, not a literal`);
47
+ }
48
+
49
+ return {
50
+ ok: true,
51
+ value: value.literal,
52
+ start: value.start,
53
+ end: value.end,
54
+ attribute: { start: stated.start, end: stated.end },
55
+ };
56
+ }
57
+
58
+ function member(
59
+ element: Element,
60
+ attribute: ClassAttribute,
61
+ imports: () => Map<string, string>,
62
+ ): Member | Refusal {
63
+ const held = element.attributes.filter((candidate) => candidate.name === attribute);
64
+ const bound = held.find((candidate) => candidate.value?.member) ?? held[0];
65
+ if (!bound?.value) return refuse(`<${element.name}> has no ${attribute}`);
66
+
67
+ const found = bound.value.member;
68
+ if (!found) {
69
+ return refuse(
70
+ `${attribute} of <${element.name}> is not one CSS module class like {styles.card}`,
71
+ );
72
+ }
73
+
74
+ return { ok: true, ...found, from: imports().get(found.object) };
75
+ }
76
+
77
+ // What the source states about an element, and the reason for each value it
78
+ // does not state. Every language answers through these rules.
79
+ // A language that fixes its class attribute overrides the project's.
80
+ export function tagOf(
81
+ source: string,
82
+ element: Element,
83
+ imports: () => Map<string, string>,
84
+ fixed?: ClassAttribute,
85
+ ): Tag {
86
+ const text = element.text;
87
+
88
+ return {
89
+ ok: true,
90
+ name: element.name,
91
+ text: VOID.has(element.name)
92
+ ? refuse(`<${element.name}> has no content`)
93
+ : text
94
+ ? { ok: true, value: source.slice(text.start, text.end), ...text }
95
+ : refuse(`<${element.name}> holds elements or expressions, not only text`),
96
+ classes: (attribute) => classes(element, fixed ?? attribute),
97
+ member: (attribute) => member(element, fixed ?? attribute, imports),
98
+ };
99
+ }