oxlint-tailwindcss 0.1.2 → 0.1.4
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 +42 -74
- package/dist/index.cjs +94 -36
- package/dist/index.mjs +94 -36
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -1,7 +1,19 @@
|
|
|
1
1
|
import { definePlugin, defineRule } from "@oxlint/plugins";
|
|
2
2
|
import { execFileSync } from "node:child_process";
|
|
3
|
+
import { createHash } from "node:crypto";
|
|
4
|
+
import { existsSync, mkdirSync, readFileSync, statSync, writeFileSync } from "node:fs";
|
|
3
5
|
import { dirname, join, resolve } from "node:path";
|
|
4
|
-
import {
|
|
6
|
+
import { tmpdir } from "node:os";
|
|
7
|
+
//#region src/utils/extractors.ts
|
|
8
|
+
/**
|
|
9
|
+
* Preserves leading/trailing spaces in fixed class strings for template literals.
|
|
10
|
+
* Without this, `h-3 w-3 ${x}` fixed to `size-3${x}` would break classes.
|
|
11
|
+
*/
|
|
12
|
+
function preserveSpaces(loc, fixed) {
|
|
13
|
+
if (loc.preserveLeadingSpace && !fixed.startsWith(" ")) fixed = ` ${fixed}`;
|
|
14
|
+
if (loc.preserveTrailingSpace && !fixed.endsWith(" ")) fixed = `${fixed} `;
|
|
15
|
+
return fixed;
|
|
16
|
+
}
|
|
5
17
|
const DEFAULT_EXTRACTOR_CONFIG = {
|
|
6
18
|
attributes: ["className", "class"],
|
|
7
19
|
callees: [
|
|
@@ -268,7 +280,7 @@ const noDuplicateClasses = defineRule({
|
|
|
268
280
|
messageId: "duplicate",
|
|
269
281
|
data: { className: dup },
|
|
270
282
|
fix(fixer) {
|
|
271
|
-
return fixer.replaceTextRange(loc.range, fixed);
|
|
283
|
+
return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixed));
|
|
272
284
|
}
|
|
273
285
|
});
|
|
274
286
|
}
|
|
@@ -684,9 +696,18 @@ async function main() {
|
|
|
684
696
|
}
|
|
685
697
|
main().catch(e => { process.stderr.write(e.message); process.exit(1); });
|
|
686
698
|
`;
|
|
699
|
+
const CACHE_DIR = join(tmpdir(), "oxlint-tailwindcss");
|
|
700
|
+
function getCachePath(cssPath, mtime) {
|
|
701
|
+
return join(CACHE_DIR, `${createHash("md5").update(`${cssPath}:${mtime}`).digest("hex")}.json`);
|
|
702
|
+
}
|
|
687
703
|
function loadDesignSystemSync(cssPath) {
|
|
688
704
|
const resolvedPath = resolve(cssPath);
|
|
689
705
|
try {
|
|
706
|
+
const mtime = statSync(resolvedPath).mtimeMs;
|
|
707
|
+
const cachePath = getCachePath(resolvedPath, mtime);
|
|
708
|
+
if (existsSync(cachePath)) try {
|
|
709
|
+
return JSON.parse(readFileSync(cachePath, "utf-8"));
|
|
710
|
+
} catch {}
|
|
690
711
|
const stdout = execFileSync(process.execPath, ["-e", PRECOMPUTE_SCRIPT], {
|
|
691
712
|
encoding: "utf-8",
|
|
692
713
|
timeout: 3e4,
|
|
@@ -697,6 +718,10 @@ function loadDesignSystemSync(cssPath) {
|
|
|
697
718
|
},
|
|
698
719
|
cwd: dirname(resolvedPath)
|
|
699
720
|
});
|
|
721
|
+
try {
|
|
722
|
+
mkdirSync(CACHE_DIR, { recursive: true });
|
|
723
|
+
writeFileSync(cachePath, stdout);
|
|
724
|
+
} catch {}
|
|
700
725
|
return JSON.parse(stdout);
|
|
701
726
|
} catch (error) {
|
|
702
727
|
console.error(`[oxlint-tailwindcss] Failed to load design system from "${resolvedPath}":`, error instanceof Error ? error.message : error);
|
|
@@ -705,21 +730,29 @@ function loadDesignSystemSync(cssPath) {
|
|
|
705
730
|
}
|
|
706
731
|
//#endregion
|
|
707
732
|
//#region src/design-system/auto-detect.ts
|
|
708
|
-
const
|
|
709
|
-
"src
|
|
710
|
-
"
|
|
711
|
-
"
|
|
712
|
-
"
|
|
713
|
-
"
|
|
714
|
-
"
|
|
715
|
-
"
|
|
716
|
-
"
|
|
717
|
-
"
|
|
718
|
-
"style.css",
|
|
719
|
-
"styles/globals.css",
|
|
720
|
-
"app/globals.css",
|
|
721
|
-
"assets/css/tailwind.css"
|
|
733
|
+
const CANDIDATE_DIRS = [
|
|
734
|
+
"src",
|
|
735
|
+
".",
|
|
736
|
+
"app",
|
|
737
|
+
"styles",
|
|
738
|
+
"style",
|
|
739
|
+
"css",
|
|
740
|
+
"assets",
|
|
741
|
+
"assets/css",
|
|
742
|
+
"resources/css"
|
|
722
743
|
];
|
|
744
|
+
const CANDIDATE_NAMES = [
|
|
745
|
+
"app",
|
|
746
|
+
"globals",
|
|
747
|
+
"global",
|
|
748
|
+
"style",
|
|
749
|
+
"styles",
|
|
750
|
+
"index",
|
|
751
|
+
"main",
|
|
752
|
+
"tailwind",
|
|
753
|
+
"tailwindcss"
|
|
754
|
+
];
|
|
755
|
+
const CANDIDATE_PATHS = CANDIDATE_DIRS.flatMap((dir) => CANDIDATE_NAMES.map((name) => dir === "." ? `${name}.css` : `${dir}/${name}.css`));
|
|
723
756
|
const TAILWIND_SIGNALS = [
|
|
724
757
|
"@import \"tailwindcss\"",
|
|
725
758
|
"@import 'tailwindcss'",
|
|
@@ -766,12 +799,24 @@ function autoDetectEntryPoint(filePath) {
|
|
|
766
799
|
//#region src/design-system/loader.ts
|
|
767
800
|
let singleton = null;
|
|
768
801
|
/**
|
|
802
|
+
* Extracts `entryPoint` from `context.settings.tailwindcss`.
|
|
803
|
+
*/
|
|
804
|
+
function entryPointFromSettings(settings) {
|
|
805
|
+
const tw = settings?.tailwindcss;
|
|
806
|
+
if (tw && typeof tw === "object" && "entryPoint" in tw) {
|
|
807
|
+
const ep = tw.entryPoint;
|
|
808
|
+
if (typeof ep === "string") return ep;
|
|
809
|
+
}
|
|
810
|
+
}
|
|
811
|
+
/**
|
|
769
812
|
* Returns the design system cache, loading synchronously on first call.
|
|
770
813
|
* Uses execFileSync internally to bridge the async Tailwind API.
|
|
771
814
|
* All rules receive the SAME instance.
|
|
815
|
+
*
|
|
816
|
+
* Resolution order: rule option `entryPoint` > `settings.tailwindcss.entryPoint` > auto-detect.
|
|
772
817
|
*/
|
|
773
|
-
function getLoadedDesignSystem(entryPoint) {
|
|
774
|
-
const cssPath = entryPoint ?? singleton?.path ?? autoDetectEntryPoint();
|
|
818
|
+
function getLoadedDesignSystem(entryPoint, settings) {
|
|
819
|
+
const cssPath = entryPoint ?? entryPointFromSettings(settings) ?? singleton?.path ?? autoDetectEntryPoint();
|
|
775
820
|
if (!cssPath) return null;
|
|
776
821
|
const resolvedPath = resolve(cssPath);
|
|
777
822
|
try {
|
|
@@ -814,6 +859,19 @@ function safeOptions(context) {
|
|
|
814
859
|
return;
|
|
815
860
|
}
|
|
816
861
|
}
|
|
862
|
+
/**
|
|
863
|
+
* Safely read context.settings.
|
|
864
|
+
*
|
|
865
|
+
* Like `safeOptions`, `context.settings` may not be accessible in `createOnce()`.
|
|
866
|
+
* The try/catch handles that gracefully.
|
|
867
|
+
*/
|
|
868
|
+
function safeSettings(context) {
|
|
869
|
+
try {
|
|
870
|
+
return context.settings ?? void 0;
|
|
871
|
+
} catch {
|
|
872
|
+
return;
|
|
873
|
+
}
|
|
874
|
+
}
|
|
817
875
|
//#endregion
|
|
818
876
|
//#region src/rules/no-unknown-classes.ts
|
|
819
877
|
const noUnknownClasses = defineRule({
|
|
@@ -842,7 +900,7 @@ const noUnknownClasses = defineRule({
|
|
|
842
900
|
},
|
|
843
901
|
createOnce(context) {
|
|
844
902
|
const options = safeOptions(context);
|
|
845
|
-
const result = getLoadedDesignSystem(options?.entryPoint);
|
|
903
|
+
const result = getLoadedDesignSystem(options?.entryPoint, safeSettings(context));
|
|
846
904
|
if (!result) return {};
|
|
847
905
|
const { cache } = result;
|
|
848
906
|
const allowlist = new Set(options?.allowlist ?? []);
|
|
@@ -910,7 +968,7 @@ const noConflictingClasses = defineRule({
|
|
|
910
968
|
messages: { conflict: "\"{{classA}}\" and \"{{classB}}\" affect {{properties}}. \"{{winner}}\" takes precedence (appears later)." }
|
|
911
969
|
},
|
|
912
970
|
createOnce(context) {
|
|
913
|
-
const result = getLoadedDesignSystem(safeOptions(context)?.entryPoint);
|
|
971
|
+
const result = getLoadedDesignSystem(safeOptions(context)?.entryPoint, safeSettings(context));
|
|
914
972
|
if (!result) return {};
|
|
915
973
|
const { cache } = result;
|
|
916
974
|
function check(locations) {
|
|
@@ -997,7 +1055,7 @@ const noDeprecatedClasses = defineRule({
|
|
|
997
1055
|
messages: { deprecated: "\"{{className}}\" is deprecated in Tailwind v4. Use \"{{replacement}}\" instead." }
|
|
998
1056
|
},
|
|
999
1057
|
createOnce(context) {
|
|
1000
|
-
const result = getLoadedDesignSystem(safeOptions(context)?.entryPoint);
|
|
1058
|
+
const result = getLoadedDesignSystem(safeOptions(context)?.entryPoint, safeSettings(context));
|
|
1001
1059
|
function check(locations) {
|
|
1002
1060
|
for (const loc of locations) {
|
|
1003
1061
|
const classes = splitClasses(loc.value);
|
|
@@ -1019,7 +1077,7 @@ const noDeprecatedClasses = defineRule({
|
|
|
1019
1077
|
},
|
|
1020
1078
|
fix(fixer) {
|
|
1021
1079
|
const fixed = loc.value.replace(cls, fullReplacement);
|
|
1022
|
-
return fixer.replaceTextRange(loc.range, fixed);
|
|
1080
|
+
return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixed));
|
|
1023
1081
|
}
|
|
1024
1082
|
});
|
|
1025
1083
|
}
|
|
@@ -1056,7 +1114,7 @@ const enforceCanonical = defineRule({
|
|
|
1056
1114
|
messages: { nonCanonical: "\"{{className}}\" can be written as \"{{canonical}}\". Use the canonical form." }
|
|
1057
1115
|
},
|
|
1058
1116
|
createOnce(context) {
|
|
1059
|
-
const result = getLoadedDesignSystem(safeOptions(context)?.entryPoint);
|
|
1117
|
+
const result = getLoadedDesignSystem(safeOptions(context)?.entryPoint, safeSettings(context));
|
|
1060
1118
|
if (!result) return {};
|
|
1061
1119
|
const { cache } = result;
|
|
1062
1120
|
function check(locations) {
|
|
@@ -1079,7 +1137,7 @@ const enforceCanonical = defineRule({
|
|
|
1079
1137
|
canonical
|
|
1080
1138
|
},
|
|
1081
1139
|
fix(fixer) {
|
|
1082
|
-
return fixer.replaceTextRange(loc.range, fixedValue);
|
|
1140
|
+
return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
|
|
1083
1141
|
}
|
|
1084
1142
|
});
|
|
1085
1143
|
} else context.report({
|
|
@@ -1130,7 +1188,7 @@ const enforceSortOrder = defineRule({
|
|
|
1130
1188
|
messages: { unsorted: "Tailwind classes are not in the recommended order." }
|
|
1131
1189
|
},
|
|
1132
1190
|
createOnce(context) {
|
|
1133
|
-
const result = getLoadedDesignSystem(safeOptions(context)?.entryPoint);
|
|
1191
|
+
const result = getLoadedDesignSystem(safeOptions(context)?.entryPoint, safeSettings(context));
|
|
1134
1192
|
if (!result) return {};
|
|
1135
1193
|
const { cache } = result;
|
|
1136
1194
|
let _mode = null;
|
|
@@ -1193,7 +1251,7 @@ const enforceSortOrder = defineRule({
|
|
|
1193
1251
|
node: loc.node,
|
|
1194
1252
|
messageId: "unsorted",
|
|
1195
1253
|
fix(fixer) {
|
|
1196
|
-
return fixer.replaceTextRange(loc.range, sortedNames.join(" "));
|
|
1254
|
+
return fixer.replaceTextRange(loc.range, preserveSpaces(loc, sortedNames.join(" ")));
|
|
1197
1255
|
}
|
|
1198
1256
|
});
|
|
1199
1257
|
}
|
|
@@ -1304,7 +1362,7 @@ const enforceShorthand = defineRule({
|
|
|
1304
1362
|
replacement: rule.replacement
|
|
1305
1363
|
},
|
|
1306
1364
|
fix(fixer) {
|
|
1307
|
-
return fixer.replaceTextRange(loc.range, remaining.join(" "));
|
|
1365
|
+
return fixer.replaceTextRange(loc.range, preserveSpaces(loc, remaining.join(" ")));
|
|
1308
1366
|
}
|
|
1309
1367
|
});
|
|
1310
1368
|
}
|
|
@@ -1387,7 +1445,7 @@ const enforceLogical = defineRule({
|
|
|
1387
1445
|
replacement
|
|
1388
1446
|
},
|
|
1389
1447
|
fix(fixer) {
|
|
1390
|
-
return fixer.replaceTextRange(loc.range, fixedValue);
|
|
1448
|
+
return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
|
|
1391
1449
|
}
|
|
1392
1450
|
});
|
|
1393
1451
|
else context.report({
|
|
@@ -1677,7 +1735,7 @@ const enforceConsistentImportantPosition = defineRule({
|
|
|
1677
1735
|
replacement
|
|
1678
1736
|
},
|
|
1679
1737
|
fix(fixer) {
|
|
1680
|
-
return fixer.replaceTextRange(loc.range, fixedValue);
|
|
1738
|
+
return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
|
|
1681
1739
|
}
|
|
1682
1740
|
});
|
|
1683
1741
|
else context.report({
|
|
@@ -1757,7 +1815,7 @@ const enforceNegativeArbitraryValues = defineRule({
|
|
|
1757
1815
|
replacement
|
|
1758
1816
|
},
|
|
1759
1817
|
fix(fixer) {
|
|
1760
|
-
return fixer.replaceTextRange(loc.range, fixedValue);
|
|
1818
|
+
return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
|
|
1761
1819
|
}
|
|
1762
1820
|
});
|
|
1763
1821
|
else context.report({
|
|
@@ -1858,7 +1916,7 @@ const enforceConsistentVariableSyntax = defineRule({
|
|
|
1858
1916
|
replacement
|
|
1859
1917
|
},
|
|
1860
1918
|
fix(fixer) {
|
|
1861
|
-
return fixer.replaceTextRange(loc.range, fixedValue);
|
|
1919
|
+
return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
|
|
1862
1920
|
}
|
|
1863
1921
|
});
|
|
1864
1922
|
else context.report({
|
|
@@ -2266,7 +2324,7 @@ const consistentVariantOrder = defineRule({
|
|
|
2266
2324
|
messages: { wrongOrder: "\"{{className}}\" has variants in wrong order. Use \"{{replacement}}\" instead." }
|
|
2267
2325
|
},
|
|
2268
2326
|
createOnce(context) {
|
|
2269
|
-
const dsCache = getLoadedDesignSystem()?.cache ?? null;
|
|
2327
|
+
const dsCache = getLoadedDesignSystem(void 0, safeSettings(context))?.cache ?? null;
|
|
2270
2328
|
let _config = null;
|
|
2271
2329
|
function getConfig() {
|
|
2272
2330
|
if (_config === null) {
|
|
@@ -2340,7 +2398,7 @@ const consistentVariantOrder = defineRule({
|
|
|
2340
2398
|
replacement
|
|
2341
2399
|
},
|
|
2342
2400
|
fix(fixer) {
|
|
2343
|
-
return fixer.replaceTextRange(loc.range, fixedValue);
|
|
2401
|
+
return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
|
|
2344
2402
|
}
|
|
2345
2403
|
});
|
|
2346
2404
|
else context.report({
|
|
@@ -2430,7 +2488,7 @@ const enforceConsistentLineWrapping = defineRule({
|
|
|
2430
2488
|
max: String(classesPerLine)
|
|
2431
2489
|
},
|
|
2432
2490
|
fix(fixer) {
|
|
2433
|
-
return fixer.replaceTextRange(loc.range, fixedValue);
|
|
2491
|
+
return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
|
|
2434
2492
|
}
|
|
2435
2493
|
});
|
|
2436
2494
|
} else context.report({
|
|
@@ -2475,7 +2533,7 @@ const noUnnecessaryArbitraryValue = defineRule({
|
|
|
2475
2533
|
messages: { unnecessaryArbitrary: "\"{{className}}\" can be written as \"{{replacement}}\". Use the named class instead." }
|
|
2476
2534
|
},
|
|
2477
2535
|
createOnce(context) {
|
|
2478
|
-
const result = getLoadedDesignSystem(safeOptions(context)?.entryPoint);
|
|
2536
|
+
const result = getLoadedDesignSystem(safeOptions(context)?.entryPoint, safeSettings(context));
|
|
2479
2537
|
if (!result) return {};
|
|
2480
2538
|
const { cache } = result;
|
|
2481
2539
|
function check(locations) {
|
|
@@ -2505,7 +2563,7 @@ const noUnnecessaryArbitraryValue = defineRule({
|
|
|
2505
2563
|
replacement
|
|
2506
2564
|
},
|
|
2507
2565
|
fix(fixer) {
|
|
2508
|
-
return fixer.replaceTextRange(loc.range, fixedValue);
|
|
2566
|
+
return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
|
|
2509
2567
|
}
|
|
2510
2568
|
});
|
|
2511
2569
|
else context.report({
|