@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,199 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Stateful single-pass scanner: source text to tokens plus lexical diagnostics.
|
|
3
|
+
*
|
|
4
|
+
* The loop advances by whole tokens, and every token covers at least one source
|
|
5
|
+
* code unit, so the scan terminates in at most `source.length + 1` iterations
|
|
6
|
+
* for any input. There is no backtracking and no regular expression, and the
|
|
7
|
+
* tokenizer never throws: malformed input produces recovered tokens plus
|
|
8
|
+
* `CHIKX1001` diagnostics and always ends with a single `eof` token.
|
|
9
|
+
*
|
|
10
|
+
* Whitespace runs and block comments become their own tokens so the stream
|
|
11
|
+
* reproduces the source exactly. `@` classification is intentionally absent —
|
|
12
|
+
* see `at-keywords.ts` and the frozen `modifier` JSDoc.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import type { Diagnostic } from "../types/diagnostics.js";
|
|
16
|
+
import type { Token, TokenKind } from "../types/token.js";
|
|
17
|
+
import { scanAtKeyword } from "./at-keywords.js";
|
|
18
|
+
import { escapeEnd, isIllegalCode, isNameCode, isWhitespaceCode } from "./characters.js";
|
|
19
|
+
import { scanComment } from "./comments.js";
|
|
20
|
+
import { LEXICAL_MESSAGES, illegalCharacterMessage, lexicalError } from "./diagnostics.js";
|
|
21
|
+
import { scanDollarRef } from "./dollar-refs.js";
|
|
22
|
+
import { identEnd, isIdentStartAt, nameEnd } from "./names.js";
|
|
23
|
+
import { isNumberStartAt, numberEnd } from "./numbers.js";
|
|
24
|
+
import { scanString } from "./strings.js";
|
|
25
|
+
import { isUrlName, scanUrl } from "./urls.js";
|
|
26
|
+
|
|
27
|
+
/** Tokens and diagnostics produced by one scan. */
|
|
28
|
+
export interface ScanResult {
|
|
29
|
+
/** Tokens in source order, always ending with exactly one zero-width `eof`. */
|
|
30
|
+
readonly tokens: readonly Token[];
|
|
31
|
+
/** Lexical errors in source order; empty for valid source. */
|
|
32
|
+
readonly diagnostics: readonly Diagnostic[];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const PUNCTUATION: Readonly<Record<string, TokenKind>> = {
|
|
36
|
+
":": "colon",
|
|
37
|
+
";": "semicolon",
|
|
38
|
+
",": "comma",
|
|
39
|
+
"{": "lbrace",
|
|
40
|
+
"}": "rbrace",
|
|
41
|
+
"(": "lparen",
|
|
42
|
+
")": "rparen",
|
|
43
|
+
"[": "lbracket",
|
|
44
|
+
"]": "rbracket",
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const QUOTE_DOUBLE = 0x22;
|
|
48
|
+
const QUOTE_SINGLE = 0x27;
|
|
49
|
+
const BACKSLASH = 0x5c;
|
|
50
|
+
const SLASH = 0x2f;
|
|
51
|
+
const STAR = 0x2a;
|
|
52
|
+
const AT = 0x40;
|
|
53
|
+
const DOLLAR = 0x24;
|
|
54
|
+
const HASH = 0x23;
|
|
55
|
+
const OPEN_PAREN = 0x28;
|
|
56
|
+
|
|
57
|
+
function makeToken(
|
|
58
|
+
kind: TokenKind,
|
|
59
|
+
source: string,
|
|
60
|
+
start: number,
|
|
61
|
+
end: number,
|
|
62
|
+
value?: string,
|
|
63
|
+
): Token {
|
|
64
|
+
return { kind, value: value ?? source.slice(start, end), start, end };
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Scan `source` in one pass.
|
|
69
|
+
*
|
|
70
|
+
* `file` is copied into every diagnostic span; token spans stay plain offsets,
|
|
71
|
+
* as the frozen `Span` contract requires.
|
|
72
|
+
*/
|
|
73
|
+
export function scan(source: string, file: string): ScanResult {
|
|
74
|
+
const tokens: Token[] = [];
|
|
75
|
+
const diagnostics: Diagnostic[] = [];
|
|
76
|
+
const length = source.length;
|
|
77
|
+
let index = 0;
|
|
78
|
+
|
|
79
|
+
while (index < length) {
|
|
80
|
+
const code = source.charCodeAt(index);
|
|
81
|
+
|
|
82
|
+
if (isWhitespaceCode(code)) {
|
|
83
|
+
let end = index + 1;
|
|
84
|
+
while (end < length && isWhitespaceCode(source.charCodeAt(end))) end += 1;
|
|
85
|
+
tokens.push(makeToken("whitespace", source, index, end));
|
|
86
|
+
index = end;
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (code === SLASH && source.charCodeAt(index + 1) === STAR) {
|
|
91
|
+
const comment = scanComment(source, index);
|
|
92
|
+
tokens.push(makeToken("comment", source, index, comment.end));
|
|
93
|
+
if (!comment.closed) {
|
|
94
|
+
diagnostics.push(
|
|
95
|
+
lexicalError(LEXICAL_MESSAGES.unterminatedComment, index, comment.end, file),
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
index = comment.end;
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (code === QUOTE_DOUBLE || code === QUOTE_SINGLE) {
|
|
103
|
+
const literal = scanString(source, index);
|
|
104
|
+
tokens.push(makeToken("string", source, index, literal.end));
|
|
105
|
+
if (!literal.closed) {
|
|
106
|
+
diagnostics.push(
|
|
107
|
+
lexicalError(LEXICAL_MESSAGES.unterminatedString, index, literal.end, file),
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
index = literal.end;
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if (code === AT) {
|
|
115
|
+
const end = scanAtKeyword(source, index);
|
|
116
|
+
tokens.push(makeToken(end > index + 1 ? "at-keyword" : "delim", source, index, end));
|
|
117
|
+
index = end;
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if (code === DOLLAR) {
|
|
122
|
+
const end = scanDollarRef(source, index);
|
|
123
|
+
if (end === null) {
|
|
124
|
+
tokens.push(makeToken("delim", source, index, index + 1));
|
|
125
|
+
index += 1;
|
|
126
|
+
} else {
|
|
127
|
+
tokens.push(makeToken("dollar-ref", source, index, end));
|
|
128
|
+
index = end;
|
|
129
|
+
}
|
|
130
|
+
continue;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
if (code === HASH) {
|
|
134
|
+
const nameStart = index + 1;
|
|
135
|
+
const named =
|
|
136
|
+
isNameCode(source.charCodeAt(nameStart)) || escapeEnd(source, nameStart) > nameStart;
|
|
137
|
+
const end = named ? nameEnd(source, nameStart) : index + 1;
|
|
138
|
+
tokens.push(makeToken(named ? "hash" : "delim", source, index, end));
|
|
139
|
+
index = end;
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if (isNumberStartAt(source, index)) {
|
|
144
|
+
const end = numberEnd(source, index);
|
|
145
|
+
tokens.push(makeToken("number", source, index, end));
|
|
146
|
+
index = end;
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
if (isIdentStartAt(source, index)) {
|
|
151
|
+
const end = identEnd(source, index);
|
|
152
|
+
if (source.charCodeAt(end) === OPEN_PAREN) {
|
|
153
|
+
const name = source.slice(index, end);
|
|
154
|
+
if (isUrlName(source, index, end)) {
|
|
155
|
+
const url = scanUrl(source, end);
|
|
156
|
+
tokens.push(makeToken("url", source, index, url.end));
|
|
157
|
+
if (url.problem !== null) {
|
|
158
|
+
diagnostics.push(
|
|
159
|
+
lexicalError(
|
|
160
|
+
url.problem === "malformed"
|
|
161
|
+
? LEXICAL_MESSAGES.malformedUrl
|
|
162
|
+
: LEXICAL_MESSAGES.unterminatedUrl,
|
|
163
|
+
index,
|
|
164
|
+
url.end,
|
|
165
|
+
file,
|
|
166
|
+
),
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
index = url.end;
|
|
170
|
+
continue;
|
|
171
|
+
}
|
|
172
|
+
tokens.push(makeToken("function", source, index, end, name));
|
|
173
|
+
index = end;
|
|
174
|
+
continue;
|
|
175
|
+
}
|
|
176
|
+
tokens.push(makeToken("ident", source, index, end));
|
|
177
|
+
index = end;
|
|
178
|
+
continue;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const punctuation = PUNCTUATION[source.charAt(index)];
|
|
182
|
+
if (punctuation !== undefined) {
|
|
183
|
+
tokens.push(makeToken(punctuation, source, index, index + 1));
|
|
184
|
+
index += 1;
|
|
185
|
+
continue;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
if (code === BACKSLASH) {
|
|
189
|
+
diagnostics.push(lexicalError(LEXICAL_MESSAGES.invalidEscape, index, index + 1, file));
|
|
190
|
+
} else if (isIllegalCode(code)) {
|
|
191
|
+
diagnostics.push(lexicalError(illegalCharacterMessage(code), index, index + 1, file));
|
|
192
|
+
}
|
|
193
|
+
tokens.push(makeToken("delim", source, index, index + 1));
|
|
194
|
+
index += 1;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
tokens.push({ kind: "eof", value: "", start: length, end: length });
|
|
198
|
+
return { tokens, diagnostics };
|
|
199
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Quoted string scanning.
|
|
3
|
+
*
|
|
4
|
+
* A string token covers the quote characters and every byte between them,
|
|
5
|
+
* escapes included and never decoded, because `Token.value` is the authored
|
|
6
|
+
* slice. `@`, `$`, `{`, `}` and `;` inside stay inert by construction: the
|
|
7
|
+
* scanner never leaves the string until it closes.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { escapeEnd, isNewlineCode } from "./characters.js";
|
|
11
|
+
|
|
12
|
+
/** Result of scanning one quoted string: where it ends and whether it closed. */
|
|
13
|
+
export interface StringScan {
|
|
14
|
+
/** Offset just past the closing quote, or where scanning stopped. */
|
|
15
|
+
readonly end: number;
|
|
16
|
+
/** False for an unterminated string (end of file or an unescaped line break). */
|
|
17
|
+
readonly closed: boolean;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Scan the string whose opening quote is at `start`.
|
|
22
|
+
*
|
|
23
|
+
* A backslash escapes the next character and a backslash before a line
|
|
24
|
+
* terminator continues the string, as in CSS. An unescaped line terminator ends
|
|
25
|
+
* the string as unterminated without consuming the break, so the caller can
|
|
26
|
+
* resume at a safe boundary; end of file does the same.
|
|
27
|
+
*/
|
|
28
|
+
export function scanString(source: string, start: number): StringScan {
|
|
29
|
+
const quote = source.charCodeAt(start);
|
|
30
|
+
let index = start + 1;
|
|
31
|
+
|
|
32
|
+
while (index < source.length) {
|
|
33
|
+
const code = source.charCodeAt(index);
|
|
34
|
+
if (code === quote) return { end: index + 1, closed: true };
|
|
35
|
+
if (isNewlineCode(code)) return { end: index, closed: false };
|
|
36
|
+
|
|
37
|
+
if (code === 0x5c) {
|
|
38
|
+
const next = source.charCodeAt(index + 1);
|
|
39
|
+
if (isNewlineCode(next)) {
|
|
40
|
+
index += next === 0x0d && source.charCodeAt(index + 2) === 0x0a ? 3 : 2;
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
const escaped = escapeEnd(source, index);
|
|
44
|
+
index = escaped > index ? escaped : index + 1;
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
index += 1;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return { end: source.length, closed: false };
|
|
52
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `url(...)` scanning.
|
|
3
|
+
*
|
|
4
|
+
* The frozen `url` JSDoc makes one token out of the whole construct, including
|
|
5
|
+
* its parentheses, quotes and any `@` or `$` inside. That also means interior
|
|
6
|
+
* brackets never contribute to parser depth: the parser only sees one token.
|
|
7
|
+
* Both authored forms (`url("x@2x.png")` and `url(x@2x.png)`) are supported, as
|
|
8
|
+
* is the empty `url()`.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { escapeEnd, isWhitespaceCode } from "./characters.js";
|
|
12
|
+
import { scanString } from "./strings.js";
|
|
13
|
+
|
|
14
|
+
/** Result of scanning one `url(...)` construct. */
|
|
15
|
+
export interface UrlScan {
|
|
16
|
+
/** Offset just past the closing parenthesis, or where scanning stopped. */
|
|
17
|
+
readonly end: number;
|
|
18
|
+
/** `null` when the construct closed properly, else which rule it broke. */
|
|
19
|
+
readonly problem: "unterminated" | "malformed" | null;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Whether the name at `[start, end)` is `url` in any ASCII casing. */
|
|
23
|
+
export function isUrlName(source: string, start: number, end: number): boolean {
|
|
24
|
+
return end - start === 3 && source.slice(start, end).toLowerCase() === "url";
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Scan the construct whose opening parenthesis is at `parenIndex`.
|
|
29
|
+
*
|
|
30
|
+
* A quoted path is followed by optional whitespace and a required `)`; an
|
|
31
|
+
* unquoted path ends at the first unescaped `)`, so `url(a\)b.png)` stays one
|
|
32
|
+
* token. On failure the returned `end` is a resumable boundary: the caller
|
|
33
|
+
* reports the diagnostic and continues lexing there.
|
|
34
|
+
*/
|
|
35
|
+
export function scanUrl(source: string, parenIndex: number): UrlScan {
|
|
36
|
+
let probe = parenIndex + 1;
|
|
37
|
+
while (isWhitespaceCode(source.charCodeAt(probe))) probe += 1;
|
|
38
|
+
|
|
39
|
+
const quote = source.charCodeAt(probe);
|
|
40
|
+
if (quote === 0x22 || quote === 0x27) {
|
|
41
|
+
const literal = scanString(source, probe);
|
|
42
|
+
if (!literal.closed) return { end: literal.end, problem: "unterminated" };
|
|
43
|
+
|
|
44
|
+
let index = literal.end;
|
|
45
|
+
while (isWhitespaceCode(source.charCodeAt(index))) index += 1;
|
|
46
|
+
if (source.charCodeAt(index) === 0x29) return { end: index + 1, problem: null };
|
|
47
|
+
return { end: index, problem: index >= source.length ? "unterminated" : "malformed" };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
let index = parenIndex + 1;
|
|
51
|
+
while (index < source.length) {
|
|
52
|
+
const code = source.charCodeAt(index);
|
|
53
|
+
if (code === 0x29) return { end: index + 1, problem: null };
|
|
54
|
+
if (code === 0x5c) {
|
|
55
|
+
const escaped = escapeEnd(source, index);
|
|
56
|
+
index = escaped > index ? escaped : index + 1;
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
index += 1;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return { end: source.length, problem: "unterminated" };
|
|
63
|
+
}
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
import type { GroupResolution } from "../resolver/index.js";
|
|
2
|
+
import type {
|
|
3
|
+
BlockItem,
|
|
4
|
+
ConditionalBlock,
|
|
5
|
+
ConditionalBlockItem,
|
|
6
|
+
Declaration,
|
|
7
|
+
GroupUseSite,
|
|
8
|
+
NativeAtRule,
|
|
9
|
+
Rule,
|
|
10
|
+
Stylesheet,
|
|
11
|
+
StylesheetItem,
|
|
12
|
+
} from "../types/ast.js";
|
|
13
|
+
import type { Diagnostic } from "../types/diagnostics.js";
|
|
14
|
+
import { error } from "../types/diagnostics.js";
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* `@group.<name>` use-site expansion (ADR-0017, SPECIFICATION.md §3.7).
|
|
18
|
+
*
|
|
19
|
+
* Expansion is a pure AST-to-AST pre-pass that runs **per module**, immediately
|
|
20
|
+
* before that module's {@link lower} call and its own {@link substitute} pass.
|
|
21
|
+
* Each use site is replaced in place by the group's defaults (definition order)
|
|
22
|
+
* followed by the use-site block's declarations (authored order), so lowering
|
|
23
|
+
* sees one ordinary declaration run at the authored position. Nothing is
|
|
24
|
+
* filtered, reordered, deduplicated or rewritten: a use-site declaration that
|
|
25
|
+
* repeats a default property stays later in the run and wins by the native
|
|
26
|
+
* cascade (§5.2).
|
|
27
|
+
*
|
|
28
|
+
* **Token attribution.** The spliced default nodes are the defining module's own
|
|
29
|
+
* {@link Declaration} objects, so they keep the defining module's spans and
|
|
30
|
+
* `span.file`. Substitution derives the token scope from `span.file`
|
|
31
|
+
* (`variables/substitute.ts`), which is exactly the ADR-0017 item 6 requirement:
|
|
32
|
+
* a default's `$refs` resolve in the module that authored them and a
|
|
33
|
+
* consumer-local token of the same name never leaks in. Copying the text without
|
|
34
|
+
* its authored spans would silently rebind it to the consumer.
|
|
35
|
+
*
|
|
36
|
+
* A rejected use site omits **exactly** its whole block: the rest of the rule
|
|
37
|
+
* continues, and a rule emptied by the omission is dropped by lowering like any
|
|
38
|
+
* other declaration-free rule. Definitions themselves are never touched here
|
|
39
|
+
* and emit no IR.
|
|
40
|
+
*
|
|
41
|
+
* Not to be confused with `./group.ts`, which is the unrelated safe
|
|
42
|
+
* declaration-run grouping post-pass (`groupRuns`, §5.3).
|
|
43
|
+
*/
|
|
44
|
+
|
|
45
|
+
/** Inputs needed to expand one module's AST. */
|
|
46
|
+
export interface GroupExpansionOptions {
|
|
47
|
+
/**
|
|
48
|
+
* Canonical path of the module whose AST is being expanded, used in
|
|
49
|
+
* diagnostics and as the consuming scope.
|
|
50
|
+
*/
|
|
51
|
+
readonly module: string;
|
|
52
|
+
/**
|
|
53
|
+
* The consuming module's visible groups, as resolved by G2
|
|
54
|
+
* (`ModuleRecord.groups`). `resolved` entries splice; `ambiguous` and absent
|
|
55
|
+
* entries are CHIKX2001 at the use site.
|
|
56
|
+
*/
|
|
57
|
+
readonly groups: ReadonlyMap<string, GroupResolution>;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Result of {@link expandGroupUseSites}. */
|
|
61
|
+
export interface GroupExpansionResult {
|
|
62
|
+
/** The expanded AST; the input instance when the module has no use sites. */
|
|
63
|
+
readonly ast: Stylesheet;
|
|
64
|
+
/** CHIKX2001 diagnostics in authored order, one per rejected use site. */
|
|
65
|
+
readonly diagnostics: readonly Diagnostic[];
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** `"a"`, `"a" and "b"`, or `"a", "b" and "c"` for an ambiguity message. */
|
|
69
|
+
function moduleList(candidates: readonly { readonly module: string }[]): string {
|
|
70
|
+
const quoted = candidates.map((candidate) => `"${candidate.module}"`);
|
|
71
|
+
const last = quoted[quoted.length - 1];
|
|
72
|
+
if (last === undefined) return "";
|
|
73
|
+
if (quoted.length === 1) return last;
|
|
74
|
+
return `${quoted.slice(0, -1).join(", ")} and ${last}`;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Expand every `@group.<name>` use site of one module in place.
|
|
79
|
+
*
|
|
80
|
+
* Deterministic and side-effect free: the input AST is never mutated and a
|
|
81
|
+
* module without use sites returns the same instance. Diagnostics are reported
|
|
82
|
+
* in authored walk order.
|
|
83
|
+
*/
|
|
84
|
+
export function expandGroupUseSites(
|
|
85
|
+
ast: Stylesheet,
|
|
86
|
+
options: GroupExpansionOptions,
|
|
87
|
+
): GroupExpansionResult {
|
|
88
|
+
const diagnostics: Diagnostic[] = [];
|
|
89
|
+
|
|
90
|
+
const expandUseSite = (site: GroupUseSite): readonly Declaration[] | null => {
|
|
91
|
+
const resolution = options.groups.get(site.name);
|
|
92
|
+
if (resolution === undefined) {
|
|
93
|
+
diagnostics.push(
|
|
94
|
+
error(
|
|
95
|
+
"CHIKX2001",
|
|
96
|
+
`Unknown group "@group.${site.name}" in "${options.module}"; define it in this module or import the module that defines it with @use "..." styles (ADR-0017).`,
|
|
97
|
+
site.span,
|
|
98
|
+
),
|
|
99
|
+
);
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
if (resolution.kind === "ambiguous") {
|
|
103
|
+
diagnostics.push(
|
|
104
|
+
error(
|
|
105
|
+
"CHIKX2001",
|
|
106
|
+
`Ambiguous group "@group.${site.name}" in "${options.module}": visible definitions in ${moduleList(resolution.candidates)}; define it locally or import only one defining module (ADR-0017).`,
|
|
107
|
+
site.span,
|
|
108
|
+
),
|
|
109
|
+
);
|
|
110
|
+
return null;
|
|
111
|
+
}
|
|
112
|
+
// Defaults in definition order, then the use-site overrides in authored
|
|
113
|
+
// order. Every node keeps its own authored module, span and tokens.
|
|
114
|
+
return [...resolution.group.definition.declarations, ...site.declarations];
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
const expandBlock = (items: readonly BlockItem[]): readonly BlockItem[] => {
|
|
118
|
+
const expanded: BlockItem[] = [];
|
|
119
|
+
for (const item of items) {
|
|
120
|
+
switch (item.kind) {
|
|
121
|
+
case "group-use-site": {
|
|
122
|
+
const spliced = expandUseSite(item);
|
|
123
|
+
if (spliced !== null) expanded.push(...spliced);
|
|
124
|
+
break;
|
|
125
|
+
}
|
|
126
|
+
case "rule":
|
|
127
|
+
expanded.push(expandRule(item));
|
|
128
|
+
break;
|
|
129
|
+
case "conditional-block":
|
|
130
|
+
expanded.push(expandConditional(item));
|
|
131
|
+
break;
|
|
132
|
+
case "native-at-rule":
|
|
133
|
+
expanded.push(expandNative(item));
|
|
134
|
+
break;
|
|
135
|
+
case "declaration":
|
|
136
|
+
expanded.push(item);
|
|
137
|
+
break;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
if (
|
|
141
|
+
expanded.length === items.length &&
|
|
142
|
+
expanded.every((entry, index) => entry === items[index])
|
|
143
|
+
) {
|
|
144
|
+
return items;
|
|
145
|
+
}
|
|
146
|
+
return expanded;
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
const expandConditional = (block: ConditionalBlock): ConditionalBlock => {
|
|
150
|
+
// A conditional block cannot hold a use site (§3.7 v0.1), but its nested
|
|
151
|
+
// rules and conditional blocks can contain one at any depth.
|
|
152
|
+
const body: ConditionalBlockItem[] = [];
|
|
153
|
+
for (const item of block.body) {
|
|
154
|
+
switch (item.kind) {
|
|
155
|
+
case "rule":
|
|
156
|
+
body.push(expandRule(item));
|
|
157
|
+
break;
|
|
158
|
+
case "conditional-block":
|
|
159
|
+
body.push(expandConditional(item));
|
|
160
|
+
break;
|
|
161
|
+
case "declaration":
|
|
162
|
+
body.push(item);
|
|
163
|
+
break;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
if (
|
|
167
|
+
body.length === block.body.length &&
|
|
168
|
+
body.every((entry, index) => entry === block.body[index])
|
|
169
|
+
) {
|
|
170
|
+
return block;
|
|
171
|
+
}
|
|
172
|
+
return { ...block, body };
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
const expandRule = (rule: Rule): Rule => {
|
|
176
|
+
const body = expandBlock(rule.body);
|
|
177
|
+
return body === rule.body ? rule : { ...rule, body };
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
const expandNative = (atRule: NativeAtRule): NativeAtRule => {
|
|
181
|
+
if (atRule.block === undefined) return atRule;
|
|
182
|
+
const block = expandBlock(atRule.block);
|
|
183
|
+
return block === atRule.block ? atRule : { ...atRule, block };
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
const items: StylesheetItem[] = [];
|
|
187
|
+
for (const item of ast.items) {
|
|
188
|
+
switch (item.kind) {
|
|
189
|
+
case "rule":
|
|
190
|
+
items.push(expandRule(item));
|
|
191
|
+
break;
|
|
192
|
+
case "native-at-rule":
|
|
193
|
+
items.push(expandNative(item));
|
|
194
|
+
break;
|
|
195
|
+
default:
|
|
196
|
+
// Definitions, `@setup`, `@use`, `@variables`, `@runtime` and
|
|
197
|
+
// `@media-types` are inert for expansion.
|
|
198
|
+
items.push(item);
|
|
199
|
+
break;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
const unchanged =
|
|
204
|
+
items.length === ast.items.length && items.every((entry, index) => entry === ast.items[index]);
|
|
205
|
+
return { ast: unchanged ? ast : { ...ast, items }, diagnostics };
|
|
206
|
+
}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { nativeBlockIr, ruleIr, stylesheetIr } from "../ir/factories.js";
|
|
2
|
+
import type { NativeBlockChild, NativeBlockIr, RuleIr, StylesheetIr } from "../ir/types.js";
|
|
3
|
+
import type { MediaGroupingMode } from "../types/options.js";
|
|
4
|
+
import { mergeRuns } from "./merge.js";
|
|
5
|
+
import { sameAncestors, sameCondition, sameContext } from "./spans.js";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Declaration-run grouping (SPECIFICATION.md §5.3, ADR-0024).
|
|
9
|
+
*
|
|
10
|
+
* Grouping is a pure post-pass over the IR: the pipeline runs it **after**
|
|
11
|
+
* class rewriting (P4/A09), so selector equality is prefix-insensitive there and
|
|
12
|
+
* two runs that will emit the same selector text are already equal.
|
|
13
|
+
*
|
|
14
|
+
* `safe` (the default) merges only **adjacent** runs with identical selector
|
|
15
|
+
* text, identical ancestor chain, identical native at-rule context stack and
|
|
16
|
+
* identical Chikx condition. It preserves declaration order and keeps the span
|
|
17
|
+
* of the first run. Different conditions, different native wrappers and nested
|
|
18
|
+
* rules therefore block a merge, so specificity, layers and scope never change
|
|
19
|
+
* and computed styles are untouched (§5.3).
|
|
20
|
+
*
|
|
21
|
+
* `merge` (opt-in, ADR-0024) runs the same safe pass and then moves conditional
|
|
22
|
+
* runs into later identical-selector identical-condition runs when every
|
|
23
|
+
* crossed declaration has a property family disjoint from the moved run and no
|
|
24
|
+
* wildcard (`all`, `var()`) is involved. See `merge.ts` for the cascade proof.
|
|
25
|
+
*
|
|
26
|
+
* Grouping never crosses a native block boundary: children of a native at-rule
|
|
27
|
+
* are grouped among themselves, and adjacent native blocks stay separate. No
|
|
28
|
+
* global "group at the bottom" pass is performed.
|
|
29
|
+
*
|
|
30
|
+
* `none` returns the IR exactly as authored (the same instance), for fixtures
|
|
31
|
+
* and for comparing against the ungrouped lowering output.
|
|
32
|
+
*/
|
|
33
|
+
export function groupRuns(ir: StylesheetIr, mode: MediaGroupingMode = "safe"): StylesheetIr {
|
|
34
|
+
if (mode === "none") return ir;
|
|
35
|
+
const grouped = stylesheetIr(groupList(ir.rules));
|
|
36
|
+
return mode === "merge" ? mergeRuns(grouped) : grouped;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function sameRun(left: RuleIr, right: RuleIr): boolean {
|
|
40
|
+
return (
|
|
41
|
+
left.selector === right.selector &&
|
|
42
|
+
sameAncestors(left.ancestors, right.ancestors) &&
|
|
43
|
+
sameCondition(left.condition, right.condition) &&
|
|
44
|
+
sameContext(left.context, right.context)
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function mergeRules(left: RuleIr, right: RuleIr): RuleIr {
|
|
49
|
+
return ruleIr({
|
|
50
|
+
selector: left.selector,
|
|
51
|
+
ancestors: left.ancestors,
|
|
52
|
+
declarations: [...left.declarations, ...right.declarations],
|
|
53
|
+
condition: left.condition,
|
|
54
|
+
context: left.context,
|
|
55
|
+
span: left.span,
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function groupList(
|
|
60
|
+
nodes: readonly (RuleIr | NativeBlockIr)[],
|
|
61
|
+
): readonly (RuleIr | NativeBlockIr)[] {
|
|
62
|
+
const grouped: (RuleIr | NativeBlockIr)[] = [];
|
|
63
|
+
for (const node of nodes) {
|
|
64
|
+
if ("declarations" in node) {
|
|
65
|
+
const previous = grouped[grouped.length - 1];
|
|
66
|
+
if (previous !== undefined && "declarations" in previous && sameRun(previous, node)) {
|
|
67
|
+
grouped[grouped.length - 1] = mergeRules(previous, node);
|
|
68
|
+
} else {
|
|
69
|
+
grouped.push(node);
|
|
70
|
+
}
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
if (node.statement === true) {
|
|
74
|
+
grouped.push(node);
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
grouped.push(
|
|
78
|
+
nativeBlockIr({
|
|
79
|
+
name: node.name,
|
|
80
|
+
prelude: node.prelude,
|
|
81
|
+
children: groupChildren(node.children),
|
|
82
|
+
span: node.span,
|
|
83
|
+
}),
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
return grouped;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function groupChildren(children: readonly NativeBlockChild[]): readonly NativeBlockChild[] {
|
|
90
|
+
const grouped: NativeBlockChild[] = [];
|
|
91
|
+
for (const child of children) {
|
|
92
|
+
if ("children" in child) {
|
|
93
|
+
if (child.statement === true) {
|
|
94
|
+
grouped.push(child);
|
|
95
|
+
} else {
|
|
96
|
+
grouped.push(
|
|
97
|
+
nativeBlockIr({
|
|
98
|
+
name: child.name,
|
|
99
|
+
prelude: child.prelude,
|
|
100
|
+
children: groupChildren(child.children),
|
|
101
|
+
span: child.span,
|
|
102
|
+
}),
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
if (!("declarations" in child)) {
|
|
108
|
+
grouped.push(child);
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
const previous = grouped[grouped.length - 1];
|
|
112
|
+
if (previous !== undefined && "declarations" in previous && sameRun(previous, child)) {
|
|
113
|
+
grouped[grouped.length - 1] = mergeRules(previous, child);
|
|
114
|
+
} else {
|
|
115
|
+
grouped.push(child);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
return grouped;
|
|
119
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AST lowerer: parsed Chikx AST to immutable CSS IR.
|
|
3
|
+
*
|
|
4
|
+
* Pure, zero-dependency and deterministic. `lower` expands inline variants in
|
|
5
|
+
* authored order, resolves media aliases (built-ins < project < file,
|
|
6
|
+
* ADR-0006), combines aliases with logical AND and keeps native at-rules as a
|
|
7
|
+
* context stack. `groupRuns` is the separate safe-grouping utility; the
|
|
8
|
+
* pipeline runs it after class rewriting (P4/A09). `expandGroupUseSites` is the
|
|
9
|
+
* `@group.<name>` pre-pass (ADR-0017): it splices use sites into declaration
|
|
10
|
+
* runs per module before `lower`, so defaults keep their defining module's
|
|
11
|
+
* spans and token scope.
|
|
12
|
+
*
|
|
13
|
+
* Not owned here: CSS text emission and nesting flattening (A06), variable
|
|
14
|
+
* substitution (A08), class naming and prefixing (P4/A09), `@runtime` emission
|
|
15
|
+
* (P3/A08). No file in this module reads the filesystem or mutates the AST.
|
|
16
|
+
*/
|
|
17
|
+
export { lower } from "./lower.js";
|
|
18
|
+
export { groupRuns } from "./group.js";
|
|
19
|
+
export { mergeRuns } from "./merge.js";
|
|
20
|
+
export { isWildcardDeclaration, propertyFamily } from "./property-families.js";
|
|
21
|
+
export { expandGroupUseSites } from "./expand-groups.js";
|
|
22
|
+
export type { GroupExpansionOptions, GroupExpansionResult } from "./expand-groups.js";
|
|
23
|
+
|
|
24
|
+
export {
|
|
25
|
+
BUILTIN_MEDIA_ALIASES,
|
|
26
|
+
buildMediaAliasTable,
|
|
27
|
+
resolveMediaCondition,
|
|
28
|
+
} from "./media.js";
|
|
29
|
+
export type {
|
|
30
|
+
ConditionAlias,
|
|
31
|
+
ConditionResolution,
|
|
32
|
+
MediaAliasTable,
|
|
33
|
+
} from "./media.js";
|
|
34
|
+
|
|
35
|
+
export type {
|
|
36
|
+
AtRuleContextIr,
|
|
37
|
+
DeclarationIr,
|
|
38
|
+
MediaConditionIr,
|
|
39
|
+
NativeBlockChild,
|
|
40
|
+
NativeBlockIr,
|
|
41
|
+
RuleIr,
|
|
42
|
+
StylesheetIr,
|
|
43
|
+
} from "../ir/types.js";
|