@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,333 @@
|
|
|
1
|
+
import { nativeBlockIr, ruleIr, stylesheetIr } from "../ir/factories.js";
|
|
2
|
+
import type { NativeBlockChild, NativeBlockIr, RuleIr, StylesheetIr } from "../ir/types.js";
|
|
3
|
+
import { flattenSelector } from "../selectors/index.js";
|
|
4
|
+
import { scanSelector } from "../selectors/scan.js";
|
|
5
|
+
import type { Diagnostic } from "../types/diagnostics.js";
|
|
6
|
+
import { error } from "../types/diagnostics.js";
|
|
7
|
+
import type { Span } from "../types/span.js";
|
|
8
|
+
import { applySelectorEdits, scanSelectorClasses } from "./scan.js";
|
|
9
|
+
import type { SelectorEdit } from "./scan.js";
|
|
10
|
+
import { isValidCssIdentifier, renameClassName } from "./table.js";
|
|
11
|
+
import type { ClassEntry, ClassTable, RenameOptions, RenameResult } from "./types.js";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Class rewrite: flattened selectors in, prefixed selectors and a class table
|
|
15
|
+
* out (SPECIFICATION.md §7.2, ADR-0010).
|
|
16
|
+
*
|
|
17
|
+
* The pass runs after variable substitution and runtime emission and before
|
|
18
|
+
* `groupRuns`. It is pure, filesystem-free and deterministic, and it never
|
|
19
|
+
* mutates its input: rules are rebuilt with the frozen IR factories.
|
|
20
|
+
*
|
|
21
|
+
* Policy:
|
|
22
|
+
*
|
|
23
|
+
* - Every local class token in a flattened selector is looked up in one table,
|
|
24
|
+
* so a class used at top level and as a nested reference is renamed once:
|
|
25
|
+
* `.button { & > .icon { } }` with prefix `ui` emits `.ui-button > .ui-icon`.
|
|
26
|
+
* - `.` + identifier is the only rewritten construct. Ids, type selectors,
|
|
27
|
+
* attribute selectors, attribute values, strings, comments, pseudo names and
|
|
28
|
+
* pseudo-element names are copied byte for byte. Class tokens inside
|
|
29
|
+
* functional pseudos (`:is(.a)`, `:not(.a)`) are real class selectors and are
|
|
30
|
+
* renamed; the conservative boundary is "every class token except inside
|
|
31
|
+
* `:global(...)`, strings, comments and attribute values".
|
|
32
|
+
* - `:global(.external-icon)` escapes renaming and the `:global(...)` wrapper is
|
|
33
|
+
* **removed** from the emitted selector (§7.2); the inner class keeps its
|
|
34
|
+
* authored text. A global class is recorded in the table with `global: true`
|
|
35
|
+
* and is never exported by `generateClassMap` (§8). MVP unwraps only a simple
|
|
36
|
+
* single-class argument; a complex argument (comma list, combinator, several
|
|
37
|
+
* classes) stays wrapped and unrenamed so the selector's meaning cannot change.
|
|
38
|
+
* - Escaped class identifiers are decoded for their identity (`.\31 23` is
|
|
39
|
+
* class `123`) and, when a prefix applies, re-emitted as the valid bare
|
|
40
|
+
* identifier `ui-123`. When no prefix applies the authored bytes are kept,
|
|
41
|
+
* so escape style is never normalized by accident.
|
|
42
|
+
* - Naming table (MVP strategies): `prefix` with a non-empty, valid prefix
|
|
43
|
+
* produces `"<prefix>-<name>"`; `none` and an empty prefix leave names
|
|
44
|
+
* unchanged. An invalid prefix or an invalid prefixed result is CHIKX4001 and
|
|
45
|
+
* the class stays unprefixed (documented fallback). Only the prefix failure is
|
|
46
|
+
* reported once; a per-class result failure is reported once for that class.
|
|
47
|
+
*
|
|
48
|
+
* Selector flattening is idempotent, so running it here is equivalent to
|
|
49
|
+
* running A06's `flattenIr` afterwards; a selector that cannot flatten is left
|
|
50
|
+
* in its authored nested form (classes are still rewritten) and the emitter
|
|
51
|
+
* reports the same CHIKX1001 it would have reported anyway. Class diagnostics
|
|
52
|
+
* use the rule span because `RuleIr` records declaration-origin spans, not
|
|
53
|
+
* selector offsets.
|
|
54
|
+
*/
|
|
55
|
+
|
|
56
|
+
interface Collector {
|
|
57
|
+
readonly entries: ClassEntry[];
|
|
58
|
+
readonly indexes: Map<string, number>;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function computeEmitted(
|
|
62
|
+
name: string,
|
|
63
|
+
prefix: string,
|
|
64
|
+
strategy: "none" | "prefix",
|
|
65
|
+
span: Span,
|
|
66
|
+
diagnostics: Diagnostic[],
|
|
67
|
+
reported: Set<string>,
|
|
68
|
+
): { readonly emitted: string; readonly renamed: boolean } {
|
|
69
|
+
const result = renameClassName(name, prefix, strategy);
|
|
70
|
+
if (!result.renamed) {
|
|
71
|
+
const applies = strategy === "prefix" && prefix !== "" && isValidCssIdentifier(prefix);
|
|
72
|
+
if (applies && !reported.has(name)) {
|
|
73
|
+
reported.add(name);
|
|
74
|
+
diagnostics.push(
|
|
75
|
+
error(
|
|
76
|
+
"CHIKX4001",
|
|
77
|
+
`Class "${name}" cannot be prefixed with "${prefix}": the result "${prefix}-${name}" is not a valid CSS identifier (SPECIFICATION.md §7.2). The class stays unprefixed.`,
|
|
78
|
+
span,
|
|
79
|
+
),
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return result;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function record(
|
|
87
|
+
collector: Collector,
|
|
88
|
+
token: { readonly name: string; readonly global: boolean },
|
|
89
|
+
span: Span,
|
|
90
|
+
prefix: string,
|
|
91
|
+
strategy: "none" | "prefix",
|
|
92
|
+
diagnostics: Diagnostic[],
|
|
93
|
+
reported: Set<string>,
|
|
94
|
+
): ClassEntry {
|
|
95
|
+
const existingIndex = collector.indexes.get(token.name);
|
|
96
|
+
if (existingIndex !== undefined) {
|
|
97
|
+
const existing = collector.entries[existingIndex];
|
|
98
|
+
if (existing === undefined) {
|
|
99
|
+
throw new Error("class table index out of range");
|
|
100
|
+
}
|
|
101
|
+
if (existing.global && !token.global) {
|
|
102
|
+
const computed = computeEmitted(token.name, prefix, strategy, span, diagnostics, reported);
|
|
103
|
+
const upgraded: ClassEntry = Object.freeze({
|
|
104
|
+
name: token.name,
|
|
105
|
+
emitted: computed.emitted,
|
|
106
|
+
global: false,
|
|
107
|
+
renamed: computed.renamed,
|
|
108
|
+
span: existing.span,
|
|
109
|
+
});
|
|
110
|
+
collector.entries[existingIndex] = upgraded;
|
|
111
|
+
return upgraded;
|
|
112
|
+
}
|
|
113
|
+
return existing;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const computed = token.global
|
|
117
|
+
? { emitted: token.name, renamed: false }
|
|
118
|
+
: computeEmitted(token.name, prefix, strategy, span, diagnostics, reported);
|
|
119
|
+
const entry: ClassEntry = Object.freeze({
|
|
120
|
+
name: token.name,
|
|
121
|
+
emitted: computed.emitted,
|
|
122
|
+
global: token.global,
|
|
123
|
+
renamed: computed.renamed,
|
|
124
|
+
span: Object.freeze(span),
|
|
125
|
+
});
|
|
126
|
+
collector.indexes.set(token.name, collector.entries.length);
|
|
127
|
+
collector.entries.push(entry);
|
|
128
|
+
return entry;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** Rewrite one flat selector text and record its classes. */
|
|
132
|
+
function rewriteFlatSelector(
|
|
133
|
+
text: string,
|
|
134
|
+
span: Span,
|
|
135
|
+
collector: Collector,
|
|
136
|
+
prefix: string,
|
|
137
|
+
strategy: "none" | "prefix",
|
|
138
|
+
diagnostics: Diagnostic[],
|
|
139
|
+
reported: Set<string>,
|
|
140
|
+
): string {
|
|
141
|
+
if (!scanSelector(text).ok) return text;
|
|
142
|
+
const scan = scanSelectorClasses(text);
|
|
143
|
+
if (!scan.ok) return text;
|
|
144
|
+
|
|
145
|
+
const edits: SelectorEdit[] = [];
|
|
146
|
+
for (const global of scan.globals) {
|
|
147
|
+
const inner = text.slice(global.openEnd, global.closeStart).trim();
|
|
148
|
+
const contained = scan.classes.filter(
|
|
149
|
+
(token) => token.start >= global.openEnd && token.end <= global.closeStart,
|
|
150
|
+
);
|
|
151
|
+
// MVP supports a simple global class escape only (SPECIFICATION.md §7.2).
|
|
152
|
+
// A complex argument (comma list, combinator, several classes) is left
|
|
153
|
+
// wrapped and unrenamed rather than expanded into a selector whose meaning
|
|
154
|
+
// would change; its classes are still recorded as global.
|
|
155
|
+
const simple = contained.length === 1 && contained[0]?.raw === inner;
|
|
156
|
+
if (!simple) continue;
|
|
157
|
+
edits.push({ start: global.start, end: global.openEnd, text: "" });
|
|
158
|
+
edits.push({ start: global.closeStart, end: global.end, text: "" });
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
for (const token of scan.classes) {
|
|
162
|
+
const entry = record(collector, token, span, prefix, strategy, diagnostics, reported);
|
|
163
|
+
if (!token.global && entry.renamed) {
|
|
164
|
+
edits.push({ start: token.start, end: token.end, text: `.${entry.emitted}` });
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
return applySelectorEdits(text, edits);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function rewriteRule(
|
|
172
|
+
rule: RuleIr,
|
|
173
|
+
collector: Collector,
|
|
174
|
+
prefix: string,
|
|
175
|
+
strategy: "none" | "prefix",
|
|
176
|
+
diagnostics: Diagnostic[],
|
|
177
|
+
reported: Set<string>,
|
|
178
|
+
): RuleIr {
|
|
179
|
+
const flattened = flattenSelector(rule.selector, rule.ancestors);
|
|
180
|
+
|
|
181
|
+
let selector: string;
|
|
182
|
+
let ancestors: readonly string[];
|
|
183
|
+
if (flattened.ok) {
|
|
184
|
+
selector = rewriteFlatSelector(
|
|
185
|
+
flattened.selector,
|
|
186
|
+
rule.span,
|
|
187
|
+
collector,
|
|
188
|
+
prefix,
|
|
189
|
+
strategy,
|
|
190
|
+
diagnostics,
|
|
191
|
+
reported,
|
|
192
|
+
);
|
|
193
|
+
ancestors = [];
|
|
194
|
+
} else {
|
|
195
|
+
selector = rewriteFlatSelector(
|
|
196
|
+
rule.selector,
|
|
197
|
+
rule.span,
|
|
198
|
+
collector,
|
|
199
|
+
prefix,
|
|
200
|
+
strategy,
|
|
201
|
+
diagnostics,
|
|
202
|
+
reported,
|
|
203
|
+
);
|
|
204
|
+
ancestors = rule.ancestors.map((ancestor) =>
|
|
205
|
+
rewriteFlatSelector(ancestor, rule.span, collector, prefix, strategy, diagnostics, reported),
|
|
206
|
+
);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
const ancestorsUnchanged =
|
|
210
|
+
ancestors.length === rule.ancestors.length &&
|
|
211
|
+
ancestors.every((ancestor, index) => ancestor === rule.ancestors[index]);
|
|
212
|
+
if (selector === rule.selector && ancestorsUnchanged) return rule;
|
|
213
|
+
|
|
214
|
+
return ruleIr({
|
|
215
|
+
selector,
|
|
216
|
+
ancestors,
|
|
217
|
+
declarations: rule.declarations,
|
|
218
|
+
condition: rule.condition,
|
|
219
|
+
context: rule.context,
|
|
220
|
+
span: rule.span,
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function rewriteChild(
|
|
225
|
+
child: NativeBlockChild,
|
|
226
|
+
collector: Collector,
|
|
227
|
+
prefix: string,
|
|
228
|
+
strategy: "none" | "prefix",
|
|
229
|
+
diagnostics: Diagnostic[],
|
|
230
|
+
reported: Set<string>,
|
|
231
|
+
): NativeBlockChild {
|
|
232
|
+
if ("children" in child) {
|
|
233
|
+
return rewriteBlock(child, collector, prefix, strategy, diagnostics, reported);
|
|
234
|
+
}
|
|
235
|
+
if ("declarations" in child) {
|
|
236
|
+
return rewriteRule(child, collector, prefix, strategy, diagnostics, reported);
|
|
237
|
+
}
|
|
238
|
+
return child;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
function rewriteBlock(
|
|
242
|
+
block: NativeBlockIr,
|
|
243
|
+
collector: Collector,
|
|
244
|
+
prefix: string,
|
|
245
|
+
strategy: "none" | "prefix",
|
|
246
|
+
diagnostics: Diagnostic[],
|
|
247
|
+
reported: Set<string>,
|
|
248
|
+
): NativeBlockIr {
|
|
249
|
+
if (block.statement === true) return block;
|
|
250
|
+
const children = block.children.map((child) =>
|
|
251
|
+
rewriteChild(child, collector, prefix, strategy, diagnostics, reported),
|
|
252
|
+
);
|
|
253
|
+
if (children.every((child, index) => child === block.children[index])) return block;
|
|
254
|
+
return nativeBlockIr({
|
|
255
|
+
name: block.name,
|
|
256
|
+
prelude: block.prelude,
|
|
257
|
+
children,
|
|
258
|
+
span: block.span,
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
function tableOf(collector: Collector): ClassTable {
|
|
263
|
+
return Object.freeze({ entries: Object.freeze([...collector.entries]) });
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* Rename every local class token and unwrap `:global(...)` wrappers.
|
|
268
|
+
*
|
|
269
|
+
* @param ir Lowered (and normally substituted) stylesheet IR.
|
|
270
|
+
* @param options Effective prefix and strategy; `options.naming.prefix` wins
|
|
271
|
+
* over `options.prefix`, and the strategy defaults to `prefix`.
|
|
272
|
+
* @returns The rebuilt IR (the input instance when nothing changed), the
|
|
273
|
+
* first-appearance {@link ClassTable} and CHIKX4001 diagnostics for invalid
|
|
274
|
+
* prefix/result configurations.
|
|
275
|
+
*/
|
|
276
|
+
export function renameClasses(ir: StylesheetIr, options: RenameOptions = {}): RenameResult {
|
|
277
|
+
const strategy: "none" | "prefix" = options.naming?.strategy ?? "prefix";
|
|
278
|
+
const prefix = options.naming?.prefix ?? options.prefix ?? "";
|
|
279
|
+
const collector: Collector = { entries: [], indexes: new Map() };
|
|
280
|
+
const diagnostics: Diagnostic[] = [];
|
|
281
|
+
const reported = new Set<string>();
|
|
282
|
+
|
|
283
|
+
if (strategy === "prefix" && prefix !== "" && !isValidCssIdentifier(prefix)) {
|
|
284
|
+
// Report once, at the first class we can point at; if the stylesheet has no
|
|
285
|
+
// classes the invalid prefix is inert and reporting would have no location.
|
|
286
|
+
const firstRule = findFirstRuleSpan(ir);
|
|
287
|
+
if (firstRule !== null) {
|
|
288
|
+
diagnostics.push(
|
|
289
|
+
error(
|
|
290
|
+
"CHIKX4001",
|
|
291
|
+
`Invalid class prefix "${prefix}": it is not a valid CSS identifier, so classes stay unprefixed (SPECIFICATION.md §7.2).`,
|
|
292
|
+
firstRule,
|
|
293
|
+
),
|
|
294
|
+
);
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
const rules = ir.rules.map((node) =>
|
|
299
|
+
"declarations" in node
|
|
300
|
+
? rewriteRule(node, collector, prefix, strategy, diagnostics, reported)
|
|
301
|
+
: rewriteBlock(node, collector, prefix, strategy, diagnostics, reported),
|
|
302
|
+
);
|
|
303
|
+
|
|
304
|
+
const changed = rules.every((node, index) => node === ir.rules[index]) === false;
|
|
305
|
+
return {
|
|
306
|
+
ir: changed ? stylesheetIr(rules) : ir,
|
|
307
|
+
classes: tableOf(collector),
|
|
308
|
+
diagnostics,
|
|
309
|
+
};
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/** Span of the first rule node, or `null` for an empty stylesheet. */
|
|
313
|
+
function findFirstRuleSpan(ir: StylesheetIr): Span | null {
|
|
314
|
+
for (const node of ir.rules) {
|
|
315
|
+
if ("declarations" in node) return node.span;
|
|
316
|
+
if (node.statement !== true) {
|
|
317
|
+
const found = findFirstRuleInChildren(node.children);
|
|
318
|
+
if (found !== null) return found;
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
return null;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
function findFirstRuleInChildren(children: readonly NativeBlockChild[]): Span | null {
|
|
325
|
+
for (const child of children) {
|
|
326
|
+
if ("declarations" in child) return child.span;
|
|
327
|
+
if ("children" in child) {
|
|
328
|
+
const found = findFirstRuleInChildren(child.children);
|
|
329
|
+
if (found !== null) return found;
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
return null;
|
|
333
|
+
}
|
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
import { readCssIdentifier } from "./table.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Class-token scanning of a flattened selector (SPECIFICATION.md §7.1, §7.2).
|
|
5
|
+
*
|
|
6
|
+
* The A06 selector module exposes structural scanning but not class ranges, so
|
|
7
|
+
* this module mirrors the same state machine — quoted strings, comments,
|
|
8
|
+
* backslash escapes and attribute selectors stay opaque — and adds exactly two
|
|
9
|
+
* observations:
|
|
10
|
+
*
|
|
11
|
+
* - `.` followed by a CSS identifier is a class token; the name is escape-decoded
|
|
12
|
+
* (`.foo\.bar` → `foo.bar`, `.\31 23` → `123`);
|
|
13
|
+
* - a `:global(...)` function around class tokens is recorded so the wrapper can
|
|
14
|
+
* be removed and its classes kept literal.
|
|
15
|
+
*
|
|
16
|
+
* Policy boundary (documented per the task): every class token is renamed
|
|
17
|
+
* **except** tokens inside `:global(...)` and inside strings, comments or
|
|
18
|
+
* attribute values. Class tokens inside functional pseudos such as `:is(.a)` or
|
|
19
|
+
* `:not(.a)` are real class selectors in the same selector and therefore are
|
|
20
|
+
* renamed; pseudo names, pseudo-element names, ids, type selectors and
|
|
21
|
+
* attribute text are never touched.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
/** One class token in a flattened selector. */
|
|
25
|
+
export interface ScannedClass {
|
|
26
|
+
/** Offset of the leading dot. */
|
|
27
|
+
readonly start: number;
|
|
28
|
+
/** Exclusive end offset after the identifier. */
|
|
29
|
+
readonly end: number;
|
|
30
|
+
/** Exact source slice, dot included. */
|
|
31
|
+
readonly raw: string;
|
|
32
|
+
/** Decoded class name. */
|
|
33
|
+
readonly name: string;
|
|
34
|
+
/** `true` when the token is inside a `:global(...)` function. */
|
|
35
|
+
readonly global: boolean;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** One `:global(...)` wrapper, kept as two removable ranges. */
|
|
39
|
+
export interface GlobalEscape {
|
|
40
|
+
/** Offset of the leading `:`. */
|
|
41
|
+
readonly start: number;
|
|
42
|
+
/** Exclusive end of `:global(`, where the inner content starts. */
|
|
43
|
+
readonly openEnd: number;
|
|
44
|
+
/** Offset of the closing `)`. */
|
|
45
|
+
readonly closeStart: number;
|
|
46
|
+
/** Exclusive end after the closing `)`. */
|
|
47
|
+
readonly end: number;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** Result of scanning one flattened selector. */
|
|
51
|
+
export type SelectorClassScan =
|
|
52
|
+
| {
|
|
53
|
+
readonly ok: true;
|
|
54
|
+
/** Class tokens in source order, globals included. */
|
|
55
|
+
readonly classes: readonly ScannedClass[];
|
|
56
|
+
/** `:global(...)` wrappers in source order. */
|
|
57
|
+
readonly globals: readonly GlobalEscape[];
|
|
58
|
+
}
|
|
59
|
+
| { readonly ok: false; readonly reason: string };
|
|
60
|
+
|
|
61
|
+
/** Top of the opener stack, or `""` when no bracket/paren is open. */
|
|
62
|
+
function top(stack: readonly string[]): string {
|
|
63
|
+
return stack[stack.length - 1] ?? "";
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Find the `)` matching the `(` at `open`, honouring strings and comments. */
|
|
67
|
+
function findClosingParen(text: string, open: number): number {
|
|
68
|
+
let depth = 0;
|
|
69
|
+
let quote: '"' | "'" | null = null;
|
|
70
|
+
let inComment = false;
|
|
71
|
+
let escaped = false;
|
|
72
|
+
|
|
73
|
+
for (let index = open; index < text.length; index += 1) {
|
|
74
|
+
const character = text.charAt(index);
|
|
75
|
+
if (inComment) {
|
|
76
|
+
if (character === "*" && text.charAt(index + 1) === "/") {
|
|
77
|
+
inComment = false;
|
|
78
|
+
index += 1;
|
|
79
|
+
}
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
if (quote !== null) {
|
|
83
|
+
if (escaped) {
|
|
84
|
+
escaped = false;
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
if (character === "\\") {
|
|
88
|
+
escaped = true;
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
if (character === quote) quote = null;
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
if (escaped) {
|
|
95
|
+
escaped = false;
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
if (character === "\\") {
|
|
99
|
+
escaped = true;
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
if (character === "/" && text.charAt(index + 1) === "*") {
|
|
103
|
+
inComment = true;
|
|
104
|
+
index += 1;
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
if (character === '"' || character === "'") {
|
|
108
|
+
quote = character;
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
if (character === "(") {
|
|
112
|
+
depth += 1;
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
if (character === ")") {
|
|
116
|
+
depth -= 1;
|
|
117
|
+
if (depth === 0) return index;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return -1;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** True when `:global(` starts at `index` (ASCII case-insensitive). */
|
|
124
|
+
function isGlobalFunction(text: string, index: number): boolean {
|
|
125
|
+
return text.slice(index, index + 8).toLowerCase() === ":global(";
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Scan one flattened selector for class tokens and `:global(...)` wrappers.
|
|
130
|
+
*
|
|
131
|
+
* Pure and deterministic. Structural failures (unbalanced brackets, an
|
|
132
|
+
* unterminated string or comment, a trailing escape) return `ok: false` with a
|
|
133
|
+
* reason; callers then leave the selector untouched so the emitter reports the
|
|
134
|
+
* same CHIKX1001 it already reports for malformed selectors.
|
|
135
|
+
*/
|
|
136
|
+
export function scanSelectorClasses(text: string): SelectorClassScan {
|
|
137
|
+
const classes: ScannedClass[] = [];
|
|
138
|
+
const globals: GlobalEscape[] = [];
|
|
139
|
+
const closers: string[] = [];
|
|
140
|
+
let quote: '"' | "'" | null = null;
|
|
141
|
+
let inComment = false;
|
|
142
|
+
let escaped = false;
|
|
143
|
+
|
|
144
|
+
for (let index = 0; index < text.length; index += 1) {
|
|
145
|
+
const character = text.charAt(index);
|
|
146
|
+
|
|
147
|
+
if (inComment) {
|
|
148
|
+
if (character === "*" && text.charAt(index + 1) === "/") {
|
|
149
|
+
inComment = false;
|
|
150
|
+
index += 1;
|
|
151
|
+
}
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
if (quote !== null) {
|
|
156
|
+
if (escaped) {
|
|
157
|
+
escaped = false;
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
if (character === "\\") {
|
|
161
|
+
escaped = true;
|
|
162
|
+
continue;
|
|
163
|
+
}
|
|
164
|
+
if (character === quote) quote = null;
|
|
165
|
+
continue;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
if (escaped) {
|
|
169
|
+
escaped = false;
|
|
170
|
+
continue;
|
|
171
|
+
}
|
|
172
|
+
if (character === "\\") {
|
|
173
|
+
escaped = true;
|
|
174
|
+
continue;
|
|
175
|
+
}
|
|
176
|
+
if (character === "/" && text.charAt(index + 1) === "*") {
|
|
177
|
+
inComment = true;
|
|
178
|
+
index += 1;
|
|
179
|
+
continue;
|
|
180
|
+
}
|
|
181
|
+
if (character === '"' || character === "'") {
|
|
182
|
+
quote = character;
|
|
183
|
+
continue;
|
|
184
|
+
}
|
|
185
|
+
if (character === "(") {
|
|
186
|
+
closers.push(")");
|
|
187
|
+
continue;
|
|
188
|
+
}
|
|
189
|
+
if (character === "[") {
|
|
190
|
+
closers.push("]");
|
|
191
|
+
continue;
|
|
192
|
+
}
|
|
193
|
+
if (character === ")" || character === "]") {
|
|
194
|
+
if (closers.pop() !== character) {
|
|
195
|
+
return { ok: false, reason: `unbalanced "${character}"` };
|
|
196
|
+
}
|
|
197
|
+
continue;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
if (top(closers) === "]") continue;
|
|
201
|
+
|
|
202
|
+
if (character === ":" && isGlobalFunction(text, index)) {
|
|
203
|
+
const closeStart = findClosingParen(text, index + 7);
|
|
204
|
+
if (closeStart !== -1) {
|
|
205
|
+
globals.push({ start: index, openEnd: index + 8, closeStart, end: closeStart + 1 });
|
|
206
|
+
}
|
|
207
|
+
continue;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
if (character === ".") {
|
|
211
|
+
const identifier = readCssIdentifier(text, index + 1);
|
|
212
|
+
if (identifier !== null) {
|
|
213
|
+
classes.push({
|
|
214
|
+
start: index,
|
|
215
|
+
end: identifier.end,
|
|
216
|
+
raw: text.slice(index, identifier.end),
|
|
217
|
+
name: identifier.name,
|
|
218
|
+
global: false,
|
|
219
|
+
});
|
|
220
|
+
index = identifier.end - 1;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
if (quote !== null) return { ok: false, reason: "unterminated string" };
|
|
226
|
+
if (inComment) return { ok: false, reason: "unterminated comment" };
|
|
227
|
+
if (escaped) return { ok: false, reason: "trailing escape" };
|
|
228
|
+
if (closers.length > 0) {
|
|
229
|
+
return { ok: false, reason: `unbalanced "${top(closers)}"` };
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
const marked = classes.map((token) => ({
|
|
233
|
+
...token,
|
|
234
|
+
global: globals.some((range) => token.start >= range.openEnd && token.end <= range.closeStart),
|
|
235
|
+
}));
|
|
236
|
+
|
|
237
|
+
return { ok: true, classes: Object.freeze(marked), globals: Object.freeze(globals) };
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/** One text replacement over `[start, end)`. */
|
|
241
|
+
export interface SelectorEdit {
|
|
242
|
+
/** Inclusive start offset. */
|
|
243
|
+
readonly start: number;
|
|
244
|
+
/** Exclusive end offset. */
|
|
245
|
+
readonly end: number;
|
|
246
|
+
/** Replacement text; empty removes the range. */
|
|
247
|
+
readonly text: string;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Apply non-overlapping selector edits in ascending offset order.
|
|
252
|
+
*
|
|
253
|
+
* Edits are sorted defensively so callers cannot depend on insertion order.
|
|
254
|
+
*/
|
|
255
|
+
export function applySelectorEdits(text: string, edits: readonly SelectorEdit[]): string {
|
|
256
|
+
if (edits.length === 0) return text;
|
|
257
|
+
const sorted = [...edits].sort((left, right) => left.start - right.start);
|
|
258
|
+
let output = "";
|
|
259
|
+
let cursor = 0;
|
|
260
|
+
for (const edit of sorted) {
|
|
261
|
+
if (edit.start < cursor) continue;
|
|
262
|
+
output += text.slice(cursor, edit.start);
|
|
263
|
+
output += edit.text;
|
|
264
|
+
cursor = edit.end;
|
|
265
|
+
}
|
|
266
|
+
return output + text.slice(cursor);
|
|
267
|
+
}
|