oxlint-tailwindcss 1.3.5 → 1.5.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 +24 -17
- package/dist/index.cjs +2498 -786
- package/dist/index.d.cts +41 -64
- package/dist/index.d.mts +41 -64
- package/dist/index.mjs +2499 -787
- package/package.json +7 -8
package/dist/index.mjs
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { createRequire } from "node:module";
|
|
2
2
|
import { Worker, threadId } from "node:worker_threads";
|
|
3
3
|
import { createHash } from "node:crypto";
|
|
4
|
-
import { closeSync, existsSync, mkdirSync, openSync, readFileSync, renameSync, statSync, unlinkSync } from "node:fs";
|
|
4
|
+
import { closeSync, existsSync, mkdirSync, openSync, readFileSync, renameSync, statSync, unlinkSync, writeFileSync } from "node:fs";
|
|
5
5
|
import { dirname, isAbsolute, join, relative, resolve } from "node:path";
|
|
6
6
|
import { tmpdir, userInfo } from "node:os";
|
|
7
7
|
//#region \0rolldown/runtime.js
|
|
8
8
|
var __require = /* #__PURE__ */ (() => createRequire(import.meta.url))();
|
|
9
9
|
//#endregion
|
|
10
|
-
//#region ../../node_modules/.pnpm/@oxlint+plugins@1.
|
|
10
|
+
//#region ../../node_modules/.pnpm/@oxlint+plugins@1.74.0/node_modules/@oxlint/plugins/index.js
|
|
11
11
|
/**
|
|
12
12
|
* Define a plugin.
|
|
13
13
|
*
|
|
@@ -657,8 +657,8 @@ function extractVariants(cls) {
|
|
|
657
657
|
const variants = [];
|
|
658
658
|
let depth = 0;
|
|
659
659
|
let segmentStart = 0;
|
|
660
|
-
for (let i = 0; i < cls.length; i++) if (cls[i] === "[") depth++;
|
|
661
|
-
else if (cls[i] === "]") depth--;
|
|
660
|
+
for (let i = 0; i < cls.length; i++) if (cls[i] === "[" || cls[i] === "(") depth++;
|
|
661
|
+
else if (cls[i] === "]" || cls[i] === ")") depth--;
|
|
662
662
|
else if (cls[i] === ":" && depth === 0) {
|
|
663
663
|
variants.push(cls.slice(segmentStart, i));
|
|
664
664
|
segmentStart = i + 1;
|
|
@@ -678,8 +678,8 @@ function extractVariants(cls) {
|
|
|
678
678
|
function extractUtility(cls) {
|
|
679
679
|
let depth = 0;
|
|
680
680
|
let lastColon = -1;
|
|
681
|
-
for (let i = 0; i < cls.length; i++) if (cls[i] === "[") depth++;
|
|
682
|
-
else if (cls[i] === "]") depth--;
|
|
681
|
+
for (let i = 0; i < cls.length; i++) if (cls[i] === "[" || cls[i] === "(") depth++;
|
|
682
|
+
else if (cls[i] === "]" || cls[i] === ")") depth--;
|
|
683
683
|
else if (cls[i] === ":" && depth === 0) lastColon = i;
|
|
684
684
|
return lastColon >= 0 ? cls.slice(lastColon + 1) : cls;
|
|
685
685
|
}
|
|
@@ -691,8 +691,8 @@ function extractUtility(cls) {
|
|
|
691
691
|
function getVariantPrefix(cls) {
|
|
692
692
|
let depth = 0;
|
|
693
693
|
let lastColon = -1;
|
|
694
|
-
for (let i = 0; i < cls.length; i++) if (cls[i] === "[") depth++;
|
|
695
|
-
else if (cls[i] === "]") depth--;
|
|
694
|
+
for (let i = 0; i < cls.length; i++) if (cls[i] === "[" || cls[i] === "(") depth++;
|
|
695
|
+
else if (cls[i] === "]" || cls[i] === ")") depth--;
|
|
696
696
|
else if (cls[i] === ":" && depth === 0) lastColon = i;
|
|
697
697
|
return lastColon >= 0 ? cls.slice(0, lastColon + 1) : "";
|
|
698
698
|
}
|
|
@@ -703,8 +703,8 @@ function getVariantPrefix(cls) {
|
|
|
703
703
|
function splitUtilityAndVariant(cls) {
|
|
704
704
|
let depth = 0;
|
|
705
705
|
let lastColon = -1;
|
|
706
|
-
for (let i = 0; i < cls.length; i++) if (cls[i] === "[") depth++;
|
|
707
|
-
else if (cls[i] === "]") depth--;
|
|
706
|
+
for (let i = 0; i < cls.length; i++) if (cls[i] === "[" || cls[i] === "(") depth++;
|
|
707
|
+
else if (cls[i] === "]" || cls[i] === ")") depth--;
|
|
708
708
|
else if (cls[i] === ":" && depth === 0) lastColon = i;
|
|
709
709
|
return {
|
|
710
710
|
utility: lastColon >= 0 ? cls.slice(lastColon + 1) : cls,
|
|
@@ -735,6 +735,18 @@ function utilityHasDynamicValue(cls) {
|
|
|
735
735
|
return utility.includes("[") || utility.includes("(");
|
|
736
736
|
}
|
|
737
737
|
/**
|
|
738
|
+
* A utility the precompute cannot have enumerated, because the VALUE comes from
|
|
739
|
+
* the user rather than from the design system's class list: arbitrary brackets,
|
|
740
|
+
* the `(--var)` shorthand, a `/` modifier, or an off-scale number.
|
|
741
|
+
*
|
|
742
|
+
* Asking the design system about exactly these is what closes the "looks like
|
|
743
|
+
* coverage" false negatives — `p-4 p-[5px]` used to be accepted silently while
|
|
744
|
+
* `p-4 p-6` reported. Takes the bare utility (no variants, no `!`).
|
|
745
|
+
*/
|
|
746
|
+
function isUserValued(utility) {
|
|
747
|
+
return utility.includes("[") || utility.includes("(") || utility.includes("/") || /-\d/.test(utility);
|
|
748
|
+
}
|
|
749
|
+
/**
|
|
738
750
|
* Gets the arbitrary value from a class, or null if none.
|
|
739
751
|
* e.g. "w-[200px]" → "200px"
|
|
740
752
|
* "bg-[#ff0000]" → "#ff0000"
|
|
@@ -783,11 +795,13 @@ function reattachImportant(bare, position) {
|
|
|
783
795
|
return bare;
|
|
784
796
|
}
|
|
785
797
|
/**
|
|
786
|
-
* Pseudo-element variants
|
|
787
|
-
*
|
|
788
|
-
*
|
|
789
|
-
*
|
|
790
|
-
*
|
|
798
|
+
* Pseudo-element variants, as a static fallback for when no design system is
|
|
799
|
+
* configured. They select a generated box (`::before`, `::marker`, …) rather than
|
|
800
|
+
* the element itself, so they must sit innermost in the variant chain (closest to
|
|
801
|
+
* the utility).
|
|
802
|
+
*
|
|
803
|
+
* Prefer the derived facts: this list cannot know a project's own variants, and
|
|
804
|
+
* `consistent-variant-order` is DS-optional by design, so both paths must work.
|
|
791
805
|
*/
|
|
792
806
|
const PSEUDO_ELEMENT_VARIANTS = /* @__PURE__ */ new Set([
|
|
793
807
|
"before",
|
|
@@ -801,32 +815,135 @@ const PSEUDO_ELEMENT_VARIANTS = /* @__PURE__ */ new Set([
|
|
|
801
815
|
"first-letter",
|
|
802
816
|
"details-content"
|
|
803
817
|
]);
|
|
804
|
-
function isPseudoElementVariant(variant) {
|
|
818
|
+
function isPseudoElementVariant(variant, facts) {
|
|
819
|
+
if (facts) return facts.pseudoElement === true;
|
|
805
820
|
return PSEUDO_ELEMENT_VARIANTS.has(variant);
|
|
806
821
|
}
|
|
807
822
|
/**
|
|
808
823
|
* Selector barriers: variants that re-point the selector at a *different*
|
|
809
|
-
* element than the ones around them — child (`*`), descendant (`**`),
|
|
810
|
-
*
|
|
824
|
+
* element than the ones around them — child (`*`), descendant (`**`), arbitrary
|
|
825
|
+
* selectors (`[&>svg]`), compound child selectors (`*:data-[…]`), and — only
|
|
826
|
+
* visible in the generated selector — ancestor/sibling variants like
|
|
827
|
+
* `group-hover` and `peer-checked`.
|
|
811
828
|
*
|
|
812
829
|
* Unlike pseudo-elements, these are NOT free to move to the end of the chain:
|
|
813
830
|
* `hover:*:flex` (`&:hover > *`) and `*:hover:flex` (`& > *:hover`) target
|
|
814
|
-
* different elements
|
|
831
|
+
* different elements, and `peer-checked:group-hover:*` is not
|
|
832
|
+
* `group-hover:peer-checked:*`. `consistent-variant-order` treats them as hard
|
|
815
833
|
* reordering barriers — variants never cross them.
|
|
834
|
+
*
|
|
835
|
+
* The syntactic checks stay as the fallback, and also as a floor: an arbitrary
|
|
836
|
+
* selector is a barrier whether or not the design system enumerated it.
|
|
816
837
|
*/
|
|
817
|
-
function isSelectorBarrier(variant) {
|
|
838
|
+
function isSelectorBarrier(variant, facts) {
|
|
818
839
|
if (variant === "*" || variant === "**") return true;
|
|
819
840
|
if (variant.startsWith("[") && variant.endsWith("]")) return true;
|
|
820
841
|
if (variant.startsWith("*:")) return true;
|
|
821
|
-
return
|
|
842
|
+
return facts?.structural === true;
|
|
822
843
|
}
|
|
823
844
|
/**
|
|
824
845
|
* Whether a variant changes the selector target (pseudo-element OR selector
|
|
825
846
|
* barrier). Such variants don't impose a condition on the same element, so they
|
|
826
847
|
* can't make a variant class contradict/duplicate a base class.
|
|
827
848
|
*/
|
|
828
|
-
function changesTarget(variant) {
|
|
829
|
-
return isPseudoElementVariant(variant) || isSelectorBarrier(variant);
|
|
849
|
+
function changesTarget(variant, facts) {
|
|
850
|
+
return isPseudoElementVariant(variant, facts) || isSelectorBarrier(variant, facts);
|
|
851
|
+
}
|
|
852
|
+
//#endregion
|
|
853
|
+
//#region src/design-system/css-declarations.ts
|
|
854
|
+
const EMPTY_DECLS = [];
|
|
855
|
+
const EMPTY_VARS = [];
|
|
856
|
+
/** Splits a scope token into its decoded parts. Unknown tokens degrade to `element`. */
|
|
857
|
+
function parseScopeToken(token) {
|
|
858
|
+
const conditional = token.startsWith("@");
|
|
859
|
+
const body = conditional ? token.slice(1) : token;
|
|
860
|
+
if (body === "") return {
|
|
861
|
+
scope: "element",
|
|
862
|
+
pseudo: "",
|
|
863
|
+
conditional
|
|
864
|
+
};
|
|
865
|
+
if (body === ">") return {
|
|
866
|
+
scope: "descendant",
|
|
867
|
+
pseudo: "",
|
|
868
|
+
conditional
|
|
869
|
+
};
|
|
870
|
+
if (body.startsWith("::")) return {
|
|
871
|
+
scope: "pseudo",
|
|
872
|
+
pseudo: body,
|
|
873
|
+
conditional
|
|
874
|
+
};
|
|
875
|
+
return {
|
|
876
|
+
scope: "element",
|
|
877
|
+
pseudo: "",
|
|
878
|
+
conditional
|
|
879
|
+
};
|
|
880
|
+
}
|
|
881
|
+
/**
|
|
882
|
+
* Builds a decoder bound to one index. The returned function turns a packed
|
|
883
|
+
* `byClass` entry into declarations, memoizing each table entry the first time
|
|
884
|
+
* it is touched — a linted file mentions hundreds of classes, not 23 000.
|
|
885
|
+
*/
|
|
886
|
+
function createDeclarationDecoder(index) {
|
|
887
|
+
const pure = new Set(index.pureValues);
|
|
888
|
+
const decoded = Array.from({ length: index.table.length });
|
|
889
|
+
function varNames(map, valueId) {
|
|
890
|
+
const ids = map[String(valueId)];
|
|
891
|
+
if (!ids || ids.length === 0) return EMPTY_VARS;
|
|
892
|
+
const out = [];
|
|
893
|
+
for (const id of ids) {
|
|
894
|
+
const name = index.vars[id];
|
|
895
|
+
if (name !== void 0) out.push(name);
|
|
896
|
+
}
|
|
897
|
+
return out;
|
|
898
|
+
}
|
|
899
|
+
function declAt(declId) {
|
|
900
|
+
const cached = decoded[declId];
|
|
901
|
+
if (cached) return cached;
|
|
902
|
+
const entry = index.table[declId];
|
|
903
|
+
if (entry === void 0) return void 0;
|
|
904
|
+
const first = entry.indexOf("|");
|
|
905
|
+
const second = entry.indexOf("|", first + 1);
|
|
906
|
+
if (first < 0 || second < 0) return void 0;
|
|
907
|
+
const scopeId = parseInt(entry.slice(0, first), 36);
|
|
908
|
+
const propId = parseInt(entry.slice(first + 1, second), 36);
|
|
909
|
+
const valueId = parseInt(entry.slice(second + 1), 36);
|
|
910
|
+
const prop = index.props[propId];
|
|
911
|
+
const value = index.values[valueId];
|
|
912
|
+
if (prop === void 0 || value === void 0) return void 0;
|
|
913
|
+
const { scope, pseudo, conditional } = parseScopeToken(index.scopes[scopeId] ?? "");
|
|
914
|
+
const decl = {
|
|
915
|
+
prop,
|
|
916
|
+
value,
|
|
917
|
+
valueId,
|
|
918
|
+
scope,
|
|
919
|
+
pseudo,
|
|
920
|
+
conditional,
|
|
921
|
+
readsVars: varNames(index.valueVars, valueId),
|
|
922
|
+
readsFallbackVars: varNames(index.valueFallbackVars, valueId),
|
|
923
|
+
pureVarRead: pure.has(valueId)
|
|
924
|
+
};
|
|
925
|
+
decoded[declId] = decl;
|
|
926
|
+
return decl;
|
|
927
|
+
}
|
|
928
|
+
return function decode(packed) {
|
|
929
|
+
if (!packed) return EMPTY_DECLS;
|
|
930
|
+
const out = [];
|
|
931
|
+
let start = 0;
|
|
932
|
+
for (let i = 0; i <= packed.length; i++) if (i === packed.length || packed[i] === ",") {
|
|
933
|
+
if (i > start) {
|
|
934
|
+
const decl = declAt(parseInt(packed.slice(start, i), 36));
|
|
935
|
+
if (decl) out.push(decl);
|
|
936
|
+
}
|
|
937
|
+
start = i + 1;
|
|
938
|
+
}
|
|
939
|
+
return out;
|
|
940
|
+
};
|
|
941
|
+
}
|
|
942
|
+
/** Shape check for a value read back from the disk cache. */
|
|
943
|
+
function isCssDeclarationIndex(value) {
|
|
944
|
+
if (typeof value !== "object" || value === null) return false;
|
|
945
|
+
const i = value;
|
|
946
|
+
return Array.isArray(i.scopes) && i.scopes.length > 0 && i.scopes[0] === "" && Array.isArray(i.props) && Array.isArray(i.values) && Array.isArray(i.vars) && Array.isArray(i.table) && Array.isArray(i.pureValues) && Array.isArray(i.partial) && typeof i.byClass === "object" && i.byClass !== null && typeof i.valueVars === "object" && i.valueVars !== null && typeof i.valueFallbackVars === "object" && i.valueFallbackVars !== null;
|
|
830
947
|
}
|
|
831
948
|
//#endregion
|
|
832
949
|
//#region src/utils/floating-point.ts
|
|
@@ -845,34 +962,55 @@ function roundRemValue(className) {
|
|
|
845
962
|
}
|
|
846
963
|
//#endregion
|
|
847
964
|
//#region src/design-system/cache.ts
|
|
965
|
+
const EMPTY_DECLARATIONS = [];
|
|
848
966
|
var DesignSystemCache = class DesignSystemCache {
|
|
849
967
|
canonicalMap = /* @__PURE__ */ new Map();
|
|
968
|
+
deprecatedMap = /* @__PURE__ */ new Map();
|
|
850
969
|
validitySet = /* @__PURE__ */ new Set();
|
|
851
970
|
orderMap = /* @__PURE__ */ new Map();
|
|
852
|
-
|
|
971
|
+
declIndex = null;
|
|
972
|
+
decodeDecls = null;
|
|
973
|
+
declMemo = /* @__PURE__ */ new Map();
|
|
974
|
+
propsMemo = /* @__PURE__ */ new Map();
|
|
975
|
+
partialSet = /* @__PURE__ */ new Set();
|
|
976
|
+
valueIdByText = null;
|
|
853
977
|
variantOrderMap = /* @__PURE__ */ new Map();
|
|
978
|
+
variantFactsMap = /* @__PURE__ */ new Map();
|
|
854
979
|
arbitraryEquivMap = /* @__PURE__ */ new Map();
|
|
855
980
|
_validClasses = [];
|
|
856
981
|
_knownPrefixes = null;
|
|
857
982
|
_maxOrder = 0n;
|
|
858
983
|
_prefix = "";
|
|
859
984
|
componentSet = /* @__PURE__ */ new Set();
|
|
985
|
+
themeRefs = /* @__PURE__ */ new Map();
|
|
986
|
+
definedVarSet = /* @__PURE__ */ new Set();
|
|
860
987
|
static fromPrecomputed(data) {
|
|
861
988
|
const cache = new DesignSystemCache();
|
|
862
989
|
cache._validClasses = data.validClasses;
|
|
863
990
|
for (const cls of data.validClasses) cache.validitySet.add(cls);
|
|
864
991
|
for (const [from, to] of Object.entries(data.canonical)) cache.canonicalMap.set(from, roundRemValue(to));
|
|
992
|
+
if (data.deprecated) for (const [from, to] of Object.entries(data.deprecated)) cache.deprecatedMap.set(from, to);
|
|
865
993
|
for (const [cls, val] of Object.entries(data.order)) {
|
|
866
994
|
const order = BigInt(val);
|
|
867
995
|
cache.orderMap.set(cls, order);
|
|
868
996
|
if (order > cache._maxOrder) cache._maxOrder = order;
|
|
869
997
|
}
|
|
870
|
-
|
|
998
|
+
if (data.cssDeclarations) {
|
|
999
|
+
cache.declIndex = data.cssDeclarations;
|
|
1000
|
+
cache.decodeDecls = createDeclarationDecoder(data.cssDeclarations);
|
|
1001
|
+
for (const cls of data.cssDeclarations.partial ?? []) cache.partialSet.add(cls);
|
|
1002
|
+
}
|
|
871
1003
|
if (data.variantOrder) for (const [name, index] of Object.entries(data.variantOrder)) cache.variantOrderMap.set(name, index);
|
|
1004
|
+
for (const [name, facts] of Object.entries(data.variantFacts ?? {})) cache.variantFactsMap.set(name, {
|
|
1005
|
+
pseudoElement: facts.p === 1,
|
|
1006
|
+
structural: facts.s === 1
|
|
1007
|
+
});
|
|
872
1008
|
if (data.componentClasses) for (const cls of data.componentClasses) {
|
|
873
1009
|
cache.validitySet.add(cls);
|
|
874
1010
|
cache.componentSet.add(cls);
|
|
875
1011
|
}
|
|
1012
|
+
for (const name of data.definedVars ?? []) cache.definedVarSet.add(name);
|
|
1013
|
+
if (data.themeRefs) for (const [name, refs] of Object.entries(data.themeRefs)) cache.themeRefs.set(name, refs);
|
|
876
1014
|
cache._prefix = data.prefix ?? "";
|
|
877
1015
|
if (data.arbitraryEquivalents) for (const [arb, named] of Object.entries(data.arbitraryEquivalents)) cache.arbitraryEquivMap.set(arb, named);
|
|
878
1016
|
return cache;
|
|
@@ -906,6 +1044,24 @@ var DesignSystemCache = class DesignSystemCache {
|
|
|
906
1044
|
return className;
|
|
907
1045
|
}
|
|
908
1046
|
/**
|
|
1047
|
+
* The v4 name of a class Tailwind renamed, or `null` if it isn't a renamed
|
|
1048
|
+
* spelling. Takes the BARE utility (no variants, no `!`) — the caller has
|
|
1049
|
+
* already split those off to rebuild the replacement around them.
|
|
1050
|
+
*
|
|
1051
|
+
* Derived by the precompute from what `canonicalizeCandidates` reports for the
|
|
1052
|
+
* v3 spellings, rather than from a map maintained by hand. Two rules read it:
|
|
1053
|
+
* `no-deprecated-classes`, which owns the diagnostic, and `enforce-canonical`,
|
|
1054
|
+
* which stays quiet about these so one class doesn't produce two identical
|
|
1055
|
+
* rewrites.
|
|
1056
|
+
*/
|
|
1057
|
+
deprecatedReplacement(bareUtility) {
|
|
1058
|
+
return this.deprecatedMap.get(this.stripProjectPrefix(bareUtility)) ?? null;
|
|
1059
|
+
}
|
|
1060
|
+
/** Whether the precompute produced a deprecation map at all (older artifacts didn't). */
|
|
1061
|
+
get hasDeprecatedMap() {
|
|
1062
|
+
return this.deprecatedMap.size > 0;
|
|
1063
|
+
}
|
|
1064
|
+
/**
|
|
909
1065
|
* Resolve a variant name to its priority in the CSS output order.
|
|
910
1066
|
* Named variants use the precomputed map. Arbitrary variants use pattern-based heuristics
|
|
911
1067
|
* to approximate their position relative to named variants.
|
|
@@ -964,6 +1120,23 @@ var DesignSystemCache = class DesignSystemCache {
|
|
|
964
1120
|
}
|
|
965
1121
|
return this._knownPrefixes;
|
|
966
1122
|
}
|
|
1123
|
+
/**
|
|
1124
|
+
* Is this class name in the precomputed set VERBATIM?
|
|
1125
|
+
*
|
|
1126
|
+
* `isValid` is deliberately tolerant: it accepts anything shaped like a dynamic
|
|
1127
|
+
* value (`w-45`, `bg-red-5000`, `bg-red-500/foo`) because there used to be no
|
|
1128
|
+
* way to ask the design system at lint time. That tolerance is what let
|
|
1129
|
+
* `no-unknown-classes` accept classes Tailwind compiles to nothing. This is the
|
|
1130
|
+
* exact answer, so a rule can tell "known to produce CSS" from "shaped like it
|
|
1131
|
+
* might" and ask the design system about the difference.
|
|
1132
|
+
*/
|
|
1133
|
+
isKnownClass(className) {
|
|
1134
|
+
return this.validitySet.has(this.stripProjectPrefix(className));
|
|
1135
|
+
}
|
|
1136
|
+
/** Variant names the design system reports, for suggesting a typo's neighbour. */
|
|
1137
|
+
variantNames() {
|
|
1138
|
+
return [...this.variantOrderMap.keys()];
|
|
1139
|
+
}
|
|
967
1140
|
isValid(className) {
|
|
968
1141
|
if (this.validitySet.has(className)) return true;
|
|
969
1142
|
const utility = extractUtility(className);
|
|
@@ -1061,15 +1234,151 @@ var DesignSystemCache = class DesignSystemCache {
|
|
|
1061
1234
|
const bucketSize = this._maxOrder + 1n;
|
|
1062
1235
|
return BigInt(firstPriority) * 1000n * bucketSize + BigInt(subPriority) * bucketSize + baseOrder;
|
|
1063
1236
|
}
|
|
1237
|
+
/**
|
|
1238
|
+
* Whether the stylesheet order for this exact class is known, as opposed to
|
|
1239
|
+
* approximated from a prefix sibling. Callers that name a winner must not do
|
|
1240
|
+
* so from an approximation: `w-[10px]` and `w-[20px]` both borrow the order of
|
|
1241
|
+
* the first `w-*` class, which says nothing about their real positions.
|
|
1242
|
+
*/
|
|
1243
|
+
hasExactOrder(className) {
|
|
1244
|
+
const key = this.stripProjectPrefix(className);
|
|
1245
|
+
if (this.orderMap.get(key) != null) return true;
|
|
1246
|
+
const { bare, position } = splitImportant(key);
|
|
1247
|
+
return position !== null && this.orderMap.get(bare) != null;
|
|
1248
|
+
}
|
|
1064
1249
|
getClassOrder(classes) {
|
|
1065
1250
|
return classes.map((cls) => [cls, this.getOrder(cls)]);
|
|
1066
1251
|
}
|
|
1252
|
+
/**
|
|
1253
|
+
* Every declaration the class emits, in emission order, not deduplicated.
|
|
1254
|
+
* Includes declarations on pseudo-elements and (for classes that style only
|
|
1255
|
+
* their descendants, like `space-x-*`) on descendants — each tagged with its
|
|
1256
|
+
* scope, so callers never mistake one box for another.
|
|
1257
|
+
*/
|
|
1258
|
+
getCssDeclarations(className) {
|
|
1259
|
+
const key = this.stripProjectPrefix(className);
|
|
1260
|
+
const memo = this.declMemo.get(key);
|
|
1261
|
+
if (memo) return memo;
|
|
1262
|
+
const byClass = this.declIndex?.byClass;
|
|
1263
|
+
let packed = byClass?.[key];
|
|
1264
|
+
if (packed === void 0) {
|
|
1265
|
+
const { bare, position } = splitImportant(key);
|
|
1266
|
+
if (position) packed = byClass?.[bare];
|
|
1267
|
+
}
|
|
1268
|
+
const decoded = this.decodeDecls?.(packed) ?? EMPTY_DECLARATIONS;
|
|
1269
|
+
this.declMemo.set(key, decoded);
|
|
1270
|
+
return decoded;
|
|
1271
|
+
}
|
|
1272
|
+
/**
|
|
1273
|
+
* Adds declarations resolved at lint time (for classes the precompute never
|
|
1274
|
+
* saw — arbitrary values, slash modifiers, off-scale numbers) and memoizes
|
|
1275
|
+
* them like any other class.
|
|
1276
|
+
*
|
|
1277
|
+
* Values are interned against the SAME table the precompute filled, which is
|
|
1278
|
+
* the point: `p-4`'s value id has to be comparable with `p-[1rem]`'s, or the
|
|
1279
|
+
* two could never be told apart from a genuine conflict.
|
|
1280
|
+
*/
|
|
1281
|
+
internDeclarations(className, raws, valueFacts) {
|
|
1282
|
+
const index = this.declIndex;
|
|
1283
|
+
if (!index) return EMPTY_DECLARATIONS;
|
|
1284
|
+
if (!this.valueIdByText) this.valueIdByText = new Map(index.values.map((value, id) => [value, id]));
|
|
1285
|
+
const decls = [];
|
|
1286
|
+
let hasElement = false;
|
|
1287
|
+
let hasDescendant = false;
|
|
1288
|
+
let hasConditional = false;
|
|
1289
|
+
for (const [scopeToken, prop, value] of raws) {
|
|
1290
|
+
let valueId = this.valueIdByText.get(value);
|
|
1291
|
+
if (valueId === void 0) {
|
|
1292
|
+
valueId = index.values.length;
|
|
1293
|
+
index.values.push(value);
|
|
1294
|
+
this.valueIdByText.set(value, valueId);
|
|
1295
|
+
}
|
|
1296
|
+
const facts = valueFacts[value];
|
|
1297
|
+
const { scope, pseudo, conditional } = parseScopeToken(scopeToken);
|
|
1298
|
+
if (conditional) hasConditional = true;
|
|
1299
|
+
else if (scope === "element") hasElement = true;
|
|
1300
|
+
else if (scope === "descendant") hasDescendant = true;
|
|
1301
|
+
decls.push({
|
|
1302
|
+
prop,
|
|
1303
|
+
value,
|
|
1304
|
+
valueId,
|
|
1305
|
+
scope,
|
|
1306
|
+
pseudo,
|
|
1307
|
+
conditional,
|
|
1308
|
+
readsVars: facts?.p ?? [],
|
|
1309
|
+
readsFallbackVars: facts?.f ?? [],
|
|
1310
|
+
pureVarRead: facts?.u ?? false
|
|
1311
|
+
});
|
|
1312
|
+
}
|
|
1313
|
+
const key = this.stripProjectPrefix(className);
|
|
1314
|
+
if (hasConditional || hasElement && hasDescendant) this.partialSet.add(key);
|
|
1315
|
+
this.declMemo.set(key, decls);
|
|
1316
|
+
this.propsMemo.delete(key);
|
|
1317
|
+
return decls;
|
|
1318
|
+
}
|
|
1319
|
+
/**
|
|
1320
|
+
* True when the class emits CSS we deliberately did not model in full
|
|
1321
|
+
* (descendant rules alongside its own, or `@media`/`@supports` blocks). Such a
|
|
1322
|
+
* class must never be reported as redundant.
|
|
1323
|
+
*/
|
|
1324
|
+
isPartial(className) {
|
|
1325
|
+
return this.partialSet.has(this.stripProjectPrefix(className));
|
|
1326
|
+
}
|
|
1327
|
+
/**
|
|
1328
|
+
* Property names the class declares on its OWN box, unconditionally.
|
|
1329
|
+
* Deduplicated, in emission order.
|
|
1330
|
+
*/
|
|
1067
1331
|
getCssProperties(className) {
|
|
1068
|
-
const
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1332
|
+
const key = this.stripProjectPrefix(className);
|
|
1333
|
+
const memo = this.propsMemo.get(key);
|
|
1334
|
+
if (memo) return memo;
|
|
1335
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1336
|
+
const props = [];
|
|
1337
|
+
for (const decl of this.getCssDeclarations(key)) {
|
|
1338
|
+
if (decl.scope !== "element" || decl.conditional) continue;
|
|
1339
|
+
if (seen.has(decl.prop)) continue;
|
|
1340
|
+
seen.add(decl.prop);
|
|
1341
|
+
props.push(decl.prop);
|
|
1342
|
+
}
|
|
1343
|
+
this.propsMemo.set(key, props);
|
|
1344
|
+
return props;
|
|
1345
|
+
}
|
|
1346
|
+
/** Strips the Tailwind project prefix (`tw:flex` → `flex`), if configured. */
|
|
1347
|
+
stripProjectPrefix(className) {
|
|
1348
|
+
if (this._prefix && className.startsWith(this._prefix + ":")) return className.slice(this._prefix.length + 1);
|
|
1349
|
+
return className;
|
|
1350
|
+
}
|
|
1351
|
+
/**
|
|
1352
|
+
* Whether `varName` resolves to `target` through theme indirection.
|
|
1353
|
+
*
|
|
1354
|
+
* `@theme inline { --color-primary: var(--primary) }` makes `bg-primary` and
|
|
1355
|
+
* `bg-(--primary)` the same declaration; a literal `--color-primary: oklch(…)`
|
|
1356
|
+
* makes them different colours. Only the theme table can tell those apart.
|
|
1357
|
+
*/
|
|
1358
|
+
themeVarResolvesTo(varName, target, depth = 4) {
|
|
1359
|
+
if (varName === target) return true;
|
|
1360
|
+
if (depth <= 0) return false;
|
|
1361
|
+
const refs = this.themeRefs.get(varName);
|
|
1362
|
+
if (!refs) return false;
|
|
1363
|
+
return refs.some((ref) => this.themeVarResolvesTo(ref, target, depth - 1));
|
|
1364
|
+
}
|
|
1365
|
+
/** Whether the project defines this custom property (theme or plain CSS). */
|
|
1366
|
+
definesVar(varName) {
|
|
1367
|
+
return this.definedVarSet.has(varName) || this.themeRefs.has(varName);
|
|
1368
|
+
}
|
|
1369
|
+
/**
|
|
1370
|
+
* What the variant's selector does, derived from the design system. `undefined`
|
|
1371
|
+
* means "no information" — the caller falls back to its static predicates,
|
|
1372
|
+
* which is what keeps the variant rules working without an entry point.
|
|
1373
|
+
*
|
|
1374
|
+
* Compound variants (`group-hover/name`, `peer-checked`) resolve through the
|
|
1375
|
+
* same normalisation the priority lookup uses.
|
|
1376
|
+
*/
|
|
1377
|
+
getVariantFacts(variant) {
|
|
1378
|
+
const direct = this.variantFactsMap.get(variant);
|
|
1379
|
+
if (direct) return direct;
|
|
1380
|
+
const slash = variant.indexOf("/");
|
|
1381
|
+
if (slash > 0) return this.variantFactsMap.get(variant.slice(0, slash));
|
|
1073
1382
|
}
|
|
1074
1383
|
getVariantPriority(variant) {
|
|
1075
1384
|
return this.variantOrderMap.get(variant) ?? null;
|
|
@@ -1165,6 +1474,29 @@ function safeGetDS(getDS, context, node) {
|
|
|
1165
1474
|
throw err;
|
|
1166
1475
|
}
|
|
1167
1476
|
}
|
|
1477
|
+
/**
|
|
1478
|
+
* `safeGetDS`'s quiet sibling, for the DS-OPTIONAL rules.
|
|
1479
|
+
*
|
|
1480
|
+
* A handful of rules work without a design system — their static fallback is
|
|
1481
|
+
* itself deterministic (`consistent-variant-order`'s variant table,
|
|
1482
|
+
* `no-dark-without-light`'s prefix grouping) — and an entry point only makes
|
|
1483
|
+
* them more accurate. Those rules must never emit `designSystemUnavailable`:
|
|
1484
|
+
* they were usable with no configuration before the DS lookup existed and have
|
|
1485
|
+
* to stay that way, or adding the lookup would break every project that runs
|
|
1486
|
+
* them without a CSS entry point.
|
|
1487
|
+
*
|
|
1488
|
+
* So: returns the load result, or `null` when the design system is unavailable
|
|
1489
|
+
* for a reason the plugin knows about. Non-fatal errors still propagate, since
|
|
1490
|
+
* those are bugs rather than missing configuration.
|
|
1491
|
+
*/
|
|
1492
|
+
function softGetDS(getDS) {
|
|
1493
|
+
try {
|
|
1494
|
+
return getDS();
|
|
1495
|
+
} catch (err) {
|
|
1496
|
+
if (isFatalError(err)) return null;
|
|
1497
|
+
throw err;
|
|
1498
|
+
}
|
|
1499
|
+
}
|
|
1168
1500
|
//#endregion
|
|
1169
1501
|
//#region src/design-system/tailwind-node.ts
|
|
1170
1502
|
/**
|
|
@@ -1232,6 +1564,181 @@ const TAILWIND_NODE_VERSION = entryPath !== null ? readVersionFromEntry(entryPat
|
|
|
1232
1564
|
*
|
|
1233
1565
|
* For arbitrary values (bg-[#123]) that aren't in the class list, we use heuristics.
|
|
1234
1566
|
*/
|
|
1567
|
+
/**
|
|
1568
|
+
* Declaration extractor, as source text, interpolated into PRECOMPUTE_SCRIPT.
|
|
1569
|
+
*
|
|
1570
|
+
* It lives in its own constant for two reasons: the benchmark in
|
|
1571
|
+
* `tests/benchmarks/precompute-phases.test.ts` used to keep a hand-copied
|
|
1572
|
+
* duplicate of the extractor that silently drifted from the real one, and it is
|
|
1573
|
+
* interpolated (not concatenated at runtime), so its text still feeds the
|
|
1574
|
+
* `CACHE_KEY` md5 and any edit here invalidates the disk cache automatically.
|
|
1575
|
+
*
|
|
1576
|
+
* Exports (in the worker scope): `walkDeclarations`, `scanVarReads`,
|
|
1577
|
+
* `isPureVarRead`.
|
|
1578
|
+
*/
|
|
1579
|
+
const DECL_EXTRACTOR_SOURCE = `
|
|
1580
|
+
// One declaration per line, '{' closing the selector line, '}' alone — the
|
|
1581
|
+
// shape candidatesToCss pretty-prints. Verified against every fixture: zero
|
|
1582
|
+
// unparsed declaration lines.
|
|
1583
|
+
const DECL_RE = /^([\\w-]+)\\s*:\\s*(.*?)\\s*;?$/;
|
|
1584
|
+
|
|
1585
|
+
// Class-name characters, plus '\\\\' because escaped selectors continue the name
|
|
1586
|
+
// ('.w-1\\\\/2' must not be matched by the token '.w-1').
|
|
1587
|
+
function isNameChar(c) {
|
|
1588
|
+
if (c === undefined) return false;
|
|
1589
|
+
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')
|
|
1590
|
+
|| c === '-' || c === '_' || c === '\\\\';
|
|
1591
|
+
}
|
|
1592
|
+
|
|
1593
|
+
// Classify what follows the class token inside a selector:
|
|
1594
|
+
// '::name' → the declaration styles that pseudo-element's box
|
|
1595
|
+
// '>' → a combinator intervenes, so the class is not the subject
|
|
1596
|
+
// '' → the element's own box (pseudo-CLASSES land here)
|
|
1597
|
+
// The combinator scan is paren-aware: tw-animate-css emits
|
|
1598
|
+
// '&:where(:dir(ltr), [dir="ltr"], [dir="ltr"]*)', whose spaces and commas sit
|
|
1599
|
+
// inside :where(). A naive "is there a space" test would mislabel 92 rules as
|
|
1600
|
+
// descendant and silently stop reporting on the slide-in-* family.
|
|
1601
|
+
function classifyAfter(after) {
|
|
1602
|
+
if (after.startsWith('::')) {
|
|
1603
|
+
let e = 2;
|
|
1604
|
+
while (e < after.length && isNameChar(after[e]) && after[e] !== '\\\\') e++;
|
|
1605
|
+
return after.slice(0, e);
|
|
1606
|
+
}
|
|
1607
|
+
let depth = 0;
|
|
1608
|
+
for (let x = 0; x < after.length; x++) {
|
|
1609
|
+
const ch = after[x];
|
|
1610
|
+
if (ch === '(') depth++;
|
|
1611
|
+
else if (ch === ')') { if (depth > 0) depth--; }
|
|
1612
|
+
else if (depth === 0) {
|
|
1613
|
+
if (ch === '>' || ch === '+' || ch === '~' || ch === ' ' || ch === '\\t') return '>';
|
|
1614
|
+
if (ch === ',') break;
|
|
1615
|
+
}
|
|
1616
|
+
}
|
|
1617
|
+
return '';
|
|
1618
|
+
}
|
|
1619
|
+
|
|
1620
|
+
// Find the token as a COMPLETE compound ('.rounded' must not match inside
|
|
1621
|
+
// '.rounded-lg'). Returns null when the token is absent, which the caller
|
|
1622
|
+
// treats as element scope: that is today's behaviour for selectors we cannot
|
|
1623
|
+
// attribute, and widening it to 'descendant' would blind the rule.
|
|
1624
|
+
function classifyTopLevel(sel, token) {
|
|
1625
|
+
let from = 0;
|
|
1626
|
+
for (;;) {
|
|
1627
|
+
const i = sel.indexOf(token, from);
|
|
1628
|
+
if (i < 0) return null;
|
|
1629
|
+
if (!isNameChar(sel[i + token.length])) return classifyAfter(sel.slice(i + token.length));
|
|
1630
|
+
from = i + 1;
|
|
1631
|
+
}
|
|
1632
|
+
}
|
|
1633
|
+
|
|
1634
|
+
// Nested block (CSS nesting). A selector that does not start with '&' is
|
|
1635
|
+
// implicitly '& <sel>', i.e. a descendant — that is what keeps prose's
|
|
1636
|
+
// ':where(p)' rules out of the element's declaration set.
|
|
1637
|
+
function classifyNested(sel) {
|
|
1638
|
+
if (sel[0] !== '&') return '>';
|
|
1639
|
+
return classifyAfter(sel.slice(1));
|
|
1640
|
+
}
|
|
1641
|
+
|
|
1642
|
+
// Tailwind emits the same value two ways: 'slide-in-from-left' declares
|
|
1643
|
+
// '-100%' and 'slide-in-from-left-full' declares 'calc(1 * -100%)'. Same
|
|
1644
|
+
// computed value, different text — and a text comparison would call that a
|
|
1645
|
+
// conflict. Only a SINGLE numeric term is unwrapped, so 'calc(1 * 2px + 3px)'
|
|
1646
|
+
// (where the multiplication is part of a larger expression) is left alone.
|
|
1647
|
+
const CALC_ONE_LEFT = /^calc\\(\\s*1\\s*\\*\\s*(-?(?:\\d+\\.?\\d*|\\.\\d+)[a-z%]*)\\s*\\)$/;
|
|
1648
|
+
const CALC_ONE_RIGHT = /^calc\\(\\s*(-?(?:\\d+\\.?\\d*|\\.\\d+)[a-z%]*)\\s*\\*\\s*1\\s*\\)$/;
|
|
1649
|
+
function normalizeDeclValue(value) {
|
|
1650
|
+
const trimmed = value.trim();
|
|
1651
|
+
const m = CALC_ONE_LEFT.exec(trimmed) || CALC_ONE_RIGHT.exec(trimmed);
|
|
1652
|
+
return m ? m[1] : trimmed;
|
|
1653
|
+
}
|
|
1654
|
+
|
|
1655
|
+
function walkDeclarations(cssText, className, sink) {
|
|
1656
|
+
const token = '.' + className.replace(/([^\\w-])/g, '\\\\$1');
|
|
1657
|
+
const stack = [];
|
|
1658
|
+
let skipDepth = 0;
|
|
1659
|
+
const lines = cssText.split('\\n');
|
|
1660
|
+
for (let li = 0; li < lines.length; li++) {
|
|
1661
|
+
const line = lines[li].trim();
|
|
1662
|
+
if (line === '') continue;
|
|
1663
|
+
if (line === '}') { if (skipDepth > 0) skipDepth--; else stack.pop(); continue; }
|
|
1664
|
+
if (line.charCodeAt(line.length - 1) === 123 /* { */) {
|
|
1665
|
+
if (skipDepth > 0) { skipDepth++; continue; }
|
|
1666
|
+
const sel = line.slice(0, -1).trim();
|
|
1667
|
+
// @property blocks describe a variable, they don't style anything: skip
|
|
1668
|
+
// the whole block (that also drops syntax/inherits/initial-value).
|
|
1669
|
+
if (sel.startsWith('@property')) { skipDepth++; continue; }
|
|
1670
|
+
stack.push(sel);
|
|
1671
|
+
continue;
|
|
1672
|
+
}
|
|
1673
|
+
if (skipDepth > 0) continue;
|
|
1674
|
+
const m = DECL_RE.exec(line);
|
|
1675
|
+
if (!m) continue;
|
|
1676
|
+
// Resolve the scope from the open block stack.
|
|
1677
|
+
let conditional = false, scope = '', seenSubject = false;
|
|
1678
|
+
for (let k = 0; k < stack.length; k++) {
|
|
1679
|
+
const sel = stack[k];
|
|
1680
|
+
if (sel.charCodeAt(0) === 64 /* @ */) { conditional = true; continue; }
|
|
1681
|
+
if (!seenSubject) {
|
|
1682
|
+
seenSubject = true;
|
|
1683
|
+
const top = classifyTopLevel(sel, token);
|
|
1684
|
+
scope = top === null ? '' : top;
|
|
1685
|
+
} else {
|
|
1686
|
+
const inner = classifyNested(sel);
|
|
1687
|
+
if (inner === '>') scope = '>';
|
|
1688
|
+
else if (inner !== '' && scope !== '>') scope = inner;
|
|
1689
|
+
}
|
|
1690
|
+
}
|
|
1691
|
+
sink((conditional ? '@' : '') + scope, m[1], normalizeDeclValue(m[2]));
|
|
1692
|
+
}
|
|
1693
|
+
}
|
|
1694
|
+
|
|
1695
|
+
// Custom properties a value reads. 'primary' means "not inside another var()'s
|
|
1696
|
+
// fallback" — NOT "at paren depth 0": the vars inside linear-gradient(...) are
|
|
1697
|
+
// primary reads. Fallback reads are reported separately because a fallback is a
|
|
1698
|
+
// last resort, not the composition channel.
|
|
1699
|
+
function scanVarReads(value) {
|
|
1700
|
+
const primary = [], fallback = [];
|
|
1701
|
+
function walk(s, inFallback) {
|
|
1702
|
+
let i = 0;
|
|
1703
|
+
for (;;) {
|
|
1704
|
+
const at = s.indexOf('var(', i);
|
|
1705
|
+
if (at < 0) break;
|
|
1706
|
+
let d = 1, j = at + 4;
|
|
1707
|
+
while (j < s.length && d > 0) { if (s[j] === '(') d++; else if (s[j] === ')') d--; j++; }
|
|
1708
|
+
const inner = s.slice(at + 4, j - 1);
|
|
1709
|
+
let c = 0, comma = -1;
|
|
1710
|
+
for (let x = 0; x < inner.length; x++) {
|
|
1711
|
+
if (inner[x] === '(') c++;
|
|
1712
|
+
else if (inner[x] === ')') c--;
|
|
1713
|
+
else if (inner[x] === ',' && c === 0) { comma = x; break; }
|
|
1714
|
+
}
|
|
1715
|
+
const name = (comma < 0 ? inner : inner.slice(0, comma)).trim();
|
|
1716
|
+
if (name.startsWith('--')) (inFallback ? fallback : primary).push(name);
|
|
1717
|
+
if (comma >= 0) walk(inner.slice(comma + 1), true);
|
|
1718
|
+
i = j;
|
|
1719
|
+
}
|
|
1720
|
+
}
|
|
1721
|
+
walk(value, false);
|
|
1722
|
+
return [[...new Set(primary)], [...new Set(fallback)]];
|
|
1723
|
+
}
|
|
1724
|
+
|
|
1725
|
+
// A value is a pure var() read when, with every balanced var(...) group removed,
|
|
1726
|
+
// only separators remain. 'var(--tw-scale-x) var(--tw-scale-y)' is pure;
|
|
1727
|
+
// 'translateZ(0) var(--tw-rotate-x,)' is not (it contributes a value of its own).
|
|
1728
|
+
function isPureVarRead(value) {
|
|
1729
|
+
let rest = '', i = 0;
|
|
1730
|
+
while (i < value.length) {
|
|
1731
|
+
if (value.startsWith('var(', i)) {
|
|
1732
|
+
let d = 1, j = i + 4;
|
|
1733
|
+
while (j < value.length && d > 0) { if (value[j] === '(') d++; else if (value[j] === ')') d--; j++; }
|
|
1734
|
+
i = j;
|
|
1735
|
+
continue;
|
|
1736
|
+
}
|
|
1737
|
+
rest += value[i++];
|
|
1738
|
+
}
|
|
1739
|
+
return /^[\\s,]*$/.test(rest);
|
|
1740
|
+
}
|
|
1741
|
+
`;
|
|
1235
1742
|
const PRECOMPUTE_SCRIPT = `
|
|
1236
1743
|
// Runs as a worker_thread (NOT a forked child process). Inputs arrive via
|
|
1237
1744
|
// workerData; the result is written straight to the disk cache and completion
|
|
@@ -1341,6 +1848,8 @@ function extractComponentClasses(cssPath, baseDir) {
|
|
|
1341
1848
|
return [...new Set(result)];
|
|
1342
1849
|
}
|
|
1343
1850
|
|
|
1851
|
+
${DECL_EXTRACTOR_SOURCE}
|
|
1852
|
+
|
|
1344
1853
|
async function main() {
|
|
1345
1854
|
const cssPath = WD_CSS_PATH;
|
|
1346
1855
|
const css = readFileSync(cssPath, 'utf-8');
|
|
@@ -1366,10 +1875,21 @@ async function main() {
|
|
|
1366
1875
|
|
|
1367
1876
|
// Validity: which classes produce CSS. Validate the prefixed form, but keep
|
|
1368
1877
|
// cssResults aligned positionally to the unprefixed classNames so the later
|
|
1369
|
-
//
|
|
1878
|
+
// declarations / arbitraryEquivalents phases keep indexing by position.
|
|
1370
1879
|
const cssResults = ds.candidatesToCss(classNames.map(pfx));
|
|
1371
1880
|
const validClasses = classNames.filter((_, i) => cssResults[i] != null);
|
|
1372
1881
|
|
|
1882
|
+
// className → its emitted CSS, for the declarations phase. Every phase that
|
|
1883
|
+
// validates extra candidates feeds this map, so classes recovered outside
|
|
1884
|
+
// getClassList() (bare utilities like \`rounded\`, negatives like \`-col-1\`,
|
|
1885
|
+
// legacy v3 spellings) get declarations too. They used to be pushed into
|
|
1886
|
+
// validClasses with their CSS thrown away, which left ~300 valid classes
|
|
1887
|
+
// invisible to no-conflicting-classes (\`rounded rounded-lg\` never reported).
|
|
1888
|
+
const declCss = {};
|
|
1889
|
+
for (let i = 0; i < classNames.length; i++) {
|
|
1890
|
+
if (cssResults[i] != null) declCss[classNames[i]] = cssResults[i];
|
|
1891
|
+
}
|
|
1892
|
+
|
|
1373
1893
|
// Expand: validate extra candidates not in getClassList() but valid in v4
|
|
1374
1894
|
const validSet = new Set(validClasses);
|
|
1375
1895
|
const knownPrefixes = new Set();
|
|
@@ -1387,7 +1907,7 @@ async function main() {
|
|
|
1387
1907
|
// utilities; their functional bases enumerate values but omit \`none\`.
|
|
1388
1908
|
// - \`max-w-screen\` (\`max-width: 100vw\`).
|
|
1389
1909
|
// Validated via candidatesToCss (self-prunes on any Tailwind version that doesn't
|
|
1390
|
-
// emit them) and their CSS is captured so the
|
|
1910
|
+
// emit them) and their CSS is captured so the declarations phase below treats them
|
|
1391
1911
|
// exactly like their getClassList siblings — otherwise no-conflicting-classes
|
|
1392
1912
|
// would flag \`@container @container-normal\` but silently accept
|
|
1393
1913
|
// \`@container @container-size\`. Named forms (\`@container-size/main\`) are covered
|
|
@@ -1398,14 +1918,13 @@ async function main() {
|
|
|
1398
1918
|
'backdrop-filter-none',
|
|
1399
1919
|
'max-w-screen',
|
|
1400
1920
|
].filter((c) => !validSet.has(c));
|
|
1401
|
-
const staticExtraCss = {};
|
|
1402
1921
|
if (staticExtras.length > 0) {
|
|
1403
1922
|
const staticResults = ds.candidatesToCss(staticExtras.map(pfx));
|
|
1404
1923
|
for (let i = 0; i < staticExtras.length; i++) {
|
|
1405
1924
|
if (staticResults[i] != null) {
|
|
1406
1925
|
validClasses.push(staticExtras[i]);
|
|
1407
1926
|
validSet.add(staticExtras[i]);
|
|
1408
|
-
|
|
1927
|
+
declCss[staticExtras[i]] = staticResults[i];
|
|
1409
1928
|
}
|
|
1410
1929
|
}
|
|
1411
1930
|
}
|
|
@@ -1427,6 +1946,7 @@ async function main() {
|
|
|
1427
1946
|
if (extraResults[i] != null) {
|
|
1428
1947
|
validClasses.push(extraCandidates[i]);
|
|
1429
1948
|
validSet.add(extraCandidates[i]);
|
|
1949
|
+
declCss[extraCandidates[i]] = extraResults[i];
|
|
1430
1950
|
}
|
|
1431
1951
|
}
|
|
1432
1952
|
}
|
|
@@ -1448,6 +1968,7 @@ async function main() {
|
|
|
1448
1968
|
if (negResults[i] != null && !validSet.has(negativeProbes[i])) {
|
|
1449
1969
|
validClasses.push(negativeProbes[i]);
|
|
1450
1970
|
validSet.add(negativeProbes[i]);
|
|
1971
|
+
declCss[negativeProbes[i]] = negResults[i];
|
|
1451
1972
|
}
|
|
1452
1973
|
}
|
|
1453
1974
|
}
|
|
@@ -1485,11 +2006,17 @@ async function main() {
|
|
|
1485
2006
|
// their v4 equivalent, so we feed them in explicitly. Without this pass,
|
|
1486
2007
|
// classes like \`break-words\` would be invisible to enforce-canonical
|
|
1487
2008
|
// (issue #16).
|
|
1488
|
-
//
|
|
1489
|
-
//
|
|
1490
|
-
//
|
|
1491
|
-
//
|
|
1492
|
-
|
|
2009
|
+
//
|
|
2010
|
+
// Two kinds, and the difference is what \`deprecated\` records:
|
|
2011
|
+
// - RENAMES: a v3 spelling of a utility v4 calls something else. Mirrors the
|
|
2012
|
+
// internal v3->v4 map in tailwindcss/canonicalize. These ARE deprecated, and
|
|
2013
|
+
// no-deprecated-classes owns them.
|
|
2014
|
+
// - Pattern renames (start-* -> inset-s-*, end-* -> inset-e-*), derived from
|
|
2015
|
+
// the inset-{s,e}-* utilities in validClasses so every numeric/named value is
|
|
2016
|
+
// covered. These are NOT deprecated: \`start-2\` is current Tailwind (it is
|
|
2017
|
+
// what the docs use), it just isn't the canonical spelling. Calling it
|
|
2018
|
+
// deprecated would be a lie, so only enforce-canonical speaks about it.
|
|
2019
|
+
const v3Renames = [
|
|
1493
2020
|
'order-none',
|
|
1494
2021
|
'break-words',
|
|
1495
2022
|
'overflow-ellipsis',
|
|
@@ -1504,12 +2031,15 @@ async function main() {
|
|
|
1504
2031
|
'bg-left-top', 'bg-right-top', 'bg-left-bottom', 'bg-right-bottom',
|
|
1505
2032
|
'object-left-top', 'object-right-top', 'object-left-bottom', 'object-right-bottom',
|
|
1506
2033
|
];
|
|
2034
|
+
const renameSet = new Set(v3Renames);
|
|
2035
|
+
const legacyCandidates = [...v3Renames];
|
|
1507
2036
|
for (const cls of validClasses) {
|
|
1508
2037
|
if (cls.startsWith('inset-s-')) legacyCandidates.push('start-' + cls.slice(8));
|
|
1509
2038
|
else if (cls.startsWith('-inset-s-')) legacyCandidates.push('-start-' + cls.slice(9));
|
|
1510
2039
|
else if (cls.startsWith('inset-e-')) legacyCandidates.push('end-' + cls.slice(8));
|
|
1511
2040
|
else if (cls.startsWith('-inset-e-')) legacyCandidates.push('-end-' + cls.slice(9));
|
|
1512
2041
|
}
|
|
2042
|
+
const deprecated = {};
|
|
1513
2043
|
const legacyToProcess = legacyCandidates.filter(cls => !validSet.has(cls));
|
|
1514
2044
|
if (legacyToProcess.length > 0) {
|
|
1515
2045
|
const legacyCssResults = ds.candidatesToCss(legacyToProcess.map(pfx));
|
|
@@ -1520,10 +2050,15 @@ async function main() {
|
|
|
1520
2050
|
const canon = result[0] ? unpfx(result[0]) : null;
|
|
1521
2051
|
if (canon && canon !== cls) {
|
|
1522
2052
|
canonical[cls] = canon;
|
|
2053
|
+
// Only a v3 rename that this Tailwind still compiles AND still renames.
|
|
2054
|
+
// A release that drops either of those drops the entry, so the rule can
|
|
2055
|
+
// never suggest a replacement for a class that no longer exists.
|
|
2056
|
+
if (renameSet.has(cls)) deprecated[cls] = canon;
|
|
1523
2057
|
}
|
|
1524
2058
|
// Mark as valid so no-unknown-classes doesn't flag legacy spellings.
|
|
1525
2059
|
validClasses.push(cls);
|
|
1526
2060
|
validSet.add(cls);
|
|
2061
|
+
declCss[cls] = legacyCssResults[i];
|
|
1527
2062
|
}
|
|
1528
2063
|
}
|
|
1529
2064
|
|
|
@@ -1538,108 +2073,126 @@ async function main() {
|
|
|
1538
2073
|
if (val !== null) order[unpfx(name)] = val.toString();
|
|
1539
2074
|
}
|
|
1540
2075
|
|
|
1541
|
-
// CSS
|
|
1542
|
-
//
|
|
1543
|
-
//
|
|
1544
|
-
|
|
1545
|
-
const
|
|
1546
|
-
const
|
|
2076
|
+
// CSS declarations per class, interned. The walker, the scope grammar and the
|
|
2077
|
+
// var-read scanner live in DECL_EXTRACTOR_SOURCE above (shared with the
|
|
2078
|
+
// benchmark so the two can no longer drift).
|
|
2079
|
+
const scopeIds = new Map([['', 0]]);
|
|
2080
|
+
const scopeTable = [''];
|
|
2081
|
+
const propIds = new Map();
|
|
2082
|
+
const propTable = [];
|
|
2083
|
+
const valueIds = new Map();
|
|
2084
|
+
const valueTable = [];
|
|
2085
|
+
const declFreq = new Map();
|
|
2086
|
+
const perClass = {};
|
|
2087
|
+
const partial = [];
|
|
1547
2088
|
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
}
|
|
1562
|
-
return false;
|
|
1563
|
-
}
|
|
2089
|
+
for (const cls in declCss) {
|
|
2090
|
+
// Collect first, intern after, so the descendant-scope decision below can
|
|
2091
|
+
// look at the whole class.
|
|
2092
|
+
const raw = [];
|
|
2093
|
+
let hasElement = false, hasDescendant = false, hasConditional = false;
|
|
2094
|
+
walkDeclarations(declCss[cls], pfx(cls), (scopeTok, prop, value) => {
|
|
2095
|
+
raw.push([scopeTok, prop, value]);
|
|
2096
|
+
const conditional = scopeTok.charCodeAt(0) === 64;
|
|
2097
|
+
const body = conditional ? scopeTok.slice(1) : scopeTok;
|
|
2098
|
+
if (conditional) hasConditional = true;
|
|
2099
|
+
if (body === '') hasElement = true;
|
|
2100
|
+
else if (body === '>') hasDescendant = true;
|
|
2101
|
+
});
|
|
1564
2102
|
|
|
1565
|
-
//
|
|
1566
|
-
//
|
|
1567
|
-
//
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
if (m && !atPropertyDescriptors.has(m[1])) props.push(m[1]);
|
|
1578
|
-
}
|
|
1579
|
-
lineStart = i + 1;
|
|
1580
|
-
} else if (body[i] === '{') {
|
|
1581
|
-
depth++;
|
|
1582
|
-
} else if (body[i] === '}') {
|
|
1583
|
-
depth--;
|
|
1584
|
-
}
|
|
1585
|
-
}
|
|
1586
|
-
return props;
|
|
1587
|
-
}
|
|
2103
|
+
// A class whose CSS styles BOTH itself and its descendants (\`prose\` and its
|
|
2104
|
+
// dozens of \`:where(p)\` rules) only advertises what it puts on the element:
|
|
2105
|
+
// comparing descendant declarations would turn every \`prose prose-sm\` into
|
|
2106
|
+
// ~40 conflicts. Classes that style ONLY descendants (\`space-x-*\`,
|
|
2107
|
+
// \`divide-*\`) keep them — that is what makes \`space-x-4 space-x-2\` a
|
|
2108
|
+
// conflict while \`ms-2 space-x-4\` is not. Pseudo-elements are always kept:
|
|
2109
|
+
// they are a distinct box and there are few of them.
|
|
2110
|
+
const dropDescendants = hasElement && hasDescendant;
|
|
2111
|
+
// Anything we deliberately did not model. A class in here must never be
|
|
2112
|
+
// called redundant: \`container\` looks like a plain \`width: 100%\` because its
|
|
2113
|
+
// breakpoint \`max-width\`es live in @media, so "remove it" would be wrong.
|
|
2114
|
+
if (dropDescendants || hasConditional) partial.push(cls);
|
|
1588
2115
|
|
|
1589
|
-
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
|
|
1603
|
-
const body = text.slice(braceIdx + 1, j - 1);
|
|
1604
|
-
if (selector.startsWith('@media') || selector.startsWith('@supports') || selector.startsWith('@layer')) {
|
|
1605
|
-
processText(body);
|
|
1606
|
-
} else if (!selector.startsWith('@')) {
|
|
1607
|
-
propRe.lastIndex = 0;
|
|
1608
|
-
let m;
|
|
1609
|
-
while ((m = propRe.exec(body)) !== null) {
|
|
1610
|
-
if (!atPropertyDescriptors.has(m[1])) allProps.push(m[1]);
|
|
1611
|
-
}
|
|
1612
|
-
if (isRoot(selector)) rootProps.push(...extractTopLevelProps(body));
|
|
1613
|
-
}
|
|
1614
|
-
i = j;
|
|
1615
|
-
}
|
|
2116
|
+
const keys = [];
|
|
2117
|
+
for (let r = 0; r < raw.length; r++) {
|
|
2118
|
+
const scopeTok = raw[r][0], prop = raw[r][1], value = raw[r][2];
|
|
2119
|
+
const body = scopeTok.charCodeAt(0) === 64 ? scopeTok.slice(1) : scopeTok;
|
|
2120
|
+
if (dropDescendants && body === '>') continue;
|
|
2121
|
+
let s = scopeIds.get(scopeTok);
|
|
2122
|
+
if (s === undefined) { s = scopeTable.length; scopeTable.push(scopeTok); scopeIds.set(scopeTok, s); }
|
|
2123
|
+
let p = propIds.get(prop);
|
|
2124
|
+
if (p === undefined) { p = propTable.length; propTable.push(prop); propIds.set(prop, p); }
|
|
2125
|
+
let v = valueIds.get(value);
|
|
2126
|
+
if (v === undefined) { v = valueTable.length; valueTable.push(value); valueIds.set(value, v); }
|
|
2127
|
+
const key = s + '|' + p + '|' + v;
|
|
2128
|
+
declFreq.set(key, (declFreq.get(key) || 0) + 1);
|
|
2129
|
+
keys.push(key);
|
|
1616
2130
|
}
|
|
1617
|
-
|
|
1618
|
-
|
|
1619
|
-
//
|
|
1620
|
-
//
|
|
1621
|
-
|
|
1622
|
-
return [...new Set(result)];
|
|
2131
|
+
// Kept in emission order and NOT deduplicated: a class that declares the
|
|
2132
|
+
// same property twice (bg-linear-to-r does, once plainly and once inside
|
|
2133
|
+
// @supports) keeps both. Within one block the LAST one wins, which is CSS
|
|
2134
|
+
// semantics — the consumer applies that, the data keeps everything.
|
|
2135
|
+
if (keys.length > 0) perClass[cls] = keys;
|
|
1623
2136
|
}
|
|
1624
2137
|
|
|
1625
|
-
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
|
|
2138
|
+
// Hottest declarations get the shortest base36 ids. Ties break on the key so
|
|
2139
|
+
// the artifact stays byte-reproducible for a given design system.
|
|
2140
|
+
const sortedKeys = [...declFreq.keys()].sort((a, b) => {
|
|
2141
|
+
const d = declFreq.get(b) - declFreq.get(a);
|
|
2142
|
+
return d !== 0 ? d : (a < b ? -1 : a > b ? 1 : 0);
|
|
2143
|
+
});
|
|
2144
|
+
const declIds = new Map();
|
|
2145
|
+
const table = [];
|
|
2146
|
+
for (let i = 0; i < sortedKeys.length; i++) {
|
|
2147
|
+
declIds.set(sortedKeys[i], i);
|
|
2148
|
+
const parts = sortedKeys[i].split('|');
|
|
2149
|
+
table.push(
|
|
2150
|
+
Number(parts[0]).toString(36) + '|' + Number(parts[1]).toString(36) + '|' + Number(parts[2]).toString(36)
|
|
2151
|
+
);
|
|
2152
|
+
}
|
|
2153
|
+
const byClass = {};
|
|
2154
|
+
for (const cls in perClass) {
|
|
2155
|
+
const ids = perClass[cls];
|
|
2156
|
+
let packed = '';
|
|
2157
|
+
for (let i = 0; i < ids.length; i++) {
|
|
2158
|
+
packed += (i === 0 ? '' : ',') + declIds.get(ids[i]).toString(36);
|
|
1631
2159
|
}
|
|
2160
|
+
byClass[cls] = packed;
|
|
1632
2161
|
}
|
|
1633
2162
|
|
|
1634
|
-
//
|
|
1635
|
-
//
|
|
1636
|
-
|
|
1637
|
-
|
|
1638
|
-
|
|
1639
|
-
|
|
2163
|
+
// Which variables a value reads is a property of the VALUE, so it is computed
|
|
2164
|
+
// once per interned value (~2.7k) instead of once per declaration (~55k).
|
|
2165
|
+
const varIds = new Map();
|
|
2166
|
+
const varTable = [];
|
|
2167
|
+
const valueVars = {};
|
|
2168
|
+
const valueFallbackVars = {};
|
|
2169
|
+
const pureValues = [];
|
|
2170
|
+
function internVar(name) {
|
|
2171
|
+
let id = varIds.get(name);
|
|
2172
|
+
if (id === undefined) { id = varTable.length; varTable.push(name); varIds.set(name, id); }
|
|
2173
|
+
return id;
|
|
2174
|
+
}
|
|
2175
|
+
for (let v = 0; v < valueTable.length; v++) {
|
|
2176
|
+
const reads = scanVarReads(valueTable[v]);
|
|
2177
|
+
if (reads[0].length > 0) valueVars[v] = reads[0].map(internVar);
|
|
2178
|
+
if (reads[1].length > 0) valueFallbackVars[v] = reads[1].map(internVar);
|
|
2179
|
+
if (isPureVarRead(valueTable[v])) pureValues.push(v);
|
|
1640
2180
|
}
|
|
1641
2181
|
|
|
1642
|
-
|
|
2182
|
+
const cssDeclarations = {
|
|
2183
|
+
partial,
|
|
2184
|
+
scopes: scopeTable,
|
|
2185
|
+
props: propTable,
|
|
2186
|
+
values: valueTable,
|
|
2187
|
+
vars: varTable,
|
|
2188
|
+
valueVars,
|
|
2189
|
+
valueFallbackVars,
|
|
2190
|
+
pureValues,
|
|
2191
|
+
table,
|
|
2192
|
+
byClass,
|
|
2193
|
+
};
|
|
2194
|
+
|
|
2195
|
+
// Variant ordering from the design system.
|
|
1643
2196
|
const variantOrder = {};
|
|
1644
2197
|
const variants = ds.getVariants();
|
|
1645
2198
|
for (let i = 0; i < variants.length; i++) {
|
|
@@ -1648,6 +2201,54 @@ async function main() {
|
|
|
1648
2201
|
}
|
|
1649
2202
|
}
|
|
1650
2203
|
|
|
2204
|
+
// What each variant DOES to the selector, so the variant rules can stop
|
|
2205
|
+
// guessing from a list of names — the list cannot know a project's own
|
|
2206
|
+
// \`@custom-variant\`.
|
|
2207
|
+
//
|
|
2208
|
+
// Derived by compiling a probe utility and reading the emitted selector, NOT
|
|
2209
|
+
// from \`variant.selectors()\`: that returns '&'-relative strings, comes back
|
|
2210
|
+
// empty for \`before\`/\`after\`, and reports \`group\`/\`peer\` as arbitrary with no
|
|
2211
|
+
// selectors at all. Compiling is the same mechanism the declaration extractor
|
|
2212
|
+
// already trusts, and \`walkDeclarations\` already classifies exactly what we
|
|
2213
|
+
// need — the scope of each emitted declaration.
|
|
2214
|
+
//
|
|
2215
|
+
// Functional variants (\`group-*\`, \`peer-*\`, \`data-*\`, \`nth-*\`) are skipped:
|
|
2216
|
+
// probing them would need a value, and none of them retargets the selector.
|
|
2217
|
+
// Measured in Tailwind 4.3.3: \`group-hover\`/\`peer-checked\` compile to
|
|
2218
|
+
// \`&:is(:where(.group):hover *)\` — an \`:is()\` on the element itself — so two of
|
|
2219
|
+
// them in either order produce equivalent selectors and neither is a
|
|
2220
|
+
// reordering barrier.
|
|
2221
|
+
const variantFacts = {};
|
|
2222
|
+
const probeNames = [];
|
|
2223
|
+
for (let i = 0; i < variants.length; i++) {
|
|
2224
|
+
const v = variants[i];
|
|
2225
|
+
if (v.isArbitrary) continue;
|
|
2226
|
+
if (v.values && v.values.length > 0) continue;
|
|
2227
|
+
probeNames.push(v.name);
|
|
2228
|
+
}
|
|
2229
|
+
if (probeNames.length > 0) {
|
|
2230
|
+
const probeClasses = probeNames.map((n) => n + ':flex');
|
|
2231
|
+
const probeCss = ds.candidatesToCss(probeClasses.map(pfx));
|
|
2232
|
+
for (let i = 0; i < probeNames.length; i++) {
|
|
2233
|
+
if (!probeCss[i]) continue;
|
|
2234
|
+
let pseudo = false, structural = false;
|
|
2235
|
+
walkDeclarations(probeCss[i], pfx(probeClasses[i]), (scopeTok) => {
|
|
2236
|
+
const body = scopeTok.charCodeAt(0) === 64 ? scopeTok.slice(1) : scopeTok;
|
|
2237
|
+
if (body.startsWith('::')) pseudo = true;
|
|
2238
|
+
else if (body === '>') structural = true;
|
|
2239
|
+
});
|
|
2240
|
+
// A variant that styles a generated box is a pseudo-element variant even
|
|
2241
|
+
// when it ALSO reaches descendants' boxes (\`marker\` emits both
|
|
2242
|
+
// \`& ::marker\` and \`&::marker\`): it belongs innermost, not treated as a
|
|
2243
|
+
// reordering barrier.
|
|
2244
|
+
if (pseudo) {
|
|
2245
|
+
variantFacts[probeNames[i]] = { p: 1 };
|
|
2246
|
+
} else if (structural) {
|
|
2247
|
+
variantFacts[probeNames[i]] = { s: 1 };
|
|
2248
|
+
}
|
|
2249
|
+
}
|
|
2250
|
+
}
|
|
2251
|
+
|
|
1651
2252
|
// Component classes from @layer components
|
|
1652
2253
|
const componentClasses = extractComponentClasses(cssPath, base);
|
|
1653
2254
|
|
|
@@ -1708,7 +2309,28 @@ async function main() {
|
|
|
1708
2309
|
}
|
|
1709
2310
|
}
|
|
1710
2311
|
|
|
1711
|
-
|
|
2312
|
+
// Theme variables that dereference to another custom property. Only the
|
|
2313
|
+
// referencing ones are stored (~a handful), and the reference list reuses the
|
|
2314
|
+
// same balanced scanner the declaration values go through.
|
|
2315
|
+
// Custom properties the project defines. The entry file covers the standard
|
|
2316
|
+
// ':root { --x: ... }' pattern; definitions inside @imported files are not
|
|
2317
|
+
// scanned, so an unseen definition reads as "undefined" — which only ever makes
|
|
2318
|
+
// prefer-theme-tokens more willing to suggest, never less accurate about a
|
|
2319
|
+
// definition it can see.
|
|
2320
|
+
const definedVars = [...new Set(css.match(/--[\\w-]+(?=\\s*:)/g) || [])];
|
|
2321
|
+
|
|
2322
|
+
const themeRefs = {};
|
|
2323
|
+
if (ds.theme && typeof ds.theme.entries === 'function') {
|
|
2324
|
+
for (const [name, entry] of ds.theme.entries()) {
|
|
2325
|
+
const value = entry && typeof entry.value === 'string' ? entry.value : '';
|
|
2326
|
+
if (!value.includes('var(')) continue;
|
|
2327
|
+
const reads = scanVarReads(value);
|
|
2328
|
+
const all = [...new Set([...reads[0], ...reads[1]])];
|
|
2329
|
+
if (all.length > 0) themeRefs[name] = all;
|
|
2330
|
+
}
|
|
2331
|
+
}
|
|
2332
|
+
|
|
2333
|
+
const json = JSON.stringify({ validClasses, canonical, deprecated, order, cssDeclarations, variantOrder, variantFacts, componentClasses, arbitraryEquivalents, themeRefs, definedVars, prefix });
|
|
1712
2334
|
// Atomic write: write to a unique temp path then rename, so a peer isolate
|
|
1713
2335
|
// busy-waiting on the cache file never observes a half-written JSON.
|
|
1714
2336
|
writeFileSync(WD_TMP_PATH, json);
|
|
@@ -1793,6 +2415,19 @@ function getContentCachePath(contentHash) {
|
|
|
1793
2415
|
return join(CACHE_DIR, `${contentHash}.json`);
|
|
1794
2416
|
}
|
|
1795
2417
|
/**
|
|
2418
|
+
* The on-disk cache artifacts for a CSS entry point: the precomputed `json`
|
|
2419
|
+
* and the coordination `lock`. Exposed for tests that exercise the
|
|
2420
|
+
* cold-cache precompute coordination (issue #24).
|
|
2421
|
+
*/
|
|
2422
|
+
function cacheArtifactPaths(cssPath) {
|
|
2423
|
+
const resolved = resolve(cssPath);
|
|
2424
|
+
const hash = computeContentHash(hashableContent(resolved, readFileSync(resolved, "utf-8")));
|
|
2425
|
+
return {
|
|
2426
|
+
json: getContentCachePath(hash),
|
|
2427
|
+
lock: join(CACHE_DIR, `${hash}.lock`)
|
|
2428
|
+
};
|
|
2429
|
+
}
|
|
2430
|
+
/**
|
|
1796
2431
|
* Minimal shape check for a cache payload. Guards against (a) a poisoned file
|
|
1797
2432
|
* planted by a local attacker and (b) a corrupt/half-written or version-skewed
|
|
1798
2433
|
* file. A `{}` that is valid JSON but missing fields would otherwise blow up
|
|
@@ -1804,7 +2439,7 @@ function isPrecomputedData(data) {
|
|
|
1804
2439
|
if (typeof data !== "object" || data === null) return false;
|
|
1805
2440
|
const d = data;
|
|
1806
2441
|
const isObject = (v) => typeof v === "object" && v !== null;
|
|
1807
|
-
return Array.isArray(d.validClasses) && Array.isArray(d.componentClasses) && typeof d.prefix === "string" && isObject(d.canonical) && isObject(d.order) &&
|
|
2442
|
+
return Array.isArray(d.validClasses) && Array.isArray(d.componentClasses) && typeof d.prefix === "string" && isObject(d.canonical) && isObject(d.order) && isCssDeclarationIndex(d.cssDeclarations) && isObject(d.variantOrder) && isObject(d.arbitraryEquivalents);
|
|
1808
2443
|
}
|
|
1809
2444
|
function tryReadCache(cachePath) {
|
|
1810
2445
|
const raw = tryReadRawCache(cachePath);
|
|
@@ -1837,7 +2472,7 @@ function lockAgeMs(lockPath) {
|
|
|
1837
2472
|
return Number.POSITIVE_INFINITY;
|
|
1838
2473
|
}
|
|
1839
2474
|
}
|
|
1840
|
-
function tryUnlink(path) {
|
|
2475
|
+
function tryUnlink$1(path) {
|
|
1841
2476
|
try {
|
|
1842
2477
|
unlinkSync(path);
|
|
1843
2478
|
} catch {}
|
|
@@ -1850,14 +2485,14 @@ function tryUnlink(path) {
|
|
|
1850
2485
|
* exists to prevent, #24). `renameSync` moves the file atomically: only one
|
|
1851
2486
|
* waiter wins the move; the loser gets ENOENT and loops back to re-check.
|
|
1852
2487
|
*/
|
|
1853
|
-
function reclaimStaleLock(lockPath) {
|
|
2488
|
+
function reclaimStaleLock$1(lockPath) {
|
|
1854
2489
|
const reclaimPath = `${lockPath}.reclaim.${process.pid}.${threadId}.${precomputeSeq++}`;
|
|
1855
2490
|
try {
|
|
1856
2491
|
renameSync(lockPath, reclaimPath);
|
|
1857
2492
|
} catch {
|
|
1858
2493
|
return;
|
|
1859
2494
|
}
|
|
1860
|
-
tryUnlink(reclaimPath);
|
|
2495
|
+
tryUnlink$1(reclaimPath);
|
|
1861
2496
|
}
|
|
1862
2497
|
/** control[2] — the worker's ready/done signal (0=loading, 1=done, -1=error). */
|
|
1863
2498
|
const PRECOMPUTE_SIGNAL_INDEX = 2;
|
|
@@ -1896,6 +2531,11 @@ function runPrecomputeViaWorker(resolvedPath, tailwindNodePath, cachePath, timeo
|
|
|
1896
2531
|
const errLengthView = new DataView(sharedBuffer, 16, 4);
|
|
1897
2532
|
const errDataArea = new Uint8Array(sharedBuffer, 20);
|
|
1898
2533
|
const tmpPath = `${cachePath}.tmp.${process.pid}.${threadId}.${precomputeSeq++}`;
|
|
2534
|
+
try {
|
|
2535
|
+
new Function(PRECOMPUTE_SCRIPT);
|
|
2536
|
+
} catch (cause) {
|
|
2537
|
+
throw precomputeLoadError(resolvedPath, cause);
|
|
2538
|
+
}
|
|
1899
2539
|
let worker;
|
|
1900
2540
|
try {
|
|
1901
2541
|
worker = new Worker(PRECOMPUTE_SCRIPT, {
|
|
@@ -1919,7 +2559,7 @@ function runPrecomputeViaWorker(resolvedPath, tailwindNodePath, cachePath, timeo
|
|
|
1919
2559
|
throw precomputeLoadError(resolvedPath, len > 0 ? new Error(Buffer.from(errDataArea.slice(0, len)).toString("utf-8")) : /* @__PURE__ */ new Error("precompute worker exited without reporting a cause (it may have been killed by the OS, e.g. under memory pressure)"));
|
|
1920
2560
|
}
|
|
1921
2561
|
} finally {
|
|
1922
|
-
tryUnlink(tmpPath);
|
|
2562
|
+
tryUnlink$1(tmpPath);
|
|
1923
2563
|
worker.terminate();
|
|
1924
2564
|
}
|
|
1925
2565
|
}
|
|
@@ -1961,18 +2601,18 @@ function computeWithLock(resolvedPath, tailwindNodePath, contentHash, cachePath,
|
|
|
1961
2601
|
lockFd = openSync(lockPath, "wx");
|
|
1962
2602
|
} catch (cause) {
|
|
1963
2603
|
if (cause.code === "EEXIST") {
|
|
1964
|
-
if (lockAgeMs(lockPath) > staleMs || Date.now() - start > staleMs * 2) reclaimStaleLock(lockPath);
|
|
2604
|
+
if (lockAgeMs(lockPath) > staleMs || Date.now() - start > staleMs * 2) reclaimStaleLock$1(lockPath);
|
|
1965
2605
|
else syncSleep(LOCK_POLL_MS);
|
|
1966
2606
|
continue;
|
|
1967
2607
|
}
|
|
1968
2608
|
return precomputeAndRead(resolvedPath, tailwindNodePath, cachePath, timeout);
|
|
1969
2609
|
}
|
|
1970
2610
|
try {
|
|
1971
|
-
tryUnlink(cachePath);
|
|
2611
|
+
tryUnlink$1(cachePath);
|
|
1972
2612
|
return precomputeAndRead(resolvedPath, tailwindNodePath, cachePath, timeout);
|
|
1973
2613
|
} finally {
|
|
1974
2614
|
closeSync(lockFd);
|
|
1975
|
-
tryUnlink(lockPath);
|
|
2615
|
+
tryUnlink$1(lockPath);
|
|
1976
2616
|
}
|
|
1977
2617
|
}
|
|
1978
2618
|
}
|
|
@@ -2394,126 +3034,353 @@ function resolveEntryPointForFile(ruleOptionEntry, settingsEntry, filePath, cwd
|
|
|
2394
3034
|
throw new MissingEntryPointError(`\`settings.tailwindcss.entryPoint\` is required${filePath ? ` (linting \`${filePath}\`)` : ""}.`, "Set it to a string (single-project) or an array of `{ files, use }` mappings (monorepo).");
|
|
2395
3035
|
}
|
|
2396
3036
|
//#endregion
|
|
2397
|
-
//#region src/
|
|
3037
|
+
//#region src/design-system/ds-worker.ts
|
|
2398
3038
|
/**
|
|
2399
|
-
*
|
|
2400
|
-
*
|
|
2401
|
-
* Nine rules (enforce-canonical, enforce-logical, enforce-physical,
|
|
2402
|
-
* enforce-consistent-variable-syntax, enforce-consistent-important-position,
|
|
2403
|
-
* enforce-negative-arbitrary-values, no-deprecated-classes,
|
|
2404
|
-
* no-unnecessary-arbitrary-value, prefer-theme-tokens) all emit
|
|
2405
|
-
* replacements via the same shape:
|
|
3039
|
+
* Generic worker-thread service wrapping `__unstable__loadDesignSystem`.
|
|
2406
3040
|
*
|
|
2407
|
-
*
|
|
2408
|
-
*
|
|
2409
|
-
*
|
|
2410
|
-
*
|
|
2411
|
-
*
|
|
3041
|
+
* `sort-service.ts` and `canonicalize-service.ts` used to be near-byte-identical
|
|
3042
|
+
* (~500 LOC) — both spawned a worker that loaded the design system, then served
|
|
3043
|
+
* synchronous requests over a SharedArrayBuffer using `Atomics.wait`. The only
|
|
3044
|
+
* real difference was the body of the request loop: `getClassOrder` vs
|
|
3045
|
+
* `canonicalizeCandidates`. This class encapsulates the entire shared protocol
|
|
3046
|
+
* — SharedArrayBuffer layout, state machine, lifecycle, fail-loud — and each
|
|
3047
|
+
* service shrinks to ~40 LOC of singleton + public signature.
|
|
2412
3048
|
*
|
|
2413
|
-
*
|
|
2414
|
-
*
|
|
2415
|
-
*
|
|
3049
|
+
* v1: failures throw `SortServiceError`. The error is sticky for the lifetime
|
|
3050
|
+
* of the process (per cssPath) so subsequent calls don't pay another
|
|
3051
|
+
* init-timeout cost. Callers wrap via `safeGetDS` to surface the failure as a
|
|
3052
|
+
* single `designSystemUnavailable` diagnostic.
|
|
2416
3053
|
*/
|
|
3054
|
+
const BUFFER_SIZE = 4 * 1024 * 1024;
|
|
3055
|
+
const HEADER_INTS = 4;
|
|
3056
|
+
const LENGTH_OFFSET = HEADER_INTS * 4;
|
|
3057
|
+
const DATA_OFFSET = 20;
|
|
3058
|
+
const INIT_TIMEOUT = 6e4;
|
|
3059
|
+
const REQUEST_TIMEOUT = 3e4;
|
|
3060
|
+
const MAX_WORKERS = 8;
|
|
2417
3061
|
/**
|
|
2418
|
-
*
|
|
2419
|
-
*
|
|
2420
|
-
*
|
|
2421
|
-
*
|
|
2422
|
-
*
|
|
3062
|
+
* Build the worker script shared by `sort-service` and `canonicalize-service`.
|
|
3063
|
+
* Owns the entire SharedArrayBuffer protocol (offsets derived from the same
|
|
3064
|
+
* constants the host uses, so they can't drift), the design-system load with
|
|
3065
|
+
* error propagation (DS-M4: the real cause is written into the buffer so the
|
|
3066
|
+
* host can surface it, not a generic "failed to load"), the ready signal, and
|
|
3067
|
+
* the request loop. `handlerExpr` is a function expression `(ds, request) =>
|
|
3068
|
+
* result` — the only part that differs between services.
|
|
2423
3069
|
*/
|
|
2424
|
-
function
|
|
2425
|
-
|
|
2426
|
-
|
|
2427
|
-
|
|
2428
|
-
|
|
2429
|
-
|
|
2430
|
-
|
|
2431
|
-
|
|
2432
|
-
|
|
2433
|
-
|
|
2434
|
-
|
|
2435
|
-
|
|
2436
|
-
|
|
2437
|
-
|
|
2438
|
-
|
|
2439
|
-
|
|
2440
|
-
|
|
2441
|
-
|
|
2442
|
-
|
|
2443
|
-
|
|
2444
|
-
|
|
2445
|
-
|
|
2446
|
-
|
|
2447
|
-
|
|
2448
|
-
|
|
2449
|
-
|
|
2450
|
-
|
|
2451
|
-
|
|
2452
|
-
|
|
2453
|
-
|
|
2454
|
-
|
|
2455
|
-
|
|
2456
|
-
|
|
2457
|
-
|
|
2458
|
-
|
|
2459
|
-
|
|
2460
|
-
|
|
2461
|
-
|
|
2462
|
-
|
|
2463
|
-
|
|
2464
|
-
|
|
2465
|
-
|
|
2466
|
-
|
|
2467
|
-
|
|
2468
|
-
|
|
2469
|
-
|
|
2470
|
-
|
|
2471
|
-
|
|
2472
|
-
|
|
2473
|
-
|
|
2474
|
-
|
|
2475
|
-
|
|
2476
|
-
|
|
2477
|
-
}
|
|
2478
|
-
|
|
2479
|
-
|
|
2480
|
-
|
|
2481
|
-
|
|
2482
|
-
|
|
2483
|
-
|
|
2484
|
-
|
|
2485
|
-
|
|
2486
|
-
|
|
2487
|
-
|
|
2488
|
-
|
|
2489
|
-
|
|
2490
|
-
|
|
2491
|
-
|
|
2492
|
-
|
|
3070
|
+
function makeWorkerScript(handlerExpr, preamble = "") {
|
|
3071
|
+
return `
|
|
3072
|
+
${preamble}
|
|
3073
|
+
const { workerData } = require('worker_threads');
|
|
3074
|
+
async function main() {
|
|
3075
|
+
const { sharedBuffer, cssPath } = workerData;
|
|
3076
|
+
const control = new Int32Array(sharedBuffer, 0, ${HEADER_INTS});
|
|
3077
|
+
const lengthView = new DataView(sharedBuffer, ${LENGTH_OFFSET}, 4);
|
|
3078
|
+
const dataArea = new Uint8Array(sharedBuffer, ${DATA_OFFSET});
|
|
3079
|
+
|
|
3080
|
+
// On load failure, write the real cause into the data region so the host can
|
|
3081
|
+
// report it instead of a bare "failed to load the design system".
|
|
3082
|
+
function signalLoadError(e) {
|
|
3083
|
+
try {
|
|
3084
|
+
const msg = Buffer.from(String((e && e.message) || e || 'unknown design-system load error'), 'utf-8');
|
|
3085
|
+
const n = Math.min(msg.length, dataArea.length);
|
|
3086
|
+
dataArea.set(msg.subarray(0, n), 0);
|
|
3087
|
+
lengthView.setUint32(0, n);
|
|
3088
|
+
} catch {}
|
|
3089
|
+
Atomics.store(control, 2, -1);
|
|
3090
|
+
Atomics.notify(control, 2);
|
|
3091
|
+
}
|
|
3092
|
+
|
|
3093
|
+
let ds;
|
|
3094
|
+
try {
|
|
3095
|
+
const { __unstable__loadDesignSystem } = require(workerData.tailwindNodePath);
|
|
3096
|
+
const { readFileSync } = require('fs');
|
|
3097
|
+
const { dirname } = require('path');
|
|
3098
|
+
const css = readFileSync(cssPath, 'utf-8');
|
|
3099
|
+
ds = await __unstable__loadDesignSystem(css, { base: dirname(cssPath) });
|
|
3100
|
+
} catch (e) {
|
|
3101
|
+
signalLoadError(e);
|
|
3102
|
+
return;
|
|
3103
|
+
}
|
|
3104
|
+
|
|
3105
|
+
Atomics.store(control, 2, 1);
|
|
3106
|
+
Atomics.notify(control, 2);
|
|
3107
|
+
|
|
3108
|
+
const handler = ${handlerExpr};
|
|
3109
|
+
|
|
3110
|
+
while (true) {
|
|
3111
|
+
Atomics.wait(control, 0, 0);
|
|
3112
|
+
const len = lengthView.getUint32(0);
|
|
3113
|
+
const requestStr = Buffer.from(dataArea.slice(0, len)).toString('utf-8');
|
|
3114
|
+
Atomics.store(control, 0, 0);
|
|
3115
|
+
|
|
3116
|
+
let response;
|
|
3117
|
+
try {
|
|
3118
|
+
const request = JSON.parse(requestStr);
|
|
3119
|
+
const result = handler(ds, request);
|
|
3120
|
+
response = Buffer.from(JSON.stringify(result), 'utf-8');
|
|
3121
|
+
} catch {
|
|
3122
|
+
response = Buffer.from('null', 'utf-8');
|
|
3123
|
+
}
|
|
3124
|
+
|
|
3125
|
+
// DS-M6: if the response doesn't fit the shared buffer, reply with a
|
|
3126
|
+
// sentinel the host reads as a rejected request, instead of letting
|
|
3127
|
+
// dataArea.set throw and kill the worker (which would hang the caller
|
|
3128
|
+
// until the request timeout and then report a misleading timeout).
|
|
3129
|
+
if (response.length > dataArea.length) {
|
|
3130
|
+
response = Buffer.from('null', 'utf-8');
|
|
3131
|
+
}
|
|
3132
|
+
|
|
3133
|
+
dataArea.set(response, 0);
|
|
3134
|
+
lengthView.setUint32(0, response.length);
|
|
3135
|
+
Atomics.store(control, 1, 1);
|
|
3136
|
+
Atomics.notify(control, 1);
|
|
3137
|
+
}
|
|
3138
|
+
}
|
|
3139
|
+
main().catch(() => {});
|
|
3140
|
+
`;
|
|
3141
|
+
}
|
|
3142
|
+
var DesignSystemWorker = class {
|
|
3143
|
+
opts;
|
|
3144
|
+
workers = /* @__PURE__ */ new Map();
|
|
3145
|
+
errors = /* @__PURE__ */ new Map();
|
|
3146
|
+
constructor(opts) {
|
|
3147
|
+
this.opts = opts;
|
|
3148
|
+
}
|
|
3149
|
+
/** Record an error as sticky for `cssPath` and return it for `throw`. */
|
|
3150
|
+
remember(cssPath, err) {
|
|
3151
|
+
this.errors.set(cssPath, err);
|
|
3152
|
+
return err;
|
|
3153
|
+
}
|
|
3154
|
+
/**
|
|
3155
|
+
* Ensure the worker is running and pointed at `cssPath`. Returns the
|
|
3156
|
+
* ready state. Throws `SortServiceError` on any failure (init timeout,
|
|
3157
|
+
* spawn error, DS-load error). Sticky: subsequent calls for the same
|
|
3158
|
+
* cssPath rethrow without retrying.
|
|
3159
|
+
*/
|
|
3160
|
+
ensure(cssPath) {
|
|
3161
|
+
const sticky = this.errors.get(cssPath);
|
|
3162
|
+
if (sticky) throw sticky;
|
|
3163
|
+
const existing = this.workers.get(cssPath);
|
|
3164
|
+
if (existing) {
|
|
3165
|
+
this.workers.delete(cssPath);
|
|
3166
|
+
this.workers.set(cssPath, existing);
|
|
3167
|
+
return existing;
|
|
2493
3168
|
}
|
|
2494
|
-
|
|
2495
|
-
|
|
2496
|
-
|
|
2497
|
-
|
|
2498
|
-
|
|
2499
|
-
|
|
2500
|
-
|
|
2501
|
-
|
|
2502
|
-
|
|
2503
|
-
|
|
2504
|
-
|
|
2505
|
-
|
|
2506
|
-
|
|
2507
|
-
|
|
2508
|
-
|
|
2509
|
-
|
|
2510
|
-
}
|
|
3169
|
+
if (TAILWIND_NODE_PATH === null) throw this.remember(cssPath, new SortServiceError(`Could not resolve '@tailwindcss/node' for the ${this.opts.serviceName} worker.`, "Install '@tailwindcss/node' (or upgrade oxlint-tailwindcss) and re-run."));
|
|
3170
|
+
const sharedBuffer = new SharedArrayBuffer(BUFFER_SIZE);
|
|
3171
|
+
const controlArray = new Int32Array(sharedBuffer, 0, HEADER_INTS);
|
|
3172
|
+
const lengthView = new DataView(sharedBuffer, HEADER_INTS * 4, 4);
|
|
3173
|
+
const dataArea = new Uint8Array(sharedBuffer, DATA_OFFSET);
|
|
3174
|
+
let worker;
|
|
3175
|
+
try {
|
|
3176
|
+
worker = new Worker(this.opts.workerScript, {
|
|
3177
|
+
eval: true,
|
|
3178
|
+
workerData: {
|
|
3179
|
+
sharedBuffer,
|
|
3180
|
+
cssPath,
|
|
3181
|
+
tailwindNodePath: TAILWIND_NODE_PATH
|
|
3182
|
+
}
|
|
3183
|
+
});
|
|
3184
|
+
} catch (cause) {
|
|
3185
|
+
throw this.remember(cssPath, new SortServiceError(`Failed to spawn ${this.opts.serviceName} worker for "${cssPath}".`, "This is unexpected; please open an issue with the error details.", { cause: cause instanceof Error ? cause : void 0 }));
|
|
2511
3186
|
}
|
|
2512
|
-
|
|
3187
|
+
worker.unref();
|
|
3188
|
+
worker.on("error", (err) => {
|
|
3189
|
+
this.errors.set(cssPath, new SortServiceError(`${this.opts.serviceName} worker died: ${err.message}`, "The worker will not be restarted in this process. Restart the lint session.", { cause: err }));
|
|
3190
|
+
const current = this.workers.get(cssPath);
|
|
3191
|
+
if (current && current.worker === worker) this.workers.delete(cssPath);
|
|
3192
|
+
});
|
|
3193
|
+
if (Atomics.wait(controlArray, 2, 0, INIT_TIMEOUT) === "timed-out") {
|
|
3194
|
+
this.terminateWorker(worker);
|
|
3195
|
+
throw this.remember(cssPath, new SortServiceError(`${this.opts.serviceName} worker timed out loading the design system from "${cssPath}" after ${INIT_TIMEOUT}ms.`, "Verify the CSS imports resolve; this can also happen if the machine or CI runner is heavily loaded."));
|
|
3196
|
+
}
|
|
3197
|
+
if (controlArray[2] === -1) {
|
|
3198
|
+
const len = lengthView.getUint32(0);
|
|
3199
|
+
const detail = len > 0 ? Buffer.from(dataArea.slice(0, len)).toString("utf-8") : "";
|
|
3200
|
+
this.terminateWorker(worker);
|
|
3201
|
+
throw this.remember(cssPath, new SortServiceError(`${this.opts.serviceName} worker failed to load the design system from "${cssPath}".${detail ? ` ${detail}` : ""}`, "Verify @tailwindcss/node is installed and the CSS file (and its imports) is valid."));
|
|
3202
|
+
}
|
|
3203
|
+
const state = {
|
|
3204
|
+
worker,
|
|
3205
|
+
controlArray,
|
|
3206
|
+
lengthView,
|
|
3207
|
+
dataArea,
|
|
3208
|
+
cssPath
|
|
3209
|
+
};
|
|
3210
|
+
this.workers.set(cssPath, state);
|
|
3211
|
+
this.evictIfNeeded();
|
|
3212
|
+
return state;
|
|
3213
|
+
}
|
|
3214
|
+
/** Evict least-recently-used workers past the cap (#77). */
|
|
3215
|
+
evictIfNeeded() {
|
|
3216
|
+
while (this.workers.size > MAX_WORKERS) {
|
|
3217
|
+
const oldestKey = this.workers.keys().next().value;
|
|
3218
|
+
if (oldestKey === void 0) break;
|
|
3219
|
+
const oldest = this.workers.get(oldestKey);
|
|
3220
|
+
this.workers.delete(oldestKey);
|
|
3221
|
+
if (oldest) this.terminateWorker(oldest.worker);
|
|
3222
|
+
}
|
|
3223
|
+
}
|
|
3224
|
+
/**
|
|
3225
|
+
* Send a request to the worker and synchronously wait for the response.
|
|
3226
|
+
* Throws `SortServiceError` on any failure.
|
|
3227
|
+
*/
|
|
3228
|
+
callSync(cssPath, req) {
|
|
3229
|
+
const state = this.ensure(cssPath);
|
|
3230
|
+
const requestBytes = Buffer.from(JSON.stringify(req), "utf-8");
|
|
3231
|
+
if (requestBytes.length > BUFFER_SIZE - DATA_OFFSET) throw new SortServiceError(`${this.opts.serviceName} request payload is too large (${requestBytes.length} > ${BUFFER_SIZE - DATA_OFFSET} bytes).`, "This is unexpected with typical class lists; please open an issue.");
|
|
3232
|
+
state.dataArea.set(requestBytes, 0);
|
|
3233
|
+
state.lengthView.setUint32(0, requestBytes.length);
|
|
3234
|
+
Atomics.store(state.controlArray, 0, 1);
|
|
3235
|
+
Atomics.notify(state.controlArray, 0);
|
|
3236
|
+
if (Atomics.wait(state.controlArray, 1, 0, REQUEST_TIMEOUT) === "timed-out") {
|
|
3237
|
+
this.dropWorker(cssPath);
|
|
3238
|
+
throw this.remember(cssPath, new SortServiceError(`${this.opts.serviceName} worker request timed out after ${REQUEST_TIMEOUT}ms.`, "This is unexpected for typical class lists; please open an issue if it persists."));
|
|
3239
|
+
}
|
|
3240
|
+
const responseLen = state.lengthView.getUint32(0);
|
|
3241
|
+
const responseStr = Buffer.from(state.dataArea.slice(0, responseLen)).toString("utf-8");
|
|
3242
|
+
Atomics.store(state.controlArray, 1, 0);
|
|
3243
|
+
let parsed;
|
|
3244
|
+
try {
|
|
3245
|
+
parsed = JSON.parse(responseStr);
|
|
3246
|
+
} catch (cause) {
|
|
3247
|
+
this.dropWorker(cssPath);
|
|
3248
|
+
throw this.remember(cssPath, new SortServiceError(`${this.opts.serviceName} worker returned non-JSON response.`, "This is a bug; please open an issue.", { cause: cause instanceof Error ? cause : void 0 }));
|
|
3249
|
+
}
|
|
3250
|
+
if (parsed === null) {
|
|
3251
|
+
this.dropWorker(cssPath);
|
|
3252
|
+
throw this.remember(cssPath, new SortServiceError(`${this.opts.serviceName} worker returned null — the request body was rejected or its response did not fit the buffer.`, "This is a bug; please open an issue with the input that triggered it."));
|
|
3253
|
+
}
|
|
3254
|
+
return parsed;
|
|
3255
|
+
}
|
|
3256
|
+
reset() {
|
|
3257
|
+
for (const state of this.workers.values()) this.terminateWorker(state.worker);
|
|
3258
|
+
this.workers.clear();
|
|
3259
|
+
this.errors.clear();
|
|
3260
|
+
}
|
|
3261
|
+
/** Terminate and forget the worker for a single cssPath (on request failure). */
|
|
3262
|
+
dropWorker(cssPath) {
|
|
3263
|
+
const state = this.workers.get(cssPath);
|
|
3264
|
+
if (state) {
|
|
3265
|
+
this.workers.delete(cssPath);
|
|
3266
|
+
this.terminateWorker(state.worker);
|
|
3267
|
+
}
|
|
3268
|
+
}
|
|
3269
|
+
terminateWorker(worker) {
|
|
3270
|
+
try {
|
|
3271
|
+
worker.removeAllListeners("error");
|
|
3272
|
+
worker.terminate();
|
|
3273
|
+
} catch {}
|
|
2513
3274
|
}
|
|
3275
|
+
};
|
|
3276
|
+
//#endregion
|
|
3277
|
+
//#region src/design-system/declaration-service.ts
|
|
3278
|
+
const declWorker = new DesignSystemWorker({
|
|
3279
|
+
workerScript: makeWorkerScript(`(ds, request) => {
|
|
3280
|
+
const decls = {};
|
|
3281
|
+
const values = {};
|
|
3282
|
+
const invalid = [];
|
|
3283
|
+
// A prefixed design system only resolves the PREFIXED form (\`tw:p-[5px]\`), and
|
|
3284
|
+
// the host asks with prefix-free names — \`extractUtility\` strips the project
|
|
3285
|
+
// prefix along with the variants. Same invariant the precompute follows: apply
|
|
3286
|
+
// the prefix only when talking to the design system, key everything returned
|
|
3287
|
+
// prefix-free. Without this the service resolved NOTHING in a prefixed project,
|
|
3288
|
+
// so every user-written value went silently uncompared.
|
|
3289
|
+
const prefix = (ds.theme && ds.theme.prefix) || '';
|
|
3290
|
+
const pfx = (c) => (prefix && !c.startsWith(prefix + ':')) ? prefix + ':' + c : c;
|
|
3291
|
+
const css = ds.candidatesToCss(request.classes.map(pfx));
|
|
3292
|
+
for (let i = 0; i < request.classes.length; i++) {
|
|
3293
|
+
if (!css[i]) { invalid.push(request.classes[i]); continue; }
|
|
3294
|
+
const cls = request.classes[i];
|
|
3295
|
+
const list = [];
|
|
3296
|
+
// The selector carries the prefix, so the scope classifier needs the
|
|
3297
|
+
// prefixed name even though the result is keyed by the bare one.
|
|
3298
|
+
walkDeclarations(css[i], pfx(cls), (scope, prop, value) => {
|
|
3299
|
+
list.push([scope, prop, value]);
|
|
3300
|
+
if (!(value in values)) {
|
|
3301
|
+
const reads = scanVarReads(value);
|
|
3302
|
+
values[value] = { p: reads[0], f: reads[1], u: isPureVarRead(value) };
|
|
3303
|
+
}
|
|
3304
|
+
});
|
|
3305
|
+
if (list.length > 0) decls[cls] = list;
|
|
3306
|
+
}
|
|
3307
|
+
return { decls, values, invalid };
|
|
3308
|
+
}`, DECL_EXTRACTOR_SOURCE),
|
|
3309
|
+
serviceName: "declarations"
|
|
2514
3310
|
});
|
|
3311
|
+
/**
|
|
3312
|
+
* Everything already asked about, per design-system CACHE — not per entry point:
|
|
3313
|
+
* class name → does the design system produce CSS for it. A miss is remembered
|
|
3314
|
+
* too, so a class that compiles to nothing is not re-queried on every AST node it
|
|
3315
|
+
* appears in.
|
|
3316
|
+
*
|
|
3317
|
+
* The lifetime has to match the cache's, because that is where the declarations
|
|
3318
|
+
* are interned. Keyed by path, a rebuilt cache (an mtime bump in a long-lived
|
|
3319
|
+
* editor process, or `resetDesignSystem()`) inherited a fully-populated map and
|
|
3320
|
+
* could never re-learn: the rules went permanently blind to user-written values
|
|
3321
|
+
* with no diagnostic. A WeakMap also means there is nothing to reset.
|
|
3322
|
+
*/
|
|
3323
|
+
const answers = /* @__PURE__ */ new WeakMap();
|
|
3324
|
+
function answersFor(cache) {
|
|
3325
|
+
let map = answers.get(cache);
|
|
3326
|
+
if (!map) {
|
|
3327
|
+
map = /* @__PURE__ */ new Map();
|
|
3328
|
+
answers.set(cache, map);
|
|
3329
|
+
}
|
|
3330
|
+
return map;
|
|
3331
|
+
}
|
|
3332
|
+
/**
|
|
3333
|
+
* Ask the design system about every class there is no answer for yet, intern the
|
|
3334
|
+
* declarations it returns, and record which classes produce no CSS at all.
|
|
3335
|
+
*
|
|
3336
|
+
* Returns false when the service is unavailable. Callers treat that as "no
|
|
3337
|
+
* information" rather than as an answer, so a broken worker degrades to the
|
|
3338
|
+
* behaviour that predates it instead of to wrong diagnostics. Real failures still
|
|
3339
|
+
* surface through the sticky error the worker records.
|
|
3340
|
+
*/
|
|
3341
|
+
function ask(cssPath, cache, classes) {
|
|
3342
|
+
const known = answersFor(cache);
|
|
3343
|
+
const pending = [...new Set(classes.filter((cls) => !known.has(cls)))];
|
|
3344
|
+
if (pending.length === 0) return true;
|
|
3345
|
+
let response;
|
|
3346
|
+
try {
|
|
3347
|
+
response = declWorker.callSync(cssPath, { classes: pending });
|
|
3348
|
+
} catch (error) {
|
|
3349
|
+
if (error instanceof SortServiceError) return false;
|
|
3350
|
+
throw error;
|
|
3351
|
+
}
|
|
3352
|
+
const invalid = new Set(response.invalid);
|
|
3353
|
+
for (const cls of pending) known.set(cls, !invalid.has(cls));
|
|
3354
|
+
for (const [cls, raws] of Object.entries(response.decls)) cache.internDeclarations(cls, raws, response.values);
|
|
3355
|
+
return true;
|
|
3356
|
+
}
|
|
3357
|
+
/**
|
|
3358
|
+
* Resolve and intern declarations for `classes` that the precompute doesn't know.
|
|
3359
|
+
* They land in the cache, so the caller reads them back through
|
|
3360
|
+
* `cache.getCssDeclarations` like any other class.
|
|
3361
|
+
*/
|
|
3362
|
+
function resolveDeclarationsSync(cssPath, cache, classes) {
|
|
3363
|
+
ask(cssPath, cache, classes);
|
|
3364
|
+
}
|
|
3365
|
+
/**
|
|
3366
|
+
* Which of these classes produce CSS. `null` when the service is unavailable, so
|
|
3367
|
+
* the caller can fall back instead of reading silence as an answer.
|
|
3368
|
+
*
|
|
3369
|
+
* This is what makes validity exact for the classes the precompute cannot
|
|
3370
|
+
* enumerate: `w-45` and `bg-red-5000` are shaped identically, and only Tailwind
|
|
3371
|
+
* knows that it compiles the first and not the second.
|
|
3372
|
+
*/
|
|
3373
|
+
function validateClassesSync(cssPath, cache, classes) {
|
|
3374
|
+
if (!ask(cssPath, cache, classes)) return null;
|
|
3375
|
+
return answersFor(cache);
|
|
3376
|
+
}
|
|
2515
3377
|
//#endregion
|
|
2516
3378
|
//#region src/rules/no-unknown-classes.ts
|
|
3379
|
+
/**
|
|
3380
|
+
* Utility used to probe a variant chain. Any always-valid utility works; `flex`
|
|
3381
|
+
* is the shortest one every Tailwind build has.
|
|
3382
|
+
*/
|
|
3383
|
+
const PROBE_UTILITY = "flex";
|
|
2517
3384
|
const noUnknownClasses = defineRule({
|
|
2518
3385
|
meta: {
|
|
2519
3386
|
type: "problem",
|
|
@@ -2542,6 +3409,8 @@ const noUnknownClasses = defineRule({
|
|
|
2542
3409
|
unknown: "\"{{className}}\" is not a valid Tailwind class.",
|
|
2543
3410
|
unknownWithSuggestion: "\"{{className}}\" is not a valid Tailwind class. Did you mean \"{{suggestion}}\"?",
|
|
2544
3411
|
missingPrefix: "\"{{className}}\" needs the \"{{prefix}}:\" prefix to produce CSS. Did you mean \"{{suggestion}}\"?",
|
|
3412
|
+
unknownVariant: "\"{{className}}\" produces no CSS: \"{{variant}}\" is not a variant this design system knows.",
|
|
3413
|
+
unknownVariantWithSuggestion: "\"{{className}}\" produces no CSS: \"{{variant}}\" is not a variant this design system knows. Did you mean \"{{suggestion}}\"?",
|
|
2545
3414
|
suggestReplace: "Replace \"{{className}}\" with \"{{replacement}}\".",
|
|
2546
3415
|
...DS_UNAVAILABLE_MESSAGE
|
|
2547
3416
|
}
|
|
@@ -2566,136 +3435,184 @@ const noUnknownClasses = defineRule({
|
|
|
2566
3435
|
position
|
|
2567
3436
|
};
|
|
2568
3437
|
}
|
|
3438
|
+
/**
|
|
3439
|
+
* Does the design system compile this variant chain?
|
|
3440
|
+
*
|
|
3441
|
+
* `variantOrder` only lists the STATIC variants (71 of them: `hover`, `md`,
|
|
3442
|
+
* `dark`, …). It has no `group`, no `data`, no `@md`, because those are
|
|
3443
|
+
* functional variants whose values are unbounded — so validating a chain
|
|
3444
|
+
* against that list would report `group-hover:` and `data-[x=1]:` as unknown.
|
|
3445
|
+
* Compiling a probe utility under the chain is the only exact answer, and it
|
|
3446
|
+
* costs one design-system call per DISTINCT chain in the project.
|
|
3447
|
+
*/
|
|
3448
|
+
const chainAnswers = /* @__PURE__ */ new Map();
|
|
3449
|
+
function chainProduces(cache, entryPoint, chain) {
|
|
3450
|
+
const key = `${entryPoint}\0${chain}`;
|
|
3451
|
+
const memo = chainAnswers.get(key);
|
|
3452
|
+
if (memo !== void 0) return memo;
|
|
3453
|
+
const probe = `${chain}${PROBE_UTILITY}`;
|
|
3454
|
+
const answers = validateClassesSync(entryPoint, cache, [probe]);
|
|
3455
|
+
const verdict = answers ? answers.get(probe) ?? null : null;
|
|
3456
|
+
chainAnswers.set(key, verdict);
|
|
3457
|
+
return verdict;
|
|
3458
|
+
}
|
|
3459
|
+
/** Replace one class in the location and report, with a quick-fix suggestion. */
|
|
3460
|
+
function reportWithSuggestion(loc, classes, cls, replacement, messageId, data, split) {
|
|
3461
|
+
const fixedValue = rebuildClassString(split, classes.map((c) => c === cls ? replacement : c));
|
|
3462
|
+
context.report({
|
|
3463
|
+
node: loc.node,
|
|
3464
|
+
messageId,
|
|
3465
|
+
data,
|
|
3466
|
+
suggest: [{
|
|
3467
|
+
messageId: "suggestReplace",
|
|
3468
|
+
data: {
|
|
3469
|
+
className: cls,
|
|
3470
|
+
replacement
|
|
3471
|
+
},
|
|
3472
|
+
fix(fixer) {
|
|
3473
|
+
return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
|
|
3474
|
+
}
|
|
3475
|
+
}]
|
|
3476
|
+
});
|
|
3477
|
+
}
|
|
2569
3478
|
function check(locations) {
|
|
2570
3479
|
if (locations.length === 0) return;
|
|
2571
3480
|
const ds = safeGetDS(getDS, context, locations[0].node);
|
|
2572
3481
|
if (!ds) return;
|
|
2573
|
-
const { cache } = ds;
|
|
3482
|
+
const { cache, entryPoint } = ds;
|
|
2574
3483
|
for (const loc of locations) {
|
|
2575
3484
|
const split = splitClassesWithSeparators(loc.value);
|
|
2576
3485
|
const classes = split.classes;
|
|
3486
|
+
let toVerify = null;
|
|
3487
|
+
for (const cls of classes) {
|
|
3488
|
+
if (shouldIgnore(cls)) continue;
|
|
3489
|
+
const bare = strippedUtility(cls).bare;
|
|
3490
|
+
if (cache.isKnownClass(bare)) continue;
|
|
3491
|
+
if (!toVerify) toVerify = [];
|
|
3492
|
+
toVerify.push(bare);
|
|
3493
|
+
}
|
|
3494
|
+
const verified = toVerify ? validateClassesSync(entryPoint, cache, toVerify) : null;
|
|
2577
3495
|
for (const cls of classes) {
|
|
2578
3496
|
if (shouldIgnore(cls)) continue;
|
|
2579
3497
|
const validity = cache.classValidity(cls);
|
|
2580
|
-
if (validity === "valid") continue;
|
|
2581
3498
|
const { variant, bare, position } = strippedUtility(cls);
|
|
2582
|
-
if (
|
|
3499
|
+
if (validity === "valid" && !variant && cache.isKnownClass(bare)) continue;
|
|
3500
|
+
if (cache.deprecatedReplacement(bare)) continue;
|
|
2583
3501
|
if (validity === "missing-prefix") {
|
|
2584
3502
|
const fixed = `${cache.prefix}:${cls}`;
|
|
2585
|
-
|
|
2586
|
-
|
|
2587
|
-
|
|
2588
|
-
|
|
2589
|
-
|
|
2590
|
-
className: cls,
|
|
2591
|
-
prefix: cache.prefix,
|
|
2592
|
-
suggestion: fixed
|
|
2593
|
-
},
|
|
2594
|
-
suggest: [{
|
|
2595
|
-
messageId: "suggestReplace",
|
|
2596
|
-
data: {
|
|
2597
|
-
className: cls,
|
|
2598
|
-
replacement: fixed
|
|
2599
|
-
},
|
|
2600
|
-
fix(fixer) {
|
|
2601
|
-
return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
|
|
2602
|
-
}
|
|
2603
|
-
}]
|
|
2604
|
-
});
|
|
3503
|
+
reportWithSuggestion(loc, classes, cls, fixed, "missingPrefix", {
|
|
3504
|
+
className: cls,
|
|
3505
|
+
prefix: cache.prefix,
|
|
3506
|
+
suggestion: fixed
|
|
3507
|
+
}, split);
|
|
2605
3508
|
continue;
|
|
2606
3509
|
}
|
|
2607
|
-
const
|
|
2608
|
-
|
|
2609
|
-
|
|
2610
|
-
const
|
|
2611
|
-
|
|
3510
|
+
const compiles = cache.isKnownClass(bare) ? true : verified?.get(bare);
|
|
3511
|
+
if (validity === "unknown" || compiles === false) {
|
|
3512
|
+
const suggestionBare = findBestSuggestion(bare, cache.validClasses);
|
|
3513
|
+
const suggestion = suggestionBare ? variant + reattachImportant(suggestionBare, position) : null;
|
|
3514
|
+
if (suggestion) reportWithSuggestion(loc, classes, cls, suggestion, "unknownWithSuggestion", {
|
|
3515
|
+
className: cls,
|
|
3516
|
+
suggestion
|
|
3517
|
+
}, split);
|
|
3518
|
+
else context.report({
|
|
2612
3519
|
node: loc.node,
|
|
2613
|
-
messageId: "
|
|
2614
|
-
data: {
|
|
2615
|
-
className: cls,
|
|
2616
|
-
suggestion
|
|
2617
|
-
},
|
|
2618
|
-
suggest: [{
|
|
2619
|
-
messageId: "suggestReplace",
|
|
2620
|
-
data: {
|
|
2621
|
-
className: cls,
|
|
2622
|
-
replacement: suggestion
|
|
2623
|
-
},
|
|
2624
|
-
fix(fixer) {
|
|
2625
|
-
return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
|
|
2626
|
-
}
|
|
2627
|
-
}]
|
|
3520
|
+
messageId: "unknown",
|
|
3521
|
+
data: { className: cls }
|
|
2628
3522
|
});
|
|
2629
|
-
|
|
3523
|
+
continue;
|
|
3524
|
+
}
|
|
3525
|
+
if (!variant) continue;
|
|
3526
|
+
if (chainProduces(cache, entryPoint, variant) !== false) continue;
|
|
3527
|
+
const { variant: offending, suggestion, fixedChain } = diagnoseChain(cache, entryPoint, variant);
|
|
3528
|
+
const fixedClass = fixedChain ? fixedChain + reattachImportant(bare, position) : null;
|
|
3529
|
+
if (fixedClass && suggestion) reportWithSuggestion(loc, classes, cls, fixedClass, "unknownVariantWithSuggestion", {
|
|
3530
|
+
className: cls,
|
|
3531
|
+
variant: offending,
|
|
3532
|
+
suggestion
|
|
3533
|
+
}, split);
|
|
3534
|
+
else context.report({
|
|
2630
3535
|
node: loc.node,
|
|
2631
|
-
messageId: "
|
|
2632
|
-
data: {
|
|
3536
|
+
messageId: "unknownVariant",
|
|
3537
|
+
data: {
|
|
3538
|
+
className: cls,
|
|
3539
|
+
variant: offending
|
|
3540
|
+
}
|
|
2633
3541
|
});
|
|
2634
3542
|
}
|
|
2635
3543
|
}
|
|
2636
3544
|
}
|
|
3545
|
+
/**
|
|
3546
|
+
* Which segment of an invalid chain is to blame, and what to use instead.
|
|
3547
|
+
*
|
|
3548
|
+
* Every candidate correction is checked against the design system before it
|
|
3549
|
+
* is offered, so a suggestion is never a guess: `group-hoverr` is corrected by
|
|
3550
|
+
* fixing its last dash-segment and confirming that `group-hover:` compiles.
|
|
3551
|
+
* That also means no list of compound roots (`group`, `peer`, `data`, …) has
|
|
3552
|
+
* to be maintained here.
|
|
3553
|
+
*/
|
|
3554
|
+
function diagnoseChain(cache, entryPoint, chain) {
|
|
3555
|
+
const segments = extractVariants(`${chain}${PROBE_UTILITY}`);
|
|
3556
|
+
const names = cache.variantNames();
|
|
3557
|
+
for (const segment of segments) {
|
|
3558
|
+
if (segment.startsWith("[")) continue;
|
|
3559
|
+
if (chainProduces(cache, entryPoint, `${segment}:`) !== false) continue;
|
|
3560
|
+
for (const candidate of correctionsFor(segment, names)) {
|
|
3561
|
+
if (chainProduces(cache, entryPoint, `${candidate}:`) !== true) continue;
|
|
3562
|
+
return {
|
|
3563
|
+
variant: segment,
|
|
3564
|
+
suggestion: candidate,
|
|
3565
|
+
fixedChain: segments.map((s) => s === segment ? candidate : s).join(":") + ":"
|
|
3566
|
+
};
|
|
3567
|
+
}
|
|
3568
|
+
return {
|
|
3569
|
+
variant: segment,
|
|
3570
|
+
suggestion: null,
|
|
3571
|
+
fixedChain: null
|
|
3572
|
+
};
|
|
3573
|
+
}
|
|
3574
|
+
return {
|
|
3575
|
+
variant: chain.slice(0, -1),
|
|
3576
|
+
suggestion: null,
|
|
3577
|
+
fixedChain: null
|
|
3578
|
+
};
|
|
3579
|
+
}
|
|
3580
|
+
/** Spelling candidates for a variant segment, nearest neighbour first. */
|
|
3581
|
+
function correctionsFor(segment, names) {
|
|
3582
|
+
const out = [];
|
|
3583
|
+
const whole = findBestSuggestion(segment, names);
|
|
3584
|
+
if (whole) out.push(whole);
|
|
3585
|
+
const dash = segment.lastIndexOf("-");
|
|
3586
|
+
if (dash > 0) {
|
|
3587
|
+
const tail = findBestSuggestion(segment.slice(dash + 1), names);
|
|
3588
|
+
if (tail) out.push(`${segment.slice(0, dash)}-${tail}`);
|
|
3589
|
+
}
|
|
3590
|
+
return out;
|
|
3591
|
+
}
|
|
2637
3592
|
return createExtractorVisitors(context, check);
|
|
2638
3593
|
}
|
|
2639
3594
|
});
|
|
2640
3595
|
//#endregion
|
|
2641
3596
|
//#region src/rules/no-conflicting-classes/spec.ts
|
|
2642
|
-
const COMPLEMENTARY_GROUPS = [
|
|
2643
|
-
|
|
2644
|
-
|
|
2645
|
-
|
|
2646
|
-
},
|
|
2647
|
-
{
|
|
2648
|
-
pattern: /^(transition|duration|ease|delay)(?:-|$)/,
|
|
2649
|
-
reason: "`transition-*` composes with `duration-*`, `ease-*`, `delay-*` — `transition-all` itself has no `--tw-*` vars so the heuristic misses it."
|
|
2650
|
-
},
|
|
2651
|
-
{
|
|
2652
|
-
pattern: /^-?(translate|scale|rotate|skew)-/,
|
|
2653
|
-
reason: "Transform axes (translate / scale / rotate / skew) compose into a single `transform`; the overlap is not captured in `cssProps`."
|
|
2654
|
-
},
|
|
2655
|
-
{
|
|
2656
|
-
pattern: /^-?mask-((?:linear|radial|conic|[trblxy])(?:-(?:from|via|to|at))?)(?:-|$)/,
|
|
2657
|
-
reason: "Mask gradients: different families (e.g. `mask-linear-*` vs `mask-radial-*`) or roles (`from`/`via`/`to`/`at`) compose; same family+role conflicts."
|
|
2658
|
-
},
|
|
2659
|
-
{
|
|
2660
|
-
pattern: /^prose(?:-|$)/,
|
|
2661
|
-
reason: "`prose` + `prose-sm`/`prose-lg`/`prose-xl` modify the same scope and are designed to compose."
|
|
2662
|
-
}
|
|
2663
|
-
];
|
|
3597
|
+
const COMPLEMENTARY_GROUPS = [{
|
|
3598
|
+
pattern: /^prose(?:-|$)/,
|
|
3599
|
+
reason: "`prose` + `prose-sm`/`prose-lg`/`prose-xl` modify the same scope and are designed to compose."
|
|
3600
|
+
}];
|
|
2664
3601
|
const COMPOSITION_PAIRS = [
|
|
2665
|
-
{
|
|
2666
|
-
a: /^text-/,
|
|
2667
|
-
b: /^leading-/,
|
|
2668
|
-
reason: "`text-*` sets line-height via its size token; `leading-*` overrides it."
|
|
2669
|
-
},
|
|
2670
|
-
{
|
|
2671
|
-
a: /^text-/,
|
|
2672
|
-
b: /^tracking-/,
|
|
2673
|
-
reason: "`text-*` may set letter-spacing via its size token; `tracking-*` overrides it."
|
|
2674
|
-
},
|
|
2675
|
-
{
|
|
2676
|
-
a: /^border(?:-[0-9]|$)/,
|
|
2677
|
-
b: /^border-(?:solid|dashed|dotted|double|hidden|none)$/,
|
|
2678
|
-
reason: "`border-*` (width) composes with `border-{solid,dashed,…}` (style)."
|
|
2679
|
-
},
|
|
2680
|
-
{
|
|
2681
|
-
a: /^divide-/,
|
|
2682
|
-
b: /^border(?:-[trblxyse])?-/,
|
|
2683
|
-
reason: "`divide-*` styles direct children; `border-*` styles the element itself."
|
|
2684
|
-
},
|
|
2685
3602
|
{
|
|
2686
3603
|
a: /^prose(?:-|$)/,
|
|
2687
3604
|
b: /^max-w-/,
|
|
2688
3605
|
reason: "`prose` sets a default max-width; `max-w-*` overrides it."
|
|
2689
3606
|
},
|
|
2690
3607
|
{
|
|
2691
|
-
a: /^
|
|
2692
|
-
b: /^(?:
|
|
2693
|
-
reason: "`
|
|
3608
|
+
a: /^prose(?:-|$)/,
|
|
3609
|
+
b: /^(?:text-|leading-|tracking-)/,
|
|
3610
|
+
reason: "`prose` sets default typographic values on the element; `text-*`/`leading-*`/`tracking-*` override them."
|
|
2694
3611
|
},
|
|
2695
3612
|
{
|
|
2696
|
-
a:
|
|
2697
|
-
b: /^(?:
|
|
2698
|
-
reason: "`
|
|
3613
|
+
a: /^-?(?:space|divide)-[xy](?:-|$)/,
|
|
3614
|
+
b: /^(?:space|divide)-[xy]-reverse$/,
|
|
3615
|
+
reason: "`space-*`/`divide-*` write the reverse flag as its default; `*-reverse` flips it — the documented way to space a reversed flex row."
|
|
2699
3616
|
},
|
|
2700
3617
|
{
|
|
2701
3618
|
a: /^mask-(?:add|subtract|intersect|exclude)$/,
|
|
@@ -2704,39 +3621,239 @@ const COMPOSITION_PAIRS = [
|
|
|
2704
3621
|
}
|
|
2705
3622
|
];
|
|
2706
3623
|
//#endregion
|
|
2707
|
-
//#region src/rules/no-conflicting-classes.ts
|
|
3624
|
+
//#region src/rules/no-conflicting-classes/decide.ts
|
|
3625
|
+
/** Builds the comparison key. Same property on two different boxes never clashes. */
|
|
3626
|
+
function declKey(decl) {
|
|
3627
|
+
return decl.scope === "element" ? decl.prop : `${decl.pseudo || ">"}|${decl.prop}`;
|
|
3628
|
+
}
|
|
3629
|
+
/** The property name a key refers to, for the diagnostic message. */
|
|
3630
|
+
function keyProp(key) {
|
|
3631
|
+
const sep = key.lastIndexOf("|");
|
|
3632
|
+
return sep < 0 ? key : key.slice(sep + 1);
|
|
3633
|
+
}
|
|
3634
|
+
/** How to describe a key's box in the message; empty for the element itself. */
|
|
3635
|
+
function keyScopeLabel(key) {
|
|
3636
|
+
const sep = key.indexOf("|");
|
|
3637
|
+
if (sep < 0) return "";
|
|
3638
|
+
const scope = key.slice(0, sep);
|
|
3639
|
+
return scope === ">" ? "direct children" : scope;
|
|
3640
|
+
}
|
|
3641
|
+
const NO_INFO_VALUES = /* @__PURE__ */ new Set([
|
|
3642
|
+
"",
|
|
3643
|
+
"initial",
|
|
3644
|
+
"unset"
|
|
3645
|
+
]);
|
|
3646
|
+
const CLOSURE_LIMIT = 64;
|
|
3647
|
+
function collectFacts(classes) {
|
|
3648
|
+
const groupWrites = /* @__PURE__ */ new Set();
|
|
3649
|
+
const effectiveWriter = /* @__PURE__ */ new Map();
|
|
3650
|
+
for (const cls of classes) for (const varName of cls.writes.keys()) {
|
|
3651
|
+
groupWrites.add(varName);
|
|
3652
|
+
const current = effectiveWriter.get(varName);
|
|
3653
|
+
if (current === void 0 || cls.order !== null && (current.order === null || cls.order > current.order)) effectiveWriter.set(varName, cls);
|
|
3654
|
+
}
|
|
3655
|
+
return {
|
|
3656
|
+
groupWrites,
|
|
3657
|
+
effectiveWriter
|
|
3658
|
+
};
|
|
3659
|
+
}
|
|
3660
|
+
/** Splits `!` off and reports whether the class was important. */
|
|
3661
|
+
function isImportant(className) {
|
|
3662
|
+
return splitImportant(className).position !== null;
|
|
3663
|
+
}
|
|
3664
|
+
/**
|
|
3665
|
+
* Which custom properties a value actually depends on.
|
|
3666
|
+
*
|
|
3667
|
+
* A `var()` fallback is dead once the variable is supplied, so fallback reads
|
|
3668
|
+
* only count when a direct read is missing from the group. Without this,
|
|
3669
|
+
* `text-sm` would appear to need `--text-sm--line-height` — a token no utility
|
|
3670
|
+
* writes — and its composition with `leading-6` would read as a conflict.
|
|
3671
|
+
*/
|
|
3672
|
+
function neededVars(decl, groupWrites) {
|
|
3673
|
+
const needed = [...decl.readsVars];
|
|
3674
|
+
if (!needed.every((v) => groupWrites.has(v))) {
|
|
3675
|
+
for (const v of decl.readsFallbackVars) if (!needed.includes(v)) needed.push(v);
|
|
3676
|
+
}
|
|
3677
|
+
return needed;
|
|
3678
|
+
}
|
|
3679
|
+
/**
|
|
3680
|
+
* Does the winner's declaration still pull in the loser's contribution?
|
|
3681
|
+
*
|
|
3682
|
+
* Walks the winner's variable reads, expanding each through the group's
|
|
3683
|
+
* effective writer, and hits when that chain reaches the loser. This is what
|
|
3684
|
+
* covers `drop-shadow-xl drop-shadow-indigo-500` directly and the gradient
|
|
3685
|
+
* family (`from-*` → `--tw-gradient-via-stops` → `via-*`) transitively.
|
|
3686
|
+
*/
|
|
3687
|
+
function winnerAbsorbsLoser(winnerDecl, loser, group) {
|
|
3688
|
+
const seen = /* @__PURE__ */ new Set();
|
|
3689
|
+
const frontier = [...winnerDecl.readsVars, ...winnerDecl.readsFallbackVars];
|
|
3690
|
+
let steps = 0;
|
|
3691
|
+
while (frontier.length > 0 && steps++ < CLOSURE_LIMIT) {
|
|
3692
|
+
const varName = frontier.pop();
|
|
3693
|
+
if (seen.has(varName)) continue;
|
|
3694
|
+
seen.add(varName);
|
|
3695
|
+
const writer = group.effectiveWriter.get(varName);
|
|
3696
|
+
if (writer === loser) return true;
|
|
3697
|
+
if (!writer) continue;
|
|
3698
|
+
const written = writer.decls.get(varName);
|
|
3699
|
+
if (written) frontier.push(...written.readsVars, ...written.readsFallbackVars);
|
|
3700
|
+
}
|
|
3701
|
+
return false;
|
|
3702
|
+
}
|
|
3703
|
+
/**
|
|
3704
|
+
* Was the loser's declaration only forwarding a value someone else supplies?
|
|
3705
|
+
*
|
|
3706
|
+
* `outline-2` declares `outline-style: var(--tw-outline-style)` — it carries no
|
|
3707
|
+
* value of its own, and `outline-solid` both writes that variable and declares
|
|
3708
|
+
* the property concretely. Losing the forwarding declaration changes nothing.
|
|
3709
|
+
*
|
|
3710
|
+
* The three guards matter:
|
|
3711
|
+
* - variables the loser writes ITSELF are excluded, so `transform-none
|
|
3712
|
+
* rotate-x-45` (whose `transform` forwards four variables nobody supplies)
|
|
3713
|
+
* stays a conflict;
|
|
3714
|
+
* - the winner must supply one of them, otherwise an unrelated class that simply
|
|
3715
|
+
* outranks a reader/writer pair would silence a real clobber;
|
|
3716
|
+
* - a loser that forwards nothing (empty set) is carrying its own contribution.
|
|
3717
|
+
*/
|
|
3718
|
+
function loserOnlyForwards(loserDecl, loser, winner, group) {
|
|
3719
|
+
if (!loserDecl.pureVarRead) return false;
|
|
3720
|
+
const needed = neededVars(loserDecl, group.groupWrites).filter((v) => !loser.writes.has(v));
|
|
3721
|
+
if (needed.length === 0) return false;
|
|
3722
|
+
if (!needed.every((v) => group.groupWrites.has(v))) return false;
|
|
3723
|
+
return needed.some((v) => winner.writes.has(v));
|
|
3724
|
+
}
|
|
3725
|
+
/** Resolves which of the two classes wins the cascade. `!` beats stylesheet order. */
|
|
3726
|
+
function resolveWinner(a, b) {
|
|
3727
|
+
if (a.important !== b.important) return a.important ? {
|
|
3728
|
+
winner: a,
|
|
3729
|
+
loser: b,
|
|
3730
|
+
orderKnown: true
|
|
3731
|
+
} : {
|
|
3732
|
+
winner: b,
|
|
3733
|
+
loser: a,
|
|
3734
|
+
orderKnown: true
|
|
3735
|
+
};
|
|
3736
|
+
if (a.order === null || b.order === null || a.order === b.order) return {
|
|
3737
|
+
winner: b,
|
|
3738
|
+
loser: a,
|
|
3739
|
+
orderKnown: false
|
|
3740
|
+
};
|
|
3741
|
+
return a.order > b.order ? {
|
|
3742
|
+
winner: a,
|
|
3743
|
+
loser: b,
|
|
3744
|
+
orderKnown: true
|
|
3745
|
+
} : {
|
|
3746
|
+
winner: b,
|
|
3747
|
+
loser: a,
|
|
3748
|
+
orderKnown: true
|
|
3749
|
+
};
|
|
3750
|
+
}
|
|
3751
|
+
/** A custom property cleared to a value that says nothing. */
|
|
3752
|
+
function isReset(decl) {
|
|
3753
|
+
return decl.prop.startsWith("--") && NO_INFO_VALUES.has(decl.value.trim());
|
|
3754
|
+
}
|
|
2708
3755
|
/**
|
|
2709
|
-
*
|
|
2710
|
-
*
|
|
2711
|
-
*
|
|
2712
|
-
*
|
|
3756
|
+
* Is clearing variables the whole point of this class?
|
|
3757
|
+
*
|
|
3758
|
+
* `blur-none` writes `--tw-blur: ` and then only forwards the filter chain;
|
|
3759
|
+
* `animate-in` clears `--tw-enter-*` but also declares real values of its own.
|
|
3760
|
+
* The first cannot afford to lose its reset, the second can.
|
|
2713
3761
|
*/
|
|
2714
|
-
function
|
|
2715
|
-
|
|
2716
|
-
const
|
|
2717
|
-
|
|
2718
|
-
|
|
3762
|
+
function resetIsAllTheClassDoes(cls) {
|
|
3763
|
+
let sawSubstance = false;
|
|
3764
|
+
for (const decl of cls.decls.values()) {
|
|
3765
|
+
if (isReset(decl)) continue;
|
|
3766
|
+
if (decl.pureVarRead) continue;
|
|
3767
|
+
sawSubstance = true;
|
|
3768
|
+
break;
|
|
3769
|
+
}
|
|
3770
|
+
return !sawSubstance;
|
|
3771
|
+
}
|
|
3772
|
+
/** Does the surviving declaration reproduce what the other one contributed? */
|
|
3773
|
+
function composes(winnerDecl, winner, loserDecl, loser, group) {
|
|
3774
|
+
return winnerAbsorbsLoser(winnerDecl, loser, group) || loserOnlyForwards(loserDecl, loser, winner, group);
|
|
3775
|
+
}
|
|
3776
|
+
function decidePair(a, b, group) {
|
|
3777
|
+
const { winner, loser, orderKnown } = resolveWinner(a, b);
|
|
3778
|
+
const conflicts = [];
|
|
3779
|
+
const duplicates = [];
|
|
3780
|
+
for (const [key, loserDecl] of loser.decls) {
|
|
3781
|
+
const winnerDecl = winner.decls.get(key);
|
|
3782
|
+
if (!winnerDecl) continue;
|
|
3783
|
+
if (loser.ambiguousKeys.has(key) || winner.ambiguousKeys.has(key)) continue;
|
|
3784
|
+
if (loserDecl.valueId === winnerDecl.valueId) {
|
|
3785
|
+
duplicates.push(key);
|
|
3786
|
+
continue;
|
|
3787
|
+
}
|
|
3788
|
+
if (isReset(loserDecl) && !resetIsAllTheClassDoes(loser)) continue;
|
|
3789
|
+
if (composes(winnerDecl, winner, loserDecl, loser, group)) continue;
|
|
3790
|
+
if (!orderKnown && composes(loserDecl, loser, winnerDecl, winner, group)) continue;
|
|
3791
|
+
conflicts.push(key);
|
|
3792
|
+
}
|
|
3793
|
+
return {
|
|
3794
|
+
winner,
|
|
3795
|
+
loser,
|
|
3796
|
+
conflicts,
|
|
3797
|
+
duplicates,
|
|
3798
|
+
orderKnown
|
|
3799
|
+
};
|
|
2719
3800
|
}
|
|
2720
3801
|
/**
|
|
2721
|
-
*
|
|
2722
|
-
*
|
|
2723
|
-
*
|
|
2724
|
-
* text-clip). Direction-sensitive: the inverse means the wider later class
|
|
2725
|
-
* clobbers a prior narrower override.
|
|
3802
|
+
* Which class (if any) is dead weight: every declaration it makes is also made,
|
|
3803
|
+
* identically, by the other one. A `partial` class is never named — its CSS is
|
|
3804
|
+
* only partly modelled, so "it adds nothing" would be a guess.
|
|
2726
3805
|
*/
|
|
2727
|
-
function
|
|
2728
|
-
if (
|
|
2729
|
-
const
|
|
2730
|
-
|
|
3806
|
+
function redundantSide(verdict) {
|
|
3807
|
+
if (verdict.conflicts.length > 0 || verdict.duplicates.length === 0) return null;
|
|
3808
|
+
const dup = new Set(verdict.duplicates);
|
|
3809
|
+
const covered = (c) => c.decls.size > 0 && [...c.decls.keys()].every((k) => dup.has(k));
|
|
3810
|
+
if (!verdict.loser.partial && covered(verdict.loser)) return {
|
|
3811
|
+
loser: verdict.loser,
|
|
3812
|
+
winner: verdict.winner
|
|
3813
|
+
};
|
|
3814
|
+
if (!verdict.winner.partial && covered(verdict.winner)) return {
|
|
3815
|
+
loser: verdict.winner,
|
|
3816
|
+
winner: verdict.loser
|
|
3817
|
+
};
|
|
3818
|
+
return null;
|
|
3819
|
+
}
|
|
3820
|
+
//#endregion
|
|
3821
|
+
//#region src/rules/no-conflicting-classes.ts
|
|
3822
|
+
/**
|
|
3823
|
+
* Compiles the `allow` option.
|
|
3824
|
+
*
|
|
3825
|
+
* A bare pattern silences every pair involving a matching class; a two-element
|
|
3826
|
+
* pattern silences that specific combination. This is the supported way to teach
|
|
3827
|
+
* the rule about a stack we cannot derive — nobody should have to wait for a
|
|
3828
|
+
* release of this plugin to silence a combination their own plugins produce.
|
|
3829
|
+
*/
|
|
3830
|
+
function compileAllow(allow) {
|
|
3831
|
+
const allowClasses = [];
|
|
3832
|
+
const allowPairs = [];
|
|
3833
|
+
for (const entry of allow ?? []) {
|
|
3834
|
+
if (typeof entry === "string") {
|
|
3835
|
+
allowClasses.push(entry);
|
|
3836
|
+
continue;
|
|
3837
|
+
}
|
|
3838
|
+
const [a, b] = compileRegexList(entry);
|
|
3839
|
+
if (a && b) allowPairs.push([a, b]);
|
|
3840
|
+
}
|
|
3841
|
+
return {
|
|
3842
|
+
allowClasses: compileRegexList(allowClasses),
|
|
3843
|
+
allowPairs
|
|
3844
|
+
};
|
|
2731
3845
|
}
|
|
2732
3846
|
/**
|
|
2733
|
-
* Returns true if two classes (within the same variant) should NOT be
|
|
2734
|
-
*
|
|
2735
|
-
*
|
|
3847
|
+
* Returns true if two classes (within the same variant) should NOT be compared
|
|
3848
|
+
* at all, despite sharing CSS properties. Pure: regex tables are passed in (or
|
|
3849
|
+
* default to the module-level constants).
|
|
3850
|
+
*
|
|
3851
|
+
* These tables are the exception, not the mechanism: everything a comparison of
|
|
3852
|
+
* the generated CSS can decide is decided in `decide.ts`. What survives here is
|
|
3853
|
+
* plugin intent that no CSS comparison can see — see `spec.ts` for why each
|
|
3854
|
+
* entry cannot be derived.
|
|
2736
3855
|
*/
|
|
2737
|
-
function shouldSkipPair(a, b,
|
|
2738
|
-
if (isCompositionViaCssVars(propsA, propsB)) return true;
|
|
2739
|
-
if (isNarrowingOverride(propsA, propsB)) return true;
|
|
3856
|
+
function shouldSkipPair(a, b, rules = {}) {
|
|
2740
3857
|
const ua = splitImportant(extractUtility(a)).bare;
|
|
2741
3858
|
const ub = splitImportant(extractUtility(b)).bare;
|
|
2742
3859
|
const groups = rules.complementaryGroups !== void 0 ? rules.complementaryGroups.map((pattern) => ({ pattern })) : COMPLEMENTARY_GROUPS;
|
|
@@ -2754,28 +3871,72 @@ function shouldSkipPair(a, b, propsA, propsB, rules = {}) {
|
|
|
2754
3871
|
for (const { a: reA, b: reB } of pairs) if (reA.test(ua) && reB.test(ub) || reA.test(ub) && reB.test(ua)) return true;
|
|
2755
3872
|
return false;
|
|
2756
3873
|
}
|
|
3874
|
+
/** Whether the user's `allow` option silences this pair. */
|
|
3875
|
+
function isAllowed(a, b, allowClasses, allowPairs) {
|
|
3876
|
+
if (matchesAny(a, allowClasses) || matchesAny(b, allowClasses)) return true;
|
|
3877
|
+
return allowPairs.some(([reA, reB]) => reA.test(a) && reB.test(b) || reA.test(b) && reB.test(a));
|
|
3878
|
+
}
|
|
3879
|
+
/**
|
|
3880
|
+
* `"color"` / `"color", "width"` / `3 CSS properties`, naming the box when it
|
|
3881
|
+
* isn't the element. A pair can clash on several boxes at once (a plugin utility
|
|
3882
|
+
* that styles itself and a `::-webkit-scrollbar` pseudo-element), so the label
|
|
3883
|
+
* goes per property unless every property shares the same box.
|
|
3884
|
+
*/
|
|
3885
|
+
function formatProperties(keys) {
|
|
3886
|
+
const scopes = new Set(keys.map(keyScopeLabel));
|
|
3887
|
+
if (scopes.size > 1) {
|
|
3888
|
+
const labelled = [...new Set(keys.map((key) => {
|
|
3889
|
+
const scope = keyScopeLabel(key);
|
|
3890
|
+
return scope ? `"${keyProp(key)}" (${scope})` : `"${keyProp(key)}"`;
|
|
3891
|
+
}))];
|
|
3892
|
+
return labelled.length <= 3 ? labelled.join(", ") : `${labelled.length} CSS properties`;
|
|
3893
|
+
}
|
|
3894
|
+
const props = [...new Set(keys.map(keyProp))];
|
|
3895
|
+
const list = props.length <= 3 ? `"${props.join("\", \"")}"` : `${props.length} CSS properties`;
|
|
3896
|
+
const scope = [...scopes][0];
|
|
3897
|
+
return scope ? `${list} (${scope})` : list;
|
|
3898
|
+
}
|
|
2757
3899
|
const noConflictingClasses = defineRule({
|
|
2758
3900
|
meta: {
|
|
2759
3901
|
type: "problem",
|
|
2760
3902
|
docs: { description: "Disallow Tailwind CSS classes that generate conflicting CSS properties" },
|
|
2761
3903
|
schema: [{
|
|
2762
3904
|
type: "object",
|
|
2763
|
-
properties: {
|
|
3905
|
+
properties: {
|
|
3906
|
+
entryPoint: { type: "string" },
|
|
3907
|
+
reportRedundant: { type: "boolean" },
|
|
3908
|
+
allow: {
|
|
3909
|
+
type: "array",
|
|
3910
|
+
items: { oneOf: [{ type: "string" }, {
|
|
3911
|
+
type: "array",
|
|
3912
|
+
items: { type: "string" },
|
|
3913
|
+
minItems: 2,
|
|
3914
|
+
maxItems: 2
|
|
3915
|
+
}] }
|
|
3916
|
+
}
|
|
3917
|
+
},
|
|
2764
3918
|
additionalProperties: false
|
|
2765
3919
|
}],
|
|
2766
|
-
defaultOptions: [{}],
|
|
3920
|
+
defaultOptions: [{ reportRedundant: true }],
|
|
2767
3921
|
messages: {
|
|
2768
|
-
conflict: "\"{{
|
|
3922
|
+
conflict: "\"{{winner}}\" overrides \"{{loser}}\" on {{properties}}. \"{{winner}}\" comes later in the generated stylesheet, so it wins no matter how the class attribute is ordered. Remove one.",
|
|
3923
|
+
conflictUnordered: "\"{{classA}}\" and \"{{classB}}\" both affect {{properties}}. Only one applies — which wins depends on the generated stylesheet order, not the attribute order. Remove one.",
|
|
3924
|
+
redundant: "\"{{loser}}\" has no effect: \"{{winner}}\" declares {{properties}} with the same value. Remove \"{{loser}}\".",
|
|
2769
3925
|
...DS_UNAVAILABLE_MESSAGE
|
|
2770
3926
|
}
|
|
2771
3927
|
},
|
|
2772
3928
|
createOnce(context) {
|
|
2773
3929
|
const getDS = createLazyLoader(context);
|
|
3930
|
+
const getOptions = createLazyOptions(context, (o) => ({
|
|
3931
|
+
reportRedundant: o?.reportRedundant ?? true,
|
|
3932
|
+
...compileAllow(o?.allow)
|
|
3933
|
+
}));
|
|
2774
3934
|
function check(locations) {
|
|
2775
3935
|
if (locations.length === 0) return;
|
|
2776
3936
|
const ds = safeGetDS(getDS, context, locations[0].node);
|
|
2777
3937
|
if (!ds) return;
|
|
2778
3938
|
const { cache } = ds;
|
|
3939
|
+
const { reportRedundant, allowClasses, allowPairs } = getOptions();
|
|
2779
3940
|
for (const loc of locations) {
|
|
2780
3941
|
const classes = splitClasses(loc.value);
|
|
2781
3942
|
if (classes.length < 2) continue;
|
|
@@ -2788,34 +3949,73 @@ const noConflictingClasses = defineRule({
|
|
|
2788
3949
|
}
|
|
2789
3950
|
for (const [, variantClasses] of byVariant) {
|
|
2790
3951
|
if (variantClasses.length < 2) continue;
|
|
2791
|
-
const
|
|
2792
|
-
|
|
2793
|
-
|
|
2794
|
-
|
|
2795
|
-
|
|
2796
|
-
|
|
2797
|
-
const
|
|
2798
|
-
const
|
|
2799
|
-
|
|
2800
|
-
|
|
2801
|
-
if (
|
|
2802
|
-
const
|
|
2803
|
-
|
|
2804
|
-
|
|
2805
|
-
|
|
2806
|
-
if (
|
|
2807
|
-
|
|
2808
|
-
|
|
2809
|
-
|
|
2810
|
-
|
|
2811
|
-
|
|
2812
|
-
|
|
2813
|
-
|
|
2814
|
-
|
|
2815
|
-
|
|
2816
|
-
|
|
2817
|
-
|
|
3952
|
+
const unknown = variantClasses.map((cls) => splitImportant(extractUtility(cls)).bare).filter((bare) => cache.getCssDeclarations(bare).length === 0 && isUserValued(bare));
|
|
3953
|
+
if (unknown.length > 0) resolveDeclarationsSync(ds.entryPoint, cache, unknown);
|
|
3954
|
+
const facts = variantClasses.map((cls) => {
|
|
3955
|
+
const utility = extractUtility(cls);
|
|
3956
|
+
const bare = splitImportant(utility).bare;
|
|
3957
|
+
const declarations = cache.getCssDeclarations(bare);
|
|
3958
|
+
const decls = /* @__PURE__ */ new Map();
|
|
3959
|
+
const writes = /* @__PURE__ */ new Map();
|
|
3960
|
+
const ambiguousKeys = /* @__PURE__ */ new Set();
|
|
3961
|
+
for (const decl of declarations) {
|
|
3962
|
+
if (decl.conditional) continue;
|
|
3963
|
+
const key = declKey(decl);
|
|
3964
|
+
const previous = decls.get(key);
|
|
3965
|
+
if (previous && previous.valueId !== decl.valueId) ambiguousKeys.add(key);
|
|
3966
|
+
decls.set(key, decl);
|
|
3967
|
+
if (decl.prop.startsWith("--")) writes.set(decl.prop, decl.valueId);
|
|
3968
|
+
}
|
|
3969
|
+
return {
|
|
3970
|
+
className: cls,
|
|
3971
|
+
decls,
|
|
3972
|
+
writes,
|
|
3973
|
+
order: cache.hasExactOrder(utility) ? cache.getOrder(cls) : null,
|
|
3974
|
+
important: isImportant(utility),
|
|
3975
|
+
partial: cache.isPartial(bare),
|
|
3976
|
+
ambiguousKeys
|
|
3977
|
+
};
|
|
3978
|
+
});
|
|
3979
|
+
const group = collectFacts(facts);
|
|
3980
|
+
for (let i = 0; i < facts.length; i++) for (let j = i + 1; j < facts.length; j++) {
|
|
3981
|
+
const a = facts[i];
|
|
3982
|
+
const b = facts[j];
|
|
3983
|
+
if (a.className === b.className) continue;
|
|
3984
|
+
if (shouldSkipPair(a.className, b.className)) continue;
|
|
3985
|
+
if (isAllowed(a.className, b.className, allowClasses, allowPairs)) continue;
|
|
3986
|
+
const verdict = decidePair(a, b, group);
|
|
3987
|
+
if (verdict.conflicts.length > 0) {
|
|
3988
|
+
const properties = formatProperties(verdict.conflicts);
|
|
3989
|
+
context.report(verdict.orderKnown ? {
|
|
3990
|
+
node: loc.node,
|
|
3991
|
+
messageId: "conflict",
|
|
3992
|
+
data: {
|
|
3993
|
+
winner: verdict.winner.className,
|
|
3994
|
+
loser: verdict.loser.className,
|
|
3995
|
+
properties
|
|
3996
|
+
}
|
|
3997
|
+
} : {
|
|
3998
|
+
node: loc.node,
|
|
3999
|
+
messageId: "conflictUnordered",
|
|
4000
|
+
data: {
|
|
4001
|
+
classA: a.className,
|
|
4002
|
+
classB: b.className,
|
|
4003
|
+
properties
|
|
4004
|
+
}
|
|
4005
|
+
});
|
|
4006
|
+
continue;
|
|
2818
4007
|
}
|
|
4008
|
+
if (!reportRedundant) continue;
|
|
4009
|
+
const dead = redundantSide(verdict);
|
|
4010
|
+
if (dead) context.report({
|
|
4011
|
+
node: loc.node,
|
|
4012
|
+
messageId: "redundant",
|
|
4013
|
+
data: {
|
|
4014
|
+
loser: dead.loser.className,
|
|
4015
|
+
winner: dead.winner.className,
|
|
4016
|
+
properties: formatProperties(verdict.duplicates)
|
|
4017
|
+
}
|
|
4018
|
+
});
|
|
2819
4019
|
}
|
|
2820
4020
|
}
|
|
2821
4021
|
}
|
|
@@ -2824,226 +4024,155 @@ const noConflictingClasses = defineRule({
|
|
|
2824
4024
|
}
|
|
2825
4025
|
});
|
|
2826
4026
|
//#endregion
|
|
2827
|
-
//#region src/
|
|
4027
|
+
//#region src/utils/report.ts
|
|
2828
4028
|
/**
|
|
2829
|
-
*
|
|
4029
|
+
* Shared reporter for the "first-as-fix, rest-as-suggestion" pattern.
|
|
2830
4030
|
*
|
|
2831
|
-
*
|
|
2832
|
-
*
|
|
2833
|
-
*
|
|
2834
|
-
*
|
|
2835
|
-
*
|
|
2836
|
-
* — SharedArrayBuffer layout, state machine, lifecycle, fail-loud — and each
|
|
2837
|
-
* service shrinks to ~40 LOC of singleton + public signature.
|
|
4031
|
+
* Nine rules (enforce-canonical, enforce-logical, enforce-physical,
|
|
4032
|
+
* enforce-consistent-variable-syntax, enforce-consistent-important-position,
|
|
4033
|
+
* enforce-negative-arbitrary-values, no-deprecated-classes,
|
|
4034
|
+
* no-unnecessary-arbitrary-value, prefer-theme-tokens) all emit
|
|
4035
|
+
* replacements via the same shape:
|
|
2838
4036
|
*
|
|
2839
|
-
*
|
|
2840
|
-
*
|
|
2841
|
-
*
|
|
2842
|
-
*
|
|
4037
|
+
* 1. Collect `{ cls, replacement }` entries from a class string.
|
|
4038
|
+
* 2. Rebuild the full class string with replacements applied.
|
|
4039
|
+
* 3. Emit ONE diagnostic with the autofix attached.
|
|
4040
|
+
* 4. Emit one MORE diagnostic per remaining offender, each carrying
|
|
4041
|
+
* the same autofix as a `suggest`.
|
|
4042
|
+
*
|
|
4043
|
+
* oxlint's fixer is single-shot per AST node, so multiple offenders in the
|
|
4044
|
+
* same class string can only get one mechanical fix; the rest stay as
|
|
4045
|
+
* suggestions the IDE can offer interactively.
|
|
2843
4046
|
*/
|
|
2844
|
-
const BUFFER_SIZE = 4 * 1024 * 1024;
|
|
2845
|
-
const HEADER_INTS = 4;
|
|
2846
|
-
const LENGTH_OFFSET = HEADER_INTS * 4;
|
|
2847
|
-
const DATA_OFFSET = 20;
|
|
2848
|
-
const INIT_TIMEOUT = 6e4;
|
|
2849
|
-
const REQUEST_TIMEOUT = 3e4;
|
|
2850
4047
|
/**
|
|
2851
|
-
*
|
|
2852
|
-
*
|
|
2853
|
-
*
|
|
2854
|
-
*
|
|
2855
|
-
*
|
|
2856
|
-
* the request loop. `handlerExpr` is a function expression `(ds, request) =>
|
|
2857
|
-
* result` — the only part that differs between services.
|
|
4048
|
+
* Emit autofix + suggestion diagnostics for a list of class replacements.
|
|
4049
|
+
*
|
|
4050
|
+
* If `offending` is empty, no-op. The fixer's `replaceTextRange` is shared
|
|
4051
|
+
* across all diagnostics — the first one is the primary fix, the rest carry
|
|
4052
|
+
* the same fix as a suggestion (oxlint can only apply one autofix per node).
|
|
2858
4053
|
*/
|
|
2859
|
-
function
|
|
2860
|
-
return
|
|
2861
|
-
const {
|
|
2862
|
-
|
|
2863
|
-
|
|
2864
|
-
|
|
2865
|
-
|
|
2866
|
-
|
|
2867
|
-
|
|
2868
|
-
|
|
2869
|
-
|
|
2870
|
-
|
|
2871
|
-
|
|
2872
|
-
const msg = Buffer.from(String((e && e.message) || e || 'unknown design-system load error'), 'utf-8');
|
|
2873
|
-
const n = Math.min(msg.length, dataArea.length);
|
|
2874
|
-
dataArea.set(msg.subarray(0, n), 0);
|
|
2875
|
-
lengthView.setUint32(0, n);
|
|
2876
|
-
} catch {}
|
|
2877
|
-
Atomics.store(control, 2, -1);
|
|
2878
|
-
Atomics.notify(control, 2);
|
|
2879
|
-
}
|
|
2880
|
-
|
|
2881
|
-
let ds;
|
|
2882
|
-
try {
|
|
2883
|
-
const { __unstable__loadDesignSystem } = require(workerData.tailwindNodePath);
|
|
2884
|
-
const { readFileSync } = require('fs');
|
|
2885
|
-
const { dirname } = require('path');
|
|
2886
|
-
const css = readFileSync(cssPath, 'utf-8');
|
|
2887
|
-
ds = await __unstable__loadDesignSystem(css, { base: dirname(cssPath) });
|
|
2888
|
-
} catch (e) {
|
|
2889
|
-
signalLoadError(e);
|
|
2890
|
-
return;
|
|
2891
|
-
}
|
|
2892
|
-
|
|
2893
|
-
Atomics.store(control, 2, 1);
|
|
2894
|
-
Atomics.notify(control, 2);
|
|
2895
|
-
|
|
2896
|
-
const handler = ${handlerExpr};
|
|
2897
|
-
|
|
2898
|
-
while (true) {
|
|
2899
|
-
Atomics.wait(control, 0, 0);
|
|
2900
|
-
const len = lengthView.getUint32(0);
|
|
2901
|
-
const requestStr = Buffer.from(dataArea.slice(0, len)).toString('utf-8');
|
|
2902
|
-
Atomics.store(control, 0, 0);
|
|
2903
|
-
|
|
2904
|
-
let response;
|
|
2905
|
-
try {
|
|
2906
|
-
const request = JSON.parse(requestStr);
|
|
2907
|
-
const result = handler(ds, request);
|
|
2908
|
-
response = Buffer.from(JSON.stringify(result), 'utf-8');
|
|
2909
|
-
} catch {
|
|
2910
|
-
response = Buffer.from('null', 'utf-8');
|
|
2911
|
-
}
|
|
2912
|
-
|
|
2913
|
-
// DS-M6: if the response doesn't fit the shared buffer, reply with a
|
|
2914
|
-
// sentinel the host reads as a rejected request, instead of letting
|
|
2915
|
-
// dataArea.set throw and kill the worker (which would hang the caller
|
|
2916
|
-
// until the request timeout and then report a misleading timeout).
|
|
2917
|
-
if (response.length > dataArea.length) {
|
|
2918
|
-
response = Buffer.from('null', 'utf-8');
|
|
2919
|
-
}
|
|
2920
|
-
|
|
2921
|
-
dataArea.set(response, 0);
|
|
2922
|
-
lengthView.setUint32(0, response.length);
|
|
2923
|
-
Atomics.store(control, 1, 1);
|
|
2924
|
-
Atomics.notify(control, 1);
|
|
2925
|
-
}
|
|
2926
|
-
}
|
|
2927
|
-
main().catch(() => {});
|
|
2928
|
-
`;
|
|
2929
|
-
}
|
|
2930
|
-
var DesignSystemWorker = class {
|
|
2931
|
-
opts;
|
|
2932
|
-
ready = null;
|
|
2933
|
-
lastError = null;
|
|
2934
|
-
lastErrorCssPath = null;
|
|
2935
|
-
constructor(opts) {
|
|
2936
|
-
this.opts = opts;
|
|
2937
|
-
}
|
|
2938
|
-
/** Record an error as sticky for `cssPath` and return it for `throw`. */
|
|
2939
|
-
remember(cssPath, err) {
|
|
2940
|
-
this.lastError = err;
|
|
2941
|
-
this.lastErrorCssPath = cssPath;
|
|
2942
|
-
return err;
|
|
2943
|
-
}
|
|
2944
|
-
/**
|
|
2945
|
-
* Ensure the worker is running and pointed at `cssPath`. Returns the
|
|
2946
|
-
* ready state. Throws `SortServiceError` on any failure (init timeout,
|
|
2947
|
-
* spawn error, DS-load error). Sticky: subsequent calls for the same
|
|
2948
|
-
* cssPath rethrow without retrying.
|
|
2949
|
-
*/
|
|
2950
|
-
ensure(cssPath) {
|
|
2951
|
-
if (this.lastError && this.lastErrorCssPath === cssPath) throw this.lastError;
|
|
2952
|
-
if (this.ready && this.ready.cssPath === cssPath) return this.ready;
|
|
2953
|
-
if (this.ready) this.cleanup();
|
|
2954
|
-
this.lastError = null;
|
|
2955
|
-
this.lastErrorCssPath = null;
|
|
2956
|
-
if (TAILWIND_NODE_PATH === null) throw this.remember(cssPath, new SortServiceError(`Could not resolve '@tailwindcss/node' for the ${this.opts.serviceName} worker.`, "Install '@tailwindcss/node' (or upgrade oxlint-tailwindcss) and re-run."));
|
|
2957
|
-
const sharedBuffer = new SharedArrayBuffer(BUFFER_SIZE);
|
|
2958
|
-
const controlArray = new Int32Array(sharedBuffer, 0, HEADER_INTS);
|
|
2959
|
-
const lengthView = new DataView(sharedBuffer, HEADER_INTS * 4, 4);
|
|
2960
|
-
const dataArea = new Uint8Array(sharedBuffer, DATA_OFFSET);
|
|
2961
|
-
let worker;
|
|
2962
|
-
try {
|
|
2963
|
-
worker = new Worker(this.opts.workerScript, {
|
|
2964
|
-
eval: true,
|
|
2965
|
-
workerData: {
|
|
2966
|
-
sharedBuffer,
|
|
2967
|
-
cssPath,
|
|
2968
|
-
tailwindNodePath: TAILWIND_NODE_PATH
|
|
2969
|
-
}
|
|
2970
|
-
});
|
|
2971
|
-
} catch (cause) {
|
|
2972
|
-
throw this.remember(cssPath, new SortServiceError(`Failed to spawn ${this.opts.serviceName} worker for "${cssPath}".`, "This is unexpected; please open an issue with the error details.", { cause: cause instanceof Error ? cause : void 0 }));
|
|
2973
|
-
}
|
|
2974
|
-
worker.unref();
|
|
2975
|
-
worker.on("error", (err) => {
|
|
2976
|
-
this.lastError = new SortServiceError(`${this.opts.serviceName} worker died: ${err.message}`, "The worker will not be restarted in this process. Restart the lint session.", { cause: err });
|
|
2977
|
-
this.lastErrorCssPath = cssPath;
|
|
2978
|
-
});
|
|
2979
|
-
if (Atomics.wait(controlArray, 2, 0, INIT_TIMEOUT) === "timed-out") {
|
|
2980
|
-
this.terminateWorker(worker);
|
|
2981
|
-
throw this.remember(cssPath, new SortServiceError(`${this.opts.serviceName} worker timed out loading the design system from "${cssPath}" after ${INIT_TIMEOUT}ms.`, "Verify the CSS imports resolve; this can also happen if the machine or CI runner is heavily loaded."));
|
|
2982
|
-
}
|
|
2983
|
-
if (controlArray[2] === -1) {
|
|
2984
|
-
const len = lengthView.getUint32(0);
|
|
2985
|
-
const detail = len > 0 ? Buffer.from(dataArea.slice(0, len)).toString("utf-8") : "";
|
|
2986
|
-
this.terminateWorker(worker);
|
|
2987
|
-
throw this.remember(cssPath, new SortServiceError(`${this.opts.serviceName} worker failed to load the design system from "${cssPath}".${detail ? ` ${detail}` : ""}`, "Verify @tailwindcss/node is installed and the CSS file (and its imports) is valid."));
|
|
2988
|
-
}
|
|
2989
|
-
this.ready = {
|
|
2990
|
-
worker,
|
|
2991
|
-
controlArray,
|
|
2992
|
-
lengthView,
|
|
2993
|
-
dataArea,
|
|
2994
|
-
cssPath
|
|
4054
|
+
function reportClassReplacements(context, loc, split, classes, offending, options) {
|
|
4055
|
+
if (offending.length === 0) return;
|
|
4056
|
+
const replacements = new Map(offending.map(({ cls, replacement }) => [cls, replacement]));
|
|
4057
|
+
const fixedValue = rebuildClassString(split, classes.map((c) => replacements.get(c) ?? c));
|
|
4058
|
+
const replacementKey = options.replacementKey ?? "replacement";
|
|
4059
|
+
const suggestMessageId = options.suggestMessageId ?? "suggestReplace";
|
|
4060
|
+
const applyFix = (fixer) => fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
|
|
4061
|
+
const report = context.report;
|
|
4062
|
+
for (let i = 0; i < offending.length; i++) {
|
|
4063
|
+
const { cls, replacement } = offending[i];
|
|
4064
|
+
const primaryData = {
|
|
4065
|
+
className: cls,
|
|
4066
|
+
[replacementKey]: replacement
|
|
2995
4067
|
};
|
|
2996
|
-
|
|
4068
|
+
if (i === 0) report({
|
|
4069
|
+
node: loc.node,
|
|
4070
|
+
messageId: options.messageId,
|
|
4071
|
+
data: primaryData,
|
|
4072
|
+
fix: applyFix
|
|
4073
|
+
});
|
|
4074
|
+
else report({
|
|
4075
|
+
node: loc.node,
|
|
4076
|
+
messageId: options.messageId,
|
|
4077
|
+
data: primaryData,
|
|
4078
|
+
suggest: [{
|
|
4079
|
+
messageId: suggestMessageId,
|
|
4080
|
+
data: {
|
|
4081
|
+
className: cls,
|
|
4082
|
+
replacement
|
|
4083
|
+
},
|
|
4084
|
+
fix: applyFix
|
|
4085
|
+
}]
|
|
4086
|
+
});
|
|
2997
4087
|
}
|
|
2998
|
-
|
|
2999
|
-
|
|
3000
|
-
|
|
3001
|
-
|
|
3002
|
-
|
|
3003
|
-
|
|
3004
|
-
|
|
3005
|
-
|
|
3006
|
-
|
|
3007
|
-
|
|
3008
|
-
|
|
3009
|
-
|
|
3010
|
-
|
|
3011
|
-
|
|
3012
|
-
|
|
3013
|
-
|
|
3014
|
-
|
|
3015
|
-
|
|
3016
|
-
|
|
3017
|
-
|
|
3018
|
-
|
|
3019
|
-
|
|
3020
|
-
|
|
3021
|
-
|
|
3022
|
-
|
|
3023
|
-
|
|
3024
|
-
|
|
3025
|
-
|
|
3026
|
-
|
|
4088
|
+
}
|
|
4089
|
+
//#endregion
|
|
4090
|
+
//#region src/utils/replacement.ts
|
|
4091
|
+
/**
|
|
4092
|
+
* Returns a predicate that answers "would this replacement produce CSS?".
|
|
4093
|
+
*
|
|
4094
|
+
* `cache.isValid` is the tolerant validity check, so it accepts arbitrary values
|
|
4095
|
+
* (`foo-[5px]`) it cannot verify. That is the right posture here: a rewrite of an
|
|
4096
|
+
* already-invalid class is `no-unknown-classes`' business, and this guard only
|
|
4097
|
+
* has to stop a rewrite from INTRODUCING an invalid class.
|
|
4098
|
+
*/
|
|
4099
|
+
function makeReplacementGuard(cache) {
|
|
4100
|
+
if (!cache) return () => true;
|
|
4101
|
+
return (replacement) => cache.isValid(replacement);
|
|
4102
|
+
}
|
|
4103
|
+
//#endregion
|
|
4104
|
+
//#region src/rules/no-deprecated-classes.ts
|
|
4105
|
+
/**
|
|
4106
|
+
* Fallback rename map, for when no design system is available.
|
|
4107
|
+
*
|
|
4108
|
+
* With an `entryPoint` configured the rule uses the map the precompute derives
|
|
4109
|
+
* from `canonicalizeCandidates`, which is strictly better: it covers the renames
|
|
4110
|
+
* this list misses (`break-words`, `order-none`, the reordered
|
|
4111
|
+
* `bg-left-top`/`object-left-top` position spellings) and it prunes itself when a
|
|
4112
|
+
* Tailwind release stops compiling one of them. This table only has to keep the
|
|
4113
|
+
* unconfigured case working exactly as it did.
|
|
4114
|
+
*/
|
|
4115
|
+
const DEPRECATED_MAP = {
|
|
4116
|
+
"flex-grow": "grow",
|
|
4117
|
+
"flex-grow-0": "grow-0",
|
|
4118
|
+
"flex-shrink": "shrink",
|
|
4119
|
+
"flex-shrink-0": "shrink-0",
|
|
4120
|
+
"overflow-ellipsis": "text-ellipsis",
|
|
4121
|
+
"decoration-slice": "box-decoration-slice",
|
|
4122
|
+
"decoration-clone": "box-decoration-clone",
|
|
4123
|
+
"bg-gradient-to-t": "bg-linear-to-t",
|
|
4124
|
+
"bg-gradient-to-tr": "bg-linear-to-tr",
|
|
4125
|
+
"bg-gradient-to-r": "bg-linear-to-r",
|
|
4126
|
+
"bg-gradient-to-br": "bg-linear-to-br",
|
|
4127
|
+
"bg-gradient-to-b": "bg-linear-to-b",
|
|
4128
|
+
"bg-gradient-to-bl": "bg-linear-to-bl",
|
|
4129
|
+
"bg-gradient-to-l": "bg-linear-to-l",
|
|
4130
|
+
"bg-gradient-to-tl": "bg-linear-to-tl"
|
|
4131
|
+
};
|
|
4132
|
+
const noDeprecatedClasses = defineRule({
|
|
4133
|
+
meta: {
|
|
4134
|
+
type: "problem",
|
|
4135
|
+
docs: { description: "Disallow deprecated Tailwind CSS v4 classes" },
|
|
4136
|
+
fixable: "code",
|
|
4137
|
+
schema: [{
|
|
4138
|
+
type: "object",
|
|
4139
|
+
properties: { entryPoint: { type: "string" } },
|
|
4140
|
+
additionalProperties: false
|
|
4141
|
+
}],
|
|
4142
|
+
hasSuggestions: true,
|
|
4143
|
+
defaultOptions: [{}],
|
|
4144
|
+
messages: {
|
|
4145
|
+
deprecated: "\"{{className}}\" is deprecated in Tailwind v4. Use \"{{replacement}}\" instead.",
|
|
4146
|
+
suggestReplace: "Replace \"{{className}}\" with \"{{replacement}}\"."
|
|
3027
4147
|
}
|
|
3028
|
-
|
|
3029
|
-
|
|
3030
|
-
|
|
3031
|
-
|
|
3032
|
-
|
|
3033
|
-
|
|
3034
|
-
|
|
3035
|
-
|
|
3036
|
-
|
|
3037
|
-
|
|
3038
|
-
|
|
4148
|
+
},
|
|
4149
|
+
createOnce(context) {
|
|
4150
|
+
const getDS = createLazyLoader(context);
|
|
4151
|
+
function check(locations) {
|
|
4152
|
+
if (locations.length === 0) return;
|
|
4153
|
+
const ds = softGetDS(getDS);
|
|
4154
|
+
const cache = ds ? ds.cache : null;
|
|
4155
|
+
const replacementFor = (bare) => cache?.hasDeprecatedMap ? cache.deprecatedReplacement(bare) : DEPRECATED_MAP[bare] ?? null;
|
|
4156
|
+
const isUsable = makeReplacementGuard(cache);
|
|
4157
|
+
for (const loc of locations) {
|
|
4158
|
+
const split = splitClassesWithSeparators(loc.value);
|
|
4159
|
+
const offending = split.classes.flatMap((cls) => {
|
|
4160
|
+
const { utility, variant } = splitUtilityAndVariant(cls);
|
|
4161
|
+
const { bare: bareUtility, position } = splitImportant(utility);
|
|
4162
|
+
const replacement = replacementFor(bareUtility);
|
|
4163
|
+
if (!replacement) return [];
|
|
4164
|
+
const rebuilt = variant + reattachImportant(replacement, position);
|
|
4165
|
+
return isUsable(rebuilt) ? [{
|
|
4166
|
+
cls,
|
|
4167
|
+
replacement: rebuilt
|
|
4168
|
+
}] : [];
|
|
4169
|
+
});
|
|
4170
|
+
reportClassReplacements(context, loc, split, split.classes, offending, { messageId: "deprecated" });
|
|
4171
|
+
}
|
|
3039
4172
|
}
|
|
4173
|
+
return createExtractorVisitors(context, check);
|
|
3040
4174
|
}
|
|
3041
|
-
|
|
3042
|
-
try {
|
|
3043
|
-
worker.terminate();
|
|
3044
|
-
} catch {}
|
|
3045
|
-
}
|
|
3046
|
-
};
|
|
4175
|
+
});
|
|
3047
4176
|
//#endregion
|
|
3048
4177
|
//#region src/design-system/canonicalize-service.ts
|
|
3049
4178
|
/**
|
|
@@ -3061,23 +4190,195 @@ var DesignSystemWorker = class {
|
|
|
3061
4190
|
* after the first few files any given class has been seen, and every
|
|
3062
4191
|
* subsequent lookup is O(1).
|
|
3063
4192
|
*/
|
|
3064
|
-
const
|
|
3065
|
-
workerScript: makeWorkerScript(`(ds, request) => {
|
|
4193
|
+
const WORKER_SCRIPT = makeWorkerScript(`(ds, request) => {
|
|
3066
4194
|
const { classes, rem } = request;
|
|
3067
4195
|
const options = rem ? { rem } : undefined;
|
|
4196
|
+
const declsOf = (cls) => {
|
|
4197
|
+
let out;
|
|
4198
|
+
try { out = ds.candidatesToCss([cls]); } catch (e) { return null; }
|
|
4199
|
+
if (!out || !out[0]) return null;
|
|
4200
|
+
const css = out[0];
|
|
4201
|
+
const open = css.indexOf('{');
|
|
4202
|
+
const close = css.lastIndexOf('}');
|
|
4203
|
+
if (open < 0 || close < 0) return null;
|
|
4204
|
+
return css.slice(open + 1, close).replace(/\\s+/g, ' ').trim();
|
|
4205
|
+
};
|
|
3068
4206
|
return classes.map((cls) => {
|
|
3069
4207
|
const r = ds.canonicalizeCandidates([cls], options);
|
|
3070
|
-
|
|
4208
|
+
const canonical = r[0] ?? cls;
|
|
4209
|
+
if (canonical === cls) return { canonical: cls, safe: true };
|
|
4210
|
+
const a = declsOf(cls);
|
|
4211
|
+
const b = declsOf(canonical);
|
|
4212
|
+
return { canonical, safe: a !== null && b !== null && a === b };
|
|
3071
4213
|
});
|
|
3072
|
-
}`)
|
|
4214
|
+
}`);
|
|
4215
|
+
const canonWorker = new DesignSystemWorker({
|
|
4216
|
+
workerScript: WORKER_SCRIPT,
|
|
3073
4217
|
serviceName: "canonicalize"
|
|
3074
4218
|
});
|
|
3075
4219
|
/**
|
|
3076
|
-
* Process-wide cache of canonicalized classes.
|
|
3077
|
-
* Key: `${cssPath}\0${rem}\0${className}` — isolates monorepos (multiple
|
|
3078
|
-
* DSs) and different rem settings. Value:
|
|
4220
|
+
* Process-wide cache of canonicalized classes.
|
|
4221
|
+
* Key: `${cssPath}\0${rem}\0${className}` — isolates monorepos (multiple
|
|
4222
|
+
* DSs) and different rem settings. Value: the canonical form + its #78 `safe`
|
|
4223
|
+
* flag.
|
|
4224
|
+
*/
|
|
4225
|
+
const canonCache = /* @__PURE__ */ new Map();
|
|
4226
|
+
/**
|
|
4227
|
+
* ## Disk persistence (per design system + rem)
|
|
4228
|
+
*
|
|
4229
|
+
* Each `oxlint` invocation is a fresh process, so without persistence every
|
|
4230
|
+
* unique dynamic class (`p-[2px]`, `bg-(--c)`, …) pays a synchronous worker
|
|
4231
|
+
* round-trip again on every run — on arbitrary-value-heavy codebases this
|
|
4232
|
+
* dominates the whole lint (seconds per run, issue: enforce-canonical perf).
|
|
4233
|
+
*
|
|
4234
|
+
* The canonical results are pure functions of the design system + rem + the
|
|
4235
|
+
* canonicalization logic, so we persist them next to the DS precompute
|
|
4236
|
+
* artifact, deriving the filename from `cacheArtifactPaths(cssPath).json`
|
|
4237
|
+
* (content-hash keyed) plus `CANON_LOGIC_HASH` (below). When the DS changes,
|
|
4238
|
+
* the artifact hash changes; when the canonicalization logic changes, the
|
|
4239
|
+
* logic hash changes — either way the canonical cache invalidates with it, so
|
|
4240
|
+
* no manual version bump is needed and stale values can never reach an autofix.
|
|
4241
|
+
*
|
|
4242
|
+
* We persist the full `CanonicalizeResult` (`canonical` + the #78 `safe`
|
|
4243
|
+
* flag), not just the string: `safe` gates whether `enforce-canonical`
|
|
4244
|
+
* actually applies the rewrite, so dropping it on restore would re-introduce
|
|
4245
|
+
* the unsafe-autofix bug #78 fixed.
|
|
4246
|
+
*
|
|
4247
|
+
* Best-effort everywhere: a missing/corrupt/unwritable cache file must never
|
|
4248
|
+
* break linting — it only costs worker round-trips.
|
|
4249
|
+
*/
|
|
4250
|
+
const CANON_LOGIC_HASH = createHash("md5").update(WORKER_SCRIPT + roundRemValue.toString()).digest("hex").slice(0, 8);
|
|
4251
|
+
let tmpSeq = 0;
|
|
4252
|
+
const LOCK_STALE_MS = 1e4;
|
|
4253
|
+
/** One persistence state per `${cssPath}\0${rem}\0` cache prefix. */
|
|
4254
|
+
const persistStates = /* @__PURE__ */ new Map();
|
|
4255
|
+
function tryUnlink(path) {
|
|
4256
|
+
try {
|
|
4257
|
+
unlinkSync(path);
|
|
4258
|
+
} catch {}
|
|
4259
|
+
}
|
|
4260
|
+
/**
|
|
4261
|
+
* Reclaim a stale lock via exclusive rename rather than unlink (mirrors
|
|
4262
|
+
* sync-loader.ts): two isolates that both judge the lock stale would otherwise
|
|
4263
|
+
* both unlink it, and the second could delete a FRESH lock a third isolate just
|
|
4264
|
+
* created. `renameSync` has a single winner; the loser gets ENOENT and moves on.
|
|
4265
|
+
*/
|
|
4266
|
+
function reclaimStaleLock(lockPath) {
|
|
4267
|
+
let ageMs;
|
|
4268
|
+
try {
|
|
4269
|
+
ageMs = Date.now() - statSync(lockPath).mtimeMs;
|
|
4270
|
+
} catch {
|
|
4271
|
+
return;
|
|
4272
|
+
}
|
|
4273
|
+
if (ageMs < LOCK_STALE_MS) return;
|
|
4274
|
+
const reclaimPath = `${lockPath}.reclaim.${process.pid}.${threadId}.${tmpSeq++}`;
|
|
4275
|
+
try {
|
|
4276
|
+
renameSync(lockPath, reclaimPath);
|
|
4277
|
+
} catch {
|
|
4278
|
+
return;
|
|
4279
|
+
}
|
|
4280
|
+
tryUnlink(reclaimPath);
|
|
4281
|
+
}
|
|
4282
|
+
/**
|
|
4283
|
+
* Acquire the per-file flush lock by atomic exclusive create (`wx`). The lock
|
|
4284
|
+
* serializes the read-merge-write below so concurrent isolates don't clobber
|
|
4285
|
+
* each other's entries (last-writer-wins → the cache would otherwise only
|
|
4286
|
+
* converge over several runs). Distinguishes contention (another isolate is
|
|
4287
|
+
* mid-flush — skip and retry next threshold) from an unwritable dir (give up).
|
|
4288
|
+
*/
|
|
4289
|
+
function acquireFlushLock(lockPath) {
|
|
4290
|
+
const tryCreate = () => {
|
|
4291
|
+
try {
|
|
4292
|
+
closeSync(openSync(lockPath, "wx"));
|
|
4293
|
+
return "acquired";
|
|
4294
|
+
} catch (e) {
|
|
4295
|
+
return e.code === "EEXIST" ? "contended" : "unavailable";
|
|
4296
|
+
}
|
|
4297
|
+
};
|
|
4298
|
+
const first = tryCreate();
|
|
4299
|
+
if (first !== "contended") return first;
|
|
4300
|
+
reclaimStaleLock(lockPath);
|
|
4301
|
+
return tryCreate();
|
|
4302
|
+
}
|
|
4303
|
+
/** Exported for tests: the exact on-disk cache path for a (cssPath, rem). */
|
|
4304
|
+
function persistFileFor(cssPath, rem) {
|
|
4305
|
+
try {
|
|
4306
|
+
const { json } = cacheArtifactPaths(cssPath);
|
|
4307
|
+
const remKey = rem === void 0 ? "default" : String(rem);
|
|
4308
|
+
return json.replace(/\.json$/, `.canon-${CANON_LOGIC_HASH}-${remKey}.json`);
|
|
4309
|
+
} catch {
|
|
4310
|
+
return null;
|
|
4311
|
+
}
|
|
4312
|
+
}
|
|
4313
|
+
/** Load the persisted map (if any) into `canonCache`, and set up flushing. */
|
|
4314
|
+
function ensurePersistLoaded(cssPath, rem, cachePrefix) {
|
|
4315
|
+
if (persistStates.has(cachePrefix)) return;
|
|
4316
|
+
const file = persistFileFor(cssPath, rem);
|
|
4317
|
+
if (file === null) {
|
|
4318
|
+
persistStates.set(cachePrefix, {
|
|
4319
|
+
file: "",
|
|
4320
|
+
dirty: -1
|
|
4321
|
+
});
|
|
4322
|
+
return;
|
|
4323
|
+
}
|
|
4324
|
+
const state = {
|
|
4325
|
+
file,
|
|
4326
|
+
dirty: 0
|
|
4327
|
+
};
|
|
4328
|
+
persistStates.set(cachePrefix, state);
|
|
4329
|
+
try {
|
|
4330
|
+
const data = JSON.parse(readFileSync(file, "utf-8"));
|
|
4331
|
+
if (typeof data === "object" && data !== null) {
|
|
4332
|
+
for (const [cls, entry] of Object.entries(data)) if (Array.isArray(entry) && typeof entry[0] === "string" && typeof entry[1] === "boolean") canonCache.set(cachePrefix + cls, {
|
|
4333
|
+
canonical: entry[0],
|
|
4334
|
+
safe: entry[1]
|
|
4335
|
+
});
|
|
4336
|
+
}
|
|
4337
|
+
} catch {}
|
|
4338
|
+
}
|
|
4339
|
+
/**
|
|
4340
|
+
* Persist this prefix's entries under a per-file lock, merging with whatever is
|
|
4341
|
+
* already on disk first. oxlint worker threads are separate JS isolates, each
|
|
4342
|
+
* with its own in-memory `canonCache`, so the file is their shared channel: a
|
|
4343
|
+
* bare overwrite would clobber the entries other isolates persisted
|
|
4344
|
+
* (last-writer-wins), leaving the cache to converge only over several runs.
|
|
4345
|
+
* Read-merge-write under the lock makes it converge in one.
|
|
3079
4346
|
*/
|
|
3080
|
-
|
|
4347
|
+
function flushPersist(cachePrefix, state) {
|
|
4348
|
+
if (state.dirty <= 0) return;
|
|
4349
|
+
const lockPath = `${state.file}.lock`;
|
|
4350
|
+
const lock = acquireFlushLock(lockPath);
|
|
4351
|
+
if (lock === "contended") return;
|
|
4352
|
+
if (lock === "unavailable") {
|
|
4353
|
+
state.dirty = -1;
|
|
4354
|
+
return;
|
|
4355
|
+
}
|
|
4356
|
+
try {
|
|
4357
|
+
const merged = Object.create(null);
|
|
4358
|
+
try {
|
|
4359
|
+
const existing = JSON.parse(readFileSync(state.file, "utf-8"));
|
|
4360
|
+
if (typeof existing === "object" && existing !== null) {
|
|
4361
|
+
for (const [cls, entry] of Object.entries(existing)) if (Array.isArray(entry) && typeof entry[0] === "string" && typeof entry[1] === "boolean") {
|
|
4362
|
+
merged[cls] = [entry[0], entry[1]];
|
|
4363
|
+
const key = cachePrefix + cls;
|
|
4364
|
+
if (!canonCache.has(key)) canonCache.set(key, {
|
|
4365
|
+
canonical: entry[0],
|
|
4366
|
+
safe: entry[1]
|
|
4367
|
+
});
|
|
4368
|
+
}
|
|
4369
|
+
}
|
|
4370
|
+
} catch {}
|
|
4371
|
+
for (const [key, value] of canonCache) if (key.startsWith(cachePrefix)) merged[key.slice(cachePrefix.length)] = [value.canonical, value.safe];
|
|
4372
|
+
const tmp = `${state.file}.${process.pid}.${threadId}.${tmpSeq++}.tmp`;
|
|
4373
|
+
writeFileSync(tmp, JSON.stringify(merged));
|
|
4374
|
+
renameSync(tmp, state.file);
|
|
4375
|
+
state.dirty = 0;
|
|
4376
|
+
} catch {
|
|
4377
|
+
state.dirty = -1;
|
|
4378
|
+
} finally {
|
|
4379
|
+
tryUnlink(lockPath);
|
|
4380
|
+
}
|
|
4381
|
+
}
|
|
3081
4382
|
/**
|
|
3082
4383
|
* Canonicalize classes via the worker thread.
|
|
3083
4384
|
*
|
|
@@ -3094,6 +4395,7 @@ function canonicalizeClassesSync(cssPath, classes, rem) {
|
|
|
3094
4395
|
const missingIdx = [];
|
|
3095
4396
|
const missing = [];
|
|
3096
4397
|
const cachePrefix = `${cssPath}\0${rem ?? ""}\0`;
|
|
4398
|
+
ensurePersistLoaded(cssPath, rem, cachePrefix);
|
|
3097
4399
|
for (let i = 0; i < classes.length; i++) {
|
|
3098
4400
|
const key = cachePrefix + classes[i];
|
|
3099
4401
|
const hit = canonCache.get(key);
|
|
@@ -3114,10 +4416,22 @@ function canonicalizeClassesSync(cssPath, classes, rem) {
|
|
|
3114
4416
|
for (let k = 0; k < uniqueMissing.length; k++) freshByClass.set(uniqueMissing[k], fresh[k]);
|
|
3115
4417
|
for (let j = 0; j < missing.length; j++) {
|
|
3116
4418
|
const cls = missing[j];
|
|
3117
|
-
const
|
|
4419
|
+
const raw = freshByClass.get(cls) ?? {
|
|
4420
|
+
canonical: cls,
|
|
4421
|
+
safe: true
|
|
4422
|
+
};
|
|
4423
|
+
const value = {
|
|
4424
|
+
canonical: roundRemValue(raw.canonical),
|
|
4425
|
+
safe: raw.safe
|
|
4426
|
+
};
|
|
3118
4427
|
canonCache.set(cachePrefix + cls, value);
|
|
3119
4428
|
out[missingIdx[j]] = value;
|
|
3120
4429
|
}
|
|
4430
|
+
const state = persistStates.get(cachePrefix);
|
|
4431
|
+
if (state && state.dirty >= 0) {
|
|
4432
|
+
state.dirty += uniqueMissing.length;
|
|
4433
|
+
flushPersist(cachePrefix, state);
|
|
4434
|
+
}
|
|
3121
4435
|
return out;
|
|
3122
4436
|
}
|
|
3123
4437
|
//#endregion
|
|
@@ -3174,15 +4488,25 @@ const enforceCanonical = defineRule({
|
|
|
3174
4488
|
const canonicals = Array.from({ length: classes.length });
|
|
3175
4489
|
const arbitraryIdx = [];
|
|
3176
4490
|
const arbitrary = [];
|
|
3177
|
-
for (let i = 0; i < classes.length; i++)
|
|
3178
|
-
|
|
3179
|
-
|
|
3180
|
-
|
|
4491
|
+
for (let i = 0; i < classes.length; i++) {
|
|
4492
|
+
const { utility } = splitUtilityAndVariant(classes[i]);
|
|
4493
|
+
if (cache.deprecatedReplacement(splitImportant(utility).bare)) {
|
|
4494
|
+
canonicals[i] = classes[i];
|
|
4495
|
+
continue;
|
|
4496
|
+
}
|
|
4497
|
+
if (utilityHasDynamicValue(classes[i])) {
|
|
4498
|
+
arbitraryIdx.push(i);
|
|
4499
|
+
arbitrary.push(classes[i]);
|
|
4500
|
+
} else canonicals[i] = cache.canonicalize(classes[i]);
|
|
4501
|
+
}
|
|
3181
4502
|
if (arbitrary.length > 0) {
|
|
3182
4503
|
const rem = getRem();
|
|
3183
4504
|
const dynamic = safeGetDS(() => canonicalizeClassesSync(entryPoint, arbitrary, rem), context, loc.node);
|
|
3184
4505
|
if (!dynamic) return;
|
|
3185
|
-
for (let k = 0; k < arbitrary.length; k++)
|
|
4506
|
+
for (let k = 0; k < arbitrary.length; k++) {
|
|
4507
|
+
const { canonical, safe } = dynamic[k];
|
|
4508
|
+
canonicals[arbitraryIdx[k]] = safe ? preserveImportantPosition(arbitrary[k], canonical) : arbitrary[k];
|
|
4509
|
+
}
|
|
3186
4510
|
}
|
|
3187
4511
|
reportClassReplacements(context, loc, split, classes, classes.flatMap((cls, i) => canonicals[i] === cls ? [] : [{
|
|
3188
4512
|
cls,
|
|
@@ -3324,121 +4648,317 @@ const enforceSortOrder = defineRule({
|
|
|
3324
4648
|
});
|
|
3325
4649
|
//#endregion
|
|
3326
4650
|
//#region src/rules/enforce-shorthand.ts
|
|
3327
|
-
const VALUE_RE = /^(?:m[trbl]|mx|my|p[trbl]|px|py|[wh]|rounded-t[lr]|rounded-b[lr]|min-[wh]|max-[wh])-(.+)$/;
|
|
3328
|
-
const INVALID_SIZE_VALUES = /* @__PURE__ */ new Set([
|
|
3329
|
-
"screen",
|
|
3330
|
-
"dvw",
|
|
3331
|
-
"dvh",
|
|
3332
|
-
"svw",
|
|
3333
|
-
"svh",
|
|
3334
|
-
"lvw",
|
|
3335
|
-
"lvh"
|
|
3336
|
-
]);
|
|
3337
4651
|
/**
|
|
3338
|
-
*
|
|
3339
|
-
*
|
|
4652
|
+
* The `m`/`p` shape, shared by `margin`, `padding`, `scroll-margin` and
|
|
4653
|
+
* `scroll-padding`: four sides, each axis, and the logical inline pair (`ms`+`me`
|
|
4654
|
+
* IS `margin-inline`, so that one is property-identical rather than merely
|
|
4655
|
+
* equivalent).
|
|
3340
4656
|
*/
|
|
3341
|
-
function
|
|
4657
|
+
function boxFamilies(p) {
|
|
3342
4658
|
return [
|
|
3343
4659
|
{
|
|
3344
4660
|
parts: [
|
|
3345
|
-
|
|
3346
|
-
|
|
3347
|
-
|
|
3348
|
-
|
|
3349
|
-
],
|
|
3350
|
-
replacement: `m-${value}`
|
|
3351
|
-
},
|
|
3352
|
-
{
|
|
3353
|
-
parts: [`mt-${value}`, `mb-${value}`],
|
|
3354
|
-
replacement: `my-${value}`
|
|
3355
|
-
},
|
|
3356
|
-
{
|
|
3357
|
-
parts: [`ml-${value}`, `mr-${value}`],
|
|
3358
|
-
replacement: `mx-${value}`
|
|
3359
|
-
},
|
|
3360
|
-
{
|
|
3361
|
-
parts: [`mx-${value}`, `my-${value}`],
|
|
3362
|
-
replacement: `m-${value}`
|
|
3363
|
-
},
|
|
3364
|
-
{
|
|
3365
|
-
parts: [
|
|
3366
|
-
`pt-${value}`,
|
|
3367
|
-
`pr-${value}`,
|
|
3368
|
-
`pb-${value}`,
|
|
3369
|
-
`pl-${value}`
|
|
4661
|
+
`${p}t`,
|
|
4662
|
+
`${p}r`,
|
|
4663
|
+
`${p}b`,
|
|
4664
|
+
`${p}l`
|
|
3370
4665
|
],
|
|
3371
|
-
|
|
4666
|
+
to: p
|
|
3372
4667
|
},
|
|
3373
4668
|
{
|
|
3374
|
-
parts: [
|
|
3375
|
-
|
|
4669
|
+
parts: [`${p}t`, `${p}b`],
|
|
4670
|
+
to: `${p}y`
|
|
3376
4671
|
},
|
|
3377
4672
|
{
|
|
3378
|
-
parts: [
|
|
3379
|
-
|
|
4673
|
+
parts: [`${p}l`, `${p}r`],
|
|
4674
|
+
to: `${p}x`
|
|
3380
4675
|
},
|
|
3381
4676
|
{
|
|
3382
|
-
parts: [
|
|
3383
|
-
|
|
4677
|
+
parts: [`${p}s`, `${p}e`],
|
|
4678
|
+
to: `${p}x`
|
|
3384
4679
|
},
|
|
3385
|
-
...!INVALID_SIZE_VALUES.has(value) ? [{
|
|
3386
|
-
parts: [`w-${value}`, `h-${value}`],
|
|
3387
|
-
replacement: `size-${value}`
|
|
3388
|
-
}] : [],
|
|
3389
4680
|
{
|
|
3390
|
-
parts: [
|
|
3391
|
-
|
|
3392
|
-
`rounded-tr-${value}`,
|
|
3393
|
-
`rounded-br-${value}`,
|
|
3394
|
-
`rounded-bl-${value}`
|
|
3395
|
-
],
|
|
3396
|
-
replacement: `rounded-${value}`
|
|
4681
|
+
parts: [`${p}x`, `${p}y`],
|
|
4682
|
+
to: p
|
|
3397
4683
|
}
|
|
3398
4684
|
];
|
|
3399
4685
|
}
|
|
4686
|
+
const SHORTHAND_FAMILIES = [
|
|
4687
|
+
...boxFamilies("m"),
|
|
4688
|
+
...boxFamilies("p"),
|
|
4689
|
+
...boxFamilies("scroll-m"),
|
|
4690
|
+
...boxFamilies("scroll-p"),
|
|
4691
|
+
{
|
|
4692
|
+
parts: ["w", "h"],
|
|
4693
|
+
to: "size",
|
|
4694
|
+
perAxisNamespaces: true
|
|
4695
|
+
},
|
|
4696
|
+
{
|
|
4697
|
+
parts: [
|
|
4698
|
+
"rounded-tl",
|
|
4699
|
+
"rounded-tr",
|
|
4700
|
+
"rounded-br",
|
|
4701
|
+
"rounded-bl"
|
|
4702
|
+
],
|
|
4703
|
+
to: "rounded"
|
|
4704
|
+
},
|
|
4705
|
+
{
|
|
4706
|
+
parts: [
|
|
4707
|
+
"rounded-ss",
|
|
4708
|
+
"rounded-se",
|
|
4709
|
+
"rounded-ee",
|
|
4710
|
+
"rounded-es"
|
|
4711
|
+
],
|
|
4712
|
+
to: "rounded"
|
|
4713
|
+
},
|
|
4714
|
+
{
|
|
4715
|
+
parts: ["rounded-tl", "rounded-tr"],
|
|
4716
|
+
to: "rounded-t"
|
|
4717
|
+
},
|
|
4718
|
+
{
|
|
4719
|
+
parts: ["rounded-bl", "rounded-br"],
|
|
4720
|
+
to: "rounded-b"
|
|
4721
|
+
},
|
|
4722
|
+
{
|
|
4723
|
+
parts: ["rounded-tl", "rounded-bl"],
|
|
4724
|
+
to: "rounded-l"
|
|
4725
|
+
},
|
|
4726
|
+
{
|
|
4727
|
+
parts: ["rounded-tr", "rounded-br"],
|
|
4728
|
+
to: "rounded-r"
|
|
4729
|
+
},
|
|
4730
|
+
{
|
|
4731
|
+
parts: ["rounded-ss", "rounded-es"],
|
|
4732
|
+
to: "rounded-s"
|
|
4733
|
+
},
|
|
4734
|
+
{
|
|
4735
|
+
parts: ["rounded-se", "rounded-ee"],
|
|
4736
|
+
to: "rounded-e"
|
|
4737
|
+
},
|
|
4738
|
+
{
|
|
4739
|
+
parts: ["rounded-t", "rounded-b"],
|
|
4740
|
+
to: "rounded"
|
|
4741
|
+
},
|
|
4742
|
+
{
|
|
4743
|
+
parts: ["rounded-l", "rounded-r"],
|
|
4744
|
+
to: "rounded"
|
|
4745
|
+
},
|
|
4746
|
+
{
|
|
4747
|
+
parts: ["rounded-s", "rounded-e"],
|
|
4748
|
+
to: "rounded"
|
|
4749
|
+
},
|
|
4750
|
+
{
|
|
4751
|
+
parts: [
|
|
4752
|
+
"border-t",
|
|
4753
|
+
"border-r",
|
|
4754
|
+
"border-b",
|
|
4755
|
+
"border-l"
|
|
4756
|
+
],
|
|
4757
|
+
to: "border"
|
|
4758
|
+
},
|
|
4759
|
+
{
|
|
4760
|
+
parts: ["border-t", "border-b"],
|
|
4761
|
+
to: "border-y"
|
|
4762
|
+
},
|
|
4763
|
+
{
|
|
4764
|
+
parts: ["border-l", "border-r"],
|
|
4765
|
+
to: "border-x"
|
|
4766
|
+
},
|
|
4767
|
+
{
|
|
4768
|
+
parts: ["border-s", "border-e"],
|
|
4769
|
+
to: "border-x"
|
|
4770
|
+
},
|
|
4771
|
+
{
|
|
4772
|
+
parts: ["border-x", "border-y"],
|
|
4773
|
+
to: "border"
|
|
4774
|
+
},
|
|
4775
|
+
{
|
|
4776
|
+
parts: [
|
|
4777
|
+
"top",
|
|
4778
|
+
"right",
|
|
4779
|
+
"bottom",
|
|
4780
|
+
"left"
|
|
4781
|
+
],
|
|
4782
|
+
to: "inset"
|
|
4783
|
+
},
|
|
4784
|
+
{
|
|
4785
|
+
parts: ["top", "bottom"],
|
|
4786
|
+
to: "inset-y"
|
|
4787
|
+
},
|
|
4788
|
+
{
|
|
4789
|
+
parts: ["left", "right"],
|
|
4790
|
+
to: "inset-x"
|
|
4791
|
+
},
|
|
4792
|
+
{
|
|
4793
|
+
parts: ["start", "end"],
|
|
4794
|
+
to: "inset-x"
|
|
4795
|
+
},
|
|
4796
|
+
{
|
|
4797
|
+
parts: ["inset-x", "inset-y"],
|
|
4798
|
+
to: "inset"
|
|
4799
|
+
},
|
|
4800
|
+
{
|
|
4801
|
+
parts: ["gap-x", "gap-y"],
|
|
4802
|
+
to: "gap"
|
|
4803
|
+
},
|
|
4804
|
+
{
|
|
4805
|
+
parts: ["overflow-x", "overflow-y"],
|
|
4806
|
+
to: "overflow"
|
|
4807
|
+
},
|
|
4808
|
+
{
|
|
4809
|
+
parts: ["overscroll-x", "overscroll-y"],
|
|
4810
|
+
to: "overscroll"
|
|
4811
|
+
},
|
|
4812
|
+
{
|
|
4813
|
+
parts: ["border-spacing-x", "border-spacing-y"],
|
|
4814
|
+
to: "border-spacing"
|
|
4815
|
+
},
|
|
4816
|
+
{
|
|
4817
|
+
parts: ["translate-x", "translate-y"],
|
|
4818
|
+
to: "translate"
|
|
4819
|
+
}
|
|
4820
|
+
];
|
|
4821
|
+
/**
|
|
4822
|
+
* Largest merges first, so four sides collapse to `m-2` instead of also
|
|
4823
|
+
* reporting the `my-2`/`mx-2` halves — three diagnostics for one problem, which
|
|
4824
|
+
* is what this rule used to emit. `sort` is stable, so same-size families keep
|
|
4825
|
+
* their declaration order and the choice stays deterministic.
|
|
4826
|
+
*/
|
|
4827
|
+
const FAMILIES_BY_SIZE = [...SHORTHAND_FAMILIES].sort((a, b) => b.parts.length - a.parts.length);
|
|
4828
|
+
/**
|
|
4829
|
+
* `^<part>-<value>$`, longest prefix first so `rounded-tl-lg` is read as
|
|
4830
|
+
* `rounded-tl` + `lg` and never as `rounded-t` + `l-lg`.
|
|
4831
|
+
*/
|
|
4832
|
+
const VALUE_RE = (() => {
|
|
4833
|
+
const prefixes = [...new Set(SHORTHAND_FAMILIES.flatMap((f) => f.parts))].sort((a, b) => b.length - a.length);
|
|
4834
|
+
return new RegExp(`^(${prefixes.join("|")})-(.+)$`);
|
|
4835
|
+
})();
|
|
4836
|
+
/** A plain number or fraction: always the shared numeric/`--spacing` scale. */
|
|
4837
|
+
const NUMERIC_VALUE_RE = /^\d+(?:\.\d+)?(?:\/\d+(?:\.\d+)?)?$/;
|
|
4838
|
+
/**
|
|
4839
|
+
* Core keywords whose per-axis forms are known to agree, for when no design
|
|
4840
|
+
* system is available to check. Deliberately excludes `screen` (`w-screen` is
|
|
4841
|
+
* `100vw`, `h-screen` is `100vh`) and every named theme token, which is the case
|
|
4842
|
+
* that can only be settled by looking at the emitted CSS.
|
|
4843
|
+
*/
|
|
4844
|
+
const AXIS_SAFE_KEYWORDS = /* @__PURE__ */ new Set([
|
|
4845
|
+
"full",
|
|
4846
|
+
"auto",
|
|
4847
|
+
"min",
|
|
4848
|
+
"max",
|
|
4849
|
+
"fit",
|
|
4850
|
+
"px",
|
|
4851
|
+
"dvw",
|
|
4852
|
+
"dvh",
|
|
4853
|
+
"lvw",
|
|
4854
|
+
"lvh",
|
|
4855
|
+
"svw",
|
|
4856
|
+
"svh"
|
|
4857
|
+
]);
|
|
4858
|
+
/** The value is a literal the user wrote, injected verbatim into every part. */
|
|
4859
|
+
function isWrittenValue(value) {
|
|
4860
|
+
return value.startsWith("[") || value.startsWith("(");
|
|
4861
|
+
}
|
|
4862
|
+
/**
|
|
4863
|
+
* Values the class declares on its own box, unconditionally. `null` when the
|
|
4864
|
+
* class produces nothing — which for a named token means it does not exist.
|
|
4865
|
+
*/
|
|
4866
|
+
function declaredValues(cache, cls) {
|
|
4867
|
+
const values = /* @__PURE__ */ new Set();
|
|
4868
|
+
for (const decl of cache.getCssDeclarations(cls)) {
|
|
4869
|
+
if (decl.conditional) continue;
|
|
4870
|
+
values.add(decl.value);
|
|
4871
|
+
}
|
|
4872
|
+
return values.size > 0 ? values : null;
|
|
4873
|
+
}
|
|
4874
|
+
function sameValues(a, b) {
|
|
4875
|
+
if (a.size !== b.size) return false;
|
|
4876
|
+
for (const value of a) if (!b.has(value)) return false;
|
|
4877
|
+
return true;
|
|
4878
|
+
}
|
|
4879
|
+
/**
|
|
4880
|
+
* Would collapsing this family at this value keep the CSS identical?
|
|
4881
|
+
*
|
|
4882
|
+
* With a design system this is answered from the emitted declarations. Without
|
|
4883
|
+
* one it falls back to what is safe by construction, which is everything except
|
|
4884
|
+
* a named token on the one family whose parts read different namespaces.
|
|
4885
|
+
*/
|
|
4886
|
+
function mergeIsSafe(family, value, cache) {
|
|
4887
|
+
const structurallySafe = NUMERIC_VALUE_RE.test(value) || isWrittenValue(value);
|
|
4888
|
+
if (!cache) {
|
|
4889
|
+
if (!family.perAxisNamespaces) return true;
|
|
4890
|
+
return structurallySafe || AXIS_SAFE_KEYWORDS.has(value);
|
|
4891
|
+
}
|
|
4892
|
+
if (!cache.isValid(`${family.to}-${value}`)) return false;
|
|
4893
|
+
if (structurallySafe) return true;
|
|
4894
|
+
const expected = declaredValues(cache, `${family.parts[0]}-${value}`);
|
|
4895
|
+
if (!expected) return false;
|
|
4896
|
+
for (let i = 1; i < family.parts.length; i++) {
|
|
4897
|
+
const actual = declaredValues(cache, `${family.parts[i]}-${value}`);
|
|
4898
|
+
if (!actual || !sameValues(expected, actual)) return false;
|
|
4899
|
+
}
|
|
4900
|
+
const replacement = declaredValues(cache, `${family.to}-${value}`);
|
|
4901
|
+
return replacement !== null && sameValues(expected, replacement);
|
|
4902
|
+
}
|
|
3400
4903
|
const enforceShorthand = defineRule({
|
|
3401
4904
|
meta: {
|
|
3402
4905
|
type: "suggestion",
|
|
3403
4906
|
docs: { description: "Enforce shorthand Tailwind CSS classes when all axes have the same value" },
|
|
3404
4907
|
fixable: "code",
|
|
3405
|
-
schema: [
|
|
4908
|
+
schema: [{
|
|
4909
|
+
type: "object",
|
|
4910
|
+
properties: { entryPoint: { type: "string" } },
|
|
4911
|
+
additionalProperties: false
|
|
4912
|
+
}],
|
|
4913
|
+
defaultOptions: [{}],
|
|
3406
4914
|
messages: { shorthand: "{{parts}} can be simplified to \"{{replacement}}\"." }
|
|
3407
4915
|
},
|
|
3408
4916
|
createOnce(context) {
|
|
4917
|
+
const getDS = createLazyLoader(context);
|
|
3409
4918
|
function check(locations) {
|
|
4919
|
+
const ds = softGetDS(getDS);
|
|
4920
|
+
const cache = ds ? ds.cache : null;
|
|
3410
4921
|
for (const loc of locations) {
|
|
3411
4922
|
const split = splitClassesWithSeparators(loc.value);
|
|
3412
4923
|
const classes = split.classes;
|
|
3413
4924
|
if (classes.length < 2) continue;
|
|
3414
|
-
const
|
|
3415
|
-
const variantValues = /* @__PURE__ */ new Set();
|
|
4925
|
+
const groups = /* @__PURE__ */ new Map();
|
|
3416
4926
|
for (const cls of classes) {
|
|
3417
4927
|
const { utility, variant } = splitUtilityAndVariant(cls);
|
|
3418
|
-
const
|
|
3419
|
-
|
|
4928
|
+
const { bare, position } = splitImportant(utility);
|
|
4929
|
+
const match = VALUE_RE.exec(bare);
|
|
4930
|
+
if (!match) continue;
|
|
4931
|
+
const [, part, value] = match;
|
|
4932
|
+
const key = `${variant}\0${position ?? ""}\0${value}`;
|
|
4933
|
+
let group = groups.get(key);
|
|
4934
|
+
if (!group) {
|
|
4935
|
+
group = {
|
|
4936
|
+
variant,
|
|
4937
|
+
position,
|
|
4938
|
+
value,
|
|
4939
|
+
byPart: /* @__PURE__ */ new Map()
|
|
4940
|
+
};
|
|
4941
|
+
groups.set(key, group);
|
|
4942
|
+
}
|
|
4943
|
+
group.byPart.set(part, cls);
|
|
3420
4944
|
}
|
|
3421
|
-
for (const
|
|
3422
|
-
|
|
3423
|
-
const
|
|
3424
|
-
const
|
|
3425
|
-
|
|
3426
|
-
|
|
3427
|
-
const
|
|
3428
|
-
const
|
|
3429
|
-
const
|
|
3430
|
-
if (!
|
|
3431
|
-
const
|
|
3432
|
-
const importantEnd = hasImportantSuffix ? "!" : "";
|
|
3433
|
-
const matchParts = rule.parts.map((p) => withVariant(`${importantStart}${p}${importantEnd}`));
|
|
3434
|
-
const remaining = classes.filter((c) => !matchParts.includes(c));
|
|
3435
|
-
remaining.push(withVariant(`${importantStart}${rule.replacement}${importantEnd}`));
|
|
4945
|
+
for (const group of groups.values()) {
|
|
4946
|
+
if (group.byPart.size < 2) continue;
|
|
4947
|
+
const consumed = /* @__PURE__ */ new Set();
|
|
4948
|
+
for (const family of FAMILIES_BY_SIZE) {
|
|
4949
|
+
if (family.parts.some((part) => !group.byPart.has(part) || consumed.has(part))) continue;
|
|
4950
|
+
if (!mergeIsSafe(family, group.value, cache)) continue;
|
|
4951
|
+
const matched = family.parts.map((part) => group.byPart.get(part));
|
|
4952
|
+
const replacement = group.variant + reattachImportant(`${family.to}-${group.value}`, group.position);
|
|
4953
|
+
const remaining = classes.filter((cls) => !matched.includes(cls));
|
|
4954
|
+
if (!remaining.includes(replacement)) remaining.push(replacement);
|
|
4955
|
+
for (const part of family.parts) consumed.add(part);
|
|
3436
4956
|
context.report({
|
|
3437
4957
|
node: loc.node,
|
|
3438
4958
|
messageId: "shorthand",
|
|
3439
4959
|
data: {
|
|
3440
|
-
parts:
|
|
3441
|
-
replacement
|
|
4960
|
+
parts: matched.map((p) => `"${p}"`).join(", "),
|
|
4961
|
+
replacement
|
|
3442
4962
|
},
|
|
3443
4963
|
fix(fixer) {
|
|
3444
4964
|
return fixer.replaceTextRange(loc.range, preserveSpaces(loc, rebuildClassString(split, remaining)));
|
|
@@ -3467,7 +4987,8 @@ const LOGICAL_PHYSICAL_SCHEMA = {
|
|
|
3467
4987
|
"block",
|
|
3468
4988
|
"both"
|
|
3469
4989
|
]
|
|
3470
|
-
}
|
|
4990
|
+
},
|
|
4991
|
+
entryPoint: { type: "string" }
|
|
3471
4992
|
},
|
|
3472
4993
|
additionalProperties: false
|
|
3473
4994
|
};
|
|
@@ -3561,17 +5082,73 @@ const PHYSICAL_TO_LOGICAL_MAPPINGS = [
|
|
|
3561
5082
|
from: "scroll-pr",
|
|
3562
5083
|
to: "scroll-pe",
|
|
3563
5084
|
axis: "inline"
|
|
5085
|
+
},
|
|
5086
|
+
{
|
|
5087
|
+
from: "float-left",
|
|
5088
|
+
to: "float-start",
|
|
5089
|
+
axis: "inline",
|
|
5090
|
+
exact: true
|
|
5091
|
+
},
|
|
5092
|
+
{
|
|
5093
|
+
from: "float-right",
|
|
5094
|
+
to: "float-end",
|
|
5095
|
+
axis: "inline",
|
|
5096
|
+
exact: true
|
|
5097
|
+
},
|
|
5098
|
+
{
|
|
5099
|
+
from: "clear-left",
|
|
5100
|
+
to: "clear-start",
|
|
5101
|
+
axis: "inline",
|
|
5102
|
+
exact: true
|
|
5103
|
+
},
|
|
5104
|
+
{
|
|
5105
|
+
from: "clear-right",
|
|
5106
|
+
to: "clear-end",
|
|
5107
|
+
axis: "inline",
|
|
5108
|
+
exact: true
|
|
5109
|
+
},
|
|
5110
|
+
{
|
|
5111
|
+
from: "text-left",
|
|
5112
|
+
to: "text-start",
|
|
5113
|
+
axis: "inline",
|
|
5114
|
+
exact: true
|
|
5115
|
+
},
|
|
5116
|
+
{
|
|
5117
|
+
from: "text-right",
|
|
5118
|
+
to: "text-end",
|
|
5119
|
+
axis: "inline",
|
|
5120
|
+
exact: true
|
|
3564
5121
|
}
|
|
3565
5122
|
];
|
|
5123
|
+
/**
|
|
5124
|
+
* Extra sources for enforce-physical ONLY.
|
|
5125
|
+
*
|
|
5126
|
+
* `enforce-logical` rewrites `left-2` to `start-2`, the spelling Tailwind's own
|
|
5127
|
+
* docs use. `enforce-canonical`, asking the design system, then rewrites that to
|
|
5128
|
+
* `inset-s-2` — same CSS, canonical name. So a codebase that ran both ends up with
|
|
5129
|
+
* `inset-s-*`, and enforce-physical had no way back: its table only knew `start`.
|
|
5130
|
+
*
|
|
5131
|
+
* Kept out of the main table so inverting it doesn't change what enforce-logical
|
|
5132
|
+
* suggests (both spellings map to the same physical class, but only one of them
|
|
5133
|
+
* can be the recommendation).
|
|
5134
|
+
*/
|
|
5135
|
+
const LOGICAL_INSET_ALIASES = [{
|
|
5136
|
+
from: "inset-s",
|
|
5137
|
+
to: "left",
|
|
5138
|
+
axis: "inline"
|
|
5139
|
+
}, {
|
|
5140
|
+
from: "inset-e",
|
|
5141
|
+
to: "right",
|
|
5142
|
+
axis: "inline"
|
|
5143
|
+
}];
|
|
3566
5144
|
/** Invert a directional table. enforce-physical consumes this. */
|
|
3567
5145
|
function invertAxisMappings(mappings) {
|
|
3568
5146
|
return mappings.map((m) => ({
|
|
5147
|
+
...m,
|
|
3569
5148
|
from: m.to,
|
|
3570
|
-
to: m.from
|
|
3571
|
-
axis: m.axis
|
|
5149
|
+
to: m.from
|
|
3572
5150
|
}));
|
|
3573
5151
|
}
|
|
3574
|
-
Object.fromEntries(PHYSICAL_TO_LOGICAL_MAPPINGS.map((m) => [m.from, m.to]));
|
|
3575
5152
|
/**
|
|
3576
5153
|
* Build the `check` callback for a directional-rewrite rule. Both
|
|
3577
5154
|
* enforce-logical and enforce-physical instantiate this with their own
|
|
@@ -3587,6 +5164,7 @@ function createDirectionalMapper(context, opts) {
|
|
|
3587
5164
|
allowlist: compileRegexList(o?.allowlist),
|
|
3588
5165
|
direction: o?.direction ?? "both"
|
|
3589
5166
|
}));
|
|
5167
|
+
const getDS = createLazyLoader(context);
|
|
3590
5168
|
function convertClass(cls) {
|
|
3591
5169
|
const { allowlist, direction } = getState();
|
|
3592
5170
|
if (matchesAny(cls, allowlist)) return null;
|
|
@@ -3594,24 +5172,26 @@ function createDirectionalMapper(context, opts) {
|
|
|
3594
5172
|
const { bare: bareUtility, position } = splitImportant(utility);
|
|
3595
5173
|
const negative = bareUtility.startsWith("-");
|
|
3596
5174
|
const core = negative ? bareUtility.slice(1) : bareUtility;
|
|
3597
|
-
for (const { from, to, axis } of opts.mappings) {
|
|
5175
|
+
for (const { from, to, axis, exact } of opts.mappings) {
|
|
3598
5176
|
if (direction !== "both" && direction !== axis) continue;
|
|
3599
|
-
if (core === from || core.startsWith(`${from}-`))
|
|
3600
|
-
|
|
3601
|
-
|
|
3602
|
-
}
|
|
5177
|
+
if (!(core === from || !exact && core.startsWith(`${from}-`))) continue;
|
|
5178
|
+
const suffix = core.slice(from.length);
|
|
5179
|
+
return `${variant}${reattachImportant(`${negative ? "-" : ""}${to}${suffix}`, position)}`;
|
|
3603
5180
|
}
|
|
3604
5181
|
return null;
|
|
3605
5182
|
}
|
|
3606
5183
|
function check(locations) {
|
|
5184
|
+
const ds = softGetDS(getDS);
|
|
5185
|
+
const isUsable = makeReplacementGuard(ds ? ds.cache : null);
|
|
3607
5186
|
for (const loc of locations) {
|
|
3608
5187
|
const split = splitClassesWithSeparators(loc.value);
|
|
3609
5188
|
const offending = split.classes.flatMap((cls) => {
|
|
3610
5189
|
const converted = convertClass(cls);
|
|
3611
|
-
|
|
5190
|
+
if (!converted || !isUsable(converted)) return [];
|
|
5191
|
+
return [{
|
|
3612
5192
|
cls,
|
|
3613
5193
|
replacement: converted
|
|
3614
|
-
}]
|
|
5194
|
+
}];
|
|
3615
5195
|
});
|
|
3616
5196
|
reportClassReplacements(context, loc, split, split.classes, offending, { messageId: opts.messageId });
|
|
3617
5197
|
}
|
|
@@ -3662,7 +5242,7 @@ const enforcePhysical = defineRule({
|
|
|
3662
5242
|
},
|
|
3663
5243
|
createOnce(context) {
|
|
3664
5244
|
const { check } = createDirectionalMapper(context, {
|
|
3665
|
-
mappings: invertAxisMappings(PHYSICAL_TO_LOGICAL_MAPPINGS),
|
|
5245
|
+
mappings: [...invertAxisMappings(PHYSICAL_TO_LOGICAL_MAPPINGS), ...LOGICAL_INSET_ALIASES],
|
|
3666
5246
|
messageId: "usePhysical"
|
|
3667
5247
|
});
|
|
3668
5248
|
return createExtractorVisitors(context, check);
|
|
@@ -3815,7 +5395,7 @@ const noArbitraryValue = defineRule({
|
|
|
3815
5395
|
for (const loc of locations) {
|
|
3816
5396
|
const classes = splitClasses(loc.value);
|
|
3817
5397
|
for (const cls of classes) {
|
|
3818
|
-
if (!
|
|
5398
|
+
if (!utilityHasDynamicValue(cls)) continue;
|
|
3819
5399
|
const utility = splitImportant(extractUtility(cls)).bare;
|
|
3820
5400
|
if (isAllowed(utility)) continue;
|
|
3821
5401
|
context.report({
|
|
@@ -4080,10 +5660,13 @@ const noDarkWithoutLight = defineRule({
|
|
|
4080
5660
|
docs: { description: "Require a base (light) utility when using dark: (or other scheme) variant" },
|
|
4081
5661
|
schema: [{
|
|
4082
5662
|
type: "object",
|
|
4083
|
-
properties: {
|
|
4084
|
-
|
|
4085
|
-
|
|
4086
|
-
|
|
5663
|
+
properties: {
|
|
5664
|
+
variants: {
|
|
5665
|
+
type: "array",
|
|
5666
|
+
items: { type: "string" }
|
|
5667
|
+
},
|
|
5668
|
+
entryPoint: { type: "string" }
|
|
5669
|
+
},
|
|
4087
5670
|
additionalProperties: false
|
|
4088
5671
|
}],
|
|
4089
5672
|
defaultOptions: [{ variants: DEFAULT_VARIANTS }],
|
|
@@ -4091,85 +5674,206 @@ const noDarkWithoutLight = defineRule({
|
|
|
4091
5674
|
},
|
|
4092
5675
|
createOnce(context) {
|
|
4093
5676
|
const getWatchedVariants = createLazyOptions(context, (o) => new Set(o?.variants ?? DEFAULT_VARIANTS));
|
|
5677
|
+
const getDS = createLazyLoader(context);
|
|
5678
|
+
const bareOf = (cls) => splitImportant(extractUtility(cls)).bare;
|
|
4094
5679
|
function check(locations) {
|
|
4095
5680
|
const watchedVariants = getWatchedVariants();
|
|
5681
|
+
const ds = softGetDS(getDS);
|
|
5682
|
+
const cache = ds ? ds.cache : null;
|
|
4096
5683
|
for (const loc of locations) {
|
|
4097
5684
|
const classes = splitClasses(loc.value);
|
|
5685
|
+
if (cache && ds) {
|
|
5686
|
+
let unresolved = null;
|
|
5687
|
+
for (const cls of classes) {
|
|
5688
|
+
const bare = bareOf(cls);
|
|
5689
|
+
if (cache.getCssProperties(bare).length > 0 || !isUserValued(bare)) continue;
|
|
5690
|
+
if (!unresolved) unresolved = [];
|
|
5691
|
+
unresolved.push(bare);
|
|
5692
|
+
}
|
|
5693
|
+
if (unresolved) resolveDeclarationsSync(ds.entryPoint, cache, unresolved);
|
|
5694
|
+
}
|
|
4098
5695
|
const basePrefixes = /* @__PURE__ */ new Set();
|
|
5696
|
+
const baseProperties = /* @__PURE__ */ new Set();
|
|
4099
5697
|
const variantClasses = [];
|
|
4100
5698
|
for (const cls of classes) {
|
|
4101
5699
|
const variants = extractVariants(cls);
|
|
4102
5700
|
const prefix = getUtilityPrefix(extractUtility(cls));
|
|
4103
|
-
|
|
4104
|
-
|
|
4105
|
-
|
|
4106
|
-
|
|
5701
|
+
const properties = cache ? cache.getCssProperties(bareOf(cls)) : [];
|
|
5702
|
+
const variant = variants.find((v) => watchedVariants.has(v));
|
|
5703
|
+
if (variant !== void 0) variantClasses.push({
|
|
5704
|
+
cls,
|
|
5705
|
+
variant,
|
|
5706
|
+
prefix,
|
|
5707
|
+
properties
|
|
5708
|
+
});
|
|
5709
|
+
else {
|
|
5710
|
+
basePrefixes.add(prefix);
|
|
5711
|
+
for (const property of properties) baseProperties.add(property);
|
|
5712
|
+
}
|
|
5713
|
+
}
|
|
5714
|
+
for (const { cls, variant, prefix, properties } of variantClasses) {
|
|
5715
|
+
if (basePrefixes.has(prefix)) continue;
|
|
5716
|
+
if (properties.some((property) => baseProperties.has(property))) continue;
|
|
5717
|
+
context.report({
|
|
5718
|
+
node: loc.node,
|
|
5719
|
+
messageId: "missingBase",
|
|
5720
|
+
data: {
|
|
5721
|
+
className: cls,
|
|
4107
5722
|
variant,
|
|
4108
5723
|
prefix
|
|
4109
|
-
}
|
|
4110
|
-
}
|
|
5724
|
+
}
|
|
5725
|
+
});
|
|
4111
5726
|
}
|
|
4112
|
-
for (const { cls, variant, prefix } of variantClasses) if (!basePrefixes.has(prefix)) context.report({
|
|
4113
|
-
node: loc.node,
|
|
4114
|
-
messageId: "missingBase",
|
|
4115
|
-
data: {
|
|
4116
|
-
className: cls,
|
|
4117
|
-
variant,
|
|
4118
|
-
prefix
|
|
4119
|
-
}
|
|
4120
|
-
});
|
|
4121
5727
|
}
|
|
4122
5728
|
}
|
|
4123
5729
|
return createExtractorVisitors(context, check);
|
|
4124
5730
|
}
|
|
4125
5731
|
});
|
|
4126
5732
|
//#endregion
|
|
5733
|
+
//#region src/utils/color-literal.ts
|
|
5734
|
+
/**
|
|
5735
|
+
* Does this CSS value contain a hardcoded color literal, anywhere in it?
|
|
5736
|
+
*
|
|
5737
|
+
* `no-hardcoded-colors` used to answer that with `^`-anchored regexes over the
|
|
5738
|
+
* whole value, which only ever matched a value that IS a color. Every real
|
|
5739
|
+
* shorthand slipped through: `shadow-[0_1px_2px_#000]` carries a hardcoded black
|
|
5740
|
+
* that the rule promised to catch and didn't. Anchoring also forced a companion
|
|
5741
|
+
* list of "color-bearing utility prefixes" to keep the match plausible, and that
|
|
5742
|
+
* list drifted from Tailwind (it has `ring` but not `inset-ring`, `shadow` but not
|
|
5743
|
+
* `inset-shadow`, and no way to spell an arbitrary property like `[color:#f00]`).
|
|
5744
|
+
*
|
|
5745
|
+
* Scanning the value instead of anchoring to it drops both problems: the value
|
|
5746
|
+
* carries the color or it doesn't, whatever utility it is attached to.
|
|
5747
|
+
*
|
|
5748
|
+
* Two things must NOT count, and they are why this is a scanner rather than a
|
|
5749
|
+
* regex:
|
|
5750
|
+
* - quoted strings — `content-['#fff']` is the text `#fff`, not a color;
|
|
5751
|
+
* - `url()` contents — `fill-[url(#gradient)]` references an SVG node by id, the
|
|
5752
|
+
* single most common `#` in a Tailwind class that isn't a color.
|
|
5753
|
+
*
|
|
5754
|
+
* The notation set is exactly the one the rule documents (hex + the CSS color
|
|
5755
|
+
* functions). Named colors (`bg-[red]`) are deliberately absent: they were never
|
|
5756
|
+
* covered, and `currentColor`/`transparent`/`inherit` make that a separate
|
|
5757
|
+
* judgement call rather than a scan.
|
|
5758
|
+
*/
|
|
5759
|
+
/** CSS color functions. `color-mix` is absent on purpose: its arguments are scanned. */
|
|
5760
|
+
const COLOR_FUNCTIONS = /* @__PURE__ */ new Set([
|
|
5761
|
+
"rgb",
|
|
5762
|
+
"rgba",
|
|
5763
|
+
"hsl",
|
|
5764
|
+
"hsla",
|
|
5765
|
+
"hwb",
|
|
5766
|
+
"lab",
|
|
5767
|
+
"lch",
|
|
5768
|
+
"oklab",
|
|
5769
|
+
"oklch",
|
|
5770
|
+
"color"
|
|
5771
|
+
]);
|
|
5772
|
+
/** Hex digit counts CSS accepts: `#rgb`, `#rgba`, `#rrggbb`, `#rrggbbaa`. */
|
|
5773
|
+
const HEX_LENGTHS = /* @__PURE__ */ new Set([
|
|
5774
|
+
3,
|
|
5775
|
+
4,
|
|
5776
|
+
6,
|
|
5777
|
+
8
|
|
5778
|
+
]);
|
|
5779
|
+
function isHexDigit(ch) {
|
|
5780
|
+
return ch >= "0" && ch <= "9" || ch >= "a" && ch <= "f" || ch >= "A" && ch <= "F";
|
|
5781
|
+
}
|
|
5782
|
+
/**
|
|
5783
|
+
* `-` belongs to the identifier so `color-mix(` is not read as `color(`. `_` does
|
|
5784
|
+
* NOT: in a Tailwind arbitrary value an underscore stands for a space, so
|
|
5785
|
+
* `0_0_4px_rgb(…)` has to break into words or the function name never surfaces.
|
|
5786
|
+
*/
|
|
5787
|
+
function isIdentChar(ch) {
|
|
5788
|
+
return ch >= "a" && ch <= "z" || ch >= "A" && ch <= "Z" || ch >= "0" && ch <= "9" || ch === "-";
|
|
5789
|
+
}
|
|
5790
|
+
function isIdentStart(ch) {
|
|
5791
|
+
return ch >= "a" && ch <= "z" || ch >= "A" && ch <= "Z";
|
|
5792
|
+
}
|
|
5793
|
+
/** Index just past the closing quote (or end of input on an unterminated string). */
|
|
5794
|
+
function skipQuoted(value, start) {
|
|
5795
|
+
const quote = value[start];
|
|
5796
|
+
let i = start + 1;
|
|
5797
|
+
while (i < value.length) {
|
|
5798
|
+
if (value[i] === "\\") {
|
|
5799
|
+
i += 2;
|
|
5800
|
+
continue;
|
|
5801
|
+
}
|
|
5802
|
+
if (value[i] === quote) return i + 1;
|
|
5803
|
+
i++;
|
|
5804
|
+
}
|
|
5805
|
+
return value.length;
|
|
5806
|
+
}
|
|
5807
|
+
/** Index just past the matching `)`, given `open` pointing at a `(`. */
|
|
5808
|
+
function skipBalanced(value, open) {
|
|
5809
|
+
let depth = 0;
|
|
5810
|
+
let i = open;
|
|
5811
|
+
while (i < value.length) {
|
|
5812
|
+
const ch = value[i];
|
|
5813
|
+
if (ch === "\"" || ch === "'") {
|
|
5814
|
+
i = skipQuoted(value, i);
|
|
5815
|
+
continue;
|
|
5816
|
+
}
|
|
5817
|
+
if (ch === "(") depth++;
|
|
5818
|
+
else if (ch === ")") {
|
|
5819
|
+
depth--;
|
|
5820
|
+
if (depth === 0) return i + 1;
|
|
5821
|
+
}
|
|
5822
|
+
i++;
|
|
5823
|
+
}
|
|
5824
|
+
return value.length;
|
|
5825
|
+
}
|
|
5826
|
+
function containsColorLiteral(value) {
|
|
5827
|
+
let i = 0;
|
|
5828
|
+
while (i < value.length) {
|
|
5829
|
+
const ch = value[i];
|
|
5830
|
+
if (ch === "\"" || ch === "'") {
|
|
5831
|
+
i = skipQuoted(value, i);
|
|
5832
|
+
continue;
|
|
5833
|
+
}
|
|
5834
|
+
if (ch === "#") {
|
|
5835
|
+
let end = i + 1;
|
|
5836
|
+
while (end < value.length && isHexDigit(value[end])) end++;
|
|
5837
|
+
if (HEX_LENGTHS.has(end - i - 1)) return true;
|
|
5838
|
+
i = end > i + 1 ? end : i + 1;
|
|
5839
|
+
continue;
|
|
5840
|
+
}
|
|
5841
|
+
if (isIdentStart(ch)) {
|
|
5842
|
+
const start = i;
|
|
5843
|
+
while (i < value.length && isIdentChar(value[i])) i++;
|
|
5844
|
+
if (value[i] !== "(") continue;
|
|
5845
|
+
const name = value.slice(start, i).toLowerCase();
|
|
5846
|
+
if (name === "url") {
|
|
5847
|
+
i = skipBalanced(value, i);
|
|
5848
|
+
continue;
|
|
5849
|
+
}
|
|
5850
|
+
if (COLOR_FUNCTIONS.has(name)) return true;
|
|
5851
|
+
continue;
|
|
5852
|
+
}
|
|
5853
|
+
i++;
|
|
5854
|
+
}
|
|
5855
|
+
return false;
|
|
5856
|
+
}
|
|
5857
|
+
//#endregion
|
|
4127
5858
|
//#region src/rules/no-hardcoded-colors.ts
|
|
4128
|
-
const COLOR_UTILITY_PREFIXES = [
|
|
4129
|
-
"bg",
|
|
4130
|
-
"text",
|
|
4131
|
-
"border",
|
|
4132
|
-
"border-t",
|
|
4133
|
-
"border-b",
|
|
4134
|
-
"border-l",
|
|
4135
|
-
"border-r",
|
|
4136
|
-
"border-s",
|
|
4137
|
-
"border-e",
|
|
4138
|
-
"border-x",
|
|
4139
|
-
"border-y",
|
|
4140
|
-
"outline",
|
|
4141
|
-
"ring",
|
|
4142
|
-
"ring-offset",
|
|
4143
|
-
"shadow",
|
|
4144
|
-
"accent",
|
|
4145
|
-
"caret",
|
|
4146
|
-
"fill",
|
|
4147
|
-
"stroke",
|
|
4148
|
-
"decoration",
|
|
4149
|
-
"divide",
|
|
4150
|
-
"placeholder",
|
|
4151
|
-
"from",
|
|
4152
|
-
"via",
|
|
4153
|
-
"to"
|
|
4154
|
-
];
|
|
4155
|
-
const HEX_RE = /^#(?:[0-9a-fA-F]{3,4}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})$/;
|
|
4156
|
-
const RGB_RE = /^rgba?\s*\(/;
|
|
4157
|
-
const HSL_RE = /^hsla?\s*\(/;
|
|
4158
|
-
const OKLCH_RE = /^oklch\s*\(/;
|
|
4159
|
-
const OKLAB_RE = /^oklab\s*\(/;
|
|
4160
|
-
const LCH_RE = /^lch\s*\(/;
|
|
4161
|
-
const LAB_RE = /^lab\s*\(/;
|
|
4162
|
-
const HWB_RE = /^hwb\s*\(/;
|
|
4163
|
-
const COLOR_RE = /^color\s*\(/;
|
|
4164
5859
|
const VAR_REF_RE = /var\(\s*--[\w-]+/i;
|
|
5860
|
+
/**
|
|
5861
|
+
* Does the arbitrary value the user wrote carry a hardcoded colour?
|
|
5862
|
+
*
|
|
5863
|
+
* This used to be a set of `^`-anchored regexes plus a list of "colour-bearing
|
|
5864
|
+
* utility prefixes", and both halves leaked. The anchoring meant only a value
|
|
5865
|
+
* that IS a colour matched, so `shadow-[0_1px_2px_#000]` passed. The prefix list
|
|
5866
|
+
* had `ring` but not `inset-ring`, `shadow` but not `inset-shadow`, and no way to
|
|
5867
|
+
* spell an arbitrary property at all (`[color:#f00]`, `[--brand:#f00]`).
|
|
5868
|
+
*
|
|
5869
|
+
* Neither is needed: the value carries the colour or it doesn't, whatever utility
|
|
5870
|
+
* it is attached to. `containsColorLiteral` skips quoted strings and `url()`
|
|
5871
|
+
* contents, which is what keeps `content-['#fff']` and `fill-[url(#gradient)]` out.
|
|
5872
|
+
*/
|
|
4165
5873
|
function isHardcodedColor(value) {
|
|
4166
5874
|
const trimmed = value.trim();
|
|
4167
5875
|
if (VAR_REF_RE.test(trimmed)) return false;
|
|
4168
|
-
return
|
|
4169
|
-
}
|
|
4170
|
-
function isColorUtility(utility) {
|
|
4171
|
-
for (const prefix of COLOR_UTILITY_PREFIXES) if (utility === prefix || utility.startsWith(`${prefix}-`)) return true;
|
|
4172
|
-
return false;
|
|
5876
|
+
return containsColorLiteral(trimmed);
|
|
4173
5877
|
}
|
|
4174
5878
|
const noHardcodedColors = defineRule({
|
|
4175
5879
|
meta: {
|
|
@@ -4195,8 +5899,6 @@ const noHardcodedColors = defineRule({
|
|
|
4195
5899
|
for (const cls of classes) {
|
|
4196
5900
|
if (allowlist.has(cls)) continue;
|
|
4197
5901
|
if (!hasArbitraryValue(cls)) continue;
|
|
4198
|
-
const utility = splitImportant(extractUtility(cls)).bare;
|
|
4199
|
-
if (!isColorUtility(utility)) continue;
|
|
4200
5902
|
const value = getArbitraryValue(cls);
|
|
4201
5903
|
if (!value) continue;
|
|
4202
5904
|
if (isHardcodedColor(value)) context.report({
|
|
@@ -4216,11 +5918,23 @@ const noContradictingVariants = defineRule({
|
|
|
4216
5918
|
meta: {
|
|
4217
5919
|
type: "suggestion",
|
|
4218
5920
|
docs: { description: "Disallow variant-prefixed classes that are redundant because the base class already applies unconditionally" },
|
|
4219
|
-
schema: [
|
|
5921
|
+
schema: [{
|
|
5922
|
+
type: "object",
|
|
5923
|
+
properties: { entryPoint: { type: "string" } },
|
|
5924
|
+
additionalProperties: false
|
|
5925
|
+
}],
|
|
5926
|
+
defaultOptions: [{}],
|
|
4220
5927
|
messages: { redundantVariant: "\"{{variantClass}}\" is redundant because \"{{baseClass}}\" already applies unconditionally." }
|
|
4221
5928
|
},
|
|
4222
5929
|
createOnce(context) {
|
|
5930
|
+
const getDS = createLazyLoader(context);
|
|
5931
|
+
function variantFactsLookup() {
|
|
5932
|
+
const ds = softGetDS(getDS);
|
|
5933
|
+
if (!ds) return () => void 0;
|
|
5934
|
+
return (variant) => ds.cache.getVariantFacts(variant);
|
|
5935
|
+
}
|
|
4223
5936
|
function check(locations) {
|
|
5937
|
+
const factsFor = variantFactsLookup();
|
|
4224
5938
|
for (const loc of locations) {
|
|
4225
5939
|
const classes = splitClasses(loc.value);
|
|
4226
5940
|
const baseUtilities = /* @__PURE__ */ new Set();
|
|
@@ -4228,7 +5942,7 @@ const noContradictingVariants = defineRule({
|
|
|
4228
5942
|
for (const cls of classes) {
|
|
4229
5943
|
const variants = extractVariants(cls);
|
|
4230
5944
|
if (variants.length === 0) continue;
|
|
4231
|
-
if (variants.some(changesTarget)) continue;
|
|
5945
|
+
if (variants.some((v) => changesTarget(v, factsFor(v)))) continue;
|
|
4232
5946
|
const utility = extractUtility(cls);
|
|
4233
5947
|
if (baseUtilities.has(utility)) context.report({
|
|
4234
5948
|
node: loc.node,
|
|
@@ -4364,15 +6078,9 @@ const consistentVariantOrder = defineRule({
|
|
|
4364
6078
|
const priorityByEntry = /* @__PURE__ */ new Map();
|
|
4365
6079
|
const prefixByEntry = /* @__PURE__ */ new Map();
|
|
4366
6080
|
function resolveForFile() {
|
|
4367
|
-
|
|
4368
|
-
|
|
4369
|
-
|
|
4370
|
-
const ds = getDS();
|
|
4371
|
-
dsCache = ds.cache;
|
|
4372
|
-
entryKey = ds.entryPoint;
|
|
4373
|
-
} catch (err) {
|
|
4374
|
-
if (!isFatalError(err)) throw err;
|
|
4375
|
-
}
|
|
6081
|
+
const ds = softGetDS(getDS);
|
|
6082
|
+
const dsCache = ds ? ds.cache : null;
|
|
6083
|
+
const entryKey = ds ? ds.entryPoint : "";
|
|
4376
6084
|
let priorityOf = priorityByEntry.get(entryKey);
|
|
4377
6085
|
if (!priorityOf) {
|
|
4378
6086
|
priorityOf = buildPriority(getOptions().order, dsCache);
|
|
@@ -4385,11 +6093,12 @@ const consistentVariantOrder = defineRule({
|
|
|
4385
6093
|
}
|
|
4386
6094
|
return {
|
|
4387
6095
|
priorityOf,
|
|
4388
|
-
prefix
|
|
6096
|
+
prefix,
|
|
6097
|
+
factsFor: dsCache ? (variant) => dsCache.getVariantFacts(variant) : () => void 0
|
|
4389
6098
|
};
|
|
4390
6099
|
}
|
|
4391
6100
|
function reorderClass(cls) {
|
|
4392
|
-
const { priorityOf, prefix } = resolveForFile();
|
|
6101
|
+
const { priorityOf, prefix, factsFor } = resolveForFile();
|
|
4393
6102
|
let pfx = "";
|
|
4394
6103
|
let body = cls;
|
|
4395
6104
|
if (prefix && cls.startsWith(prefix + ":")) {
|
|
@@ -4400,7 +6109,7 @@ const consistentVariantOrder = defineRule({
|
|
|
4400
6109
|
if (variants.length < 2) return null;
|
|
4401
6110
|
const pseudo = [];
|
|
4402
6111
|
const rest = [];
|
|
4403
|
-
for (const v of variants) if (isPseudoElementVariant(v)) pseudo.push(v);
|
|
6112
|
+
for (const v of variants) if (isPseudoElementVariant(v, factsFor(v))) pseudo.push(v);
|
|
4404
6113
|
else rest.push(v);
|
|
4405
6114
|
const reorderedRest = [];
|
|
4406
6115
|
let segment = [];
|
|
@@ -4410,7 +6119,7 @@ const consistentVariantOrder = defineRule({
|
|
|
4410
6119
|
reorderedRest.push(...segment);
|
|
4411
6120
|
segment = [];
|
|
4412
6121
|
};
|
|
4413
|
-
for (const v of rest) if (isSelectorBarrier(v)) {
|
|
6122
|
+
for (const v of rest) if (isSelectorBarrier(v, factsFor(v))) {
|
|
4414
6123
|
flushSegment();
|
|
4415
6124
|
reorderedRest.push(v);
|
|
4416
6125
|
} else segment.push(v);
|
|
@@ -4676,6 +6385,9 @@ const plugin = definePlugin({
|
|
|
4676
6385
|
if (!match) return [];
|
|
4677
6386
|
const candidate = `${match.prefix}-${match.varName}${match.modifier}`;
|
|
4678
6387
|
if (!cache.isValid(candidate)) return [];
|
|
6388
|
+
const target = `--${match.varName}`;
|
|
6389
|
+
const baseCandidate = `${match.prefix}-${match.varName}`;
|
|
6390
|
+
if (!cache.getCssDeclarations(baseCandidate).some((decl) => [...decl.readsVars, ...decl.readsFallbackVars].some((read) => cache.themeVarResolvesTo(read, target))) && cache.definesVar(target)) return [];
|
|
4679
6391
|
const namedEquivalent = cache.getNamedEquivalent(bareUtility);
|
|
4680
6392
|
if (namedEquivalent && namedEquivalent === `${match.prefix}-${match.varName}`) return [];
|
|
4681
6393
|
return [{
|