@chikx/compiler 0.0.0-stage → 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/LICENSE +21 -0
- package/README.md +40 -2
- package/dist/emitter/index.d.ts +96 -0
- package/dist/emitter/index.d.ts.map +1 -0
- package/dist/emitter/index.js +415 -0
- package/dist/emitter/index.js.map +1 -0
- package/dist/formatter/index.d.ts +53 -0
- package/dist/formatter/index.d.ts.map +1 -0
- package/dist/formatter/index.js +464 -0
- package/dist/formatter/index.js.map +1 -0
- package/dist/host/node.d.ts +34 -0
- package/dist/host/node.d.ts.map +1 -0
- package/dist/host/node.js +80 -0
- package/dist/host/node.js.map +1 -0
- package/dist/index.d.ts +93 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +792 -0
- package/dist/index.js.map +1 -0
- package/dist/ir/factories.d.ts +35 -0
- package/dist/ir/factories.d.ts.map +1 -0
- package/dist/ir/factories.js +47 -0
- package/dist/ir/factories.js.map +1 -0
- package/dist/ir/index.d.ts +13 -0
- package/dist/ir/index.d.ts.map +1 -0
- package/dist/ir/index.js +2 -0
- package/dist/ir/index.js.map +1 -0
- package/dist/ir/types.d.ts +139 -0
- package/dist/ir/types.d.ts.map +1 -0
- package/dist/ir/types.js +2 -0
- package/dist/ir/types.js.map +1 -0
- package/dist/lexer/at-keywords.d.ts +27 -0
- package/dist/lexer/at-keywords.d.ts.map +1 -0
- package/dist/lexer/at-keywords.js +35 -0
- package/dist/lexer/at-keywords.js.map +1 -0
- package/dist/lexer/characters.d.ts +38 -0
- package/dist/lexer/characters.d.ts.map +1 -0
- package/dist/lexer/characters.js +74 -0
- package/dist/lexer/characters.js.map +1 -0
- package/dist/lexer/comments.d.ts +22 -0
- package/dist/lexer/comments.d.ts.map +1 -0
- package/dist/lexer/comments.js +20 -0
- package/dist/lexer/comments.js.map +1 -0
- package/dist/lexer/diagnostics.d.ts +24 -0
- package/dist/lexer/diagnostics.d.ts.map +1 -0
- package/dist/lexer/diagnostics.js +29 -0
- package/dist/lexer/diagnostics.js.map +1 -0
- package/dist/lexer/dollar-refs.d.ts +13 -0
- package/dist/lexer/dollar-refs.d.ts.map +1 -0
- package/dist/lexer/dollar-refs.js +22 -0
- package/dist/lexer/dollar-refs.js.map +1 -0
- package/dist/lexer/index.d.ts +32 -0
- package/dist/lexer/index.d.ts.map +1 -0
- package/dist/lexer/index.js +26 -0
- package/dist/lexer/index.js.map +1 -0
- package/dist/lexer/names.d.ts +23 -0
- package/dist/lexer/names.d.ts.map +1 -0
- package/dist/lexer/names.js +48 -0
- package/dist/lexer/names.js.map +1 -0
- package/dist/lexer/numbers.d.ts +22 -0
- package/dist/lexer/numbers.d.ts.map +1 -0
- package/dist/lexer/numbers.js +60 -0
- package/dist/lexer/numbers.js.map +1 -0
- package/dist/lexer/scanner.d.ts +30 -0
- package/dist/lexer/scanner.d.ts.map +1 -0
- package/dist/lexer/scanner.js +158 -0
- package/dist/lexer/scanner.js.map +1 -0
- package/dist/lexer/strings.d.ts +25 -0
- package/dist/lexer/strings.d.ts.map +1 -0
- package/dist/lexer/strings.js +41 -0
- package/dist/lexer/strings.js.map +1 -0
- package/dist/lexer/urls.d.ts +28 -0
- package/dist/lexer/urls.d.ts.map +1 -0
- package/dist/lexer/urls.js +54 -0
- package/dist/lexer/urls.js.map +1 -0
- package/dist/lowering/expand-groups.d.ts +61 -0
- package/dist/lowering/expand-groups.d.ts.map +1 -0
- package/dist/lowering/expand-groups.js +117 -0
- package/dist/lowering/expand-groups.js.map +1 -0
- package/dist/lowering/group.d.ts +30 -0
- package/dist/lowering/group.d.ts.map +1 -0
- package/dist/lowering/group.js +109 -0
- package/dist/lowering/group.js.map +1 -0
- package/dist/lowering/index.d.ts +26 -0
- package/dist/lowering/index.d.ts.map +1 -0
- package/dist/lowering/index.js +23 -0
- package/dist/lowering/index.js.map +1 -0
- package/dist/lowering/lower.d.ts +21 -0
- package/dist/lowering/lower.d.ts.map +1 -0
- package/dist/lowering/lower.js +296 -0
- package/dist/lowering/lower.js.map +1 -0
- package/dist/lowering/media.d.ts +63 -0
- package/dist/lowering/media.d.ts.map +1 -0
- package/dist/lowering/media.js +237 -0
- package/dist/lowering/media.js.map +1 -0
- package/dist/lowering/merge.d.ts +35 -0
- package/dist/lowering/merge.d.ts.map +1 -0
- package/dist/lowering/merge.js +146 -0
- package/dist/lowering/merge.js.map +1 -0
- package/dist/lowering/property-families.d.ts +50 -0
- package/dist/lowering/property-families.d.ts.map +1 -0
- package/dist/lowering/property-families.js +175 -0
- package/dist/lowering/property-families.js.map +1 -0
- package/dist/lowering/spans.d.ts +36 -0
- package/dist/lowering/spans.d.ts.map +1 -0
- package/dist/lowering/spans.js +100 -0
- package/dist/lowering/spans.js.map +1 -0
- package/dist/naming/index.d.ts +37 -0
- package/dist/naming/index.d.ts.map +1 -0
- package/dist/naming/index.js +34 -0
- package/dist/naming/index.js.map +1 -0
- package/dist/naming/mapgen.d.ts +70 -0
- package/dist/naming/mapgen.d.ts.map +1 -0
- package/dist/naming/mapgen.js +153 -0
- package/dist/naming/mapgen.js.map +1 -0
- package/dist/naming/merge.d.ts +62 -0
- package/dist/naming/merge.d.ts.map +1 -0
- package/dist/naming/merge.js +77 -0
- package/dist/naming/merge.js.map +1 -0
- package/dist/naming/rewrite.d.ts +14 -0
- package/dist/naming/rewrite.d.ts.map +1 -0
- package/dist/naming/rewrite.js +191 -0
- package/dist/naming/rewrite.js.map +1 -0
- package/dist/naming/scan.d.ts +80 -0
- package/dist/naming/scan.d.ts.map +1 -0
- package/dist/naming/scan.js +193 -0
- package/dist/naming/scan.js.map +1 -0
- package/dist/naming/table.d.ts +68 -0
- package/dist/naming/table.d.ts.map +1 -0
- package/dist/naming/table.js +132 -0
- package/dist/naming/table.js.map +1 -0
- package/dist/naming/types.d.ts +94 -0
- package/dist/naming/types.d.ts.map +1 -0
- package/dist/naming/types.js +2 -0
- package/dist/naming/types.js.map +1 -0
- package/dist/output/import-path.d.ts +24 -0
- package/dist/output/import-path.d.ts.map +1 -0
- package/dist/output/import-path.js +30 -0
- package/dist/output/import-path.js.map +1 -0
- package/dist/output/index.d.ts +40 -0
- package/dist/output/index.d.ts.map +1 -0
- package/dist/output/index.js +138 -0
- package/dist/output/index.js.map +1 -0
- package/dist/output/manifest.d.ts +17 -0
- package/dist/output/manifest.d.ts.map +1 -0
- package/dist/output/manifest.js +22 -0
- package/dist/output/manifest.js.map +1 -0
- package/dist/output/paths.d.ts +54 -0
- package/dist/output/paths.d.ts.map +1 -0
- package/dist/output/paths.js +183 -0
- package/dist/output/paths.js.map +1 -0
- package/dist/output/resolve.d.ts +100 -0
- package/dist/output/resolve.d.ts.map +1 -0
- package/dist/output/resolve.js +313 -0
- package/dist/output/resolve.js.map +1 -0
- package/dist/output/validate.d.ts +82 -0
- package/dist/output/validate.d.ts.map +1 -0
- package/dist/output/validate.js +128 -0
- package/dist/output/validate.js.map +1 -0
- package/dist/parser/cursor.d.ts +55 -0
- package/dist/parser/cursor.d.ts.map +1 -0
- package/dist/parser/cursor.js +120 -0
- package/dist/parser/cursor.js.map +1 -0
- package/dist/parser/declaration.d.ts +21 -0
- package/dist/parser/declaration.d.ts.map +1 -0
- package/dist/parser/declaration.js +201 -0
- package/dist/parser/declaration.js.map +1 -0
- package/dist/parser/diagnostics.d.ts +52 -0
- package/dist/parser/diagnostics.d.ts.map +1 -0
- package/dist/parser/diagnostics.js +55 -0
- package/dist/parser/diagnostics.js.map +1 -0
- package/dist/parser/group.d.ts +26 -0
- package/dist/parser/group.d.ts.map +1 -0
- package/dist/parser/group.js +219 -0
- package/dist/parser/group.js.map +1 -0
- package/dist/parser/index.d.ts +30 -0
- package/dist/parser/index.d.ts.map +1 -0
- package/dist/parser/index.js +48 -0
- package/dist/parser/index.js.map +1 -0
- package/dist/parser/modifier.d.ts +25 -0
- package/dist/parser/modifier.d.ts.map +1 -0
- package/dist/parser/modifier.js +41 -0
- package/dist/parser/modifier.js.map +1 -0
- package/dist/parser/recovery.d.ts +23 -0
- package/dist/parser/recovery.d.ts.map +1 -0
- package/dist/parser/recovery.js +80 -0
- package/dist/parser/recovery.js.map +1 -0
- package/dist/parser/rule.d.ts +39 -0
- package/dist/parser/rule.d.ts.map +1 -0
- package/dist/parser/rule.js +337 -0
- package/dist/parser/rule.js.map +1 -0
- package/dist/parser/statement.d.ts +21 -0
- package/dist/parser/statement.d.ts.map +1 -0
- package/dist/parser/statement.js +48 -0
- package/dist/parser/statement.js.map +1 -0
- package/dist/parser/statements.d.ts +33 -0
- package/dist/parser/statements.d.ts.map +1 -0
- package/dist/parser/statements.js +605 -0
- package/dist/parser/statements.js.map +1 -0
- package/dist/parser/value.d.ts +57 -0
- package/dist/parser/value.d.ts.map +1 -0
- package/dist/parser/value.js +226 -0
- package/dist/parser/value.js.map +1 -0
- package/dist/resolver/graph.d.ts +20 -0
- package/dist/resolver/graph.d.ts.map +1 -0
- package/dist/resolver/graph.js +382 -0
- package/dist/resolver/graph.js.map +1 -0
- package/dist/resolver/groups.d.ts +70 -0
- package/dist/resolver/groups.d.ts.map +1 -0
- package/dist/resolver/groups.js +143 -0
- package/dist/resolver/groups.js.map +1 -0
- package/dist/resolver/index.d.ts +46 -0
- package/dist/resolver/index.d.ts.map +1 -0
- package/dist/resolver/index.js +46 -0
- package/dist/resolver/index.js.map +1 -0
- package/dist/resolver/keyframes.d.ts +56 -0
- package/dist/resolver/keyframes.d.ts.map +1 -0
- package/dist/resolver/keyframes.js +102 -0
- package/dist/resolver/keyframes.js.map +1 -0
- package/dist/resolver/paths.d.ts +37 -0
- package/dist/resolver/paths.d.ts.map +1 -0
- package/dist/resolver/paths.js +135 -0
- package/dist/resolver/paths.js.map +1 -0
- package/dist/resolver/rules.d.ts +34 -0
- package/dist/resolver/rules.d.ts.map +1 -0
- package/dist/resolver/rules.js +52 -0
- package/dist/resolver/rules.js.map +1 -0
- package/dist/resolver/scopes.d.ts +44 -0
- package/dist/resolver/scopes.d.ts.map +1 -0
- package/dist/resolver/scopes.js +213 -0
- package/dist/resolver/scopes.js.map +1 -0
- package/dist/resolver/types.d.ts +238 -0
- package/dist/resolver/types.d.ts.map +1 -0
- package/dist/resolver/types.js +11 -0
- package/dist/resolver/types.js.map +1 -0
- package/dist/selectors/flatten.d.ts +95 -0
- package/dist/selectors/flatten.d.ts.map +1 -0
- package/dist/selectors/flatten.js +384 -0
- package/dist/selectors/flatten.js.map +1 -0
- package/dist/selectors/index.d.ts +15 -0
- package/dist/selectors/index.d.ts.map +1 -0
- package/dist/selectors/index.js +14 -0
- package/dist/selectors/index.js.map +1 -0
- package/dist/selectors/scan.d.ts +54 -0
- package/dist/selectors/scan.d.ts.map +1 -0
- package/dist/selectors/scan.js +191 -0
- package/dist/selectors/scan.js.map +1 -0
- package/dist/selectors/specificity.d.ts +28 -0
- package/dist/selectors/specificity.d.ts.map +1 -0
- package/dist/selectors/specificity.js +297 -0
- package/dist/selectors/specificity.js.map +1 -0
- package/dist/setup-list.d.ts +41 -0
- package/dist/setup-list.d.ts.map +1 -0
- package/dist/setup-list.js +122 -0
- package/dist/setup-list.js.map +1 -0
- package/dist/sourcemaps/index.d.ts +14 -0
- package/dist/sourcemaps/index.d.ts.map +1 -0
- package/dist/sourcemaps/index.js +13 -0
- package/dist/sourcemaps/index.js.map +1 -0
- package/dist/sourcemaps/mappings.d.ts +77 -0
- package/dist/sourcemaps/mappings.d.ts.map +1 -0
- package/dist/sourcemaps/mappings.js +124 -0
- package/dist/sourcemaps/mappings.js.map +1 -0
- package/dist/sourcemaps/vlq.d.ts +12 -0
- package/dist/sourcemaps/vlq.d.ts.map +1 -0
- package/dist/sourcemaps/vlq.js +29 -0
- package/dist/sourcemaps/vlq.js.map +1 -0
- package/dist/types/ast.d.ts +291 -0
- package/dist/types/ast.d.ts.map +1 -0
- package/dist/types/ast.js +2 -0
- package/dist/types/ast.js.map +1 -0
- package/dist/types/diagnostics.d.ts +55 -0
- package/dist/types/diagnostics.d.ts.map +1 -0
- package/dist/types/diagnostics.js +61 -0
- package/dist/types/diagnostics.js.map +1 -0
- package/dist/types/options.d.ts +274 -0
- package/dist/types/options.d.ts.map +1 -0
- package/dist/types/options.js +11 -0
- package/dist/types/options.js.map +1 -0
- package/dist/types/result.d.ts +112 -0
- package/dist/types/result.d.ts.map +1 -0
- package/dist/types/result.js +2 -0
- package/dist/types/result.js.map +1 -0
- package/dist/types/span.d.ts +19 -0
- package/dist/types/span.d.ts.map +1 -0
- package/dist/types/span.js +2 -0
- package/dist/types/span.js.map +1 -0
- package/dist/types/token.d.ts +68 -0
- package/dist/types/token.d.ts.map +1 -0
- package/dist/types/token.js +2 -0
- package/dist/types/token.js.map +1 -0
- package/dist/variables/ast.d.ts +56 -0
- package/dist/variables/ast.d.ts.map +1 -0
- package/dist/variables/ast.js +167 -0
- package/dist/variables/ast.js.map +1 -0
- package/dist/variables/expand.d.ts +109 -0
- package/dist/variables/expand.d.ts.map +1 -0
- package/dist/variables/expand.js +213 -0
- package/dist/variables/expand.js.map +1 -0
- package/dist/variables/index.d.ts +36 -0
- package/dist/variables/index.d.ts.map +1 -0
- package/dist/variables/index.js +33 -0
- package/dist/variables/index.js.map +1 -0
- package/dist/variables/naming.d.ts +39 -0
- package/dist/variables/naming.d.ts.map +1 -0
- package/dist/variables/naming.js +14 -0
- package/dist/variables/naming.js.map +1 -0
- package/dist/variables/runtime.d.ts +81 -0
- package/dist/variables/runtime.d.ts.map +1 -0
- package/dist/variables/runtime.js +228 -0
- package/dist/variables/runtime.js.map +1 -0
- package/dist/variables/substitute.d.ts +65 -0
- package/dist/variables/substitute.d.ts.map +1 -0
- package/dist/variables/substitute.js +73 -0
- package/dist/variables/substitute.js.map +1 -0
- package/dist/variables/walk.d.ts +37 -0
- package/dist/variables/walk.d.ts.map +1 -0
- package/dist/variables/walk.js +113 -0
- package/dist/variables/walk.js.map +1 -0
- package/package.json +35 -4
- 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
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Validation helpers for the pure output planner.
|
|
3
|
+
*
|
|
4
|
+
* Everything here operates on already-resolved absolute POSIX paths. No
|
|
5
|
+
* filesystem call is made; an optional host may canonicalize paths so symlink
|
|
6
|
+
* escapes are detected, and callers may supply targets already owned by other
|
|
7
|
+
* entries so multi-entry collisions are rejected before writing.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type { Diagnostic } from "../types/diagnostics.js";
|
|
11
|
+
import { error, warning } from "../types/diagnostics.js";
|
|
12
|
+
import type { OutputKind } from "../types/result.js";
|
|
13
|
+
import type { Span } from "../types/span.js";
|
|
14
|
+
import { isAbsolutePath, isWithinPath, joinPath, normalizePath, relativePath } from "./paths.js";
|
|
15
|
+
|
|
16
|
+
/** Minimal host surface needed by the planner; defaults to lexical identity. */
|
|
17
|
+
export interface OutputPlannerHost {
|
|
18
|
+
/**
|
|
19
|
+
* Return the canonical path used for containment checks, resolving symlinks
|
|
20
|
+
* and platform case rules. Implementations may throw; the planner converts a
|
|
21
|
+
* throw into a CHIKX5001 error instead of leaking the exception.
|
|
22
|
+
*/
|
|
23
|
+
canonicalize(path: string): string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** A resolved artifact before overlap, duplicate and escape validation. */
|
|
27
|
+
export interface OutputCandidate {
|
|
28
|
+
/** Artifact kind, which determines writer and manifest handling. */
|
|
29
|
+
readonly kind: OutputKind;
|
|
30
|
+
/** Absolute POSIX destination. */
|
|
31
|
+
readonly absolutePath: string;
|
|
32
|
+
/** Workspace-relative POSIX destination. */
|
|
33
|
+
readonly workspacePath: string;
|
|
34
|
+
/** Workspace-relative POSIX source path. */
|
|
35
|
+
readonly sourceFile: string;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** One source path in absolute and workspace-relative forms. */
|
|
39
|
+
export interface SourceLocation {
|
|
40
|
+
/** Workspace-relative POSIX source path. */
|
|
41
|
+
readonly relative: string;
|
|
42
|
+
/** Absolute POSIX source path. */
|
|
43
|
+
readonly absolute: string;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Inputs shared by candidate and name validation. */
|
|
47
|
+
export interface ValidationContext {
|
|
48
|
+
/** Absolute POSIX workspace root. */
|
|
49
|
+
readonly workspaceRoot: string;
|
|
50
|
+
/** Normalized source location used for diagnostics and overlap checks. */
|
|
51
|
+
readonly source: SourceLocation;
|
|
52
|
+
/** Diagnostic span for output-planning failures (zero-width at file start). */
|
|
53
|
+
readonly span: Span;
|
|
54
|
+
/** Optional canonicalization host. */
|
|
55
|
+
readonly host?: OutputPlannerHost;
|
|
56
|
+
/** Absolute or workspace-relative targets already planned by other entries. */
|
|
57
|
+
readonly occupiedTargets?: readonly string[];
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Validate a CSS or JS basename.
|
|
62
|
+
*
|
|
63
|
+
* Names are basenames only: separators, `.`/`..`, empty names and the wrong
|
|
64
|
+
* extension are rejected with CHIKX5001. Spaces and other legal filename
|
|
65
|
+
* characters are accepted.
|
|
66
|
+
*/
|
|
67
|
+
export function validateName(name: string, kind: "css" | "js", span: Span): Diagnostic | undefined {
|
|
68
|
+
const extension = kind === "css" ? ".css" : ".js";
|
|
69
|
+
const label = kind === "css" ? "css-name" : "map-name";
|
|
70
|
+
if (name.length === 0) {
|
|
71
|
+
return error("CHIKX5001", `${label} must not be empty.`, span);
|
|
72
|
+
}
|
|
73
|
+
if (name === "." || name === "..") {
|
|
74
|
+
return error("CHIKX5001", `${label} "${name}" is a traversal segment, not a filename.`, span);
|
|
75
|
+
}
|
|
76
|
+
if (name.includes("/") || name.includes("\\")) {
|
|
77
|
+
return error(
|
|
78
|
+
"CHIKX5001",
|
|
79
|
+
`${label} "${name}" must be a basename without path separators.`,
|
|
80
|
+
span,
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
if (name.includes("\0")) {
|
|
84
|
+
return error("CHIKX5001", `${label} contains a NUL byte.`, span);
|
|
85
|
+
}
|
|
86
|
+
if (!name.endsWith(extension)) {
|
|
87
|
+
return error(
|
|
88
|
+
"CHIKX5001",
|
|
89
|
+
`${label} "${name}" must end in ${extension} (extension normalization is not enabled).`,
|
|
90
|
+
span,
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
return undefined;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Find two or more planned artifacts that resolve to the same destination.
|
|
98
|
+
*
|
|
99
|
+
* Extension rules make this unreachable for a single entry today, but the check
|
|
100
|
+
* keeps the planner safe if artifact kinds or naming rules grow.
|
|
101
|
+
*/
|
|
102
|
+
export function findDuplicateTargets(
|
|
103
|
+
candidates: readonly OutputCandidate[],
|
|
104
|
+
span: Span,
|
|
105
|
+
): readonly Diagnostic[] {
|
|
106
|
+
const seen = new Map<string, OutputCandidate>();
|
|
107
|
+
const diagnostics: Diagnostic[] = [];
|
|
108
|
+
for (const candidate of candidates) {
|
|
109
|
+
const key = normalizePath(candidate.absolutePath);
|
|
110
|
+
const previous = seen.get(key);
|
|
111
|
+
if (previous === undefined) {
|
|
112
|
+
seen.set(key, candidate);
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
diagnostics.push(
|
|
116
|
+
error(
|
|
117
|
+
"CHIKX5001",
|
|
118
|
+
`Duplicate output target "${candidate.workspacePath}": ${previous.kind} and ${candidate.kind} plan the same destination.`,
|
|
119
|
+
span,
|
|
120
|
+
),
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
return diagnostics;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Validate one resolved candidate against workspace containment, symlink
|
|
128
|
+
* escapes, source overlap and targets owned by other entries.
|
|
129
|
+
*/
|
|
130
|
+
export function validateCandidate(
|
|
131
|
+
candidate: OutputCandidate,
|
|
132
|
+
context: ValidationContext,
|
|
133
|
+
): readonly Diagnostic[] {
|
|
134
|
+
const diagnostics: Diagnostic[] = [];
|
|
135
|
+
const workspaceRoot = normalizePath(context.workspaceRoot);
|
|
136
|
+
const target = normalizePath(candidate.absolutePath);
|
|
137
|
+
const label = `${candidate.kind} output "${candidate.workspacePath}"`;
|
|
138
|
+
|
|
139
|
+
const workspaceRelative = relativePath(workspaceRoot, target);
|
|
140
|
+
if (
|
|
141
|
+
isAbsolutePath(workspaceRelative) ||
|
|
142
|
+
workspaceRelative === ".." ||
|
|
143
|
+
workspaceRelative.startsWith("../")
|
|
144
|
+
) {
|
|
145
|
+
diagnostics.push(
|
|
146
|
+
error(
|
|
147
|
+
"CHIKX5001",
|
|
148
|
+
`${label} escapes the workspace root "${workspaceRoot}" (resolved to "${target}").`,
|
|
149
|
+
context.span,
|
|
150
|
+
),
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const canonicalRoot = canonicalizeOrThrow(
|
|
155
|
+
workspaceRoot,
|
|
156
|
+
workspaceRoot,
|
|
157
|
+
context.host,
|
|
158
|
+
context.span,
|
|
159
|
+
);
|
|
160
|
+
if (typeof canonicalRoot === "string") {
|
|
161
|
+
const canonicalTarget = canonicalizeOrThrow(target, workspaceRoot, context.host, context.span);
|
|
162
|
+
if (
|
|
163
|
+
typeof canonicalTarget === "string" &&
|
|
164
|
+
!isWithinPath(canonicalRoot, canonicalTarget) &&
|
|
165
|
+
canonicalTarget !== target
|
|
166
|
+
) {
|
|
167
|
+
diagnostics.push(
|
|
168
|
+
error(
|
|
169
|
+
"CHIKX5001",
|
|
170
|
+
`${label} resolves through a symlink outside the workspace root (canonical path "${canonicalTarget}").`,
|
|
171
|
+
context.span,
|
|
172
|
+
),
|
|
173
|
+
);
|
|
174
|
+
} else if (typeof canonicalTarget !== "string") {
|
|
175
|
+
diagnostics.push(canonicalTarget);
|
|
176
|
+
}
|
|
177
|
+
} else {
|
|
178
|
+
diagnostics.push(canonicalRoot);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
if (
|
|
182
|
+
target === normalizePath(context.source.absolute) ||
|
|
183
|
+
candidate.workspacePath === context.source.relative
|
|
184
|
+
) {
|
|
185
|
+
diagnostics.push(
|
|
186
|
+
error(
|
|
187
|
+
"CHIKX5001",
|
|
188
|
+
`${label} would overwrite the input source "${context.source.relative}".`,
|
|
189
|
+
context.span,
|
|
190
|
+
),
|
|
191
|
+
);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
for (const occupied of context.occupiedTargets ?? []) {
|
|
195
|
+
const occupiedAbsolute = isAbsolutePath(occupied)
|
|
196
|
+
? normalizePath(occupied)
|
|
197
|
+
: joinPath(workspaceRoot, occupied);
|
|
198
|
+
if (occupiedAbsolute === target) {
|
|
199
|
+
const occupiedRelative = relativePath(workspaceRoot, occupiedAbsolute);
|
|
200
|
+
diagnostics.push(
|
|
201
|
+
error(
|
|
202
|
+
"CHIKX5001",
|
|
203
|
+
`${label} collides with the already-planned target "${occupiedRelative.startsWith("..") ? occupiedAbsolute : occupiedRelative}".`,
|
|
204
|
+
context.span,
|
|
205
|
+
),
|
|
206
|
+
);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
return diagnostics;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function canonicalizeOrThrow(
|
|
214
|
+
path: string,
|
|
215
|
+
workspaceRoot: string,
|
|
216
|
+
host: OutputPlannerHost | undefined,
|
|
217
|
+
span: Span,
|
|
218
|
+
): string | Diagnostic {
|
|
219
|
+
try {
|
|
220
|
+
const canonical = host === undefined ? path : host.canonicalize(path);
|
|
221
|
+
const normalized = normalizePath(canonical);
|
|
222
|
+
return isAbsolutePath(normalized) ? normalized : joinPath(workspaceRoot, normalized);
|
|
223
|
+
} catch (cause) {
|
|
224
|
+
const detail = cause instanceof Error ? cause.message : String(cause);
|
|
225
|
+
return error(
|
|
226
|
+
"CHIKX5001",
|
|
227
|
+
`Could not canonicalize "${path}" for output containment: ${detail}.`,
|
|
228
|
+
span,
|
|
229
|
+
);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Build the documented warning emitted when map emission is disabled but type
|
|
235
|
+
* declarations were requested (ADR-0010).
|
|
236
|
+
*/
|
|
237
|
+
export function suppressedTypesWarning(span: Span): Diagnostic {
|
|
238
|
+
return warning(
|
|
239
|
+
"CHIKX5001",
|
|
240
|
+
"Type declarations were suppressed because emit-map is false; set map-types to false to acknowledge this warning.",
|
|
241
|
+
span,
|
|
242
|
+
);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Return the workspace-relative destination for an absolute artifact path, or
|
|
247
|
+
* the absolute path when it is not a safe relative form.
|
|
248
|
+
*/
|
|
249
|
+
export function workspacePathFor(workspaceRoot: string, absolutePath: string): string {
|
|
250
|
+
return relativePath(workspaceRoot, absolutePath);
|
|
251
|
+
}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import type { Span } from "../types/span.js";
|
|
2
|
+
import type { Token } from "../types/token.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Cursor and raw-slice helpers over a `readonly Token[]` stream.
|
|
6
|
+
*
|
|
7
|
+
* The parser never receives the source text: every raw AST slice is rebuilt by
|
|
8
|
+
* concatenating token values, which reproduces the authored source exactly
|
|
9
|
+
* (the lexer keeps whitespace and comments, and a `function` token is followed
|
|
10
|
+
* by its own `lparen`). Spans come from token offsets, so line and column stay
|
|
11
|
+
* derivable by every consumer.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/** True for tokens the grammar treats as insignificant between statements. */
|
|
15
|
+
export function isTrivia(token: Token): boolean {
|
|
16
|
+
return token.kind === "whitespace" || token.kind === "comment";
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** True when a token is whitespace (comments are content for boundary trimming). */
|
|
20
|
+
export function isWhitespace(token: Token | undefined): boolean {
|
|
21
|
+
return token !== undefined && token.kind === "whitespace";
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** True when a whitespace token carries a line break, so a new statement may start. */
|
|
25
|
+
export function startsNewLine(token: Token | undefined): boolean {
|
|
26
|
+
return token !== undefined && token.kind === "whitespace" && token.value.includes("\n");
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** A zero-width `eof` token at the given source offset. */
|
|
30
|
+
export function eofToken(offset: number): Token {
|
|
31
|
+
return { kind: "eof", value: "", start: offset, end: offset };
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Token at an absolute stream index, or a synthetic `eof` when out of range. */
|
|
35
|
+
export function tokenAt(tokens: readonly Token[], index: number, eofOffset: number): Token {
|
|
36
|
+
if (index < 0) return eofToken(eofOffset);
|
|
37
|
+
return tokens[index] ?? eofToken(eofOffset);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Raw index of the next non-trivia token at or after `index`. */
|
|
41
|
+
export function skipTriviaAt(tokens: readonly Token[], index: number): number {
|
|
42
|
+
let cursor = index;
|
|
43
|
+
while (true) {
|
|
44
|
+
const token = tokens[cursor];
|
|
45
|
+
if (token === undefined || !isTrivia(token)) return cursor;
|
|
46
|
+
cursor++;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** Exact source text of the raw range `[start, end)`. */
|
|
51
|
+
export function textOf(tokens: readonly Token[], start: number, end: number): string {
|
|
52
|
+
let text = "";
|
|
53
|
+
for (let i = start; i < end; i++) text += tokens[i]?.value ?? "";
|
|
54
|
+
return text;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Narrow a raw range by dropping leading and trailing whitespace tokens. */
|
|
58
|
+
export function trimWhitespace(
|
|
59
|
+
tokens: readonly Token[],
|
|
60
|
+
start: number,
|
|
61
|
+
end: number,
|
|
62
|
+
): { readonly start: number; readonly end: number } {
|
|
63
|
+
let first = start;
|
|
64
|
+
let last = end;
|
|
65
|
+
while (first < last && tokens[first]?.kind === "whitespace") first++;
|
|
66
|
+
while (last > first && tokens[last - 1]?.kind === "whitespace") last--;
|
|
67
|
+
return { start: first, end: last };
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Index of the last non-whitespace token in `[start, end)`, or `start - 1` when empty. */
|
|
71
|
+
export function lastContentIndex(tokens: readonly Token[], start: number, end: number): number {
|
|
72
|
+
for (let i = end - 1; i >= start; i--) {
|
|
73
|
+
const token = tokens[i];
|
|
74
|
+
if (token !== undefined && token.kind !== "whitespace") return i;
|
|
75
|
+
}
|
|
76
|
+
return start - 1;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** Span of the raw range `[start, end)`; zero-width at end of stream when empty. */
|
|
80
|
+
export function spanOf(tokens: readonly Token[], file: string, start: number, end: number): Span {
|
|
81
|
+
const first = tokens[start];
|
|
82
|
+
const last = tokens[end - 1];
|
|
83
|
+
if (first === undefined || last === undefined) {
|
|
84
|
+
const offset = tokens[tokens.length - 1]?.end ?? 0;
|
|
85
|
+
return { start: offset, end: offset, file };
|
|
86
|
+
}
|
|
87
|
+
return { start: first.start, end: last.end, file };
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** Mutable read position over an immutable token stream. */
|
|
91
|
+
export class TokenCursor {
|
|
92
|
+
readonly tokens: readonly Token[];
|
|
93
|
+
readonly file: string;
|
|
94
|
+
/** End-of-input offset from the trailing `eof` token, or the last token end. */
|
|
95
|
+
readonly endOffset: number;
|
|
96
|
+
index = 0;
|
|
97
|
+
|
|
98
|
+
constructor(tokens: readonly Token[], file: string) {
|
|
99
|
+
this.tokens = tokens;
|
|
100
|
+
this.file = file;
|
|
101
|
+
const last = tokens[tokens.length - 1];
|
|
102
|
+
this.endOffset = last === undefined ? 0 : last.kind === "eof" ? last.start : last.end;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** Current raw token, never `undefined`. */
|
|
106
|
+
current(): Token {
|
|
107
|
+
return tokenAt(this.tokens, this.index, this.endOffset);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** Synthetic end-of-input token at the stream offset. */
|
|
111
|
+
eof(): Token {
|
|
112
|
+
return eofToken(this.endOffset);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** Advance past whitespace and comment tokens. */
|
|
116
|
+
skipTrivia(): void {
|
|
117
|
+
this.index = skipTriviaAt(this.tokens, this.index);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** Consume one raw token; the synthetic `eof` is never consumed. */
|
|
121
|
+
advance(): Token {
|
|
122
|
+
const token = this.current();
|
|
123
|
+
if (token.kind !== "eof") this.index++;
|
|
124
|
+
return token;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
span(token: Token): Span {
|
|
128
|
+
return { start: token.start, end: token.end, file: this.file };
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
spanOfRange(start: number, end: number): Span {
|
|
132
|
+
return spanOf(this.tokens, this.file, start, end);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
text(start: number, end: number): string {
|
|
136
|
+
return textOf(this.tokens, start, end);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
import type { Declaration } from "../types/ast.js";
|
|
2
|
+
import type { Token } from "../types/token.js";
|
|
3
|
+
import {
|
|
4
|
+
type TokenCursor,
|
|
5
|
+
isTrivia,
|
|
6
|
+
lastContentIndex,
|
|
7
|
+
startsNewLine,
|
|
8
|
+
tokenAt,
|
|
9
|
+
trimWhitespace,
|
|
10
|
+
} from "./cursor.js";
|
|
11
|
+
import type { DiagnosticBag } from "./diagnostics.js";
|
|
12
|
+
import { messages } from "./diagnostics.js";
|
|
13
|
+
import { recoverStatement, skipBalancedBlock } from "./recovery.js";
|
|
14
|
+
import { containsDollarRef, findSemicolonlessRuleStart, segmentValue } from "./value.js";
|
|
15
|
+
|
|
16
|
+
/** Shared mutable parse state for one file. */
|
|
17
|
+
export interface ParseContext {
|
|
18
|
+
readonly cursor: TokenCursor;
|
|
19
|
+
readonly bag: DiagnosticBag;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** How a declaration value ended. */
|
|
23
|
+
type Terminator = "semicolon" | "rbrace" | "eof" | "lbrace" | "next-declaration";
|
|
24
|
+
|
|
25
|
+
interface ScannedValue {
|
|
26
|
+
readonly terminator: Terminator;
|
|
27
|
+
/** Raw index of the terminator token (or the new statement start). */
|
|
28
|
+
readonly boundaryIndex: number;
|
|
29
|
+
readonly malformed: boolean;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** True when `property:` begins at `index` (trivia tolerant). */
|
|
33
|
+
function looksLikeProperty(tokens: readonly Token[], index: number, eofOffset: number): boolean {
|
|
34
|
+
let cursor = index;
|
|
35
|
+
while (true) {
|
|
36
|
+
const token = tokenAt(tokens, cursor, eofOffset);
|
|
37
|
+
if (token.kind === "eof") return false;
|
|
38
|
+
if (isTrivia(token)) {
|
|
39
|
+
cursor++;
|
|
40
|
+
continue;
|
|
41
|
+
}
|
|
42
|
+
if (token.kind !== "colon") return false;
|
|
43
|
+
const after = tokenAt(tokens, cursor + 1, eofOffset);
|
|
44
|
+
return after.kind !== "eof" && after.kind !== "rbrace" && after.kind !== "semicolon";
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Raw index of the declaration's property colon, or `-1`. */
|
|
49
|
+
function findPropertyColon(
|
|
50
|
+
tokens: readonly Token[],
|
|
51
|
+
startIndex: number,
|
|
52
|
+
eofOffset: number,
|
|
53
|
+
): number {
|
|
54
|
+
let depth = 0;
|
|
55
|
+
let index = startIndex;
|
|
56
|
+
while (true) {
|
|
57
|
+
const token = tokenAt(tokens, index, eofOffset);
|
|
58
|
+
if (token.kind === "eof") return -1;
|
|
59
|
+
if (token.kind === "lparen" || token.kind === "lbracket") depth++;
|
|
60
|
+
else if (token.kind === "rparen" || token.kind === "rbracket") depth = Math.max(0, depth - 1);
|
|
61
|
+
else if (depth === 0) {
|
|
62
|
+
if (token.kind === "colon") return index;
|
|
63
|
+
if (token.kind === "semicolon" || token.kind === "rbrace" || token.kind === "lbrace") {
|
|
64
|
+
return -1;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
index++;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Scan a value up to its statement boundary, tracking paren/bracket balance.
|
|
73
|
+
*
|
|
74
|
+
* A `;` always ends the statement even when brackets are unbalanced (CHIKX1003
|
|
75
|
+
* recovery), and a `property:` that starts on a later line ends a declaration
|
|
76
|
+
* that forgot its semicolon (CHIKX1002, errors/missing-semicolon).
|
|
77
|
+
*
|
|
78
|
+
* A balanced `{` ends the statement too: when the prefix is a declaration and
|
|
79
|
+
* the brace opens a nested rule (`color: red` then `&:hover {`), the boundary
|
|
80
|
+
* moves to the selector start so the declaration keeps its authored value and
|
|
81
|
+
* CHIKX1002 recovery can resume at the rule (ADR-0011). Otherwise the brace
|
|
82
|
+
* stays malformed (CHIKX1003).
|
|
83
|
+
*/
|
|
84
|
+
function scanValue(
|
|
85
|
+
tokens: readonly Token[],
|
|
86
|
+
statementStart: number,
|
|
87
|
+
startIndex: number,
|
|
88
|
+
eofOffset: number,
|
|
89
|
+
): ScannedValue {
|
|
90
|
+
const closers: Array<"rparen" | "rbracket"> = [];
|
|
91
|
+
let malformed = false;
|
|
92
|
+
let index = startIndex;
|
|
93
|
+
|
|
94
|
+
while (true) {
|
|
95
|
+
const token = tokenAt(tokens, index, eofOffset);
|
|
96
|
+
if (token.kind === "eof") {
|
|
97
|
+
return { terminator: "eof", boundaryIndex: index, malformed };
|
|
98
|
+
}
|
|
99
|
+
if (token.kind === "semicolon") {
|
|
100
|
+
return {
|
|
101
|
+
terminator: "semicolon",
|
|
102
|
+
boundaryIndex: index,
|
|
103
|
+
malformed: malformed || closers.length > 0,
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
if (token.kind === "rbrace") {
|
|
107
|
+
return {
|
|
108
|
+
terminator: "rbrace",
|
|
109
|
+
boundaryIndex: index,
|
|
110
|
+
malformed: malformed || closers.length > 0,
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
if (token.kind === "lbrace" && closers.length === 0) {
|
|
114
|
+
if (malformed) return { terminator: "lbrace", boundaryIndex: index, malformed: true };
|
|
115
|
+
const ruleIndex = findSemicolonlessRuleStart(tokens, statementStart, index);
|
|
116
|
+
if (ruleIndex === -1) {
|
|
117
|
+
return { terminator: "lbrace", boundaryIndex: index, malformed: true };
|
|
118
|
+
}
|
|
119
|
+
return { terminator: "lbrace", boundaryIndex: ruleIndex, malformed: false };
|
|
120
|
+
}
|
|
121
|
+
if (token.kind === "lparen") closers.push("rparen");
|
|
122
|
+
else if (token.kind === "lbracket") closers.push("rbracket");
|
|
123
|
+
else if (token.kind === "rparen" || token.kind === "rbracket") {
|
|
124
|
+
if (closers.pop() !== token.kind) malformed = true;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
if (
|
|
128
|
+
closers.length === 0 &&
|
|
129
|
+
token.kind === "ident" &&
|
|
130
|
+
index > startIndex &&
|
|
131
|
+
startsNewLine(tokens[index - 1]) &&
|
|
132
|
+
looksLikeProperty(tokens, index + 1, eofOffset)
|
|
133
|
+
) {
|
|
134
|
+
return { terminator: "next-declaration", boundaryIndex: index, malformed };
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
index++;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function endOfRawRange(
|
|
142
|
+
tokens: readonly Token[],
|
|
143
|
+
start: number,
|
|
144
|
+
end: number,
|
|
145
|
+
fallback: number,
|
|
146
|
+
): number {
|
|
147
|
+
const last = lastContentIndex(tokens, start, end);
|
|
148
|
+
return last >= start ? (tokens[last]?.end ?? fallback) : fallback;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Parse one `property: value [@alias value]... ;` declaration.
|
|
153
|
+
*
|
|
154
|
+
* Terminator policy (ADR-0011): a plain CSS final declaration without `;`
|
|
155
|
+
* before `}` is accepted; one carrying a modifier or `$` reference is reported
|
|
156
|
+
* as CHIKX1002 and omitted. A declaration that runs into a nested rule's `{`
|
|
157
|
+
* is reported as CHIKX1002, kept, and parsing resumes at that rule, so the
|
|
158
|
+
* authored value and the nested rule both survive. Malformed values report
|
|
159
|
+
* CHIKX1003 once and recover at the next statement boundary. The returned node
|
|
160
|
+
* is frozen and its span stops at the last value character, excluding the `;`.
|
|
161
|
+
*/
|
|
162
|
+
export function parseDeclaration(ctx: ParseContext): Declaration | undefined {
|
|
163
|
+
const { cursor, bag } = ctx;
|
|
164
|
+
const tokens = cursor.tokens;
|
|
165
|
+
const file = cursor.file;
|
|
166
|
+
const startIndex = cursor.index;
|
|
167
|
+
const propertyToken = cursor.current();
|
|
168
|
+
|
|
169
|
+
const colonIndex = findPropertyColon(tokens, startIndex, cursor.endOffset);
|
|
170
|
+
if (colonIndex === -1) {
|
|
171
|
+
bag.add("CHIKX1001", messages.unexpectedStatement, cursor.span(propertyToken));
|
|
172
|
+
recoverStatement(cursor);
|
|
173
|
+
return undefined;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
const propertyBounds = trimWhitespace(tokens, startIndex, colonIndex);
|
|
177
|
+
const property = cursor.text(propertyBounds.start, propertyBounds.end);
|
|
178
|
+
if (property.length === 0) {
|
|
179
|
+
bag.add("CHIKX1001", messages.emptyProperty, cursor.span(propertyToken));
|
|
180
|
+
recoverStatement(cursor);
|
|
181
|
+
return undefined;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
const valueStart = colonIndex + 1;
|
|
185
|
+
const scan = scanValue(tokens, startIndex, valueStart, cursor.endOffset);
|
|
186
|
+
const valueBounds = trimWhitespace(tokens, valueStart, scan.boundaryIndex);
|
|
187
|
+
const colonToken = tokenAt(tokens, colonIndex, cursor.endOffset);
|
|
188
|
+
const boundaryToken = tokenAt(tokens, scan.boundaryIndex, cursor.endOffset);
|
|
189
|
+
const valueEnd = endOfRawRange(tokens, valueStart, scan.boundaryIndex, colonToken.end);
|
|
190
|
+
|
|
191
|
+
const statementSpan = { start: propertyToken.start, end: valueEnd, file };
|
|
192
|
+
|
|
193
|
+
if (scan.malformed) {
|
|
194
|
+
bag.add("CHIKX1003", messages.malformedValue, {
|
|
195
|
+
start: propertyToken.start,
|
|
196
|
+
end: scan.terminator === "lbrace" ? valueEnd : boundaryToken.end,
|
|
197
|
+
file,
|
|
198
|
+
});
|
|
199
|
+
if (scan.terminator === "lbrace") {
|
|
200
|
+
cursor.index = skipBalancedBlock(tokens, scan.boundaryIndex, cursor.endOffset);
|
|
201
|
+
} else if (scan.terminator === "semicolon") {
|
|
202
|
+
cursor.index = scan.boundaryIndex + 1;
|
|
203
|
+
} else {
|
|
204
|
+
cursor.index = Math.min(scan.boundaryIndex, tokens.length);
|
|
205
|
+
}
|
|
206
|
+
return undefined;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
const parts = segmentValue(tokens, valueBounds.start, valueBounds.end, file);
|
|
210
|
+
const chikxValue =
|
|
211
|
+
parts.variants.length > 0 || containsDollarRef(tokens, valueStart, scan.boundaryIndex);
|
|
212
|
+
|
|
213
|
+
const declaration: Declaration = Object.freeze({
|
|
214
|
+
kind: "declaration",
|
|
215
|
+
property,
|
|
216
|
+
value: parts.base.value,
|
|
217
|
+
...(parts.base.tokens === undefined ? {} : { tokens: parts.base.tokens }),
|
|
218
|
+
variants: parts.variants,
|
|
219
|
+
...(parts.base.important === undefined ? {} : { important: parts.base.important }),
|
|
220
|
+
span: statementSpan,
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
if (scan.terminator === "semicolon") {
|
|
224
|
+
cursor.index = scan.boundaryIndex + 1;
|
|
225
|
+
return declaration;
|
|
226
|
+
}
|
|
227
|
+
if (scan.terminator === "next-declaration") {
|
|
228
|
+
bag.add("CHIKX1002", messages.missingSemicolon("a declaration"), statementSpan);
|
|
229
|
+
cursor.index = Math.min(scan.boundaryIndex, tokens.length);
|
|
230
|
+
return undefined;
|
|
231
|
+
}
|
|
232
|
+
if (scan.terminator === "lbrace") {
|
|
233
|
+
// `color: red` immediately before `&:hover { … }`: the declaration lost its
|
|
234
|
+
// semicolon. Keep the authored declaration, report once and resume at the
|
|
235
|
+
// nested rule so it still parses (ADR-0011, invariant 2).
|
|
236
|
+
bag.add("CHIKX1002", messages.missingSemicolon("a declaration"), statementSpan);
|
|
237
|
+
cursor.index = Math.min(scan.boundaryIndex, tokens.length);
|
|
238
|
+
return declaration;
|
|
239
|
+
}
|
|
240
|
+
if (chikxValue) {
|
|
241
|
+
bag.add("CHIKX1002", messages.missingSemicolon("a Chikx declaration"), statementSpan);
|
|
242
|
+
cursor.index = Math.min(scan.boundaryIndex, tokens.length);
|
|
243
|
+
return undefined;
|
|
244
|
+
}
|
|
245
|
+
cursor.index = Math.min(scan.boundaryIndex, tokens.length);
|
|
246
|
+
return declaration;
|
|
247
|
+
}
|