@better-css-modules/core 0.2.1 → 0.3.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.
|
@@ -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
|
};
|
|
@@ -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,
|
|
@@ -874,6 +900,7 @@ function globalCssFrom(files) {
|
|
|
874
900
|
return {
|
|
875
901
|
files,
|
|
876
902
|
tokens,
|
|
903
|
+
values: given,
|
|
877
904
|
layers: layersOf(files)
|
|
878
905
|
};
|
|
879
906
|
}
|
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 {
|
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-De2E7b59.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;
|
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-De2E7b59.mjs";
|
|
2
2
|
import path from "node:path";
|
|
3
3
|
import { watch } from "chokidar";
|
|
4
4
|
//#region src/watcher.ts
|
package/package.json
CHANGED