@chikx/compiler 0.1.0 → 0.1.1
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 +5 -5
- package/package.json +3 -3
- package/src/emitter/index.ts +658 -0
- package/src/formatter/index.ts +544 -0
- package/src/host/node.ts +85 -0
- package/src/index.ts +1021 -0
- package/src/ir/factories.ts +85 -0
- package/src/ir/index.ts +28 -0
- package/src/ir/types.ts +145 -0
- package/src/lexer/at-keywords.ts +35 -0
- package/src/lexer/characters.ts +82 -0
- package/src/lexer/comments.ts +27 -0
- package/src/lexer/diagnostics.ts +42 -0
- package/src/lexer/dollar-refs.ts +23 -0
- package/src/lexer/index.ts +37 -0
- package/src/lexer/names.ts +49 -0
- package/src/lexer/numbers.ts +59 -0
- package/src/lexer/scanner.ts +199 -0
- package/src/lexer/strings.ts +52 -0
- package/src/lexer/urls.ts +63 -0
- package/src/lowering/expand-groups.ts +206 -0
- package/src/lowering/group.ts +119 -0
- package/src/lowering/index.ts +43 -0
- package/src/lowering/lower.ts +441 -0
- package/src/lowering/media.ts +314 -0
- package/src/lowering/merge.ts +163 -0
- package/src/lowering/property-families.ts +194 -0
- package/src/lowering/spans.ts +118 -0
- package/src/naming/index.ts +44 -0
- package/src/naming/mapgen.ts +252 -0
- package/src/naming/merge.ts +114 -0
- package/src/naming/rewrite.ts +333 -0
- package/src/naming/scan.ts +267 -0
- package/src/naming/table.ts +153 -0
- package/src/naming/types.ts +100 -0
- package/src/output/import-path.ts +30 -0
- package/src/output/index.ts +213 -0
- package/src/output/manifest.ts +28 -0
- package/src/output/paths.ts +200 -0
- package/src/output/resolve.ts +401 -0
- package/src/output/validate.ts +251 -0
- package/src/parser/cursor.ts +138 -0
- package/src/parser/declaration.ts +247 -0
- package/src/parser/diagnostics.ts +75 -0
- package/src/parser/group.ts +303 -0
- package/src/parser/index.ts +50 -0
- package/src/parser/modifier.ts +60 -0
- package/src/parser/recovery.ts +88 -0
- package/src/parser/rule.ts +370 -0
- package/src/parser/statement.ts +47 -0
- package/src/parser/statements.ts +667 -0
- package/src/parser/value.ts +265 -0
- package/src/resolver/graph.ts +467 -0
- package/src/resolver/groups.ts +177 -0
- package/src/resolver/index.ts +68 -0
- package/src/resolver/keyframes.ts +131 -0
- package/src/resolver/paths.ts +142 -0
- package/src/resolver/rules.ts +54 -0
- package/src/resolver/scopes.ts +342 -0
- package/src/resolver/types.ts +259 -0
- package/src/selectors/flatten.ts +448 -0
- package/src/selectors/index.ts +14 -0
- package/src/selectors/scan.ts +227 -0
- package/src/selectors/specificity.ts +319 -0
- package/src/setup-list.ts +172 -0
- package/src/sourcemaps/index.ts +25 -0
- package/src/sourcemaps/mappings.ts +190 -0
- package/src/sourcemaps/vlq.ts +29 -0
- package/src/types/ast.ts +318 -0
- package/src/types/diagnostics.ts +124 -0
- package/src/types/options.ts +286 -0
- package/src/types/result.ts +121 -0
- package/src/types/span.ts +18 -0
- package/src/types/token.ts +77 -0
- package/src/variables/ast.ts +182 -0
- package/src/variables/expand.ts +319 -0
- package/src/variables/index.ts +38 -0
- package/src/variables/naming.ts +46 -0
- package/src/variables/runtime.ts +336 -0
- package/src/variables/substitute.ts +148 -0
- package/src/variables/walk.ts +147 -0
- package/dist/output/importPath.d.ts +0 -27
- package/dist/output/importPath.d.ts.map +0 -1
- package/dist/output/importPath.js +0 -84
- package/dist/output/importPath.js.map +0 -1
|
@@ -0,0 +1,467 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Module graph loading, cycle detection and topological ordering.
|
|
3
|
+
*
|
|
4
|
+
* Entry paths are loaded breadth-first so every file is read and parsed exactly
|
|
5
|
+
* once per canonical path. All I/O goes through the injected
|
|
6
|
+
* {@link ModuleHost}; the graph stage is otherwise pure and deterministic.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { tokenize } from "../lexer/index.js";
|
|
10
|
+
import { parseTokens } from "../parser/index.js";
|
|
11
|
+
import type { Stylesheet } from "../types/ast.js";
|
|
12
|
+
import type { Diagnostic } from "../types/diagnostics.js";
|
|
13
|
+
import { error } from "../types/diagnostics.js";
|
|
14
|
+
import type { CompileOptions, ModuleHost } from "../types/options.js";
|
|
15
|
+
import type { Span } from "../types/span.js";
|
|
16
|
+
import { collectGroups, visibleGroups } from "./groups.js";
|
|
17
|
+
import type { GroupSource } from "./groups.js";
|
|
18
|
+
import { collectKeyframes, visibleKeyframes } from "./keyframes.js";
|
|
19
|
+
import type { KeyframeSource } from "./keyframes.js";
|
|
20
|
+
import { decodeCssString, isWithinWorkspace, normalizePosixPath } from "./paths.js";
|
|
21
|
+
import { buildScopes, collectLocalSymbols } from "./scopes.js";
|
|
22
|
+
import type { LocalSymbols } from "./scopes.js";
|
|
23
|
+
import type { ModuleGraph, ModuleRecord, ResolvedImport, TokenScope } from "./types.js";
|
|
24
|
+
|
|
25
|
+
/** One successfully parsed module plus its resolved imports. */
|
|
26
|
+
interface LoadedModule {
|
|
27
|
+
readonly file: string;
|
|
28
|
+
readonly ast: Stylesheet;
|
|
29
|
+
readonly imports: readonly ResolvedImport[];
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Resolve entries into a validated module graph. Internal counterpart of
|
|
34
|
+
* {@link buildModuleGraph}; kept separate so the facade stays a thin, documented
|
|
35
|
+
* entry point.
|
|
36
|
+
*
|
|
37
|
+
* `options` is accepted for pipeline symmetry and future policy; workspace
|
|
38
|
+
* containment always comes from `host.workspaceRoot()` (ADR-0007), and entries
|
|
39
|
+
* are absolute POSIX paths supplied by the host.
|
|
40
|
+
*/
|
|
41
|
+
export async function resolveModuleGraph(
|
|
42
|
+
entries: readonly string[],
|
|
43
|
+
host: ModuleHost,
|
|
44
|
+
_options: CompileOptions,
|
|
45
|
+
): Promise<ModuleGraph> {
|
|
46
|
+
const diagnostics: Diagnostic[] = [];
|
|
47
|
+
const report = (diagnostic: Diagnostic): void => {
|
|
48
|
+
diagnostics.push(diagnostic);
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
const loaded = await loadModules(entries, host, report);
|
|
52
|
+
const cycleMembers = findImportCycles(loaded, report);
|
|
53
|
+
const locals = new Map<string, LocalSymbols>();
|
|
54
|
+
|
|
55
|
+
for (const [file, module] of loaded) {
|
|
56
|
+
locals.set(file, collectLocalSymbols(file, module.ast, report));
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const scopes = new Map<
|
|
60
|
+
string,
|
|
61
|
+
{ readonly compileScope: TokenScope; readonly runtimeScope: TokenScope }
|
|
62
|
+
>();
|
|
63
|
+
for (const [file, module] of loaded) {
|
|
64
|
+
scopes.set(
|
|
65
|
+
file,
|
|
66
|
+
buildScopes(
|
|
67
|
+
file,
|
|
68
|
+
localsForFile(file, locals),
|
|
69
|
+
module.imports,
|
|
70
|
+
(target) => locals.get(target),
|
|
71
|
+
report,
|
|
72
|
+
),
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const importers = new Map<string, string[]>();
|
|
77
|
+
for (const [file, module] of loaded) {
|
|
78
|
+
for (const target of dependencyTargets(module.imports)) {
|
|
79
|
+
if (!loaded.has(target)) continue;
|
|
80
|
+
const list = importers.get(target);
|
|
81
|
+
if (list === undefined) importers.set(target, [file]);
|
|
82
|
+
else list.push(file);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const modules = new Map<string, ModuleRecord>();
|
|
87
|
+
const keyframeSources = new Map<string, KeyframeSource>();
|
|
88
|
+
const groupSources = new Map<string, GroupSource>();
|
|
89
|
+
for (const [file, module] of loaded) {
|
|
90
|
+
const styleDependencies = dependencyTargets(module.imports, "styles").filter((target) =>
|
|
91
|
+
loaded.has(target),
|
|
92
|
+
);
|
|
93
|
+
keyframeSources.set(file, { own: collectKeyframes(module.ast), styleDependencies });
|
|
94
|
+
groupSources.set(file, {
|
|
95
|
+
own: collectGroups(file, module.ast, report),
|
|
96
|
+
styleDependencies,
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
const keyframes = visibleKeyframes(keyframeSources);
|
|
100
|
+
const groups = visibleGroups(groupSources);
|
|
101
|
+
|
|
102
|
+
for (const [file, module] of loaded) {
|
|
103
|
+
const scope = scopes.get(file);
|
|
104
|
+
if (scope === undefined) continue;
|
|
105
|
+
const importedBy = importers.get(file) ?? [];
|
|
106
|
+
importedBy.sort(comparePaths);
|
|
107
|
+
modules.set(file, {
|
|
108
|
+
file,
|
|
109
|
+
ast: module.ast,
|
|
110
|
+
dependencies: dependencyTargets(module.imports).filter((target) => loaded.has(target)),
|
|
111
|
+
styleDependencies: keyframeSources.get(file)?.styleDependencies ?? [],
|
|
112
|
+
importedBy,
|
|
113
|
+
compileScope: scope.compileScope,
|
|
114
|
+
runtimeScope: scope.runtimeScope,
|
|
115
|
+
keyframes: keyframes.get(file) ?? new Map(),
|
|
116
|
+
groups: groups.get(file) ?? new Map(),
|
|
117
|
+
hasRules: hasTopLevelRules(module.ast),
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return {
|
|
122
|
+
modules,
|
|
123
|
+
order: topologicalOrder(loaded, cycleMembers),
|
|
124
|
+
diagnostics: sortDiagnostics(diagnostics),
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Load entries and their transitive imports breadth-first.
|
|
130
|
+
*
|
|
131
|
+
* `scheduled` prevents duplicate reads; `loaded` is the canonical-path cache
|
|
132
|
+
* required by SPECIFICATION.md §6.1, so each file is tokenized and parsed once
|
|
133
|
+
* even when several modules import it.
|
|
134
|
+
*/
|
|
135
|
+
async function loadModules(
|
|
136
|
+
entries: readonly string[],
|
|
137
|
+
host: ModuleHost,
|
|
138
|
+
report: (diagnostic: Diagnostic) => void,
|
|
139
|
+
): Promise<Map<string, LoadedModule>> {
|
|
140
|
+
const loaded = new Map<string, LoadedModule>();
|
|
141
|
+
const scheduled = new Set<string>();
|
|
142
|
+
const queue: string[] = [];
|
|
143
|
+
const workspaceRoot = normalizePosixPath(host.workspaceRoot());
|
|
144
|
+
const canonicalRoot = normalizePosixPath(host.canonicalize(workspaceRoot));
|
|
145
|
+
|
|
146
|
+
const schedule = (file: string): void => {
|
|
147
|
+
if (scheduled.has(file)) return;
|
|
148
|
+
scheduled.add(file);
|
|
149
|
+
queue.push(file);
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
for (const entry of entries) {
|
|
153
|
+
schedule(normalizePosixPath(host.canonicalize(normalizePosixPath(entry))));
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
for (let index = 0; index < queue.length; index += 1) {
|
|
157
|
+
const file = queue[index];
|
|
158
|
+
if (file === undefined || loaded.has(file)) continue;
|
|
159
|
+
|
|
160
|
+
const exists = await host.fileExists(file);
|
|
161
|
+
if (!exists) {
|
|
162
|
+
report(
|
|
163
|
+
error("CHIKX2001", `Cannot load module "${file}": file not found.`, {
|
|
164
|
+
start: 0,
|
|
165
|
+
end: 0,
|
|
166
|
+
file,
|
|
167
|
+
}),
|
|
168
|
+
);
|
|
169
|
+
continue;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
let source: string;
|
|
173
|
+
try {
|
|
174
|
+
source = await host.readFile(file);
|
|
175
|
+
} catch {
|
|
176
|
+
report(
|
|
177
|
+
error("CHIKX2001", `Cannot read module "${file}".`, {
|
|
178
|
+
start: 0,
|
|
179
|
+
end: 0,
|
|
180
|
+
file,
|
|
181
|
+
}),
|
|
182
|
+
);
|
|
183
|
+
continue;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
const lexed = tokenize(source, file);
|
|
187
|
+
const parsed = parseTokens(lexed.tokens, file);
|
|
188
|
+
for (const diagnostic of lexed.diagnostics) report(diagnostic);
|
|
189
|
+
for (const diagnostic of parsed.diagnostics) report(diagnostic);
|
|
190
|
+
|
|
191
|
+
const imports: ResolvedImport[] = [];
|
|
192
|
+
for (const item of parsed.ast.items) {
|
|
193
|
+
if (item.kind !== "use") continue;
|
|
194
|
+
imports.push(await resolveImport(item, file, host, workspaceRoot, canonicalRoot, report));
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
loaded.set(file, { file, ast: parsed.ast, imports });
|
|
198
|
+
for (const imported of imports) {
|
|
199
|
+
if (imported.target !== undefined) schedule(imported.target);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
return loaded;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Resolve one `@use` statement relative to its importer.
|
|
208
|
+
*
|
|
209
|
+
* Order matters for clear diagnostics: decode the CSS string, reject lexical
|
|
210
|
+
* workspace escapes before touching existence, canonicalize (host symlink
|
|
211
|
+
* policy), reject canonical escapes, then test existence. Failures yield
|
|
212
|
+
* CHIKX2001 at the `@use` statement and no edge, so downstream passes never see
|
|
213
|
+
* a half-resolved dependency.
|
|
214
|
+
*/
|
|
215
|
+
async function resolveImport(
|
|
216
|
+
item: ResolvedImport["item"],
|
|
217
|
+
importer: string,
|
|
218
|
+
host: ModuleHost,
|
|
219
|
+
workspaceRoot: string,
|
|
220
|
+
canonicalRoot: string,
|
|
221
|
+
report: (diagnostic: Diagnostic) => void,
|
|
222
|
+
): Promise<ResolvedImport> {
|
|
223
|
+
const specifier = decodeCssString(item.path);
|
|
224
|
+
const styles = item.styles === true;
|
|
225
|
+
|
|
226
|
+
if (specifier.trim() === "") {
|
|
227
|
+
report(error("CHIKX2001", `@use specifier is empty in "${importer}".`, item.span));
|
|
228
|
+
return { item, specifier, styles };
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
const lexical = normalizePosixPath(host.resolvePath(importer, specifier));
|
|
232
|
+
if (!isWithinWorkspace(workspaceRoot, lexical)) {
|
|
233
|
+
report(
|
|
234
|
+
error(
|
|
235
|
+
"CHIKX2001",
|
|
236
|
+
`Import "${specifier}" resolves outside the workspace root "${workspaceRoot}": "${lexical}".`,
|
|
237
|
+
item.span,
|
|
238
|
+
),
|
|
239
|
+
);
|
|
240
|
+
return { item, specifier, styles };
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
const canonical = normalizePosixPath(host.canonicalize(lexical));
|
|
244
|
+
if (!isWithinWorkspace(canonicalRoot, canonical)) {
|
|
245
|
+
report(
|
|
246
|
+
error(
|
|
247
|
+
"CHIKX2001",
|
|
248
|
+
`Import "${specifier}" escapes the workspace root through "${lexical}" -> "${canonical}".`,
|
|
249
|
+
item.span,
|
|
250
|
+
),
|
|
251
|
+
);
|
|
252
|
+
return { item, specifier, styles };
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
const exists = await host.fileExists(canonical);
|
|
256
|
+
if (!exists) {
|
|
257
|
+
report(
|
|
258
|
+
error(
|
|
259
|
+
"CHIKX2001",
|
|
260
|
+
`Import "${specifier}" cannot be resolved: "${canonical}" does not exist.`,
|
|
261
|
+
item.span,
|
|
262
|
+
),
|
|
263
|
+
);
|
|
264
|
+
return { item, specifier, styles };
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
return { item, specifier, target: canonical, styles };
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* Detect import cycles (including self-imports) with a depth-first search over
|
|
272
|
+
* the canonical dependency edges.
|
|
273
|
+
*
|
|
274
|
+
* Returns every module that participates in at least one cycle. One CHIKX2001
|
|
275
|
+
* is reported per distinct cycle at the `@use` statement that closes it, with
|
|
276
|
+
* the rotation-stabilized path in the message. Modules stay in the graph so
|
|
277
|
+
* tooling can recover; the topological order excludes them separately.
|
|
278
|
+
*/
|
|
279
|
+
function findImportCycles(
|
|
280
|
+
loaded: Map<string, LoadedModule>,
|
|
281
|
+
report: (diagnostic: Diagnostic) => void,
|
|
282
|
+
): Set<string> {
|
|
283
|
+
const colours = new Map<string, 1 | 2>();
|
|
284
|
+
const stack: string[] = [];
|
|
285
|
+
const seenCycles = new Set<string>();
|
|
286
|
+
const members = new Set<string>();
|
|
287
|
+
|
|
288
|
+
const recordCycle = (from: string, target: string): void => {
|
|
289
|
+
const start = stack.indexOf(target);
|
|
290
|
+
if (start < 0) return;
|
|
291
|
+
const path = stack.slice(start);
|
|
292
|
+
const rotated = rotateToSmallest(path);
|
|
293
|
+
const key = rotated.join("\u0000");
|
|
294
|
+
if (seenCycles.has(key)) return;
|
|
295
|
+
seenCycles.add(key);
|
|
296
|
+
for (const member of rotated) members.add(member);
|
|
297
|
+
|
|
298
|
+
const display = [...rotated, rotated[0] ?? target].join(" -> ");
|
|
299
|
+
report(
|
|
300
|
+
error("CHIKX2001", `Import cycle detected: ${display}.`, cycleEdgeSpan(loaded, from, target)),
|
|
301
|
+
);
|
|
302
|
+
};
|
|
303
|
+
|
|
304
|
+
const visit = (node: string): void => {
|
|
305
|
+
colours.set(node, 1);
|
|
306
|
+
stack.push(node);
|
|
307
|
+
|
|
308
|
+
const module = loaded.get(node);
|
|
309
|
+
const dependencies =
|
|
310
|
+
module === undefined ? [] : dependencyTargets(module.imports).sort(comparePaths);
|
|
311
|
+
for (const dependency of dependencies) {
|
|
312
|
+
if (!loaded.has(dependency)) continue;
|
|
313
|
+
const colour = colours.get(dependency);
|
|
314
|
+
if (colour === undefined) visit(dependency);
|
|
315
|
+
else if (colour === 1) recordCycle(node, dependency);
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
stack.pop();
|
|
319
|
+
colours.set(node, 2);
|
|
320
|
+
};
|
|
321
|
+
|
|
322
|
+
for (const file of [...loaded.keys()].sort(comparePaths)) {
|
|
323
|
+
if (!colours.has(file)) visit(file);
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
return members;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/**
|
|
330
|
+
* Deterministic topological order using Kahn's algorithm.
|
|
331
|
+
*
|
|
332
|
+
* Dependencies are emitted before importers; among ready modules the smallest
|
|
333
|
+
* canonical path wins, so ties are stable across runs and platforms. Edges into
|
|
334
|
+
* cycle members are ignored because those modules are excluded from the order.
|
|
335
|
+
*/
|
|
336
|
+
function topologicalOrder(loaded: Map<string, LoadedModule>, excluded: Set<string>): string[] {
|
|
337
|
+
const nodes = [...loaded.keys()].filter((file) => !excluded.has(file)).sort(comparePaths);
|
|
338
|
+
const indegree = new Map<string, number>();
|
|
339
|
+
const importers = new Map<string, string[]>();
|
|
340
|
+
|
|
341
|
+
for (const node of nodes) {
|
|
342
|
+
const module = loaded.get(node);
|
|
343
|
+
const dependencies =
|
|
344
|
+
module === undefined
|
|
345
|
+
? []
|
|
346
|
+
: dependencyTargets(module.imports).filter(
|
|
347
|
+
(target) => !excluded.has(target) && loaded.has(target),
|
|
348
|
+
);
|
|
349
|
+
indegree.set(node, dependencies.length);
|
|
350
|
+
for (const dependency of dependencies) {
|
|
351
|
+
const list = importers.get(dependency);
|
|
352
|
+
if (list === undefined) importers.set(dependency, [node]);
|
|
353
|
+
else list.push(node);
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
const ready = nodes.filter((node) => (indegree.get(node) ?? 0) === 0);
|
|
358
|
+
const order: string[] = [];
|
|
359
|
+
|
|
360
|
+
while (ready.length > 0) {
|
|
361
|
+
const node = ready.shift();
|
|
362
|
+
if (node === undefined) break;
|
|
363
|
+
order.push(node);
|
|
364
|
+
|
|
365
|
+
const list = importers.get(node);
|
|
366
|
+
if (list === undefined) continue;
|
|
367
|
+
for (const importer of list) {
|
|
368
|
+
const remaining = (indegree.get(importer) ?? 0) - 1;
|
|
369
|
+
indegree.set(importer, remaining);
|
|
370
|
+
if (remaining === 0) insertSorted(ready, importer);
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
if (order.length < nodes.length) {
|
|
375
|
+
for (const node of nodes) {
|
|
376
|
+
if (!order.includes(node)) order.push(node);
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
return order;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
/** Distinct canonical targets in authored `@use` order, optionally only style edges. */
|
|
384
|
+
function dependencyTargets(imports: readonly ResolvedImport[], mode?: "styles"): string[] {
|
|
385
|
+
const targets = new Set<string>();
|
|
386
|
+
for (const imported of imports) {
|
|
387
|
+
if (mode === "styles" && !imported.styles) continue;
|
|
388
|
+
if (imported.target !== undefined) targets.add(imported.target);
|
|
389
|
+
}
|
|
390
|
+
return [...targets];
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
/** Span of the first `@use` in `from` that targets `target`. */
|
|
394
|
+
function cycleEdgeSpan(loaded: Map<string, LoadedModule>, from: string, target: string): Span {
|
|
395
|
+
const module = loaded.get(from);
|
|
396
|
+
if (module !== undefined) {
|
|
397
|
+
for (const imported of module.imports) {
|
|
398
|
+
if (imported.target === target) return imported.item.span;
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
return { start: 0, end: 0, file: from };
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
/** Rotate a cycle path so the smallest canonical path comes first. */
|
|
405
|
+
function rotateToSmallest(path: readonly string[]): string[] {
|
|
406
|
+
let best = 0;
|
|
407
|
+
for (let index = 1; index < path.length; index += 1) {
|
|
408
|
+
const candidate = path[index];
|
|
409
|
+
const current = path[best];
|
|
410
|
+
if (candidate !== undefined && current !== undefined && comparePaths(candidate, current) < 0) {
|
|
411
|
+
best = index;
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
return [...path.slice(best), ...path.slice(0, best)];
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
/** Whether the module contributes any stylesheet output of its own. */
|
|
418
|
+
function hasTopLevelRules(ast: Stylesheet): boolean {
|
|
419
|
+
return ast.items.some((item) => item.kind === "rule" || item.kind === "native-at-rule");
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
/** First-defined local symbols for a loaded module. */
|
|
423
|
+
function localsForFile(file: string, locals: ReadonlyMap<string, LocalSymbols>): LocalSymbols {
|
|
424
|
+
const symbols = locals.get(file);
|
|
425
|
+
if (symbols !== undefined) return symbols;
|
|
426
|
+
return { compile: new Map(), runtime: new Map() };
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
/** Insert into a sorted string array without changing deterministic order. */
|
|
430
|
+
function insertSorted(list: string[], value: string): void {
|
|
431
|
+
let low = 0;
|
|
432
|
+
let high = list.length;
|
|
433
|
+
while (low < high) {
|
|
434
|
+
const middle = (low + high) >>> 1;
|
|
435
|
+
const current = list[middle];
|
|
436
|
+
if (current !== undefined && comparePaths(current, value) < 0) low = middle + 1;
|
|
437
|
+
else high = middle;
|
|
438
|
+
}
|
|
439
|
+
list.splice(low, 0, value);
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
/** Stable diagnostic order: file, offset, code, message, then input order. */
|
|
443
|
+
function sortDiagnostics(diagnostics: readonly Diagnostic[]): readonly Diagnostic[] {
|
|
444
|
+
return diagnostics
|
|
445
|
+
.map((diagnostic, index) => ({ diagnostic, index }))
|
|
446
|
+
.sort((left, right) => {
|
|
447
|
+
const byFile = comparePaths(left.diagnostic.span.file, right.diagnostic.span.file);
|
|
448
|
+
if (byFile !== 0) return byFile;
|
|
449
|
+
const byStart = left.diagnostic.span.start - right.diagnostic.span.start;
|
|
450
|
+
if (byStart !== 0) return byStart;
|
|
451
|
+
const byEnd = left.diagnostic.span.end - right.diagnostic.span.end;
|
|
452
|
+
if (byEnd !== 0) return byEnd;
|
|
453
|
+
const byCode = comparePaths(left.diagnostic.code, right.diagnostic.code);
|
|
454
|
+
if (byCode !== 0) return byCode;
|
|
455
|
+
const byMessage = comparePaths(left.diagnostic.message, right.diagnostic.message);
|
|
456
|
+
if (byMessage !== 0) return byMessage;
|
|
457
|
+
return left.index - right.index;
|
|
458
|
+
})
|
|
459
|
+
.map((entry) => entry.diagnostic);
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
/** Locale-independent string comparison. */
|
|
463
|
+
function comparePaths(left: string, right: string): number {
|
|
464
|
+
if (left < right) return -1;
|
|
465
|
+
if (left > right) return 1;
|
|
466
|
+
return 0;
|
|
467
|
+
}
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reusable declaration group collection and visibility (ADR-0017).
|
|
3
|
+
*
|
|
4
|
+
* A `@group.<name>` definition is inert: it emits no CSS, and only a matching
|
|
5
|
+
* use site splices its declarations into a rule. The resolver records the
|
|
6
|
+
* definitions a module may use so the expansion pass can resolve a use site
|
|
7
|
+
* with the consuming module's view. It never inspects use sites itself and
|
|
8
|
+
* never expands, reorders or validates declarations.
|
|
9
|
+
*
|
|
10
|
+
* Visibility mirrors ADR-0015's keyframe rule and the `styles` closure of
|
|
11
|
+
* ADR-0014, but for a different reason: the owner decision is that group
|
|
12
|
+
* definitions travel with the CSS a build emits. A module sees its own
|
|
13
|
+
* top-level definitions plus the definitions of every module reached through a
|
|
14
|
+
* `styles` `@use` edge, transitively in authored order. Token-only imports
|
|
15
|
+
* expose no groups.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import type { Stylesheet } from "../types/ast.js";
|
|
19
|
+
import type { Diagnostic } from "../types/diagnostics.js";
|
|
20
|
+
import { error } from "../types/diagnostics.js";
|
|
21
|
+
import type { GroupResolution, VisibleGroup } from "./types.js";
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Directive name reserved for groups (SPECIFICATION.md §3.6, ADR-0017 item 5).
|
|
25
|
+
*
|
|
26
|
+
* An `@use "…" as group` alias would compete with the built-in construct, so it
|
|
27
|
+
* is rejected like an alias named `keyframes` (ADR-0015) and creates no
|
|
28
|
+
* namespace.
|
|
29
|
+
*/
|
|
30
|
+
export const GROUP_DIRECTIVE = "group";
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Collect one module's top-level `@group.<name>` definitions in authored order.
|
|
34
|
+
*
|
|
35
|
+
* A duplicate name inside one module is CHIKX2001 at the later definition and
|
|
36
|
+
* is omitted, so the first definition wins — the same recovery the duplicate
|
|
37
|
+
* local-variable rule uses (SPECIFICATION.md §6.1). The surviving list has at
|
|
38
|
+
* most one definition per name.
|
|
39
|
+
*/
|
|
40
|
+
export function collectGroups(
|
|
41
|
+
file: string,
|
|
42
|
+
ast: Stylesheet,
|
|
43
|
+
report: (diagnostic: Diagnostic) => void,
|
|
44
|
+
): VisibleGroup[] {
|
|
45
|
+
const groups: VisibleGroup[] = [];
|
|
46
|
+
const seen = new Set<string>();
|
|
47
|
+
|
|
48
|
+
for (const item of ast.items) {
|
|
49
|
+
if (item.kind !== "group-definition") continue;
|
|
50
|
+
if (seen.has(item.name)) {
|
|
51
|
+
report(
|
|
52
|
+
error(
|
|
53
|
+
"CHIKX2001",
|
|
54
|
+
`Duplicate group definition "@group.${item.name}" in "${file}".`,
|
|
55
|
+
item.span,
|
|
56
|
+
),
|
|
57
|
+
);
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
seen.add(item.name);
|
|
61
|
+
groups.push({ name: item.name, module: file, span: item.span, definition: item });
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return groups;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Per-module inputs for {@link visibleGroups}.
|
|
69
|
+
*
|
|
70
|
+
* Split from `ModuleRecord` for the same reason as keyframe collection: the
|
|
71
|
+
* record stores the *computed* visibility, so building the closure must not
|
|
72
|
+
* depend on it.
|
|
73
|
+
*/
|
|
74
|
+
export interface GroupSource {
|
|
75
|
+
/** Top-level definitions authored in this module, first definition of a name wins. */
|
|
76
|
+
readonly own: readonly VisibleGroup[];
|
|
77
|
+
/** Canonical paths of `styles` `@use` targets, in authored order (ADR-0014). */
|
|
78
|
+
readonly styleDependencies: readonly string[];
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Resolve every module's visible group names.
|
|
83
|
+
*
|
|
84
|
+
* The reachable set is the module's own definitions first, then each `styles`
|
|
85
|
+
* dependency depth-first in authored order with that dependency's own merge
|
|
86
|
+
* closure, deduplicated per defining module (diamonds contribute once). A name
|
|
87
|
+
* the module defines itself stays in the reachable set of its importers but is
|
|
88
|
+
* shadowed for the module itself, matching ADR-0017 item 5:
|
|
89
|
+
*
|
|
90
|
+
* - a local definition resolves the name — no diagnostic, imports are shadowed;
|
|
91
|
+
* - exactly one imported definition resolves the name;
|
|
92
|
+
* - two or more imported definitions with no local one are
|
|
93
|
+
* {@link GroupResolution} `ambiguous`; the resolver reports nothing here
|
|
94
|
+
* because the diagnostic belongs to the use site (plan G3).
|
|
95
|
+
*
|
|
96
|
+
* Modules are visited in sorted canonical-path order with a shared memo, so the
|
|
97
|
+
* result is deterministic even when a cycle is present (cycles are already
|
|
98
|
+
* CHIKX2001 at the closing edge; a back edge contributes nothing).
|
|
99
|
+
*/
|
|
100
|
+
export function visibleGroups(
|
|
101
|
+
sources: ReadonlyMap<string, GroupSource>,
|
|
102
|
+
): ReadonlyMap<string, ReadonlyMap<string, GroupResolution>> {
|
|
103
|
+
const reachable = new Map<string, Map<string, VisibleGroup[]>>();
|
|
104
|
+
const files = [...sources.keys()].sort(comparePaths);
|
|
105
|
+
for (const file of files) collect(file, sources, reachable, new Set());
|
|
106
|
+
|
|
107
|
+
const scopes = new Map<string, Map<string, GroupResolution>>();
|
|
108
|
+
for (const file of files) {
|
|
109
|
+
const visible = reachable.get(file);
|
|
110
|
+
if (visible === undefined) continue;
|
|
111
|
+
const scope = new Map<string, GroupResolution>();
|
|
112
|
+
for (const [name, groups] of visible) {
|
|
113
|
+
const first = groups[0];
|
|
114
|
+
if (first === undefined) continue;
|
|
115
|
+
// Own definitions are collected first, so `first.module === file` means
|
|
116
|
+
// a local definition shadows every imported definition of that name.
|
|
117
|
+
if (groups.length === 1 || first.module === file) {
|
|
118
|
+
scope.set(name, { kind: "resolved", group: first });
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
scope.set(name, { kind: "ambiguous", name, candidates: groups });
|
|
122
|
+
}
|
|
123
|
+
scopes.set(file, scope);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
return scopes;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Collect the definitions reachable from `file`, memoized per module.
|
|
131
|
+
*
|
|
132
|
+
* Arrays stored in the memo are never exposed mutably: each caller merges the
|
|
133
|
+
* groups it receives into a fresh local list, so one module's closure cannot be
|
|
134
|
+
* altered by its importers.
|
|
135
|
+
*/
|
|
136
|
+
function collect(
|
|
137
|
+
file: string,
|
|
138
|
+
sources: ReadonlyMap<string, GroupSource>,
|
|
139
|
+
memo: Map<string, Map<string, VisibleGroup[]>>,
|
|
140
|
+
visiting: Set<string>,
|
|
141
|
+
): Map<string, VisibleGroup[]> {
|
|
142
|
+
const cached = memo.get(file);
|
|
143
|
+
if (cached !== undefined) return cached;
|
|
144
|
+
const source = sources.get(file);
|
|
145
|
+
if (source === undefined) return new Map();
|
|
146
|
+
|
|
147
|
+
// Post-error recovery: a back edge in a cycle contributes nothing, keeping the
|
|
148
|
+
// walk terminating and deterministic (the cycle itself is already reported).
|
|
149
|
+
if (visiting.has(file)) return new Map();
|
|
150
|
+
visiting.add(file);
|
|
151
|
+
|
|
152
|
+
const visible = new Map<string, VisibleGroup[]>();
|
|
153
|
+
for (const group of source.own) {
|
|
154
|
+
if (!visible.has(group.name)) visible.set(group.name, [group]);
|
|
155
|
+
}
|
|
156
|
+
for (const dependency of source.styleDependencies) {
|
|
157
|
+
for (const [name, groups] of collect(dependency, sources, memo, visiting)) {
|
|
158
|
+
const merged = visible.get(name) ?? [];
|
|
159
|
+
for (const group of groups) {
|
|
160
|
+
if (merged.some((existing) => existing.module === group.module)) continue;
|
|
161
|
+
merged.push(group);
|
|
162
|
+
}
|
|
163
|
+
if (merged.length > 0) visible.set(name, merged);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
visiting.delete(file);
|
|
168
|
+
memo.set(file, visible);
|
|
169
|
+
return visible;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** Locale-independent string comparison. */
|
|
173
|
+
function comparePaths(left: string, right: string): number {
|
|
174
|
+
if (left < right) return -1;
|
|
175
|
+
if (left > right) return 1;
|
|
176
|
+
return 0;
|
|
177
|
+
}
|