@better-css-modules/core 0.2.1 → 0.4.0
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 +3 -2
- package/dist/{dts-YHloeOKZ.mjs → dts--_fgXHCu.mjs} +45 -13
- package/dist/{dts-BLFmeUu6.d.mts → dts-BFi7f7uh.d.mts} +26 -4
- package/dist/index.d.mts +90 -2
- package/dist/index.mjs +821 -30
- package/dist/internal.d.mts +1 -1
- package/dist/internal.mjs +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -49,9 +49,10 @@ try {
|
|
|
49
49
|
- `loadConfig({ cwd?, config? })` loads the config file `config` names, relative to `cwd`, or the `better-css-modules.config.*` in `cwd` (by default the working directory), and returns it as a `ResolvedConfig`: the defaults filled in, checked, and with `root`, the directory every relative path starts from, and `file`, the config file or `null`.
|
|
50
50
|
- `generate(config)` writes the `.d.ts` files and their declaration maps, removes the ones it wrote for stylesheets that are gone, and returns a `GenerateResult`: `files`, `removed` and the syntax `diagnostics`.
|
|
51
51
|
- `check(config)` runs every check and returns a `CheckResult`: the sorted `diagnostics`, how many `modules` it checked, and the token categories (`tokens`) the global CSS restricts.
|
|
52
|
+
- `exportTokens(config)` reads the global CSS and returns its tokens as a DTCG resolver document, the `DtcgResolver` the `tokens` command prints; see [Token export](https://github.com/k35o/better-css-modules#token-export).
|
|
52
53
|
- `formatDiagnostic(diagnostic, cwd)` gives the `file:line:col error rule: message` line of the CLI, and `formatGitHubAnnotation(diagnostic, cwd)` the GitHub Actions workflow command of `--format github`.
|
|
53
|
-
- `ConfigError` is a mistake in the config or in the files it names. `loadConfig` throws it, and so does `check` when `include` matches no file, the global CSS cannot be read, or it does not declare the `layer
|
|
54
|
-
- The types `Config`, `ResolvedConfig`, `GenerateResult`, `CheckResult`, `Diagnostic` and `
|
|
54
|
+
- `ConfigError` is a mistake in the config or in the files it names. `loadConfig` throws it, and so does `check` when `include` matches no file, the global CSS cannot be read, or it does not declare the `layer`, and `exportTokens` when the global CSS cannot be read or a token has a name DTCG cannot hold.
|
|
55
|
+
- The types `Config`, `ResolvedConfig`, `GenerateResult`, `CheckResult`, `Diagnostic`, `RuleId`, `DtcgResolver` and `DtcgToken` describe these.
|
|
55
56
|
|
|
56
57
|
`@better-css-modules/core/internal` holds what the CLI and the plugins share. It is for those packages only and outside semver.
|
|
57
58
|
|
|
@@ -670,11 +670,18 @@ const tokenCategories = {
|
|
|
670
670
|
},
|
|
671
671
|
shadow: {
|
|
672
672
|
properties: {
|
|
673
|
-
"box-shadow": "
|
|
674
|
-
"text-shadow": "
|
|
673
|
+
"box-shadow": "items",
|
|
674
|
+
"text-shadow": "items"
|
|
675
675
|
},
|
|
676
676
|
keywords: ["none"]
|
|
677
677
|
},
|
|
678
|
+
"font-family": {
|
|
679
|
+
properties: {
|
|
680
|
+
"font-family": "items",
|
|
681
|
+
font: "font"
|
|
682
|
+
},
|
|
683
|
+
keywords: []
|
|
684
|
+
},
|
|
678
685
|
"font-size": {
|
|
679
686
|
properties: {
|
|
680
687
|
"font-size": "value",
|
|
@@ -696,6 +703,10 @@ const tokenCategories = {
|
|
|
696
703
|
},
|
|
697
704
|
keywords: ["normal"]
|
|
698
705
|
},
|
|
706
|
+
"letter-spacing": {
|
|
707
|
+
properties: { "letter-spacing": "value" },
|
|
708
|
+
keywords: ["normal"]
|
|
709
|
+
},
|
|
699
710
|
"z-index": {
|
|
700
711
|
properties: { "z-index": "value" },
|
|
701
712
|
keywords: ["auto"]
|
|
@@ -706,11 +717,20 @@ const tokenCategories = {
|
|
|
706
717
|
"transition-delay": "value",
|
|
707
718
|
"animation-duration": "value",
|
|
708
719
|
"animation-delay": "value",
|
|
709
|
-
transition: "
|
|
710
|
-
animation: "
|
|
720
|
+
transition: "transition",
|
|
721
|
+
animation: "transition"
|
|
711
722
|
},
|
|
712
723
|
keywords: ["auto"]
|
|
713
724
|
},
|
|
725
|
+
easing: {
|
|
726
|
+
properties: {
|
|
727
|
+
"transition-timing-function": "items",
|
|
728
|
+
"animation-timing-function": "items",
|
|
729
|
+
transition: "transition",
|
|
730
|
+
animation: "transition"
|
|
731
|
+
},
|
|
732
|
+
keywords: ["linear"]
|
|
733
|
+
},
|
|
714
734
|
breakpoint: {
|
|
715
735
|
properties: {},
|
|
716
736
|
keywords: []
|
|
@@ -844,8 +864,13 @@ function isForeign(node) {
|
|
|
844
864
|
function globalCssFrom(files) {
|
|
845
865
|
const declarations = /* @__PURE__ */ new Map();
|
|
846
866
|
const registrations = /* @__PURE__ */ new Map();
|
|
867
|
+
const given = /* @__PURE__ */ new Map();
|
|
868
|
+
const give = (name, value) => {
|
|
869
|
+
if (value.trim() !== "") given.set(name, [...given.get(name) ?? [], value.trim()]);
|
|
870
|
+
};
|
|
847
871
|
for (const { file, root, conditional } of files) root.walk((node) => {
|
|
848
872
|
if (node.type === "decl" && node.prop.startsWith("--")) {
|
|
873
|
+
if (!isForeign(node)) give(node.prop, node.value);
|
|
849
874
|
if (declaresToken(node, conditional)) declarations.set(node.prop, {
|
|
850
875
|
node,
|
|
851
876
|
file,
|
|
@@ -853,6 +878,7 @@ function globalCssFrom(files) {
|
|
|
853
878
|
});
|
|
854
879
|
} else if (node.type === "atrule" && node.name.toLowerCase() === "property") {
|
|
855
880
|
const name = node.params.trim();
|
|
881
|
+
give(name, initialValueOf(node));
|
|
856
882
|
if (name.startsWith("--") && declaresToken(node, conditional)) registrations.set(name, {
|
|
857
883
|
node,
|
|
858
884
|
file,
|
|
@@ -864,16 +890,18 @@ function globalCssFrom(files) {
|
|
|
864
890
|
for (const [name, source] of registrations) if (!sources.has(name)) sources.set(name, source);
|
|
865
891
|
const values = resolveValues(new Map([...sources].map(([name, { text }]) => [name, text])));
|
|
866
892
|
const tokens = /* @__PURE__ */ new Map();
|
|
867
|
-
for (const [name, { node, file }] of sources) tokens.set(name, {
|
|
893
|
+
for (const [name, { node, file, text }] of sources) tokens.set(name, {
|
|
868
894
|
name,
|
|
869
895
|
category: categoryOf(name),
|
|
870
896
|
value: values.get(name) ?? "",
|
|
897
|
+
text,
|
|
871
898
|
file,
|
|
872
899
|
node
|
|
873
900
|
});
|
|
874
901
|
return {
|
|
875
902
|
files,
|
|
876
903
|
tokens,
|
|
904
|
+
values: given,
|
|
877
905
|
layers: layersOf(files)
|
|
878
906
|
};
|
|
879
907
|
}
|
|
@@ -900,7 +928,7 @@ function layersOf(files) {
|
|
|
900
928
|
return;
|
|
901
929
|
}
|
|
902
930
|
const imported = imports.get(node);
|
|
903
|
-
if (!imported || imported.
|
|
931
|
+
if (!imported || imported.condition !== null) return;
|
|
904
932
|
const target = byFile.get(imported.file);
|
|
905
933
|
if (imported.layer === null && target) visit(target);
|
|
906
934
|
else if (imported.layer) names.add(imported.layer);
|
|
@@ -1045,18 +1073,18 @@ var Loader = class {
|
|
|
1045
1073
|
if (node.type !== "atrule" || node.name.toLowerCase() !== "import") continue;
|
|
1046
1074
|
const prelude = readImport(node.params);
|
|
1047
1075
|
if (!prelude) fail(`${this.at(file, node)}: cannot read @import ${node.params}`);
|
|
1048
|
-
const { url, layer,
|
|
1076
|
+
const { url, layer, condition } = prelude;
|
|
1049
1077
|
if (/^([a-z][a-z\d+.-]*:|\/\/)/i.test(url)) fail(`${this.at(file, node)}: global CSS cannot import ${url}`);
|
|
1050
1078
|
const imported = this.imports.sync(path.dirname(file), url).path;
|
|
1051
1079
|
if (!imported) fail(`${this.at(file, node)}: cannot resolve "${url}"`);
|
|
1052
1080
|
entry.imports.push({
|
|
1053
1081
|
rule: node,
|
|
1054
1082
|
file: imported,
|
|
1055
|
-
|
|
1083
|
+
condition,
|
|
1056
1084
|
layer
|
|
1057
1085
|
});
|
|
1058
|
-
if (
|
|
1059
|
-
await this.load(imported, conditional || condition, [...importers, file]);
|
|
1086
|
+
if (condition === null) imports.push(imported);
|
|
1087
|
+
await this.load(imported, conditional || condition !== null, [...importers, file]);
|
|
1060
1088
|
}
|
|
1061
1089
|
this.files.push(entry);
|
|
1062
1090
|
}
|
|
@@ -1095,19 +1123,23 @@ function readImport(params) {
|
|
|
1095
1123
|
const [target, ...rest] = prelude.type === "AtrulePrelude" ? prelude.children.toArray() : [];
|
|
1096
1124
|
if (target?.type !== "String" && target?.type !== "Url" || !target.loc) return null;
|
|
1097
1125
|
let layer = null;
|
|
1098
|
-
let
|
|
1126
|
+
let supports = null;
|
|
1127
|
+
let media = null;
|
|
1128
|
+
const text = ({ loc }) => loc ? params.slice(loc.start.offset, loc.end.offset) : "";
|
|
1099
1129
|
for (const node of rest) {
|
|
1100
1130
|
const name = node.type === "Function" || node.type === "Identifier" ? node.name.toLowerCase() : "";
|
|
1101
1131
|
if (name === "layer") {
|
|
1102
1132
|
const [named] = node.type === "Function" ? node.children.toArray() : [];
|
|
1103
1133
|
layer = named?.type === "Layer" ? named.name : "";
|
|
1104
|
-
} else if (node.type === "
|
|
1134
|
+
} else if (node.type === "Function" && name === "supports") supports = `@supports (${text(node).slice(9, -1).trim()})`;
|
|
1135
|
+
else if (node.type === "MediaQueryList" && !isUnknownMediaType(node)) media = `@media ${text(node)}`;
|
|
1105
1136
|
}
|
|
1137
|
+
const conditions = [supports, media].filter((condition) => condition !== null);
|
|
1106
1138
|
return {
|
|
1107
1139
|
url: target.value,
|
|
1108
1140
|
urlEnd: target.loc.end.offset,
|
|
1109
1141
|
layer,
|
|
1110
|
-
|
|
1142
|
+
condition: conditions.length > 0 ? conditions.join(" ") : null
|
|
1111
1143
|
};
|
|
1112
1144
|
}
|
|
1113
1145
|
/**
|
|
@@ -129,11 +129,18 @@ declare const tokenCategories: {
|
|
|
129
129
|
};
|
|
130
130
|
shadow: {
|
|
131
131
|
properties: {
|
|
132
|
-
"box-shadow": "
|
|
133
|
-
"text-shadow": "
|
|
132
|
+
"box-shadow": "items";
|
|
133
|
+
"text-shadow": "items";
|
|
134
134
|
};
|
|
135
135
|
keywords: string[];
|
|
136
136
|
};
|
|
137
|
+
"font-family": {
|
|
138
|
+
properties: {
|
|
139
|
+
"font-family": "items";
|
|
140
|
+
font: "font";
|
|
141
|
+
};
|
|
142
|
+
keywords: never[];
|
|
143
|
+
};
|
|
137
144
|
"font-size": {
|
|
138
145
|
properties: {
|
|
139
146
|
"font-size": "value";
|
|
@@ -155,6 +162,12 @@ declare const tokenCategories: {
|
|
|
155
162
|
};
|
|
156
163
|
keywords: string[];
|
|
157
164
|
};
|
|
165
|
+
"letter-spacing": {
|
|
166
|
+
properties: {
|
|
167
|
+
"letter-spacing": "value";
|
|
168
|
+
};
|
|
169
|
+
keywords: string[];
|
|
170
|
+
};
|
|
158
171
|
"z-index": {
|
|
159
172
|
properties: {
|
|
160
173
|
"z-index": "value";
|
|
@@ -167,8 +180,17 @@ declare const tokenCategories: {
|
|
|
167
180
|
"transition-delay": "value";
|
|
168
181
|
"animation-duration": "value";
|
|
169
182
|
"animation-delay": "value";
|
|
170
|
-
transition: "
|
|
171
|
-
animation: "
|
|
183
|
+
transition: "transition";
|
|
184
|
+
animation: "transition";
|
|
185
|
+
};
|
|
186
|
+
keywords: string[];
|
|
187
|
+
};
|
|
188
|
+
easing: {
|
|
189
|
+
properties: {
|
|
190
|
+
"transition-timing-function": "items";
|
|
191
|
+
"animation-timing-function": "items";
|
|
192
|
+
transition: "transition";
|
|
193
|
+
animation: "transition";
|
|
172
194
|
};
|
|
173
195
|
keywords: string[];
|
|
174
196
|
};
|
package/dist/index.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { a as Diagnostic, c as RuleId, d as ConfigError, f as ResolvedConfig, l as TokenCategory, m as loadConfig, n as generate, o as formatDiagnostic, p as defineConfig, s as formatGitHubAnnotation, t as GenerateResult, u as Config } from "./dts-
|
|
1
|
+
import { a as Diagnostic, c as RuleId, d as ConfigError, f as ResolvedConfig, l as TokenCategory, m as loadConfig, n as generate, o as formatDiagnostic, p as defineConfig, s as formatGitHubAnnotation, t as GenerateResult, u as Config } from "./dts-BFi7f7uh.mjs";
|
|
2
2
|
import "postcss";
|
|
3
3
|
//#region src/check.d.ts
|
|
4
4
|
interface CheckResult {
|
|
@@ -17,4 +17,92 @@ interface CheckResult {
|
|
|
17
17
|
*/
|
|
18
18
|
export declare function check(config: ResolvedConfig): Promise<CheckResult>;
|
|
19
19
|
//#endregion
|
|
20
|
-
|
|
20
|
+
//#region src/dtcg.d.ts
|
|
21
|
+
/** A reference to another token: its name without the leading `--`, in braces. */
|
|
22
|
+
type DtcgAlias = `{${string}}`;
|
|
23
|
+
type DtcgComponent = number | "none";
|
|
24
|
+
interface DtcgColor {
|
|
25
|
+
colorSpace: "srgb" | "srgb-linear" | "hsl" | "hwb" | "lab" | "lch" | "oklab" | "oklch" | "display-p3" | "a98-rgb" | "prophoto-rgb" | "rec2020" | "xyz-d65" | "xyz-d50";
|
|
26
|
+
components: [DtcgComponent, DtcgComponent, DtcgComponent];
|
|
27
|
+
/** Present when the color is written with one. */
|
|
28
|
+
alpha?: number;
|
|
29
|
+
}
|
|
30
|
+
interface DtcgDimension {
|
|
31
|
+
value: number;
|
|
32
|
+
unit: "px" | "rem";
|
|
33
|
+
}
|
|
34
|
+
interface DtcgDuration {
|
|
35
|
+
value: number;
|
|
36
|
+
unit: "ms" | "s";
|
|
37
|
+
}
|
|
38
|
+
interface DtcgShadow {
|
|
39
|
+
color: DtcgColor | DtcgAlias;
|
|
40
|
+
offsetX: DtcgDimension;
|
|
41
|
+
offsetY: DtcgDimension;
|
|
42
|
+
blur: DtcgDimension;
|
|
43
|
+
spread: DtcgDimension;
|
|
44
|
+
inset?: true;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* A token. A value DTCG has no form for keeps its CSS text and has no type,
|
|
48
|
+
* which DTCG takes for an invalid token; so does an alias of one.
|
|
49
|
+
*/
|
|
50
|
+
type DtcgToken = {
|
|
51
|
+
$type: "color";
|
|
52
|
+
$value: DtcgColor | DtcgAlias;
|
|
53
|
+
} | {
|
|
54
|
+
$type: "dimension";
|
|
55
|
+
$value: DtcgDimension | DtcgAlias;
|
|
56
|
+
} | {
|
|
57
|
+
$type: "duration";
|
|
58
|
+
$value: DtcgDuration | DtcgAlias;
|
|
59
|
+
} | {
|
|
60
|
+
$type: "number";
|
|
61
|
+
$value: number | DtcgAlias;
|
|
62
|
+
} | {
|
|
63
|
+
$type: "fontWeight";
|
|
64
|
+
$value: number | "normal" | "bold" | DtcgAlias;
|
|
65
|
+
} | {
|
|
66
|
+
$type: "fontFamily";
|
|
67
|
+
$value: string | string[];
|
|
68
|
+
} | {
|
|
69
|
+
$type: "cubicBezier";
|
|
70
|
+
$value: [number, number, number, number] | DtcgAlias;
|
|
71
|
+
} | {
|
|
72
|
+
$type: "shadow";
|
|
73
|
+
$value: DtcgShadow | DtcgShadow[] | DtcgAlias;
|
|
74
|
+
} | {
|
|
75
|
+
$type?: undefined;
|
|
76
|
+
$value: string;
|
|
77
|
+
};
|
|
78
|
+
/** Tokens by their names without the leading `--`. */
|
|
79
|
+
type DtcgTokens = Record<string, DtcgToken>;
|
|
80
|
+
interface DtcgSet {
|
|
81
|
+
type: "set";
|
|
82
|
+
name: ":root";
|
|
83
|
+
sources: [DtcgTokens];
|
|
84
|
+
}
|
|
85
|
+
/** A place other than `:root` that overrides tokens, named by its conditions as written. */
|
|
86
|
+
interface DtcgModifier {
|
|
87
|
+
type: "modifier";
|
|
88
|
+
name: string;
|
|
89
|
+
contexts: {
|
|
90
|
+
off: [];
|
|
91
|
+
on: [DtcgTokens];
|
|
92
|
+
};
|
|
93
|
+
default: "off";
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* The tokens as a DTCG resolver document (Resolver Module 2025.10): the tokens
|
|
97
|
+
* at `:root` as a set, then each mode as a modifier the input turns on, in the
|
|
98
|
+
* source order of its first place. The input turns on every mode whose
|
|
99
|
+
* conditions hold at once.
|
|
100
|
+
*/
|
|
101
|
+
interface DtcgResolver {
|
|
102
|
+
version: "2025.10";
|
|
103
|
+
resolutionOrder: [DtcgSet, ...DtcgModifier[]];
|
|
104
|
+
}
|
|
105
|
+
/** Read the global CSS a config lists and write its tokens as a DTCG resolver document. */
|
|
106
|
+
export declare function exportTokens(config: ResolvedConfig): Promise<DtcgResolver>;
|
|
107
|
+
//#endregion
|
|
108
|
+
export { type CheckResult, type Config, ConfigError, type Diagnostic, type DtcgResolver, type DtcgToken, type GenerateResult, type ResolvedConfig, type RuleId, defineConfig, formatDiagnostic, formatGitHubAnnotation, generate, loadConfig };
|
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { C as find, D as property, E as parse, O as walk$1, S as valueStart, T as lexer, _ as sortDiagnostics, a as resolveLayer, b as loadConfig, c as loadGlobalCss, d as findCssModules, f as loadCssModules, g as formatGitHubAnnotation, h as formatDiagnostic, m as tokenCategories, o as declaresToken, p as categoryOf, r as checkLayer, s as isForeign, t as generate, u as defaultIgnore, v as ConfigError, w as generate$1, x as paramsStart, y as defineConfig } from "./dts
|
|
1
|
+
import { C as find, D as property, E as parse, O as walk$1, S as valueStart, T as lexer, _ as sortDiagnostics, a as resolveLayer, b as loadConfig, c as loadGlobalCss, d as findCssModules, f as loadCssModules, g as formatGitHubAnnotation, h as formatDiagnostic, m as tokenCategories, o as declaresToken, p as categoryOf, r as checkLayer, s as isForeign, t as generate, u as defaultIgnore, v as ConfigError, w as generate$1, x as paramsStart, y as defineConfig } from "./dts--_fgXHCu.mjs";
|
|
2
2
|
import fs from "node:fs";
|
|
3
3
|
import path from "node:path";
|
|
4
4
|
import { ResolverFactory } from "oxc-resolver";
|
|
@@ -357,15 +357,27 @@ function visitScope(atRule, context, checker) {
|
|
|
357
357
|
}
|
|
358
358
|
const scope = find(prelude, (node) => node.type === "Scope");
|
|
359
359
|
const root = scope?.root?.type === "SelectorList" ? scope.root : null;
|
|
360
|
+
const limit = scope?.limit?.type === "SelectorList" ? scope.limit : null;
|
|
361
|
+
const unrooted = !scope?.root;
|
|
362
|
+
const rooted = root !== null && hasLocalSubjects(root, context.local, context.nested);
|
|
360
363
|
const scan = {
|
|
361
364
|
local: false,
|
|
362
365
|
globals: [],
|
|
363
366
|
ids: []
|
|
364
367
|
};
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
368
|
+
if (root) scanList(root, context.local, false, text, scan);
|
|
369
|
+
const limitScan = {
|
|
370
|
+
local: false,
|
|
371
|
+
globals: [],
|
|
372
|
+
ids: []
|
|
373
|
+
};
|
|
374
|
+
if (limit) scanList(limit, context.local, false, text, limitScan);
|
|
375
|
+
const inside = rooted && context.nested && limit ? globalsInside(limit) : /* @__PURE__ */ new Set();
|
|
376
|
+
checker.report(atRule, [
|
|
377
|
+
...[...scan.globals, ...limitScan.globals.filter(({ node }) => !inside.has(node))].map((found) => globalDiagnostic(found, checker.file)),
|
|
378
|
+
...[...scan.ids, ...limitScan.ids].map((found) => idDiagnostic(found, checker.file)),
|
|
379
|
+
...root && context.nested ? repeatedRoots(atRule, root, text, checker.file) : []
|
|
380
|
+
]);
|
|
369
381
|
return {
|
|
370
382
|
...context,
|
|
371
383
|
scoped: !unrooted && (context.scoped || rooted),
|
|
@@ -400,7 +412,8 @@ function checkSelectorList(list, text, context, file) {
|
|
|
400
412
|
node: selector,
|
|
401
413
|
text
|
|
402
414
|
}, file));
|
|
403
|
-
|
|
415
|
+
const inside = context.scoped && context.nested ? globalsInside(selector) : /* @__PURE__ */ new Set();
|
|
416
|
+
diagnostics.push(...scan.globals.filter(({ node }) => !inside.has(node)).map((found) => globalDiagnostic(found, file)), ...scan.ids.map((found) => idDiagnostic(found, file)));
|
|
404
417
|
});
|
|
405
418
|
return diagnostics;
|
|
406
419
|
}
|
|
@@ -505,6 +518,65 @@ function hasLocalSubjects(list, local, nested) {
|
|
|
505
518
|
function isReportedElsewhere(parts) {
|
|
506
519
|
return parts.some(({ node, local }) => !local || find(node, (child) => child.type === "IdSelector" || child.type === "PseudoClassSelector" && child.name.toLowerCase() === "global") !== null);
|
|
507
520
|
}
|
|
521
|
+
/**
|
|
522
|
+
* The `:global` of a selector matched inside an @scope that stay inside its
|
|
523
|
+
* root: those after the last compound holding `&` or `:scope`, reached from it
|
|
524
|
+
* by a descendant or child combinator, or anywhere when no compound holds
|
|
525
|
+
* either, since the selector is then relative to the root. A `:global` in the
|
|
526
|
+
* argument of another pseudo-class may hold a selector that reaches the
|
|
527
|
+
* ancestors of the root, so it is not counted.
|
|
528
|
+
*/
|
|
529
|
+
function globalsInside(node) {
|
|
530
|
+
const inside = /* @__PURE__ */ new Set();
|
|
531
|
+
if (node.type === "SelectorList") {
|
|
532
|
+
node.children.forEach((selector) => {
|
|
533
|
+
if (selector.type !== "Selector") return;
|
|
534
|
+
for (const found of globalsInside(selector)) inside.add(found);
|
|
535
|
+
});
|
|
536
|
+
return inside;
|
|
537
|
+
}
|
|
538
|
+
const parts = node.children.toArray();
|
|
539
|
+
let anchor = -1;
|
|
540
|
+
parts.forEach((part, index) => {
|
|
541
|
+
if (part.type !== "Combinator" && find(part, isAnchor) !== null) anchor = index;
|
|
542
|
+
});
|
|
543
|
+
const start = anchor === -1 ? 0 : parts.findIndex((part, index) => index > anchor && part.type === "Combinator");
|
|
544
|
+
if (start === -1) return inside;
|
|
545
|
+
const step = parts[start];
|
|
546
|
+
if (step?.type === "Combinator" && (step.name === "+" || step.name === "~")) return inside;
|
|
547
|
+
for (const part of parts.slice(start)) if (part.type === "PseudoClassSelector" && part.name.toLowerCase() === "global") {
|
|
548
|
+
const argument = argumentOf(part);
|
|
549
|
+
if (!argument || find(argument.list, isAnchor) === null) inside.add(part);
|
|
550
|
+
}
|
|
551
|
+
return inside;
|
|
552
|
+
}
|
|
553
|
+
/**
|
|
554
|
+
* A root selector of an @scope nested in a rule is relative to that rule, so
|
|
555
|
+
* one that repeats the rule's selector roots the @scope at a copy of it
|
|
556
|
+
* nested inside it, not at the element the rule styles.
|
|
557
|
+
*/
|
|
558
|
+
function repeatedRoots(atRule, root, text, file) {
|
|
559
|
+
let parent = atRule.parent ?? void 0;
|
|
560
|
+
while (parent && parent.type !== "rule") parent = parent.parent ?? void 0;
|
|
561
|
+
if (parent?.type !== "rule") return [];
|
|
562
|
+
const own = new Set(parent.selectors.map(normalize));
|
|
563
|
+
const diagnostics = [];
|
|
564
|
+
root.children.forEach((selector) => {
|
|
565
|
+
if (selector.type !== "Selector" || !selector.loc) return;
|
|
566
|
+
if (!own.has(normalize(text.slice(selector.loc.start.offset, selector.loc.end.offset)))) return;
|
|
567
|
+
diagnostics.push(diagnosticAt$1({
|
|
568
|
+
node: selector,
|
|
569
|
+
text
|
|
570
|
+
}, file, "pure/scope", (written) => `the root of an @scope nested in ${written} is relative to it, so this one is a ${written} inside ${written}; write @scope (&)`));
|
|
571
|
+
});
|
|
572
|
+
return diagnostics;
|
|
573
|
+
}
|
|
574
|
+
function normalize(selector) {
|
|
575
|
+
return selector.trim().replace(/\s+/g, " ");
|
|
576
|
+
}
|
|
577
|
+
function isAnchor(node) {
|
|
578
|
+
return isNesting(node) || isScopePseudoClass(node);
|
|
579
|
+
}
|
|
508
580
|
function isScopePseudoClass(node) {
|
|
509
581
|
return node.type === "PseudoClassSelector" && node.name.toLowerCase() === "scope";
|
|
510
582
|
}
|
|
@@ -1947,6 +2019,21 @@ const GRADIENT_FUNCTIONS = /* @__PURE__ */ new Set([
|
|
|
1947
2019
|
"repeating-conic-gradient"
|
|
1948
2020
|
]);
|
|
1949
2021
|
const TIME_UNITS = /* @__PURE__ */ new Set(["s", "ms"]);
|
|
2022
|
+
/** Keywords and functions that spell an easing out. */
|
|
2023
|
+
const EASING_KEYWORDS = /* @__PURE__ */ new Set([
|
|
2024
|
+
"linear",
|
|
2025
|
+
"ease",
|
|
2026
|
+
"ease-in",
|
|
2027
|
+
"ease-out",
|
|
2028
|
+
"ease-in-out",
|
|
2029
|
+
"step-start",
|
|
2030
|
+
"step-end"
|
|
2031
|
+
]);
|
|
2032
|
+
const EASING_FUNCTIONS = /* @__PURE__ */ new Set([
|
|
2033
|
+
"linear",
|
|
2034
|
+
"cubic-bezier",
|
|
2035
|
+
"steps"
|
|
2036
|
+
]);
|
|
1950
2037
|
const FONT_SIZE_KEYWORDS = /* @__PURE__ */ new Set([
|
|
1951
2038
|
"xx-small",
|
|
1952
2039
|
"x-small",
|
|
@@ -1969,7 +2056,7 @@ const FONT_WEIGHT_KEYWORDS = /* @__PURE__ */ new Set([
|
|
|
1969
2056
|
const WHOLE_VALUE_PARTS = /* @__PURE__ */ new Set([
|
|
1970
2057
|
"value",
|
|
1971
2058
|
"colors",
|
|
1972
|
-
"
|
|
2059
|
+
"items"
|
|
1973
2060
|
]);
|
|
1974
2061
|
const PARTS_BY_PROPERTY = /* @__PURE__ */ new Map();
|
|
1975
2062
|
for (const [category, definition] of Object.entries(tokenCategories)) for (const [name, part] of Object.entries(definition.properties)) {
|
|
@@ -2071,6 +2158,7 @@ function contextOf(file, globalCss, global, layer) {
|
|
|
2071
2158
|
return {
|
|
2072
2159
|
file,
|
|
2073
2160
|
tokens: globalCss.tokens,
|
|
2161
|
+
values: globalCss.values,
|
|
2074
2162
|
restrictions,
|
|
2075
2163
|
breakpoints,
|
|
2076
2164
|
global,
|
|
@@ -2100,6 +2188,13 @@ function checkRoot(root, context, disabled) {
|
|
|
2100
2188
|
if (context.restrictions.has("breakpoint")) root.walkAtRules(/^media$/i, (atRule) => {
|
|
2101
2189
|
keep(atRule, checkMediaQuery(atRule, context.breakpoints, context.file));
|
|
2102
2190
|
});
|
|
2191
|
+
const own = /* @__PURE__ */ new Set();
|
|
2192
|
+
root.walkDecls((declaration) => {
|
|
2193
|
+
if (declaration.prop.startsWith("--")) own.add(declaration.prop);
|
|
2194
|
+
});
|
|
2195
|
+
root.walkAtRules(/^container$/i, (atRule) => {
|
|
2196
|
+
keep(atRule, checkContainerQuery(atRule, context, own));
|
|
2197
|
+
});
|
|
2103
2198
|
if (context.global) {
|
|
2104
2199
|
for (const token of context.tokens.values()) if (token.file === context.file) keep(token.node, checkBreakpointToken(token));
|
|
2105
2200
|
}
|
|
@@ -2161,7 +2256,7 @@ function checkDeclaredName(name, start, node, context) {
|
|
|
2161
2256
|
return report(`${name} is not declared at :root; a mode can only override a token, so declare the token in a :root rule`);
|
|
2162
2257
|
}
|
|
2163
2258
|
const category = categoryOf(name);
|
|
2164
|
-
if (category && context.restrictions.has(category)) return report(`${name} is
|
|
2259
|
+
if (category && context.restrictions.has(category)) return report(`${name} is ${aToken(category)} name and cannot be declared here; rename the custom property`);
|
|
2165
2260
|
if (!category && context.tokens.has(name)) return report(`${name} is internal to the global CSS and cannot be declared here; rename the custom property`);
|
|
2166
2261
|
return [];
|
|
2167
2262
|
}
|
|
@@ -2190,18 +2285,139 @@ function checkReferences(nodes, context, diagnostics) {
|
|
|
2190
2285
|
}
|
|
2191
2286
|
/** The declared token of the category nearest to a misspelt name, within two edits. */
|
|
2192
2287
|
function closestToken(name, category, tokens) {
|
|
2288
|
+
return closestName(name, [...tokens.values()].filter((token) => token.category === category).map((token) => token.name));
|
|
2289
|
+
}
|
|
2290
|
+
/** The name nearest to a misspelt one, within two edits. */
|
|
2291
|
+
function closestName(name, names) {
|
|
2193
2292
|
let closest = null;
|
|
2194
2293
|
let best = 3;
|
|
2195
|
-
for (const
|
|
2196
|
-
|
|
2197
|
-
const distance = editDistance(name, token.name);
|
|
2294
|
+
for (const candidate of names) {
|
|
2295
|
+
const distance = editDistance(name, candidate);
|
|
2198
2296
|
if (distance < best) {
|
|
2199
2297
|
best = distance;
|
|
2200
|
-
closest =
|
|
2298
|
+
closest = candidate;
|
|
2201
2299
|
}
|
|
2202
2300
|
}
|
|
2203
2301
|
return closest;
|
|
2204
2302
|
}
|
|
2303
|
+
/**
|
|
2304
|
+
* Report what a style query of `@container` reads that the CSS does not set.
|
|
2305
|
+
* A style query may read an internal name: a module follows a mode the global
|
|
2306
|
+
* CSS sets that way. Reported are a token name the global CSS does not
|
|
2307
|
+
* declare, as in a var(); a name without a category that neither the global
|
|
2308
|
+
* CSS nor the stylesheet declares, when it is within two edits of one that
|
|
2309
|
+
* is, since the markup may set any other in a style attribute; and a keyword
|
|
2310
|
+
* that the global CSS never gives the name it compares, when it gives it only
|
|
2311
|
+
* keywords.
|
|
2312
|
+
*/
|
|
2313
|
+
function checkContainerQuery(atRule, context, own) {
|
|
2314
|
+
const text = atRule.raws.params?.raw ?? atRule.params;
|
|
2315
|
+
const start = paramsStart(atRule);
|
|
2316
|
+
let prelude;
|
|
2317
|
+
try {
|
|
2318
|
+
prelude = parse(text, {
|
|
2319
|
+
context: "atrulePrelude",
|
|
2320
|
+
atrule: "container",
|
|
2321
|
+
positions: true
|
|
2322
|
+
});
|
|
2323
|
+
} catch {
|
|
2324
|
+
return [];
|
|
2325
|
+
}
|
|
2326
|
+
const diagnostics = [];
|
|
2327
|
+
const report = (offset, length, message) => {
|
|
2328
|
+
const first = positionIn(text, start, offset);
|
|
2329
|
+
const last = positionIn(text, start, offset + length);
|
|
2330
|
+
diagnostics.push({
|
|
2331
|
+
file: context.file,
|
|
2332
|
+
line: first.line,
|
|
2333
|
+
column: first.column,
|
|
2334
|
+
endLine: last.line,
|
|
2335
|
+
endColumn: last.column,
|
|
2336
|
+
rule: "tokens/unknown",
|
|
2337
|
+
message
|
|
2338
|
+
});
|
|
2339
|
+
};
|
|
2340
|
+
walk$1(prelude, (node) => {
|
|
2341
|
+
if (node.type !== "FeatureFunction" || node.feature.toLowerCase() !== "style") return;
|
|
2342
|
+
for (const { name, offset, value } of styleQueriesOf(node)) {
|
|
2343
|
+
const problem = unknownName(name, context, own);
|
|
2344
|
+
if (problem) {
|
|
2345
|
+
report(offset, name.length, problem);
|
|
2346
|
+
continue;
|
|
2347
|
+
}
|
|
2348
|
+
const given = context.values.get(name) ?? [];
|
|
2349
|
+
if (value && context.tokens.has(name) && isKeywordText(value.text) && given.length > 0 && given.every(isKeywordText) && !given.includes(value.text)) {
|
|
2350
|
+
const values = [...new Set(given)].join(", ");
|
|
2351
|
+
report(value.offset, value.text.length, `${value.text} is not a value the global CSS gives ${name}; it gives ${values}`);
|
|
2352
|
+
}
|
|
2353
|
+
}
|
|
2354
|
+
});
|
|
2355
|
+
return diagnostics;
|
|
2356
|
+
}
|
|
2357
|
+
/**
|
|
2358
|
+
* The custom properties a style query reads. css-tree reads `style(--a: b)`
|
|
2359
|
+
* as a declaration and leaves the rest as raw text: a name alone, conditions
|
|
2360
|
+
* joined with `and` / `or`, and ranges. Only the first form is compared.
|
|
2361
|
+
*/
|
|
2362
|
+
function styleQueriesOf(node) {
|
|
2363
|
+
const value = node.value;
|
|
2364
|
+
if (value.type === "Declaration") {
|
|
2365
|
+
if (!value.loc || !value.property.startsWith("--")) return [];
|
|
2366
|
+
const offset = value.loc.start.offset;
|
|
2367
|
+
const raw = value.value.type === "Raw" ? value.value : null;
|
|
2368
|
+
const text = raw?.value.trim() ?? "";
|
|
2369
|
+
const compared = raw?.loc && text !== "" ? {
|
|
2370
|
+
text,
|
|
2371
|
+
offset: raw.loc.start.offset + raw.value.indexOf(text)
|
|
2372
|
+
} : null;
|
|
2373
|
+
return [{
|
|
2374
|
+
name: value.property,
|
|
2375
|
+
offset,
|
|
2376
|
+
value: compared
|
|
2377
|
+
}];
|
|
2378
|
+
}
|
|
2379
|
+
if (value.type !== "Raw" || !value.loc) return [];
|
|
2380
|
+
const queries = [];
|
|
2381
|
+
for (const match of value.value.matchAll(/(?:^|(?<![\w-])\()\s*(--[\w-]+)/g)) {
|
|
2382
|
+
const name = match[1];
|
|
2383
|
+
const offset = value.loc.start.offset + match.index + match[0].length - name.length;
|
|
2384
|
+
queries.push({
|
|
2385
|
+
name,
|
|
2386
|
+
offset,
|
|
2387
|
+
value: null
|
|
2388
|
+
});
|
|
2389
|
+
}
|
|
2390
|
+
return queries;
|
|
2391
|
+
}
|
|
2392
|
+
/** Why a name a style query reads is unknown, or null. */
|
|
2393
|
+
function unknownName(name, context, own) {
|
|
2394
|
+
const category = categoryOf(name);
|
|
2395
|
+
if (category) {
|
|
2396
|
+
if (!context.restrictions.has(category) || context.tokens.has(name)) return null;
|
|
2397
|
+
const suggestion = closestToken(name, category, context.tokens);
|
|
2398
|
+
return `${name} is not defined in the global CSS${suggestion ? `; did you mean ${suggestion}?` : ""}`;
|
|
2399
|
+
}
|
|
2400
|
+
if (context.tokens.has(name) || own.has(name)) return null;
|
|
2401
|
+
const suggestion = closestName(name, [...[...context.tokens.values()].filter((token) => token.category === null).map((token) => token.name), ...own]);
|
|
2402
|
+
return suggestion ? `${name} is declared neither in the global CSS nor in this stylesheet; did you mean ${suggestion}?` : null;
|
|
2403
|
+
}
|
|
2404
|
+
/** A value that is one identifier, such as a mode a custom property names. */
|
|
2405
|
+
function isKeywordText(text) {
|
|
2406
|
+
return /^-?[a-z_][\w-]*$/i.test(text);
|
|
2407
|
+
}
|
|
2408
|
+
/** The position of an offset in a prelude that starts at `start`. */
|
|
2409
|
+
function positionIn(text, start, offset) {
|
|
2410
|
+
const before = text.slice(0, offset);
|
|
2411
|
+
const lastBreak = before.lastIndexOf("\n");
|
|
2412
|
+
if (lastBreak === -1) return {
|
|
2413
|
+
line: start.line,
|
|
2414
|
+
column: start.column + offset
|
|
2415
|
+
};
|
|
2416
|
+
return {
|
|
2417
|
+
line: start.line + before.split("\n").length - 1,
|
|
2418
|
+
column: offset - lastBreak
|
|
2419
|
+
};
|
|
2420
|
+
}
|
|
2205
2421
|
/** Optimal string alignment distance: a swap of two neighbours counts as one edit. */
|
|
2206
2422
|
function editDistance(a, b) {
|
|
2207
2423
|
const rows = Array.from({ length: a.length + 1 }, (_, i) => Array.from({ length: b.length + 1 }, (_, j) => i === 0 ? j : j === 0 ? i : 0));
|
|
@@ -2229,13 +2445,13 @@ function checkPart(part, nodes, scope) {
|
|
|
2229
2445
|
case "gradient-colors":
|
|
2230
2446
|
checkColors(nodes, scope, "unknown");
|
|
2231
2447
|
break;
|
|
2232
|
-
case "
|
|
2233
|
-
|
|
2448
|
+
case "items":
|
|
2449
|
+
checkItems(nodes, scope);
|
|
2234
2450
|
break;
|
|
2235
2451
|
case "font":
|
|
2236
2452
|
checkFont(nodes, scope);
|
|
2237
2453
|
break;
|
|
2238
|
-
case "
|
|
2454
|
+
case "transition": checkTransition(nodes, scope);
|
|
2239
2455
|
}
|
|
2240
2456
|
}
|
|
2241
2457
|
function checkQuantity(node, scope, arithmetic) {
|
|
@@ -2309,23 +2525,25 @@ function checkChannelFunction(node, scope) {
|
|
|
2309
2525
|
else reportRaw(scope, node);
|
|
2310
2526
|
}
|
|
2311
2527
|
function checkGradient(node, scope) {
|
|
2312
|
-
layersOf(node.children.toArray())
|
|
2313
|
-
|
|
2528
|
+
const argumentsOf = layersOf(node.children.toArray());
|
|
2529
|
+
argumentsOf.forEach((argument, index) => {
|
|
2530
|
+
const token = loneVar(argument);
|
|
2531
|
+
checkColors(argument, scope, index === 0 && (argument.some((child) => child.type === "Identifier" && !scope.restriction.keywords.has(child.name.toLowerCase()) && !isRawColorName(child, scope)) || token !== null && !allowsReference(token, scope) && argumentsOf.length > 2) ? "unknown" : "stop-color");
|
|
2314
2532
|
});
|
|
2315
2533
|
}
|
|
2316
|
-
function
|
|
2534
|
+
function checkItems(nodes, scope) {
|
|
2317
2535
|
for (const layer of layersOf(nodes)) {
|
|
2318
2536
|
const token = loneVar(layer);
|
|
2319
2537
|
if (token) {
|
|
2320
2538
|
checkToken(token, scope);
|
|
2321
|
-
|
|
2539
|
+
checkItems(fallbackOf(token), scope);
|
|
2322
2540
|
} else if (layer.length !== 1 || !isKeyword(layer[0], scope.restriction)) reportRaw(scope, layer[0], layer[layer.length - 1]);
|
|
2323
2541
|
}
|
|
2324
2542
|
}
|
|
2325
2543
|
/**
|
|
2326
|
-
* `font: <style, variant, weight, width> <size> / <line-height> <
|
|
2327
|
-
* slash pins the size before it
|
|
2328
|
-
* raw values can be told apart.
|
|
2544
|
+
* `font: <style, variant, weight, width> <size> / <line-height> <families>`.
|
|
2545
|
+
* The slash pins the size before it, the line height after it and the families
|
|
2546
|
+
* after that; without one only raw values can be told apart.
|
|
2329
2547
|
*/
|
|
2330
2548
|
function checkFont(nodes, scope) {
|
|
2331
2549
|
const slash = nodes.findIndex((node) => node.type === "Operator" && node.value === "/");
|
|
@@ -2342,15 +2560,40 @@ function checkFont(nodes, scope) {
|
|
|
2342
2560
|
for (const node of nodes) if (node.type === "Dimension" || node.type === "Percentage") checkQuantity(node, scope, "none");
|
|
2343
2561
|
else if (isIdentifierIn(node, FONT_SIZE_KEYWORDS)) reportRaw(scope, node);
|
|
2344
2562
|
break;
|
|
2345
|
-
case "font-weight":
|
|
2346
|
-
|
|
2563
|
+
case "font-weight":
|
|
2564
|
+
for (const node of size ? nodes.slice(0, slash - 1) : nodes) if (node.type === "Number") checkQuantity(node, scope, "none");
|
|
2565
|
+
else if (isIdentifierIn(node, FONT_WEIGHT_KEYWORDS)) reportRaw(scope, node);
|
|
2566
|
+
break;
|
|
2567
|
+
case "font-family": checkItems(nodes.slice(familiesStart(nodes, slash)), scope);
|
|
2347
2568
|
}
|
|
2348
2569
|
}
|
|
2349
|
-
/**
|
|
2350
|
-
|
|
2570
|
+
/**
|
|
2571
|
+
* Where the families of `font` start. Without a slash a var() before the
|
|
2572
|
+
* first comma may be the size as well as a family, so only the names that end
|
|
2573
|
+
* it are taken: a string, or identifiers back to the size.
|
|
2574
|
+
*/
|
|
2575
|
+
function familiesStart(nodes, slash) {
|
|
2576
|
+
if (slash !== -1) return slash + 2;
|
|
2577
|
+
const comma = nodes.findIndex((node) => node.type === "Operator" && node.value === ",");
|
|
2578
|
+
const end = comma === -1 ? nodes.length : comma;
|
|
2579
|
+
if (nodes[end - 1]?.type === "String") return end - 1;
|
|
2580
|
+
let start = end;
|
|
2581
|
+
while (nodes[start - 1]?.type === "Identifier" && (start === end || !isIdentifierIn(nodes[start - 1], FONT_SIZE_KEYWORDS))) start--;
|
|
2582
|
+
return start;
|
|
2583
|
+
}
|
|
2584
|
+
/** A var() in `transition` or `animation` may be a time, an easing or a name; only raw values are told apart. */
|
|
2585
|
+
function checkTransition(nodes, scope) {
|
|
2351
2586
|
for (const node of nodes) walk$1(node, (child) => {
|
|
2352
|
-
if (child.type === "
|
|
2353
|
-
|
|
2587
|
+
if (child.type === "Function" && child.name.toLowerCase() === "var") {
|
|
2588
|
+
checkTransition(fallbackOf(child), scope);
|
|
2589
|
+
return;
|
|
2590
|
+
}
|
|
2591
|
+
switch (scope.restriction.category) {
|
|
2592
|
+
case "duration":
|
|
2593
|
+
if (child.type === "Dimension" && TIME_UNITS.has(child.unit.toLowerCase())) checkQuantity(child, scope, "none");
|
|
2594
|
+
break;
|
|
2595
|
+
case "easing": if (isEasing(child) && !isKeyword(child, scope.restriction)) reportRaw(scope, child);
|
|
2596
|
+
}
|
|
2354
2597
|
});
|
|
2355
2598
|
}
|
|
2356
2599
|
/**
|
|
@@ -2366,7 +2609,11 @@ function checkToken(node, scope, inShorthand = false) {
|
|
|
2366
2609
|
const own = categoryOf(name.name);
|
|
2367
2610
|
if (own === category) return;
|
|
2368
2611
|
if (own === null && scope.context.tokens.has(name.name)) return;
|
|
2369
|
-
report(scope, node, node, inShorthand ? `${name.name} is taken for the ${category} of this shorthand and is not
|
|
2612
|
+
report(scope, node, node, inShorthand ? `${name.name} is taken for the ${category} of this shorthand and is not ${aToken(category)}; ${hint}, or write the longhand` : `${name.name} is not ${aToken(category)}; ${hint}`);
|
|
2613
|
+
}
|
|
2614
|
+
/** `a color token`, `an easing token`. */
|
|
2615
|
+
function aToken(category) {
|
|
2616
|
+
return `${/^[aeiou]/.test(category) ? "an" : "a"} ${category} token`;
|
|
2370
2617
|
}
|
|
2371
2618
|
/** The components of the fallback of a `var()`, which css-tree keeps as raw text. */
|
|
2372
2619
|
function fallbackOf(node) {
|
|
@@ -2436,6 +2683,10 @@ function isKeyword(node, restriction) {
|
|
|
2436
2683
|
function isIdentifierIn(node, names) {
|
|
2437
2684
|
return node.type === "Identifier" && names.has(node.name.toLowerCase());
|
|
2438
2685
|
}
|
|
2686
|
+
function isEasing(node) {
|
|
2687
|
+
if (node.type === "Function") return EASING_FUNCTIONS.has(node.name.toLowerCase());
|
|
2688
|
+
return isIdentifierIn(node, EASING_KEYWORDS);
|
|
2689
|
+
}
|
|
2439
2690
|
/** Named and system colors, which the grammar knows; the category's own keywords are not raw. */
|
|
2440
2691
|
function isRawColorName(node, scope) {
|
|
2441
2692
|
if (scope.restriction.keywords.has(node.name.toLowerCase())) return false;
|
|
@@ -2445,4 +2696,544 @@ function isZero(node) {
|
|
|
2445
2696
|
return Number(node.value) === 0;
|
|
2446
2697
|
}
|
|
2447
2698
|
//#endregion
|
|
2448
|
-
|
|
2699
|
+
//#region src/dtcg.ts
|
|
2700
|
+
/** Read the global CSS a config lists and write its tokens as a DTCG resolver document. */
|
|
2701
|
+
async function exportTokens(config) {
|
|
2702
|
+
return dtcgFrom(await loadGlobalCss(config));
|
|
2703
|
+
}
|
|
2704
|
+
/** Write the tokens of global CSS already read as a DTCG resolver document. Pure. */
|
|
2705
|
+
function dtcgFrom(globalCss) {
|
|
2706
|
+
const { tokens } = globalCss;
|
|
2707
|
+
for (const token of tokens.values()) checkName(token);
|
|
2708
|
+
const writer = new Writer(tokens);
|
|
2709
|
+
const write = (values) => Object.fromEntries([...values].map(([name, text]) => [name.slice(2), writer.token(text, tokens.get(name)?.category ?? null)]));
|
|
2710
|
+
const modifiers = [...modesOf(globalCss)].map(([name, values]) => ({
|
|
2711
|
+
type: "modifier",
|
|
2712
|
+
name,
|
|
2713
|
+
contexts: {
|
|
2714
|
+
off: [],
|
|
2715
|
+
on: [write(values)]
|
|
2716
|
+
},
|
|
2717
|
+
default: "off"
|
|
2718
|
+
}));
|
|
2719
|
+
return {
|
|
2720
|
+
version: "2025.10",
|
|
2721
|
+
resolutionOrder: [{
|
|
2722
|
+
type: "set",
|
|
2723
|
+
name: ":root",
|
|
2724
|
+
sources: [write([...tokens.values()].map(({ name, text }) => [name, text]))]
|
|
2725
|
+
}, ...modifiers]
|
|
2726
|
+
};
|
|
2727
|
+
}
|
|
2728
|
+
/** Refuse a name DTCG cannot hold: it forbids `.`, `{` and `}` in a name and `$` at its start. */
|
|
2729
|
+
function checkName({ name, file, node }) {
|
|
2730
|
+
if (!/[.{}]/.test(name) && !name.startsWith("--$")) return;
|
|
2731
|
+
const start = node.source?.start;
|
|
2732
|
+
throw new ConfigError(`${path.relative(process.cwd(), file)}:${start?.line ?? 1}:${start?.column ?? 1}: ${name} cannot be a DTCG token name: DTCG forbids ".", "{" and "}" in names, and "$" at their start`);
|
|
2733
|
+
}
|
|
2734
|
+
/**
|
|
2735
|
+
* The modes by name, each with the value it gives each token it overrides, in
|
|
2736
|
+
* cascade order: what the global CSS declares outside `:root` for a name
|
|
2737
|
+
* `:root` declares. A place that overrides no token is no mode.
|
|
2738
|
+
*/
|
|
2739
|
+
function modesOf({ files, tokens }) {
|
|
2740
|
+
const conditions = conditionsOf(files);
|
|
2741
|
+
const modes = /* @__PURE__ */ new Map();
|
|
2742
|
+
for (const sheet of files) sheet.root.walkDecls((decl) => {
|
|
2743
|
+
if (!tokens.has(decl.prop) || isForeign(decl) || declaresToken(decl, sheet.conditional)) return;
|
|
2744
|
+
const name = placeOf(decl, conditions.get(sheet.file) ?? "");
|
|
2745
|
+
modes.set(name, (modes.get(name) ?? /* @__PURE__ */ new Map()).set(decl.prop, decl.value));
|
|
2746
|
+
});
|
|
2747
|
+
return modes;
|
|
2748
|
+
}
|
|
2749
|
+
/**
|
|
2750
|
+
* The conditions of the imports each stylesheet read only under conditions
|
|
2751
|
+
* is first reached through, outermost first.
|
|
2752
|
+
*/
|
|
2753
|
+
function conditionsOf(files) {
|
|
2754
|
+
const byFile = new Map(files.map((sheet) => [sheet.file, sheet]));
|
|
2755
|
+
const conditions = /* @__PURE__ */ new Map();
|
|
2756
|
+
const reached = /* @__PURE__ */ new Set();
|
|
2757
|
+
const visit = (sheet, outer) => {
|
|
2758
|
+
for (const { file, condition } of sheet.imports) {
|
|
2759
|
+
const target = byFile.get(file);
|
|
2760
|
+
if (!target || reached.has(file)) continue;
|
|
2761
|
+
reached.add(file);
|
|
2762
|
+
const inner = target.conditional ? joined([outer, condition ?? ""]) : "";
|
|
2763
|
+
if (target.conditional) conditions.set(file, inner);
|
|
2764
|
+
visit(target, inner);
|
|
2765
|
+
}
|
|
2766
|
+
};
|
|
2767
|
+
for (const sheet of files) if (sheet.listed) visit(sheet, "");
|
|
2768
|
+
return conditions;
|
|
2769
|
+
}
|
|
2770
|
+
/** A place by its conditions: the rules and at-rules around it, outermost first, without `@layer`. */
|
|
2771
|
+
function placeOf(node, conditions) {
|
|
2772
|
+
const parts = [];
|
|
2773
|
+
for (let parent = node.parent; parent && parent.type !== "root"; parent = parent.parent) if (parent.type === "rule") parts.unshift(parent.selector);
|
|
2774
|
+
else if (parent.type === "atrule" && parent.name.toLowerCase() !== "layer") {
|
|
2775
|
+
const { name, params } = parent;
|
|
2776
|
+
parts.unshift(`@${name} ${params}`);
|
|
2777
|
+
}
|
|
2778
|
+
return joined([conditions, ...parts]);
|
|
2779
|
+
}
|
|
2780
|
+
function joined(parts) {
|
|
2781
|
+
return parts.join(" ").replace(/\s+/g, " ").trim();
|
|
2782
|
+
}
|
|
2783
|
+
/** Writes the values of the custom properties of the global CSS as tokens. */
|
|
2784
|
+
var Writer = class {
|
|
2785
|
+
tokens;
|
|
2786
|
+
references = /* @__PURE__ */ new Map();
|
|
2787
|
+
types = /* @__PURE__ */ new Map();
|
|
2788
|
+
constructor(tokens) {
|
|
2789
|
+
this.tokens = tokens;
|
|
2790
|
+
}
|
|
2791
|
+
token(text, category) {
|
|
2792
|
+
const nodes = valueNodes(text);
|
|
2793
|
+
if (nodes) {
|
|
2794
|
+
const alias = this.alias(nodes);
|
|
2795
|
+
if (alias) return alias.$type ? alias : { $value: alias.$value };
|
|
2796
|
+
const token = (category ? READERS[category] : readInternal)(nodes, this);
|
|
2797
|
+
if (token) return token;
|
|
2798
|
+
}
|
|
2799
|
+
return { $value: text.trim() };
|
|
2800
|
+
}
|
|
2801
|
+
/** A color, or an alias of a color token. */
|
|
2802
|
+
color(node) {
|
|
2803
|
+
const written = color(node);
|
|
2804
|
+
if (written) return written;
|
|
2805
|
+
const alias = this.alias([node]);
|
|
2806
|
+
return alias?.$type === "color" ? alias.$value : null;
|
|
2807
|
+
}
|
|
2808
|
+
/**
|
|
2809
|
+
* The alias a value that is one var() of a token is, with the type of the
|
|
2810
|
+
* token its chain of aliases ends at; null when the chain loops.
|
|
2811
|
+
*/
|
|
2812
|
+
alias(nodes) {
|
|
2813
|
+
const target = this.reference(nodes);
|
|
2814
|
+
const end = target === null ? null : this.end(target);
|
|
2815
|
+
if (target === null || end === null) return null;
|
|
2816
|
+
return {
|
|
2817
|
+
$type: this.typeOf(end),
|
|
2818
|
+
$value: `{${target.slice(2)}}`
|
|
2819
|
+
};
|
|
2820
|
+
}
|
|
2821
|
+
/** The token a value that is one var() of a token names, whatever its fallback. */
|
|
2822
|
+
reference(nodes) {
|
|
2823
|
+
const [node] = nodes;
|
|
2824
|
+
if (nodes.length !== 1 || node.type !== "Function" || node.name.toLowerCase() !== "var") return null;
|
|
2825
|
+
const [name] = node.children.toArray();
|
|
2826
|
+
return name?.type === "Identifier" && this.tokens.has(name.name) ? name.name : null;
|
|
2827
|
+
}
|
|
2828
|
+
/** The token a chain of aliases from a token ends at, or null when it loops. */
|
|
2829
|
+
end(name) {
|
|
2830
|
+
const seen = /* @__PURE__ */ new Set();
|
|
2831
|
+
let current = name;
|
|
2832
|
+
for (let next = this.referenceOf(current); next !== null; next = this.referenceOf(current)) {
|
|
2833
|
+
seen.add(current);
|
|
2834
|
+
if (seen.has(next)) return null;
|
|
2835
|
+
current = next;
|
|
2836
|
+
}
|
|
2837
|
+
return current;
|
|
2838
|
+
}
|
|
2839
|
+
/** The token the value a token is declared with is one var() of, or null. */
|
|
2840
|
+
referenceOf(name) {
|
|
2841
|
+
if (!this.references.has(name)) {
|
|
2842
|
+
const nodes = valueNodes(this.tokens.get(name)?.text ?? "");
|
|
2843
|
+
this.references.set(name, nodes && this.reference(nodes));
|
|
2844
|
+
}
|
|
2845
|
+
return this.references.get(name) ?? null;
|
|
2846
|
+
}
|
|
2847
|
+
typeOf(name) {
|
|
2848
|
+
const token = this.tokens.get(name);
|
|
2849
|
+
if (!this.types.has(name) && token) {
|
|
2850
|
+
this.types.set(name, void 0);
|
|
2851
|
+
this.types.set(name, this.token(token.text, token.category).$type);
|
|
2852
|
+
}
|
|
2853
|
+
return this.types.get(name);
|
|
2854
|
+
}
|
|
2855
|
+
};
|
|
2856
|
+
function valueNodes(text) {
|
|
2857
|
+
let value;
|
|
2858
|
+
try {
|
|
2859
|
+
value = parse(text, { context: "value" });
|
|
2860
|
+
} catch {
|
|
2861
|
+
return null;
|
|
2862
|
+
}
|
|
2863
|
+
return value.type === "Value" ? value.children.toArray() : null;
|
|
2864
|
+
}
|
|
2865
|
+
/** The token a value written in the form of `type` is, or null. */
|
|
2866
|
+
function typed(type, value) {
|
|
2867
|
+
return value === null ? null : {
|
|
2868
|
+
$type: type,
|
|
2869
|
+
$value: value
|
|
2870
|
+
};
|
|
2871
|
+
}
|
|
2872
|
+
function one(nodes, read) {
|
|
2873
|
+
return nodes.length === 1 ? read(nodes[0]) : null;
|
|
2874
|
+
}
|
|
2875
|
+
function dimensionToken(nodes) {
|
|
2876
|
+
return typed("dimension", one(nodes, dimension));
|
|
2877
|
+
}
|
|
2878
|
+
/** How each category's value is read as a token; null when DTCG has no form for it. */
|
|
2879
|
+
const READERS = {
|
|
2880
|
+
color: (nodes) => typed("color", one(nodes, color)),
|
|
2881
|
+
spacing: dimensionToken,
|
|
2882
|
+
radius: dimensionToken,
|
|
2883
|
+
shadow: (nodes, writer) => typed("shadow", shadow(nodes, writer)),
|
|
2884
|
+
"font-family": (nodes) => typed("fontFamily", fontFamily(nodes)),
|
|
2885
|
+
"font-size": dimensionToken,
|
|
2886
|
+
"font-weight": (nodes) => typed("fontWeight", one(nodes, fontWeight)),
|
|
2887
|
+
"line-height": (nodes) => typed("number", one(nodes, number)) ?? dimensionToken(nodes),
|
|
2888
|
+
"letter-spacing": dimensionToken,
|
|
2889
|
+
"z-index": (nodes) => typed("number", one(nodes, integer)),
|
|
2890
|
+
duration: (nodes) => typed("duration", one(nodes, duration)),
|
|
2891
|
+
easing: (nodes) => typed("cubicBezier", one(nodes, easing)),
|
|
2892
|
+
breakpoint: dimensionToken
|
|
2893
|
+
};
|
|
2894
|
+
/** A name without a category is typed by how its value is written. */
|
|
2895
|
+
const readInternal = (nodes) => typed("color", one(nodes, color)) ?? typed("dimension", one(nodes, (node) => node.type === "Dimension" ? dimension(node) : null)) ?? typed("duration", one(nodes, duration)) ?? typed("number", one(nodes, number)) ?? typed("cubicBezier", one(nodes, cubicBezier));
|
|
2896
|
+
function number(node) {
|
|
2897
|
+
return node.type === "Number" ? Number(node.value) : null;
|
|
2898
|
+
}
|
|
2899
|
+
function integer(node) {
|
|
2900
|
+
const value = number(node);
|
|
2901
|
+
return value !== null && Number.isInteger(value) ? value : null;
|
|
2902
|
+
}
|
|
2903
|
+
/** A length in `px` or `rem`, or zero, which is a length in any unit. */
|
|
2904
|
+
function dimension(node) {
|
|
2905
|
+
if (node.type === "Number") return Number(node.value) === 0 ? {
|
|
2906
|
+
value: 0,
|
|
2907
|
+
unit: "px"
|
|
2908
|
+
} : null;
|
|
2909
|
+
if (node.type !== "Dimension") return null;
|
|
2910
|
+
const unit = node.unit.toLowerCase();
|
|
2911
|
+
const value = Number(node.value);
|
|
2912
|
+
if (unit === "px" || unit === "rem") return {
|
|
2913
|
+
value,
|
|
2914
|
+
unit
|
|
2915
|
+
};
|
|
2916
|
+
return value === 0 && lexer.matchType("length", node).matched !== null ? {
|
|
2917
|
+
value: 0,
|
|
2918
|
+
unit: "px"
|
|
2919
|
+
} : null;
|
|
2920
|
+
}
|
|
2921
|
+
function duration(node) {
|
|
2922
|
+
if (node.type !== "Dimension") return null;
|
|
2923
|
+
const unit = node.unit.toLowerCase();
|
|
2924
|
+
return unit === "ms" || unit === "s" ? {
|
|
2925
|
+
value: Number(node.value),
|
|
2926
|
+
unit
|
|
2927
|
+
} : null;
|
|
2928
|
+
}
|
|
2929
|
+
function fontWeight(node) {
|
|
2930
|
+
if (node.type === "Identifier") {
|
|
2931
|
+
const keyword = node.name.toLowerCase();
|
|
2932
|
+
return keyword === "normal" || keyword === "bold" ? keyword : null;
|
|
2933
|
+
}
|
|
2934
|
+
const value = number(node);
|
|
2935
|
+
return value !== null && value >= 1 && value <= 1e3 ? value : null;
|
|
2936
|
+
}
|
|
2937
|
+
/** The families, each a string or the identifiers of an unquoted name. */
|
|
2938
|
+
function fontFamily(nodes) {
|
|
2939
|
+
const families = [];
|
|
2940
|
+
for (const family of items(nodes)) {
|
|
2941
|
+
const [first] = family;
|
|
2942
|
+
if (family.length === 1 && first.type === "String") families.push(first.value);
|
|
2943
|
+
else if (family.length > 0 && family.every((node) => node.type === "Identifier")) families.push(family.map((node) => node.name).join(" "));
|
|
2944
|
+
else return null;
|
|
2945
|
+
}
|
|
2946
|
+
if (families.length === 1 && CSS_WIDE_KEYWORDS.has(families[0].toLowerCase())) return null;
|
|
2947
|
+
return families.length === 1 ? families[0] : families;
|
|
2948
|
+
}
|
|
2949
|
+
const EASINGS = /* @__PURE__ */ new Map([
|
|
2950
|
+
["linear", [
|
|
2951
|
+
0,
|
|
2952
|
+
0,
|
|
2953
|
+
1,
|
|
2954
|
+
1
|
|
2955
|
+
]],
|
|
2956
|
+
["ease", [
|
|
2957
|
+
.25,
|
|
2958
|
+
.1,
|
|
2959
|
+
.25,
|
|
2960
|
+
1
|
|
2961
|
+
]],
|
|
2962
|
+
["ease-in", [
|
|
2963
|
+
.42,
|
|
2964
|
+
0,
|
|
2965
|
+
1,
|
|
2966
|
+
1
|
|
2967
|
+
]],
|
|
2968
|
+
["ease-out", [
|
|
2969
|
+
0,
|
|
2970
|
+
0,
|
|
2971
|
+
.58,
|
|
2972
|
+
1
|
|
2973
|
+
]],
|
|
2974
|
+
["ease-in-out", [
|
|
2975
|
+
.42,
|
|
2976
|
+
0,
|
|
2977
|
+
.58,
|
|
2978
|
+
1
|
|
2979
|
+
]]
|
|
2980
|
+
]);
|
|
2981
|
+
function easing(node) {
|
|
2982
|
+
return node.type === "Identifier" ? EASINGS.get(node.name.toLowerCase()) ?? null : cubicBezier(node);
|
|
2983
|
+
}
|
|
2984
|
+
function cubicBezier(node) {
|
|
2985
|
+
if (node.type !== "Function" || node.name.toLowerCase() !== "cubic-bezier") return null;
|
|
2986
|
+
const points = items(node.children.toArray()).map((item) => item.length === 1 ? number(item[0]) : null);
|
|
2987
|
+
if (points.length !== 4 || points.includes(null)) return null;
|
|
2988
|
+
const [x1, y1, x2, y2] = points;
|
|
2989
|
+
return [x1, x2].every((x) => x >= 0 && x <= 1) ? [
|
|
2990
|
+
x1,
|
|
2991
|
+
y1,
|
|
2992
|
+
x2,
|
|
2993
|
+
y2
|
|
2994
|
+
] : null;
|
|
2995
|
+
}
|
|
2996
|
+
function shadow(nodes, writer) {
|
|
2997
|
+
const shadows = [];
|
|
2998
|
+
for (const layer of items(nodes)) {
|
|
2999
|
+
const read = shadowOf(layer, writer);
|
|
3000
|
+
if (!read) return null;
|
|
3001
|
+
shadows.push(read);
|
|
3002
|
+
}
|
|
3003
|
+
return shadows.length === 1 ? shadows[0] : shadows;
|
|
3004
|
+
}
|
|
3005
|
+
/** One shadow: two to four lengths together, a color, and `inset`, in any order. */
|
|
3006
|
+
function shadowOf(layer, writer) {
|
|
3007
|
+
const read = layer.map((node) => node.type === "Number" || node.type === "Dimension" ? dimension(node) : null);
|
|
3008
|
+
const at = read.flatMap((length, index) => length ? [index] : []);
|
|
3009
|
+
const [first, last] = [at[0] ?? -1, at.at(-1) ?? -1];
|
|
3010
|
+
const lengths = read.slice(first, last + 1);
|
|
3011
|
+
if (first === -1 || lengths.length < 2 || lengths.length > 4 || lengths.includes(null)) return null;
|
|
3012
|
+
let inset = false;
|
|
3013
|
+
let color = null;
|
|
3014
|
+
for (const node of [...layer.slice(0, first), ...layer.slice(last + 1)]) if (!inset && node.type === "Identifier" && node.name.toLowerCase() === "inset") inset = true;
|
|
3015
|
+
else if (color === null) {
|
|
3016
|
+
color = writer.color(node);
|
|
3017
|
+
if (color === null) return null;
|
|
3018
|
+
} else return null;
|
|
3019
|
+
if (color === null) return null;
|
|
3020
|
+
const zero = {
|
|
3021
|
+
value: 0,
|
|
3022
|
+
unit: "px"
|
|
3023
|
+
};
|
|
3024
|
+
const [offsetX, offsetY, blur = zero, spread = zero] = lengths;
|
|
3025
|
+
return {
|
|
3026
|
+
color,
|
|
3027
|
+
offsetX,
|
|
3028
|
+
offsetY,
|
|
3029
|
+
blur,
|
|
3030
|
+
spread,
|
|
3031
|
+
...inset && { inset: true }
|
|
3032
|
+
};
|
|
3033
|
+
}
|
|
3034
|
+
/** A color written with its channels, or `transparent`. */
|
|
3035
|
+
function color(node) {
|
|
3036
|
+
if (node.type === "Hash") return hex(node.value);
|
|
3037
|
+
if (node.type === "Identifier") return node.name.toLowerCase() === "transparent" ? {
|
|
3038
|
+
colorSpace: "srgb",
|
|
3039
|
+
components: [
|
|
3040
|
+
0,
|
|
3041
|
+
0,
|
|
3042
|
+
0
|
|
3043
|
+
],
|
|
3044
|
+
alpha: 0
|
|
3045
|
+
} : null;
|
|
3046
|
+
if (node.type !== "Function") return null;
|
|
3047
|
+
let args = node.children.toArray();
|
|
3048
|
+
let space = FUNCTIONS.get(node.name.toLowerCase());
|
|
3049
|
+
if (node.name.toLowerCase() === "color") {
|
|
3050
|
+
const [named, ...rest] = args;
|
|
3051
|
+
space = named?.type === "Identifier" ? PREDEFINED.get(named.name.toLowerCase()) : void 0;
|
|
3052
|
+
args = rest;
|
|
3053
|
+
}
|
|
3054
|
+
const written = space && channelsOf(args);
|
|
3055
|
+
if (!space || !written) return null;
|
|
3056
|
+
const components = written.channels.map((value, index) => space.channels[index](value));
|
|
3057
|
+
if (components.includes(null)) return null;
|
|
3058
|
+
const result = {
|
|
3059
|
+
colorSpace: space.colorSpace,
|
|
3060
|
+
components
|
|
3061
|
+
};
|
|
3062
|
+
if (!written.alpha) return result;
|
|
3063
|
+
const alpha = alphaOf(written.alpha);
|
|
3064
|
+
return alpha === null ? null : {
|
|
3065
|
+
...result,
|
|
3066
|
+
alpha
|
|
3067
|
+
};
|
|
3068
|
+
}
|
|
3069
|
+
/** The channels and the alpha of a color function, which the legacy syntax separates with commas. */
|
|
3070
|
+
function channelsOf(args) {
|
|
3071
|
+
let channels;
|
|
3072
|
+
let alpha;
|
|
3073
|
+
if (args.some((arg) => arg.type === "Operator" && arg.value === ",")) {
|
|
3074
|
+
const values = items(args);
|
|
3075
|
+
if (values.some((value) => value.length !== 1)) return null;
|
|
3076
|
+
[channels, alpha] = [values.slice(0, 3).flat(), values.slice(3).flat()];
|
|
3077
|
+
} else {
|
|
3078
|
+
const slash = args.findIndex((arg) => arg.type === "Operator" && arg.value === "/");
|
|
3079
|
+
[channels, alpha] = slash === -1 ? [args, []] : [args.slice(0, slash), args.slice(slash + 1)];
|
|
3080
|
+
if (slash !== -1 && alpha.length === 0) return null;
|
|
3081
|
+
}
|
|
3082
|
+
return channels.length === 3 && alpha.length <= 1 ? {
|
|
3083
|
+
channels,
|
|
3084
|
+
alpha: alpha[0]
|
|
3085
|
+
} : null;
|
|
3086
|
+
}
|
|
3087
|
+
function scaled(value) {
|
|
3088
|
+
return Number(value.toPrecision(15));
|
|
3089
|
+
}
|
|
3090
|
+
/** A channel that is a number, or a percentage of `reference`. */
|
|
3091
|
+
function percent(reference) {
|
|
3092
|
+
return (node) => {
|
|
3093
|
+
if (node.type === "Number") return Number(node.value);
|
|
3094
|
+
if (node.type === "Percentage") return scaled(Number(node.value) * reference / 100);
|
|
3095
|
+
return none(node);
|
|
3096
|
+
};
|
|
3097
|
+
}
|
|
3098
|
+
/** A red, green or blue channel of rgb(): a number of 255, or a percentage. */
|
|
3099
|
+
const rgb = (node) => node.type === "Number" ? scaled(Number(node.value) / 255) : percent(1)(node);
|
|
3100
|
+
const DEGREES = /* @__PURE__ */ new Map([
|
|
3101
|
+
["deg", 1],
|
|
3102
|
+
["grad", 360 / 400],
|
|
3103
|
+
["rad", 180 / Math.PI],
|
|
3104
|
+
["turn", 360]
|
|
3105
|
+
]);
|
|
3106
|
+
/** A hue in degrees, from 0 up to 360. */
|
|
3107
|
+
const hue = (node) => {
|
|
3108
|
+
if (node.type !== "Number" && node.type !== "Dimension") return none(node);
|
|
3109
|
+
const factor = node.type === "Number" ? 1 : DEGREES.get(node.unit.toLowerCase());
|
|
3110
|
+
if (factor === void 0) return null;
|
|
3111
|
+
const degrees = scaled(Number(node.value) * factor);
|
|
3112
|
+
return degrees >= 0 && degrees < 360 ? degrees : scaled((degrees % 360 + 360) % 360);
|
|
3113
|
+
};
|
|
3114
|
+
function none(node) {
|
|
3115
|
+
return node.type === "Identifier" && node.name.toLowerCase() === "none" ? "none" : null;
|
|
3116
|
+
}
|
|
3117
|
+
function alphaOf(node) {
|
|
3118
|
+
if (node.type === "Number") return Number(node.value);
|
|
3119
|
+
if (node.type === "Percentage") return scaled(Number(node.value) / 100);
|
|
3120
|
+
return null;
|
|
3121
|
+
}
|
|
3122
|
+
const FUNCTIONS = /* @__PURE__ */ new Map([
|
|
3123
|
+
["rgb", {
|
|
3124
|
+
colorSpace: "srgb",
|
|
3125
|
+
channels: [
|
|
3126
|
+
rgb,
|
|
3127
|
+
rgb,
|
|
3128
|
+
rgb
|
|
3129
|
+
]
|
|
3130
|
+
}],
|
|
3131
|
+
["rgba", {
|
|
3132
|
+
colorSpace: "srgb",
|
|
3133
|
+
channels: [
|
|
3134
|
+
rgb,
|
|
3135
|
+
rgb,
|
|
3136
|
+
rgb
|
|
3137
|
+
]
|
|
3138
|
+
}],
|
|
3139
|
+
["hsl", {
|
|
3140
|
+
colorSpace: "hsl",
|
|
3141
|
+
channels: [
|
|
3142
|
+
hue,
|
|
3143
|
+
percent(100),
|
|
3144
|
+
percent(100)
|
|
3145
|
+
]
|
|
3146
|
+
}],
|
|
3147
|
+
["hsla", {
|
|
3148
|
+
colorSpace: "hsl",
|
|
3149
|
+
channels: [
|
|
3150
|
+
hue,
|
|
3151
|
+
percent(100),
|
|
3152
|
+
percent(100)
|
|
3153
|
+
]
|
|
3154
|
+
}],
|
|
3155
|
+
["hwb", {
|
|
3156
|
+
colorSpace: "hwb",
|
|
3157
|
+
channels: [
|
|
3158
|
+
hue,
|
|
3159
|
+
percent(100),
|
|
3160
|
+
percent(100)
|
|
3161
|
+
]
|
|
3162
|
+
}],
|
|
3163
|
+
["lab", {
|
|
3164
|
+
colorSpace: "lab",
|
|
3165
|
+
channels: [
|
|
3166
|
+
percent(100),
|
|
3167
|
+
percent(125),
|
|
3168
|
+
percent(125)
|
|
3169
|
+
]
|
|
3170
|
+
}],
|
|
3171
|
+
["lch", {
|
|
3172
|
+
colorSpace: "lch",
|
|
3173
|
+
channels: [
|
|
3174
|
+
percent(100),
|
|
3175
|
+
percent(150),
|
|
3176
|
+
hue
|
|
3177
|
+
]
|
|
3178
|
+
}],
|
|
3179
|
+
["oklab", {
|
|
3180
|
+
colorSpace: "oklab",
|
|
3181
|
+
channels: [
|
|
3182
|
+
percent(1),
|
|
3183
|
+
percent(.4),
|
|
3184
|
+
percent(.4)
|
|
3185
|
+
]
|
|
3186
|
+
}],
|
|
3187
|
+
["oklch", {
|
|
3188
|
+
colorSpace: "oklch",
|
|
3189
|
+
channels: [
|
|
3190
|
+
percent(1),
|
|
3191
|
+
percent(.4),
|
|
3192
|
+
hue
|
|
3193
|
+
]
|
|
3194
|
+
}]
|
|
3195
|
+
]);
|
|
3196
|
+
/** The spaces of color(), whose channels are all from 0 to 1. */
|
|
3197
|
+
const PREDEFINED = new Map([
|
|
3198
|
+
["srgb", "srgb"],
|
|
3199
|
+
["srgb-linear", "srgb-linear"],
|
|
3200
|
+
["display-p3", "display-p3"],
|
|
3201
|
+
["a98-rgb", "a98-rgb"],
|
|
3202
|
+
["prophoto-rgb", "prophoto-rgb"],
|
|
3203
|
+
["rec2020", "rec2020"],
|
|
3204
|
+
["xyz", "xyz-d65"],
|
|
3205
|
+
["xyz-d65", "xyz-d65"],
|
|
3206
|
+
["xyz-d50", "xyz-d50"]
|
|
3207
|
+
].map(([name, colorSpace]) => [name, {
|
|
3208
|
+
colorSpace,
|
|
3209
|
+
channels: [
|
|
3210
|
+
percent(1),
|
|
3211
|
+
percent(1),
|
|
3212
|
+
percent(1)
|
|
3213
|
+
]
|
|
3214
|
+
}]));
|
|
3215
|
+
function hex(digits) {
|
|
3216
|
+
if (!/^([\da-f]{3,4}|[\da-f]{6}|[\da-f]{8})$/i.test(digits)) return null;
|
|
3217
|
+
const [r, g, b, alpha] = (digits.length <= 4 ? [...digits].map((digit) => digit + digit) : digits.match(/../g)).map((pair) => scaled(parseInt(pair, 16) / 255));
|
|
3218
|
+
const result = {
|
|
3219
|
+
colorSpace: "srgb",
|
|
3220
|
+
components: [
|
|
3221
|
+
r,
|
|
3222
|
+
g,
|
|
3223
|
+
b
|
|
3224
|
+
]
|
|
3225
|
+
};
|
|
3226
|
+
return alpha === void 0 ? result : {
|
|
3227
|
+
...result,
|
|
3228
|
+
alpha
|
|
3229
|
+
};
|
|
3230
|
+
}
|
|
3231
|
+
/** A comma-separated list, item by item. */
|
|
3232
|
+
function items(nodes) {
|
|
3233
|
+
const list = [[]];
|
|
3234
|
+
for (const node of nodes) if (node.type === "Operator" && node.value === ",") list.push([]);
|
|
3235
|
+
else list[list.length - 1].push(node);
|
|
3236
|
+
return list;
|
|
3237
|
+
}
|
|
3238
|
+
//#endregion
|
|
3239
|
+
export { ConfigError, check, defineConfig, exportTokens, formatDiagnostic, formatGitHubAnnotation, generate, loadConfig };
|
package/dist/internal.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { f as ResolvedConfig, i as createLayerWrapper, r as LayerConfig, t as GenerateResult } from "./dts-
|
|
1
|
+
import { f as ResolvedConfig, i as createLayerWrapper, r as LayerConfig, t as GenerateResult } from "./dts-BFi7f7uh.mjs";
|
|
2
2
|
//#region src/watcher.d.ts
|
|
3
3
|
/**
|
|
4
4
|
* Run `generate` and print how many `.d.ts` files it wrote and which it
|
package/dist/internal.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { h as formatDiagnostic, i as createLayerWrapper, l as createMatcher, n as regenerateDts, t as generate } from "./dts
|
|
1
|
+
import { h as formatDiagnostic, i as createLayerWrapper, l as createMatcher, n as regenerateDts, t as generate } from "./dts--_fgXHCu.mjs";
|
|
2
2
|
import path from "node:path";
|
|
3
3
|
import { watch } from "chokidar";
|
|
4
4
|
//#region src/watcher.ts
|
package/package.json
CHANGED