@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,118 @@
|
|
|
1
|
+
import type { AtRuleContextIr, MediaConditionIr } from "../ir/types.js";
|
|
2
|
+
import type { Declaration, Modifier } from "../types/ast.js";
|
|
3
|
+
import type { Span } from "../types/span.js";
|
|
4
|
+
import type { ConditionAlias } from "./media.js";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Span and equality helpers shared by lowering and safe grouping.
|
|
8
|
+
*
|
|
9
|
+
* Everything here is a pure function of AST/IR data: no source text is read
|
|
10
|
+
* back, so spans are derived only from offsets authored by the parser.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
function clamp(value: number, minimum: number, maximum: number): number {
|
|
14
|
+
if (value < minimum) return minimum;
|
|
15
|
+
if (value > maximum) return maximum;
|
|
16
|
+
return value;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Origin of the base segment plus one origin per inline-variant segment. */
|
|
20
|
+
export interface SegmentSpans {
|
|
21
|
+
/** Property through the first modifier (or the whole declaration). */
|
|
22
|
+
readonly base: Span;
|
|
23
|
+
/** One span per variant, in authored order. */
|
|
24
|
+
readonly variants: readonly Span[];
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Partition a declaration's authored span into segment origins.
|
|
29
|
+
*
|
|
30
|
+
* A segment starts at its modifier and ends at the next modifier, so the
|
|
31
|
+
* segments tile the declaration with no gaps or overlaps. The base segment ends
|
|
32
|
+
* where the first modifier begins. The frozen AST stores no value span for a
|
|
33
|
+
* variant (A04 contract note), so a variant origin necessarily covers its
|
|
34
|
+
* modifier plus the value and separator whitespace that follow it.
|
|
35
|
+
*/
|
|
36
|
+
export function declarationSegmentSpans(declaration: Declaration): SegmentSpans {
|
|
37
|
+
const file = declaration.span.file;
|
|
38
|
+
const { start: declarationStart, end: declarationEnd } = declaration.span;
|
|
39
|
+
if (declaration.variants.length === 0) {
|
|
40
|
+
return { base: declaration.span, variants: Object.freeze([]) };
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const variants: Span[] = [];
|
|
44
|
+
for (let index = 0; index < declaration.variants.length; index++) {
|
|
45
|
+
const variant = declaration.variants[index] as Declaration["variants"][number];
|
|
46
|
+
const next = declaration.variants[index + 1];
|
|
47
|
+
const start = clamp(variant.span.start, declarationStart, declarationEnd);
|
|
48
|
+
const end = clamp(next === undefined ? declarationEnd : next.span.start, start, declarationEnd);
|
|
49
|
+
variants.push(Object.freeze({ start, end, file }));
|
|
50
|
+
}
|
|
51
|
+
const baseEnd = clamp(
|
|
52
|
+
(declaration.variants[0] as Declaration["variants"][number]).span.start,
|
|
53
|
+
declarationStart,
|
|
54
|
+
declarationEnd,
|
|
55
|
+
);
|
|
56
|
+
return {
|
|
57
|
+
base: Object.freeze({ start: declarationStart, end: baseEnd, file }),
|
|
58
|
+
variants: Object.freeze(variants),
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Exact spans for the aliases of one modifier chain.
|
|
64
|
+
*
|
|
65
|
+
* `@` and `+` are single authored characters, so alias origins can be derived
|
|
66
|
+
* by walking the modifier span in alias order. Escaped alias text keeps its
|
|
67
|
+
* authored length, so the walk stays correct without decoding.
|
|
68
|
+
*/
|
|
69
|
+
export function modifierAliasSpans(modifier: Modifier): readonly ConditionAlias[] {
|
|
70
|
+
const spans: ConditionAlias[] = [];
|
|
71
|
+
const { start, end, file } = modifier.span;
|
|
72
|
+
let cursor = start + 1;
|
|
73
|
+
for (const alias of modifier.aliases) {
|
|
74
|
+
const aliasStart = clamp(cursor, start, end);
|
|
75
|
+
const aliasEnd = clamp(aliasStart + alias.length, aliasStart, end);
|
|
76
|
+
spans.push({ alias, span: Object.freeze({ start: aliasStart, end: aliasEnd, file }) });
|
|
77
|
+
cursor = aliasEnd + 1;
|
|
78
|
+
}
|
|
79
|
+
return spans;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Structural equality of two Chikx conditions, used to bound declaration runs. */
|
|
83
|
+
export function sameCondition(
|
|
84
|
+
left: MediaConditionIr | null,
|
|
85
|
+
right: MediaConditionIr | null,
|
|
86
|
+
): boolean {
|
|
87
|
+
if (left === null || right === null) return left === right;
|
|
88
|
+
if (left.query !== right.query || left.keys.length !== right.keys.length) return false;
|
|
89
|
+
for (let index = 0; index < left.keys.length; index++) {
|
|
90
|
+
if (left.keys[index] !== right.keys[index]) return false;
|
|
91
|
+
}
|
|
92
|
+
return true;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** Structural equality of two native at-rule context stacks. */
|
|
96
|
+
export function sameContext(
|
|
97
|
+
left: readonly AtRuleContextIr[],
|
|
98
|
+
right: readonly AtRuleContextIr[],
|
|
99
|
+
): boolean {
|
|
100
|
+
if (left.length !== right.length) return false;
|
|
101
|
+
for (let index = 0; index < left.length; index++) {
|
|
102
|
+
const a = left[index] as AtRuleContextIr;
|
|
103
|
+
const b = right[index] as AtRuleContextIr;
|
|
104
|
+
if (a.name !== b.name || a.prelude !== b.prelude) return false;
|
|
105
|
+
if (a.span.start !== b.span.start || a.span.end !== b.span.end) return false;
|
|
106
|
+
if (a.span.file !== b.span.file) return false;
|
|
107
|
+
}
|
|
108
|
+
return true;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** Structural equality of two ancestor selector chains. */
|
|
112
|
+
export function sameAncestors(left: readonly string[], right: readonly string[]): boolean {
|
|
113
|
+
if (left.length !== right.length) return false;
|
|
114
|
+
for (let index = 0; index < left.length; index++) {
|
|
115
|
+
if (left[index] !== right[index]) return false;
|
|
116
|
+
}
|
|
117
|
+
return true;
|
|
118
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Class naming and export generation (P4, SPECIFICATION.md §7.2 / §8).
|
|
3
|
+
*
|
|
4
|
+
* Two pure passes complete the naming story of the pipeline:
|
|
5
|
+
*
|
|
6
|
+
* 1. {@link renameClasses} — walks the lowered IR after {@link substitute},
|
|
7
|
+
* rebuilds every rule selector with the naming table applied (prefix strategy,
|
|
8
|
+
* `:global(...)` escape), and returns the first-appearance class table.
|
|
9
|
+
* 2. `generateClassMap` — turns that table into an ESM module of camelCase
|
|
10
|
+
* `export const` bindings plus matching opt-in TypeScript declarations, with
|
|
11
|
+
* deterministic `_`/`_N` identifier adjustments and CHIKX4001 warnings.
|
|
12
|
+
* 3. {@link mergeClassTables} — unions the renamed tables of a rule-bearing
|
|
13
|
+
* (`styles`) module graph into the single table the importing build exports
|
|
14
|
+
* (ADR-0014), enforcing the strict same-name/different-emitted conflict.
|
|
15
|
+
*
|
|
16
|
+
* Canonical pipeline position for consumers:
|
|
17
|
+
*
|
|
18
|
+
* ```text
|
|
19
|
+
* parse → buildModuleGraph → (per module) lower → substitute → renameClasses
|
|
20
|
+
* → mergeClassTables → emitRuntimeTokens → groupRuns → flattenIr → emitCss
|
|
21
|
+
* (→ generateClassMap for the JS map and opt-in .d.ts)
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* `renameClasses` only rewrites selectors, so it is independent of the single
|
|
25
|
+
* merged-build runtime emission; the class table is identical either way.
|
|
26
|
+
*
|
|
27
|
+
* Every pass is deterministic, frozen-output and filesystem-free; they never
|
|
28
|
+
* mutate their inputs and never throw on malformed IR. Barrel wiring is
|
|
29
|
+
* lead-directed; import this facade until integration.
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
export { renameClasses } from "./rewrite.js";
|
|
33
|
+
export { generateClassMap } from "./mapgen.js";
|
|
34
|
+
export type { ClassMapOptions } from "./mapgen.js";
|
|
35
|
+
export { mergeClassTables } from "./merge.js";
|
|
36
|
+
export type {
|
|
37
|
+
ClassEntry,
|
|
38
|
+
ClassMapResult,
|
|
39
|
+
ClassNamingOptions,
|
|
40
|
+
ClassTable,
|
|
41
|
+
RenameOptions,
|
|
42
|
+
RenameResult,
|
|
43
|
+
} from "./types.js";
|
|
44
|
+
export type { ClassTableSource, MergedClassTable } from "./merge.js";
|
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
import type { Diagnostic } from "../types/diagnostics.js";
|
|
2
|
+
import { warning } from "../types/diagnostics.js";
|
|
3
|
+
import type { ClassEntry, ClassMapResult, ClassTable } from "./types.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* JavaScript class-map and TypeScript declaration generation
|
|
7
|
+
* (SPECIFICATION.md §8, ADR-0010, CSSX-A52).
|
|
8
|
+
*
|
|
9
|
+
* Output contract, fixed here and covered by goldens:
|
|
10
|
+
*
|
|
11
|
+
* - **Named exports only.** The map is one `export const <identifier> = "<emitted>";`
|
|
12
|
+
* line per local class, in the class table's first-appearance order. There is
|
|
13
|
+
* no default export and no object literal: the map's single purpose is to
|
|
14
|
+
* provide the DOM `class` value as an importable JavaScript binding, so
|
|
15
|
+
* consumers write `import { contactPanel } from "./x.map.js"` and
|
|
16
|
+
* `class={contactPanel}`. Export order is deterministic.
|
|
17
|
+
* - **Identifier shape.** Each class name is split on `-` and `_` and camelCased
|
|
18
|
+
* (`card-header` → `cardHeader`, `panel_head` → `panelHead`); leading and
|
|
19
|
+
* trailing `_` runs are kept (`__proto__` → `__proto__`). Three adjustments
|
|
20
|
+
* keep every class reachable, because there is no object fallback:
|
|
21
|
+
* - characters outside the JavaScript identifier set become `_`
|
|
22
|
+
* (`foo.bar` → `foo_bar`);
|
|
23
|
+
* - an alias that would start with a digit, or that sanitizes to nothing, is
|
|
24
|
+
* prefixed with `_` (`2col` → `_2col`, `-` → `_`);
|
|
25
|
+
* - a reserved word is prefixed with `_` (`default` → `_default`).
|
|
26
|
+
* - **Collisions are resolved, not dropped.** If two classes resolve to the same
|
|
27
|
+
* identifier, the first-authored class keeps it and later ones get `_2`, `_3`
|
|
28
|
+
* (… first free suffix) in first-appearance order. Suffixes are allocated
|
|
29
|
+
* against the identifiers already emitted, so if an earlier export already
|
|
30
|
+
* uses `fooBar_2`, the next collision becomes `fooBar_3`.
|
|
31
|
+
* - **Diagnostics.** Each adjusted identifier (shape or collision) reports one
|
|
32
|
+
* CHIKX4001 **warning** naming the authored class and the exported binding.
|
|
33
|
+
* Warnings never fail a build; the output is already complete and unambiguous.
|
|
34
|
+
* - **Declarations mirror the JS exactly** when enabled: one
|
|
35
|
+
* `export declare const <identifier>: "<emitted>";` line per class in the same
|
|
36
|
+
* order, with literal string types and no `any`. The declarations are opt-in
|
|
37
|
+
* (`map-types` / `emit-types`, default off) and never contain a `map` object or
|
|
38
|
+
* a default export.
|
|
39
|
+
* - `__proto__` needs no special handling: as a `const` binding it is an ordinary
|
|
40
|
+
* identifier and there is no object literal to poison.
|
|
41
|
+
* - Global classes never reach either file (§8).
|
|
42
|
+
*
|
|
43
|
+
* Formatting is fixed so generated files are stable: one line per binding and
|
|
44
|
+
* one trailing newline. By default the map has no CSS side-effect import;
|
|
45
|
+
* loading the stylesheet is the consumer's responsibility (`<link>` or an
|
|
46
|
+
* explicit `import "./x.css"`). A consumer that wants the map to carry the
|
|
47
|
+
* stylesheet through its bundler opts in with `import-css: true` /
|
|
48
|
+
* `output.importCss` (ADR-0021): exactly one `import "<specifier>";` line is
|
|
49
|
+
* prepended and `.d.ts` declarations stay pure. An empty table yields an empty
|
|
50
|
+
* file unless that import was requested.
|
|
51
|
+
*/
|
|
52
|
+
|
|
53
|
+
/** Options for {@link generateClassMap}. */
|
|
54
|
+
export interface ClassMapOptions {
|
|
55
|
+
/**
|
|
56
|
+
* Relative specifier of the emitted stylesheet (ADR-0021). Present only when
|
|
57
|
+
* `import-css` is enabled and the build emits CSS; the caller computes it
|
|
58
|
+
* from the planned artifacts with {@link stylesheetImportSpecifier}.
|
|
59
|
+
*/
|
|
60
|
+
readonly cssImport?: string;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** `true` when `name` can be declared as a bare JavaScript binding. */
|
|
64
|
+
function isValidJsIdentifier(name: string): boolean {
|
|
65
|
+
return /^[$_\p{ID_Start}][$_\p{ID_Continue}]*$/u.test(name);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Naive camelCase alias before identifier adjustments.
|
|
70
|
+
*
|
|
71
|
+
* Leading and trailing `_` runs are preserved (they carry meaning for names such
|
|
72
|
+
* as `__proto__`), while interior `-`/`_` runs are separators and the following
|
|
73
|
+
* letter is capitalized. A name made only of separators keeps its underscores.
|
|
74
|
+
*/
|
|
75
|
+
function camelCaseAlias(name: string): string {
|
|
76
|
+
const leading = /^_+/u.exec(name)?.[0] ?? "";
|
|
77
|
+
const rest = name.slice(leading.length);
|
|
78
|
+
const trailing = /_+$/u.exec(rest)?.[0] ?? "";
|
|
79
|
+
const core = rest.slice(0, rest.length - trailing.length);
|
|
80
|
+
const parts = core.split(/[-_]+/u).filter((part) => part.length > 0);
|
|
81
|
+
const first = parts[0] ?? "";
|
|
82
|
+
let alias = first;
|
|
83
|
+
for (let index = 1; index < parts.length; index += 1) {
|
|
84
|
+
const part = parts[index] ?? "";
|
|
85
|
+
alias += part.charAt(0).toUpperCase() + part.slice(1);
|
|
86
|
+
}
|
|
87
|
+
return `${leading}${alias}${trailing}`;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Words that cannot be a named export binding (or are unsafe as one under ESM
|
|
92
|
+
* strict mode). Kept explicit so the policy is auditable rather than depending
|
|
93
|
+
* on engine behavior.
|
|
94
|
+
*/
|
|
95
|
+
const RESERVED_IDENTIFIERS: ReadonlySet<string> = new Set([
|
|
96
|
+
"arguments",
|
|
97
|
+
"await",
|
|
98
|
+
"break",
|
|
99
|
+
"case",
|
|
100
|
+
"catch",
|
|
101
|
+
"class",
|
|
102
|
+
"const",
|
|
103
|
+
"continue",
|
|
104
|
+
"debugger",
|
|
105
|
+
"default",
|
|
106
|
+
"delete",
|
|
107
|
+
"do",
|
|
108
|
+
"else",
|
|
109
|
+
"enum",
|
|
110
|
+
"eval",
|
|
111
|
+
"export",
|
|
112
|
+
"extends",
|
|
113
|
+
"false",
|
|
114
|
+
"finally",
|
|
115
|
+
"for",
|
|
116
|
+
"function",
|
|
117
|
+
"if",
|
|
118
|
+
"implements",
|
|
119
|
+
"import",
|
|
120
|
+
"in",
|
|
121
|
+
"instanceof",
|
|
122
|
+
"interface",
|
|
123
|
+
"let",
|
|
124
|
+
"new",
|
|
125
|
+
"null",
|
|
126
|
+
"package",
|
|
127
|
+
"private",
|
|
128
|
+
"protected",
|
|
129
|
+
"public",
|
|
130
|
+
"return",
|
|
131
|
+
"static",
|
|
132
|
+
"super",
|
|
133
|
+
"switch",
|
|
134
|
+
"this",
|
|
135
|
+
"throw",
|
|
136
|
+
"true",
|
|
137
|
+
"try",
|
|
138
|
+
"typeof",
|
|
139
|
+
"var",
|
|
140
|
+
"void",
|
|
141
|
+
"while",
|
|
142
|
+
"with",
|
|
143
|
+
"yield",
|
|
144
|
+
]);
|
|
145
|
+
|
|
146
|
+
/** One exportable class plus its final, unique binding identifier. */
|
|
147
|
+
interface PreparedEntry {
|
|
148
|
+
readonly entry: ClassEntry;
|
|
149
|
+
readonly identifier: string;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** Quote a string as a JS/TS double-quoted literal. */
|
|
153
|
+
function quote(text: string): string {
|
|
154
|
+
return JSON.stringify(text);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** Base identifier for one class, before uniqueness suffixing. */
|
|
158
|
+
function baseIdentifier(name: string): {
|
|
159
|
+
readonly alias: string;
|
|
160
|
+
readonly base: string;
|
|
161
|
+
readonly adjusted: boolean;
|
|
162
|
+
} {
|
|
163
|
+
const alias = camelCaseAlias(name);
|
|
164
|
+
let base = alias.replace(/[^\p{ID_Continue}$]/gu, "_");
|
|
165
|
+
let adjusted = base !== alias;
|
|
166
|
+
if (base === "") {
|
|
167
|
+
return { alias, base: "_", adjusted: true };
|
|
168
|
+
}
|
|
169
|
+
if (!isValidJsIdentifier(base) || RESERVED_IDENTIFIERS.has(base)) {
|
|
170
|
+
base = `_${base}`;
|
|
171
|
+
adjusted = true;
|
|
172
|
+
}
|
|
173
|
+
return { alias, base, adjusted };
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** Prepare export identifiers and collect CHIKX4001 warnings. */
|
|
177
|
+
function prepare(table: ClassTable, diagnostics: Diagnostic[]): readonly PreparedEntry[] {
|
|
178
|
+
const exportable = table.entries.filter((entry) => !entry.global);
|
|
179
|
+
const owners = new Map<string, string>();
|
|
180
|
+
const prepared: PreparedEntry[] = [];
|
|
181
|
+
|
|
182
|
+
for (const entry of exportable) {
|
|
183
|
+
const { alias, base, adjusted } = baseIdentifier(entry.name);
|
|
184
|
+
let identifier = base;
|
|
185
|
+
let suffix = 1;
|
|
186
|
+
while (owners.has(identifier)) {
|
|
187
|
+
suffix += 1;
|
|
188
|
+
identifier = `${base}_${suffix}`;
|
|
189
|
+
}
|
|
190
|
+
const owner = owners.get(base);
|
|
191
|
+
owners.set(identifier, entry.name);
|
|
192
|
+
|
|
193
|
+
if (owner === undefined && adjusted) {
|
|
194
|
+
diagnostics.push(
|
|
195
|
+
warning(
|
|
196
|
+
"CHIKX4001",
|
|
197
|
+
`Class "${entry.name}" maps to "${alias}", which is not a usable JavaScript export binding (reserved word or invalid identifier); it is exported as "${identifier}" (SPECIFICATION.md §8).`,
|
|
198
|
+
entry.span,
|
|
199
|
+
),
|
|
200
|
+
);
|
|
201
|
+
} else if (owner !== undefined) {
|
|
202
|
+
diagnostics.push(
|
|
203
|
+
warning(
|
|
204
|
+
"CHIKX4001",
|
|
205
|
+
`Class "${entry.name}" maps to the camelCase identifier "${alias}", which class "${owner}" already exports; it is exported as "${identifier}" (SPECIFICATION.md §8).`,
|
|
206
|
+
entry.span,
|
|
207
|
+
),
|
|
208
|
+
);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
prepared.push({ entry, identifier });
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
return prepared;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Generate the ESM class map and matching `.d.ts` text for one class table.
|
|
219
|
+
*
|
|
220
|
+
* Pure and deterministic: the same table and options always produce
|
|
221
|
+
* byte-identical text. Global classes are excluded; reserved identifiers and
|
|
222
|
+
* digit-leading names are prefixed with `_` and identifier collisions get a
|
|
223
|
+
* numeric suffix, each with one CHIKX4001 warning.
|
|
224
|
+
*
|
|
225
|
+
* @param classes Class table produced by `renameClasses`.
|
|
226
|
+
* @param options Optional stylesheet import (ADR-0021).
|
|
227
|
+
*/
|
|
228
|
+
export function generateClassMap(
|
|
229
|
+
classes: ClassTable,
|
|
230
|
+
options: ClassMapOptions = {},
|
|
231
|
+
): ClassMapResult {
|
|
232
|
+
const diagnostics: Diagnostic[] = [];
|
|
233
|
+
const prepared = prepare(classes, diagnostics);
|
|
234
|
+
|
|
235
|
+
const jsLines: string[] = [];
|
|
236
|
+
if (options.cssImport !== undefined && options.cssImport !== "") {
|
|
237
|
+
jsLines.push(`import ${quote(options.cssImport)};`);
|
|
238
|
+
}
|
|
239
|
+
for (const entry of prepared) {
|
|
240
|
+
jsLines.push(`export const ${entry.identifier} = ${quote(entry.entry.emitted)};`);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
const typeLines = prepared.map(
|
|
244
|
+
(entry) => `export declare const ${entry.identifier}: ${quote(entry.entry.emitted)};`,
|
|
245
|
+
);
|
|
246
|
+
|
|
247
|
+
return {
|
|
248
|
+
js: jsLines.length === 0 ? "" : `${jsLines.join("\n")}\n`,
|
|
249
|
+
types: typeLines.length === 0 ? "" : `${typeLines.join("\n")}\n`,
|
|
250
|
+
diagnostics,
|
|
251
|
+
};
|
|
252
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Merged class tables for rule-bearing (`styles`) imports (ADR-0014).
|
|
3
|
+
*
|
|
4
|
+
* A merged build has ONE class map and ONE `.d.ts` for the importing file,
|
|
5
|
+
* covering the entry's own classes plus every class contributed by
|
|
6
|
+
* style-imported modules. Each module was renamed with its OWN `@setup` prefix
|
|
7
|
+
* before merging, so the merge is a pure table union, not a re-naming pass.
|
|
8
|
+
*
|
|
9
|
+
* Policy (owner decision of 2026-11-25, strict):
|
|
10
|
+
*
|
|
11
|
+
* - The same original class name resolving to the same emitted name in several
|
|
12
|
+
* modules is allowed and unifies into one entry at its first position.
|
|
13
|
+
* - The same original class name resolving to different emitted names is
|
|
14
|
+
* CHIKX4001: a single map key cannot have two values, and silently picking one
|
|
15
|
+
* would make `className` usage wrong in one of the modules. The first
|
|
16
|
+
* occurrence wins for deterministic recovery after the error.
|
|
17
|
+
* - A `:global(...)` reference and a local class of the same name are not a
|
|
18
|
+
* conflict: the local definition upgrades the entry in place exactly as the
|
|
19
|
+
* single-module rewrite does, and globals still never reach the map.
|
|
20
|
+
* - camelCase named-export alias uniqueness across the merged table is enforced
|
|
21
|
+
* afterwards by `generateClassMap`, which sees the merged table as one table.
|
|
22
|
+
* - A module that authors `@setup { map-inherit: false; }` is skipped entirely:
|
|
23
|
+
* its CSS still merges in cascade order, but none of its bindings reach the
|
|
24
|
+
* map or `.d.ts`, and it can never raise CHIKX4001 (ADR-0020, the documented
|
|
25
|
+
* exception to the §3 one-table lockstep).
|
|
26
|
+
*
|
|
27
|
+
* The pass is pure, deterministic and never mutates its inputs.
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
import type { Diagnostic } from "../types/diagnostics.js";
|
|
31
|
+
import { error } from "../types/diagnostics.js";
|
|
32
|
+
import type { ClassEntry, ClassTable } from "./types.js";
|
|
33
|
+
|
|
34
|
+
/** One renamed module table plus an optional human label for diagnostics. */
|
|
35
|
+
export interface ClassTableSource {
|
|
36
|
+
/** Table produced by `renameClasses` for one module (or the entry). */
|
|
37
|
+
readonly table: ClassTable;
|
|
38
|
+
/**
|
|
39
|
+
* Module identity used in conflict messages, normally the workspace-relative
|
|
40
|
+
* POSIX source path. Omitted for synthetic tables.
|
|
41
|
+
*/
|
|
42
|
+
readonly label?: string;
|
|
43
|
+
/**
|
|
44
|
+
* Whether this table's bindings join the merged build (ADR-0020). Omitted
|
|
45
|
+
* means `true` (the ADR-0014 default); `false` excludes a style-merged
|
|
46
|
+
* module's bindings while leaving its CSS in the imported-first IR.
|
|
47
|
+
*/
|
|
48
|
+
readonly mapInherit?: boolean;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Result of {@link mergeClassTables}. */
|
|
52
|
+
export interface MergedClassTable {
|
|
53
|
+
/** Union table in first-appearance order: style modules first, then the entry. */
|
|
54
|
+
readonly table: ClassTable;
|
|
55
|
+
/** CHIKX4001 diagnostics for same-name/different-emitted conflicts. */
|
|
56
|
+
readonly diagnostics: readonly Diagnostic[];
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Merge renamed class tables into the one table a merged build exports.
|
|
61
|
+
*
|
|
62
|
+
* @param sources Renamed tables in emission order (style-imported modules
|
|
63
|
+
* depth-first, then the importing entry).
|
|
64
|
+
* @returns The union table plus any CHIKX4001 conflicts.
|
|
65
|
+
*/
|
|
66
|
+
export function mergeClassTables(sources: readonly ClassTableSource[]): MergedClassTable {
|
|
67
|
+
const entries: ClassEntry[] = [];
|
|
68
|
+
const indexes = new Map<string, number>();
|
|
69
|
+
const owners = new Map<string, string>();
|
|
70
|
+
const diagnostics: Diagnostic[] = [];
|
|
71
|
+
|
|
72
|
+
for (const source of sources) {
|
|
73
|
+
// An opted-out module (ADR-0020) contributes rules to the stylesheet but no
|
|
74
|
+
// bindings to the merged table, so it cannot collide either.
|
|
75
|
+
if (source.mapInherit === false) continue;
|
|
76
|
+
const label = source.label ?? "an unnamed source";
|
|
77
|
+
for (const entry of source.table.entries) {
|
|
78
|
+
const existingIndex = indexes.get(entry.name);
|
|
79
|
+
if (existingIndex === undefined) {
|
|
80
|
+
indexes.set(entry.name, entries.length);
|
|
81
|
+
owners.set(entry.name, label);
|
|
82
|
+
entries.push(entry);
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const existing = entries[existingIndex];
|
|
87
|
+
if (existing === undefined) {
|
|
88
|
+
throw new Error("merged class table index out of range");
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (existing.global && !entry.global) {
|
|
92
|
+
// A local definition replaces a global-only reference in place: the
|
|
93
|
+
// class is exported once, from its first position (ADR-0010).
|
|
94
|
+
entries[existingIndex] = entry;
|
|
95
|
+
owners.set(entry.name, label);
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
if (entry.global || existing.emitted === entry.emitted) continue;
|
|
99
|
+
|
|
100
|
+
diagnostics.push(
|
|
101
|
+
error(
|
|
102
|
+
"CHIKX4001",
|
|
103
|
+
`Class "${entry.name}" is emitted as both "${existing.emitted}" (${owners.get(entry.name) ?? "an unnamed source"}) and "${entry.emitted}" (${label}); a merged build has one class map, so the first name wins (ADR-0014).`,
|
|
104
|
+
entry.span,
|
|
105
|
+
),
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return {
|
|
111
|
+
table: Object.freeze({ entries: Object.freeze(entries) }),
|
|
112
|
+
diagnostics,
|
|
113
|
+
};
|
|
114
|
+
}
|