@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,286 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Options and host contracts for parsing, compiling and planning outputs.
|
|
3
|
+
*
|
|
4
|
+
* All paths crossing this API are workspace-relative or absolute POSIX paths
|
|
5
|
+
* with forward slashes; hosts convert platform separators once at their
|
|
6
|
+
* boundary. The compiler core never reads the filesystem: project operations
|
|
7
|
+
* go through an injected {@link ModuleHost}, and single-source operations are
|
|
8
|
+
* pure functions.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/** Options for `parse`. */
|
|
12
|
+
export interface ParseOptions {
|
|
13
|
+
/**
|
|
14
|
+
* Workspace-relative POSIX path recorded as `Span.file` on every node and
|
|
15
|
+
* diagnostic produced for this source. The source text itself is supplied
|
|
16
|
+
* directly to `parse` and is never read from disk.
|
|
17
|
+
*/
|
|
18
|
+
readonly file: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** Class naming strategies available in v0.1 (ADR-0010). */
|
|
22
|
+
export type NamingStrategy = "none" | "prefix";
|
|
23
|
+
|
|
24
|
+
/** Class naming configuration. */
|
|
25
|
+
export interface NamingOptions {
|
|
26
|
+
/** `none` keeps authored class names; `prefix` prepends the effective prefix. */
|
|
27
|
+
readonly strategy: NamingStrategy;
|
|
28
|
+
/**
|
|
29
|
+
* Prefix used by the `prefix` strategy, for example `ui` producing `ui-card`.
|
|
30
|
+
* An empty string leaves names unchanged. Omitted means the file-level
|
|
31
|
+
* `prefix` option or setup key applies.
|
|
32
|
+
*/
|
|
33
|
+
readonly prefix?: string;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Media grouping modes accepted by v0.1.
|
|
38
|
+
*
|
|
39
|
+
* `safe` (the public default) merges only adjacent compatible rules and media
|
|
40
|
+
* contexts without changing declaration order, specificity, layer or scope;
|
|
41
|
+
* `none` emits rules as authored and performs no merging. `merge` is an opt-in
|
|
42
|
+
* optimization (ADR-0024): it keeps the safe adjacent pass and additionally
|
|
43
|
+
* moves same-selector/same-condition conditional runs across declarations whose
|
|
44
|
+
* property families are provably disjoint (`all` and `var()` values are
|
|
45
|
+
* barriers). It never merges unconditional runs, different selectors,
|
|
46
|
+
* different conditions or different native contexts. Future modes such as
|
|
47
|
+
* ordered-media grouping remain deliberately absent and must
|
|
48
|
+
* be rejected as invalid configuration values.
|
|
49
|
+
*/
|
|
50
|
+
export type MediaGroupingMode = "safe" | "none" | "merge";
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Output settings shared by file-level setup, project configuration and CLI
|
|
54
|
+
* overrides (SPECIFICATION.md §9).
|
|
55
|
+
*
|
|
56
|
+
* Defaults: `cssDir`/`mapDir` = the source directory; `cssName` =
|
|
57
|
+
* `<stem>.css`; `mapName` = `<stem>.map.js`; `emitCss` and `emitMap` = `true`;
|
|
58
|
+
* `mapTypes` = `false`; `sourceMap` = `false`; `minify` = `true`;
|
|
59
|
+
* `importCss` = `false`. `minify` defaults on (ADR-0022) and compacts generated
|
|
60
|
+
* structural whitespace while preserving authored CSS tokens and declaration
|
|
61
|
+
* order; `minify: false` / `--no-minify` opts out.
|
|
62
|
+
* `mapTypes` is opt-in only: the JS class map is the normal artifact and the
|
|
63
|
+
* `.d.ts` beside it is generated only when requested (CSSX-A52). `importCss`
|
|
64
|
+
* is opt-in only: the map carries no CSS import unless a bundler-driven
|
|
65
|
+
* consumer asks for one (ADR-0021).
|
|
66
|
+
*/
|
|
67
|
+
export interface OutputOptions {
|
|
68
|
+
/**
|
|
69
|
+
* Directory for the CSS artifact. A file-level directory resolves relative
|
|
70
|
+
* to the source file (never the working directory); a project-level
|
|
71
|
+
* directory resolves relative to the workspace root. A file-level directory
|
|
72
|
+
* is exact, without mirrored subfolders.
|
|
73
|
+
*/
|
|
74
|
+
readonly cssDir?: string;
|
|
75
|
+
/**
|
|
76
|
+
* Optional CSS basename, for example `button.css`. Basenames only: no
|
|
77
|
+
* separators or traversal, and the `.css` extension is required.
|
|
78
|
+
*/
|
|
79
|
+
readonly cssName?: string;
|
|
80
|
+
/** Directory for the JS class map, resolved like {@link OutputOptions.cssDir}. */
|
|
81
|
+
readonly mapDir?: string;
|
|
82
|
+
/**
|
|
83
|
+
* Optional JS map basename, for example `classes.js`. The opt-in declaration
|
|
84
|
+
* file becomes `classes.d.ts`.
|
|
85
|
+
*/
|
|
86
|
+
readonly mapName?: string;
|
|
87
|
+
/** Write the stylesheet. Default `true`. */
|
|
88
|
+
readonly emitCss?: boolean;
|
|
89
|
+
/** Write the JS class map. Default `true`. */
|
|
90
|
+
readonly emitMap?: boolean;
|
|
91
|
+
/**
|
|
92
|
+
* Write a `.d.ts` beside the JS map with one literal `export declare const`
|
|
93
|
+
* per class. Default `false` (opt-in, CSSX-A52); automatically suppressed with
|
|
94
|
+
* a documented warning when map emission is on but types were requested
|
|
95
|
+
* without a map (SPECIFICATION.md §8).
|
|
96
|
+
*/
|
|
97
|
+
readonly mapTypes?: boolean;
|
|
98
|
+
/**
|
|
99
|
+
* Deprecated alias for {@link OutputOptions.mapTypes}, kept for existing
|
|
100
|
+
* configurations. `map-types` wins when both are authored in one layer.
|
|
101
|
+
*/
|
|
102
|
+
readonly emitTypes?: boolean;
|
|
103
|
+
/** Write a CSS debugging source map. Default `false`; never written when CSS is disabled. */
|
|
104
|
+
readonly sourceMap?: boolean;
|
|
105
|
+
/**
|
|
106
|
+
* Compact CSS output. Default `true` (ADR-0022). Compacts generated structural
|
|
107
|
+
* whitespace only and preserves authored CSS tokens, quoted strings, `calc()`
|
|
108
|
+
* spacing and declaration order. Set `false` (or pass `--no-minify`) for
|
|
109
|
+
* readable output.
|
|
110
|
+
*/
|
|
111
|
+
readonly minify?: boolean;
|
|
112
|
+
/**
|
|
113
|
+
* Whether a style-merged module's class bindings join the importing build's
|
|
114
|
+
* class map and declarations. Default `true` (the ADR-0014 one-table rule).
|
|
115
|
+
* A module opts out with `@setup { map-inherit: false; }` (ADR-0020): its
|
|
116
|
+
* rules still merge into the importing stylesheet, but its bindings stay out
|
|
117
|
+
* of the importer's `map.js`, `.d.ts` and `CompileResult.classes`. The
|
|
118
|
+
* module's own entry map is never affected.
|
|
119
|
+
*/
|
|
120
|
+
readonly mapInherit?: boolean;
|
|
121
|
+
/**
|
|
122
|
+
* Opt-in CSS side-effect import in the generated class map. Default `false`,
|
|
123
|
+
* so the map stays importable by plain Node without a loader (CSSX-A52).
|
|
124
|
+
* With `@setup { import-css: true; }` (ADR-0021) the map starts with one
|
|
125
|
+
* `import "<relative stylesheet>";` naming the emitted CSS artifact, which
|
|
126
|
+
* bundlers that carry CSS through imports (Vite, Rspack, Next-style loaders)
|
|
127
|
+
* then process. Omitted when the build emits no stylesheet; `.d.ts` output
|
|
128
|
+
* never carries the import.
|
|
129
|
+
*/
|
|
130
|
+
readonly importCss?: boolean;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** Options for compiling one source string with `compile`. */
|
|
134
|
+
export interface CompileOptions extends ParseOptions, OutputOptions {
|
|
135
|
+
/** Effective file-level class prefix; `naming.prefix` wins when both are set. */
|
|
136
|
+
readonly prefix?: string;
|
|
137
|
+
/** Class naming strategy and prefix. Defaults to strategy `prefix` with an empty prefix. */
|
|
138
|
+
readonly naming?: NamingOptions;
|
|
139
|
+
/**
|
|
140
|
+
* Project-level media alias overrides merged after built-in defaults and
|
|
141
|
+
* before file-level `@media-types`, which remain the source-level override
|
|
142
|
+
* layer (ADR-0006).
|
|
143
|
+
*/
|
|
144
|
+
readonly media?: Readonly<Record<string, string>>;
|
|
145
|
+
/**
|
|
146
|
+
* Enable strict validation, including rejection of unknown configuration
|
|
147
|
+
* keys. Default `true` (ADR-0023); pass `false` to keep the warning path.
|
|
148
|
+
*/
|
|
149
|
+
readonly strict?: boolean;
|
|
150
|
+
/** Media grouping mode. Default `safe`; `merge` is the opt-in optimizer (ADR-0024). */
|
|
151
|
+
readonly mediaGrouping?: MediaGroupingMode;
|
|
152
|
+
/**
|
|
153
|
+
* Absolute POSIX workspace root used for containment checks. When omitted,
|
|
154
|
+
* `compile` stays filesystem-free and resolves paths relative to `file`.
|
|
155
|
+
*/
|
|
156
|
+
readonly workspaceRoot?: string;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** Compiler behaviour settings as they appear in `chikx.config.json` (`compiler`). */
|
|
160
|
+
export interface CompilerConfig {
|
|
161
|
+
/**
|
|
162
|
+
* Reject unknown configuration keys and other lenient recoveries. Default
|
|
163
|
+
* `true` (ADR-0023); `false` keeps the warning path.
|
|
164
|
+
*/
|
|
165
|
+
readonly strict?: boolean;
|
|
166
|
+
/** Media grouping mode. Default `safe`. */
|
|
167
|
+
readonly mediaGrouping?: MediaGroupingMode;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Declarative project configuration read from `chikx.config.json`
|
|
172
|
+
* (SPECIFICATION.md §10).
|
|
173
|
+
*
|
|
174
|
+
* Keys are camelCase in JSON; source `@setup` keys stay kebab-case. The
|
|
175
|
+
* document schema is frozen in P5, so these structural fields mirror §10
|
|
176
|
+
* without adding validation policy.
|
|
177
|
+
*/
|
|
178
|
+
export interface ProjectConfig {
|
|
179
|
+
/** Glob patterns for project entries. */
|
|
180
|
+
readonly include?: readonly string[];
|
|
181
|
+
/** Glob patterns excluded from project discovery. */
|
|
182
|
+
readonly exclude?: readonly string[];
|
|
183
|
+
/** Project-level output settings, resolved relative to the workspace root. */
|
|
184
|
+
readonly output?: OutputOptions;
|
|
185
|
+
/** Project-level class naming configuration. */
|
|
186
|
+
readonly classes?: NamingOptions;
|
|
187
|
+
/** Project-level media alias overrides. */
|
|
188
|
+
readonly media?: Readonly<Record<string, string>>;
|
|
189
|
+
/** Compiler behaviour settings. */
|
|
190
|
+
readonly compiler?: CompilerConfig;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** Options for compiling several entries through an injected host. */
|
|
194
|
+
export interface CompileProjectOptions {
|
|
195
|
+
/**
|
|
196
|
+
* Absolute POSIX workspace root override. Defaults to the nearest ancestor
|
|
197
|
+
* with `chikx.config.json`, else `package.json`, as reported by the host.
|
|
198
|
+
*/
|
|
199
|
+
readonly projectRoot?: string;
|
|
200
|
+
/** Explicit configuration file path, relative to `projectRoot`. */
|
|
201
|
+
readonly configFile?: string;
|
|
202
|
+
/** Project-level output settings overriding the configuration file. */
|
|
203
|
+
readonly output?: OutputOptions;
|
|
204
|
+
/** Project-level naming overriding the configuration file. */
|
|
205
|
+
readonly naming?: NamingOptions;
|
|
206
|
+
/** Project-level media aliases overriding the configuration file. */
|
|
207
|
+
readonly media?: Readonly<Record<string, string>>;
|
|
208
|
+
/** Strict validation override. */
|
|
209
|
+
readonly strict?: boolean;
|
|
210
|
+
/** Media grouping override. */
|
|
211
|
+
readonly mediaGrouping?: MediaGroupingMode;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Effective output settings after merging built-in defaults, project
|
|
216
|
+
* configuration, file `@setup` and explicit CLI overrides.
|
|
217
|
+
*
|
|
218
|
+
* Every setting has a value, but directory fields are still authored strings:
|
|
219
|
+
* `planOutputs` resolves them against the documented base (source file for
|
|
220
|
+
* file-level settings, workspace root for project-level settings) and
|
|
221
|
+
* validates the result before any write. The authored `mapTypes` alias is not
|
|
222
|
+
* carried here: it resolves into {@link ResolvedSetup.emitTypes}, the single
|
|
223
|
+
* flag the planner reads.
|
|
224
|
+
*/
|
|
225
|
+
export interface ResolvedSetup
|
|
226
|
+
extends Required<Omit<OutputOptions, "mapTypes" | "mapInherit" | "importCss">> {
|
|
227
|
+
/** Effective class prefix after naming precedence is applied. */
|
|
228
|
+
readonly prefix: string;
|
|
229
|
+
/**
|
|
230
|
+
* Effective module map-inheritance flag (ADR-0020). Defaults to `true` in
|
|
231
|
+
* `defaultSetupFor`; optional so hosts that only consume destination planning
|
|
232
|
+
* can omit it, and the compile pipeline treats an absent value as `true`.
|
|
233
|
+
*/
|
|
234
|
+
readonly mapInherit?: boolean;
|
|
235
|
+
/**
|
|
236
|
+
* Effective class-map CSS import flag (ADR-0021). Defaults to `false` in
|
|
237
|
+
* `defaultSetupFor`; optional for the same host-compatibility reason as
|
|
238
|
+
* {@link ResolvedSetup.mapInherit}, and an absent value means `false`.
|
|
239
|
+
*/
|
|
240
|
+
readonly importCss?: boolean;
|
|
241
|
+
/**
|
|
242
|
+
* File-level `@setup { css-dir: "a", "b"; }` list ([ADR-0019]).
|
|
243
|
+
*
|
|
244
|
+
* Present only when a file authored more than one CSS destination. Every
|
|
245
|
+
* entry resolves exactly relative to the source file, like the single
|
|
246
|
+
* {@link OutputOptions.cssDir}, and replaces any project/CLI single directory
|
|
247
|
+
* for the CSS artifact. `cssDir` keeps the first entry for consumers that
|
|
248
|
+
* still read the single value. Project configuration and CLI overrides cannot
|
|
249
|
+
* author a list in v0.1.
|
|
250
|
+
*/
|
|
251
|
+
readonly cssDirs?: readonly string[];
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/** Explicit CLI overrides; partial by design and never flatten multiple outputs silently. */
|
|
255
|
+
export type CliOutputOverrides = OutputOptions & {
|
|
256
|
+
/** CLI-level class prefix override. */
|
|
257
|
+
readonly prefix?: string;
|
|
258
|
+
};
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Filesystem and policy operations the compiler needs from its embedding
|
|
262
|
+
* environment.
|
|
263
|
+
*
|
|
264
|
+
* Every method is asynchronous where it may hit the disk, and paths are
|
|
265
|
+
* absolute POSIX paths unless stated otherwise. Implementations own caching,
|
|
266
|
+
* case sensitivity and symlink policy; the compiler never imports `fs`.
|
|
267
|
+
*/
|
|
268
|
+
export interface ModuleHost {
|
|
269
|
+
/** Read a UTF-8 source file. Rejects when the file is missing or unreadable. */
|
|
270
|
+
readFile(path: string): Promise<string>;
|
|
271
|
+
/** Test whether a path exists and is readable as a file. */
|
|
272
|
+
fileExists(path: string): Promise<boolean>;
|
|
273
|
+
/**
|
|
274
|
+
* Return the canonical real path used as module cache key, resolving
|
|
275
|
+
* symlinks and platform case rules.
|
|
276
|
+
*/
|
|
277
|
+
canonicalize(path: string): string;
|
|
278
|
+
/**
|
|
279
|
+
* Resolve an import specifier `to` relative to the importing file `from`,
|
|
280
|
+
* returning an absolute POSIX path. Resolution is lexical here; callers use
|
|
281
|
+
* {@link ModuleHost.canonicalize} to detect symlink escapes.
|
|
282
|
+
*/
|
|
283
|
+
resolvePath(from: string, to: string): string;
|
|
284
|
+
/** Absolute POSIX path of the workspace root used for containment checks. */
|
|
285
|
+
workspaceRoot(): string;
|
|
286
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import type { Stylesheet } from "./ast.js";
|
|
2
|
+
import type { Diagnostic } from "./diagnostics.js";
|
|
3
|
+
import type { CliOutputOverrides, ProjectConfig, ResolvedSetup } from "./options.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Map from authored class name to emitted class name, for example
|
|
7
|
+
* `{ "button-primary": "ui-button-primary" }`.
|
|
8
|
+
*
|
|
9
|
+
* Keys are the exact authored class tokens (safe quoted keys in generated JS),
|
|
10
|
+
* so `__proto__` and other hazardous names must be represented with a
|
|
11
|
+
* null-prototype object or computed keys. The same key set backs camelCase
|
|
12
|
+
* named exports and the optional default-export aliases; an alias is added only
|
|
13
|
+
* when it is unambiguous, and a collision such as `foo-bar` with `fooBar` is a
|
|
14
|
+
* CHIKX4001 error rather than a silent omission (ADR-0010). Global classes are
|
|
15
|
+
* never present. Iteration order is deterministic.
|
|
16
|
+
*/
|
|
17
|
+
export type ClassMap = Readonly<Record<string, string>>;
|
|
18
|
+
|
|
19
|
+
/** Result of compiling one source string. */
|
|
20
|
+
export interface CompileResult {
|
|
21
|
+
/** Generated stylesheet when CSS emission is enabled. */
|
|
22
|
+
readonly css?: string;
|
|
23
|
+
/** Generated JS class map when map emission is enabled. */
|
|
24
|
+
readonly js?: string;
|
|
25
|
+
/** Generated TypeScript declarations when type emission is enabled. */
|
|
26
|
+
readonly types?: string;
|
|
27
|
+
/** Standard v3 CSS source map text when requested and CSS is emitted. */
|
|
28
|
+
readonly sourceMap?: string;
|
|
29
|
+
/** Class name table with null-prototype safety semantics. */
|
|
30
|
+
readonly classes: ClassMap;
|
|
31
|
+
/** All diagnostics, errors and warnings, in source order. */
|
|
32
|
+
readonly diagnostics: readonly Diagnostic[];
|
|
33
|
+
/** Workspace-relative POSIX paths of every module the source depends on. */
|
|
34
|
+
readonly dependencies: readonly string[];
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Result of a tolerant parse. */
|
|
38
|
+
export interface ParseResult {
|
|
39
|
+
/** Partial AST recovered at safe statement boundaries; never `undefined`. */
|
|
40
|
+
readonly ast: Stylesheet;
|
|
41
|
+
/** Syntax and lexical diagnostics in source order. */
|
|
42
|
+
readonly diagnostics: readonly Diagnostic[];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Kinds of artifact the output planner can plan. */
|
|
46
|
+
export type OutputKind = "css" | "js" | "types" | "cssSourceMap";
|
|
47
|
+
|
|
48
|
+
/** One planned artifact; the planner never writes it. */
|
|
49
|
+
export interface PlannedOutput {
|
|
50
|
+
/** Artifact kind, which determines the writer and manifest handling. */
|
|
51
|
+
readonly kind: OutputKind;
|
|
52
|
+
/** Absolute, POSIX-normalized destination path. */
|
|
53
|
+
readonly absolutePath: string;
|
|
54
|
+
/** Workspace-relative POSIX destination used for manifests and reporting. */
|
|
55
|
+
readonly workspacePath: string;
|
|
56
|
+
/** Workspace-relative POSIX path of the source that produced the artifact. */
|
|
57
|
+
readonly sourceFile: string;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* One compiler-owned artifact recorded in the build manifest.
|
|
62
|
+
*
|
|
63
|
+
* Manifests let `clean` remove only files the compiler created and left
|
|
64
|
+
* untouched, and let rebuilds drop obsolete owned outputs after a successful
|
|
65
|
+
* build. Entries are deterministic and never carry timestamps.
|
|
66
|
+
*/
|
|
67
|
+
export interface ManifestEntry {
|
|
68
|
+
/** Artifact kind of the tracked file. */
|
|
69
|
+
readonly kind: OutputKind;
|
|
70
|
+
/** Workspace-relative POSIX path of the tracked file. */
|
|
71
|
+
readonly workspacePath: string;
|
|
72
|
+
/** Workspace-relative POSIX path of the source that produced it. */
|
|
73
|
+
readonly sourceFile: string;
|
|
74
|
+
/** Content hash recorded after a successful write, when available. */
|
|
75
|
+
readonly contentHash?: string;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Ordered, validated set of artifacts for one entry plus its manifest records.
|
|
80
|
+
*
|
|
81
|
+
* `outputs` follows a stable kind order (CSS, JS map, declarations, CSS source
|
|
82
|
+
* map) so builds are deterministic; `manifest` mirrors the same artifacts in
|
|
83
|
+
* the same order.
|
|
84
|
+
*/
|
|
85
|
+
export interface OutputPlan {
|
|
86
|
+
/** Planned artifacts in deterministic order. */
|
|
87
|
+
readonly outputs: readonly PlannedOutput[];
|
|
88
|
+
/** Manifest records for the planned artifacts, in the same order. */
|
|
89
|
+
readonly manifest: readonly ManifestEntry[];
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** Inputs to the pure output planner. */
|
|
93
|
+
export interface PlanOutputsInput {
|
|
94
|
+
/** Workspace-relative POSIX path of the entry source. */
|
|
95
|
+
readonly sourceFile: string;
|
|
96
|
+
/** Effective output settings after defaults, config, `@setup` and CLI precedence. */
|
|
97
|
+
readonly setup: ResolvedSetup;
|
|
98
|
+
/** Project configuration contributing project-level directories and output flags. */
|
|
99
|
+
readonly projectConfig?: ProjectConfig;
|
|
100
|
+
/** Explicit CLI overrides applied last. */
|
|
101
|
+
readonly cliOverrides?: CliOutputOverrides;
|
|
102
|
+
/** Absolute POSIX workspace root used for containment checks. */
|
|
103
|
+
readonly workspaceRoot: string;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** Result of planning outputs: the plan plus any collision or safety diagnostics. */
|
|
107
|
+
export interface PlanOutputsResult {
|
|
108
|
+
/** Valid plan containing only artifacts that passed every CHIKX5001 check. */
|
|
109
|
+
readonly plan: OutputPlan;
|
|
110
|
+
/** Output-planning diagnostics; any error means the plan must not be written. */
|
|
111
|
+
readonly diagnostics: readonly Diagnostic[];
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Signature of the pure output planner exposed as `planOutputs`.
|
|
116
|
+
*
|
|
117
|
+
* It resolves every destination, validates collisions, overlap, workspace and
|
|
118
|
+
* symlink escapes, and returns diagnostics instead of writing or throwing for
|
|
119
|
+
* expected planning failures.
|
|
120
|
+
*/
|
|
121
|
+
export type PlanOutputs = (input: PlanOutputsInput) => PlanOutputsResult;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A half-open range of offsets into one source file.
|
|
3
|
+
*
|
|
4
|
+
* `start` is inclusive and `end` is exclusive, so a zero-width span has
|
|
5
|
+
* `start === end`. Offsets are UTF-16 code-unit indices into the authored
|
|
6
|
+
* source text. Line and column numbers are deliberately **not** stored: every
|
|
7
|
+
* consumer derives them from `start`/`end` with the same offset-to-position
|
|
8
|
+
* rule, which keeps diagnostics, source maps and editor ranges consistent
|
|
9
|
+
* across stages.
|
|
10
|
+
*/
|
|
11
|
+
export interface Span {
|
|
12
|
+
/** Inclusive offset of the first covered character. */
|
|
13
|
+
readonly start: number;
|
|
14
|
+
/** Exclusive offset just past the last covered character. */
|
|
15
|
+
readonly end: number;
|
|
16
|
+
/** Workspace-relative, POSIX-normalized path (forward slashes) of the source file. */
|
|
17
|
+
readonly file: string;
|
|
18
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The lexical vocabulary shared by the tokenizer, parser and AST.
|
|
3
|
+
*
|
|
4
|
+
* A token always covers one contiguous slice of the original source and never
|
|
5
|
+
* normalizes escaped text. `Token.value` is the exact source text for the
|
|
6
|
+
* token, with the single documented exception of `function`, whose value
|
|
7
|
+
* excludes the opening parenthesis so parentheses stay individually balanced.
|
|
8
|
+
*/
|
|
9
|
+
export type TokenKind =
|
|
10
|
+
/** Identifier or keyword, including escaped characters (`\31 23`, `status\:active`). */
|
|
11
|
+
| "ident"
|
|
12
|
+
/** Numeric token such as `12`, `0.5` or `10px`, kept as authored. */
|
|
13
|
+
| "number"
|
|
14
|
+
/** Quoted string including its delimiters; embedded `@`, `$`, `{`, `}` and `;` stay inert. */
|
|
15
|
+
| "string"
|
|
16
|
+
/** A complete `url(...)` construct including parentheses, quotes and any `@` or `$` inside. */
|
|
17
|
+
| "url"
|
|
18
|
+
/** A CSS function name; the immediately following `(` is emitted as a separate `lparen`. */
|
|
19
|
+
| "function"
|
|
20
|
+
/** Any other single character without a dedicated kind. */
|
|
21
|
+
| "delim"
|
|
22
|
+
| "colon"
|
|
23
|
+
| "semicolon"
|
|
24
|
+
| "comma"
|
|
25
|
+
| "lbrace"
|
|
26
|
+
| "rbrace"
|
|
27
|
+
| "lparen"
|
|
28
|
+
| "rparen"
|
|
29
|
+
| "lbracket"
|
|
30
|
+
| "rbracket"
|
|
31
|
+
/** A `#` name or hash value such as `#2563eb`. */
|
|
32
|
+
| "hash"
|
|
33
|
+
/**
|
|
34
|
+
* An `@` name such as `@media` or `@setup`, including the `@`. Directive and
|
|
35
|
+
* native at-rule names both arrive here because alias tables are
|
|
36
|
+
* configuration-dependent; the parser promotes recognized condition names to
|
|
37
|
+
* `modifier`.
|
|
38
|
+
*/
|
|
39
|
+
| "at-keyword"
|
|
40
|
+
/** A compile-time variable reference, either `$name` or `$name.member`, including the `$`. */
|
|
41
|
+
| "dollar-ref"
|
|
42
|
+
/**
|
|
43
|
+
* A recognized Chikx condition modifier such as `@md` or `@xl+dark`, with the
|
|
44
|
+
* `@` and any `+` joins preserved. The tokenizer never emits this kind on its
|
|
45
|
+
* own: the parser reclassifies `at-keyword` to `modifier` only at top-level
|
|
46
|
+
* value depth (inline variants) or in block-condition position. It must not
|
|
47
|
+
* occur anywhere else, so an `@` inside strings, comments, URLs, functions or
|
|
48
|
+
* without preceding whitespace never becomes one.
|
|
49
|
+
*/
|
|
50
|
+
| "modifier"
|
|
51
|
+
/** Insignificant whitespace, emitted so token streams can reproduce the source exactly. */
|
|
52
|
+
| "whitespace"
|
|
53
|
+
/** A block comment emitted for fidelity and skipped where insignificant. */
|
|
54
|
+
| "comment"
|
|
55
|
+
/** Zero-width end-of-input marker with an empty value. */
|
|
56
|
+
| "eof";
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* One lexical token pointing into the original source.
|
|
60
|
+
*
|
|
61
|
+
* Tokens carry offsets only; derive line and column from `start`/`end` when
|
|
62
|
+
* reporting. Escapes are never rewritten, so a stream that keeps whitespace and
|
|
63
|
+
* comment tokens reproduces the authored source byte for byte.
|
|
64
|
+
*/
|
|
65
|
+
export interface Token {
|
|
66
|
+
readonly kind: TokenKind;
|
|
67
|
+
/**
|
|
68
|
+
* Exact source slice covered by `[start, end)` for every kind except
|
|
69
|
+
* `function`, whose value is the function name without its `(`. `eof` has an
|
|
70
|
+
* empty value.
|
|
71
|
+
*/
|
|
72
|
+
readonly value: string;
|
|
73
|
+
/** Inclusive source offset of the token. */
|
|
74
|
+
readonly start: number;
|
|
75
|
+
/** Exclusive source offset of the token. */
|
|
76
|
+
readonly end: number;
|
|
77
|
+
}
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import type { BlockItem, Declaration, Stylesheet, TokenDeclaration } from "../types/ast.js";
|
|
2
|
+
import type { Span } from "../types/span.js";
|
|
3
|
+
import type { Token } from "../types/token.js";
|
|
4
|
+
import { splitReference } from "./expand.js";
|
|
5
|
+
import type { TokenReference } from "./expand.js";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* AST lookups used by substitution and runtime emission.
|
|
9
|
+
*
|
|
10
|
+
* The AST keeps absolute token offsets for values that contain `$` references
|
|
11
|
+
* (ADR-0005), which is exactly the provenance needed to report a reference at
|
|
12
|
+
* its authored position after lowering has replaced the AST with the IR.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/** One authored `$` reference found in the AST, with its absolute span. */
|
|
16
|
+
export interface AstReference {
|
|
17
|
+
/** Parsed name and optional namespace. */
|
|
18
|
+
readonly reference: TokenReference;
|
|
19
|
+
/** Exact authored span of the reference token. */
|
|
20
|
+
readonly span: Span;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Map each declaration segment start to its parsed value tokens.
|
|
25
|
+
*
|
|
26
|
+
* Lowering tiles a declaration span into one segment per value (A05), and the
|
|
27
|
+
* IR keeps that segment origin in `DeclarationIr.span`, so an IR declaration
|
|
28
|
+
* can recover the authored tokens by its `span.start`. The value is `undefined`
|
|
29
|
+
* when the authored segment contains no `$` reference (`tokens` is optional by
|
|
30
|
+
* contract), which is also the case where substitution has nothing to do.
|
|
31
|
+
*/
|
|
32
|
+
export function segmentValueTokens(ast: Stylesheet | undefined): Map<number, readonly Token[]> {
|
|
33
|
+
const segments = new Map<number, readonly Token[]>();
|
|
34
|
+
if (ast === undefined) return segments;
|
|
35
|
+
|
|
36
|
+
const addDeclaration = (declaration: Declaration): void => {
|
|
37
|
+
if (declaration.tokens !== undefined) segments.set(declaration.span.start, declaration.tokens);
|
|
38
|
+
for (const variant of declaration.variants) {
|
|
39
|
+
if (variant.tokens !== undefined) segments.set(variant.span.start, variant.tokens);
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
const visitBody = (items: readonly BlockItem[]): void => {
|
|
44
|
+
for (const item of items) {
|
|
45
|
+
switch (item.kind) {
|
|
46
|
+
case "declaration":
|
|
47
|
+
addDeclaration(item);
|
|
48
|
+
break;
|
|
49
|
+
case "rule":
|
|
50
|
+
visitBody(item.body);
|
|
51
|
+
break;
|
|
52
|
+
case "conditional-block":
|
|
53
|
+
visitBody(item.body);
|
|
54
|
+
break;
|
|
55
|
+
case "native-at-rule":
|
|
56
|
+
if (item.block !== undefined) visitBody(item.block);
|
|
57
|
+
break;
|
|
58
|
+
case "group-use-site":
|
|
59
|
+
// Use-site overrides are ordinary declarations of the consuming
|
|
60
|
+
// module even before expansion replaces the node (ADR-0017).
|
|
61
|
+
for (const declaration of item.declarations) addDeclaration(declaration);
|
|
62
|
+
break;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
for (const item of ast.items) {
|
|
68
|
+
if (item.kind === "rule") visitBody(item.body);
|
|
69
|
+
else if (item.kind === "native-at-rule" && item.block !== undefined) visitBody(item.block);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return segments;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Collect the segment starts authored inside top-level `@group.<name>`
|
|
77
|
+
* definitions (ADR-0017).
|
|
78
|
+
*
|
|
79
|
+
* Definitions emit no IR, so a {@link DeclarationIr} whose origin segment is in
|
|
80
|
+
* this set can only have reached the IR through use-site expansion. Substitution
|
|
81
|
+
* uses that to enforce owner decision (b): group defaults are compile-time only,
|
|
82
|
+
* so a `@runtime` reference there is CHIKX2001 and the segment is omitted
|
|
83
|
+
* instead of rewritten to `var(--...)` with the consuming build's prefix.
|
|
84
|
+
*
|
|
85
|
+
* Both the base segment and every inline-variant segment are included, matching
|
|
86
|
+
* the IR's per-segment spans.
|
|
87
|
+
*/
|
|
88
|
+
export function groupDefaultSegments(ast: Stylesheet | undefined): Set<number> {
|
|
89
|
+
const starts = new Set<number>();
|
|
90
|
+
if (ast === undefined) return starts;
|
|
91
|
+
for (const item of ast.items) {
|
|
92
|
+
if (item.kind !== "group-definition") continue;
|
|
93
|
+
for (const declaration of item.declarations) {
|
|
94
|
+
starts.add(declaration.span.start);
|
|
95
|
+
for (const variant of declaration.variants) starts.add(variant.span.start);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return starts;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Collect authored `$` references from one module's AST.
|
|
103
|
+
*
|
|
104
|
+
* `includeRules` adds references from stylesheet rule values (used for the entry
|
|
105
|
+
* module, whose declarations are compiled); without it only `@variables` and
|
|
106
|
+
* `@runtime` values are visited, which is what imported token-only modules can
|
|
107
|
+
* contribute to the entry's artifact (SPECIFICATION.md §6.1: imports bring in
|
|
108
|
+
* tokens only, not rules). Rule values include the declarations of `@group.`
|
|
109
|
+
* use-site blocks, because this module's build emits them (ADR-0017).
|
|
110
|
+
*/
|
|
111
|
+
export function astReferences(
|
|
112
|
+
ast: Stylesheet,
|
|
113
|
+
options: { readonly includeRules: boolean },
|
|
114
|
+
): AstReference[] {
|
|
115
|
+
const references: AstReference[] = [];
|
|
116
|
+
|
|
117
|
+
const collect = (tokens: readonly Token[] | undefined, file: string): void => {
|
|
118
|
+
if (tokens === undefined) return;
|
|
119
|
+
for (const token of tokens) {
|
|
120
|
+
if (token.kind !== "dollar-ref") continue;
|
|
121
|
+
const reference = splitReference(token.value);
|
|
122
|
+
if (reference === null) continue;
|
|
123
|
+
references.push({ reference, span: { start: token.start, end: token.end, file } });
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
const collectDeclaration = (declaration: Declaration, file: string): void => {
|
|
128
|
+
collect(declaration.tokens, file);
|
|
129
|
+
for (const variant of declaration.variants) collect(variant.tokens, file);
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
const collectTokenDeclaration = (token: TokenDeclaration, file: string): void => {
|
|
133
|
+
collect(token.tokens, file);
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
const visitBody = (items: readonly BlockItem[], file: string): void => {
|
|
137
|
+
for (const item of items) {
|
|
138
|
+
switch (item.kind) {
|
|
139
|
+
case "declaration":
|
|
140
|
+
collectDeclaration(item, file);
|
|
141
|
+
break;
|
|
142
|
+
case "rule":
|
|
143
|
+
visitBody(item.body, file);
|
|
144
|
+
break;
|
|
145
|
+
case "conditional-block":
|
|
146
|
+
visitBody(item.body, file);
|
|
147
|
+
break;
|
|
148
|
+
case "native-at-rule":
|
|
149
|
+
if (item.block !== undefined) visitBody(item.block, file);
|
|
150
|
+
break;
|
|
151
|
+
case "group-use-site":
|
|
152
|
+
// Use-site overrides are emitted in this module's build, so a
|
|
153
|
+
// `@runtime` reference authored there must register its custom
|
|
154
|
+
// property exactly like a plain declaration (ADR-0017 item 6).
|
|
155
|
+
for (const declaration of item.declarations) collectDeclaration(declaration, file);
|
|
156
|
+
break;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
const file = ast.file;
|
|
162
|
+
for (const item of ast.items) {
|
|
163
|
+
switch (item.kind) {
|
|
164
|
+
case "variables":
|
|
165
|
+
for (const token of item.tokens) collectTokenDeclaration(token, file);
|
|
166
|
+
break;
|
|
167
|
+
case "runtime":
|
|
168
|
+
for (const declaration of item.declarations) collectDeclaration(declaration, file);
|
|
169
|
+
break;
|
|
170
|
+
case "rule":
|
|
171
|
+
if (options.includeRules) visitBody(item.body, file);
|
|
172
|
+
break;
|
|
173
|
+
case "native-at-rule":
|
|
174
|
+
if (options.includeRules && item.block !== undefined) visitBody(item.block, file);
|
|
175
|
+
break;
|
|
176
|
+
default:
|
|
177
|
+
break;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
return references;
|
|
182
|
+
}
|