oxlint-tailwindcss 0.2.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,13 +1,14 @@
1
1
  # oxlint-tailwindcss
2
2
 
3
- 22 Tailwind CSS linting rules for [oxlint](https://oxc.rs/docs/guide/usage/linter). Built for Tailwind CSS v4 with zero-config auto-detection, typo suggestions, and autofixes.
3
+ 22 Tailwind CSS linting rules for [oxlint](https://oxc.rs/docs/guide/usage/linter). Built for Tailwind CSS v4 with auto-detection, typo suggestions, and autofixes.
4
4
 
5
5
  Read the story behind this plugin: [oxlint-tailwindcss: The Linting Plugin Tailwind v4 Needed](https://sergioazocar.com/en/blog/oxlint-tailwindcss-the-linting-plugin-tailwind-v4-needed)
6
6
 
7
7
  ## Highlights
8
8
 
9
- - **Zero config** — Auto-detects your Tailwind CSS entry point. Works out of the box.
10
- - **Fast** — Native oxlint plugin with a shared design system cache. Loads once, used by all rules.
9
+ - **Works out of the box** — Auto-detects your Tailwind CSS entry point. Fully configurable when needed.
10
+ - **Monorepo-ready** — Each package resolves its own design system automatically. Run `oxlint` once from the workspace root.
11
+ - **Fast** — Native oxlint plugin with per-entry-point caching and content-based disk cache for monorepo deduplication.
11
12
  - **Tailwind CSS v4** — Designed for v4 from day one.
12
13
  - **Typo suggestions** — `itms-center` → "Did you mean `items-center`?"
13
14
  - **Conflict detection** — Shows exactly which CSS properties conflict and which class wins.
@@ -75,7 +76,7 @@ assets/{name}.css assets/css/{name}.css resources/css/{name}.css
75
76
 
76
77
  Where `{name}` is one of: `app`, `globals`, `global`, `style`, `styles`, `index`, `main`, `tailwind`, `tailwindcss`.
77
78
 
78
- The search is monorepo-aware — it stops at `package.json` boundaries so each package resolves its own Tailwind config.
79
+ The search is monorepo-aware — it stops at `package.json` boundaries so each package resolves its own Tailwind config. If the signal isn't found directly in the CSS file, the auto-detector follows `@import` statements one level deep — supporting both relative paths and package imports (e.g. `@import '@company/theme/tailwind.config.css'`).
79
80
 
80
81
  If auto-detection doesn't find your CSS file, set `entryPoint` once in `settings`:
81
82
 
@@ -94,7 +95,7 @@ If auto-detection doesn't find your CSS file, set `entryPoint` once in `settings
94
95
  }
95
96
  ```
96
97
 
97
- The design system is loaded once and shared across all rules.
98
+ The design system is loaded once per entry point and shared across all rules. In monorepos, each package resolves its own entry point automatically via `package.json` boundaries.
98
99
 
99
100
  You can also override per rule if needed:
100
101
 
@@ -108,6 +109,18 @@ You can also override per rule if needed:
108
109
 
109
110
  Resolution order: rule option > `settings.tailwindcss.entryPoint` > auto-detect.
110
111
 
112
+ For slow environments (large monorepos, CI), you can increase the design system loading timeout:
113
+
114
+ ```jsonc
115
+ {
116
+ "settings": {
117
+ "tailwindcss": {
118
+ "timeout": 60000, // milliseconds (default: 30000)
119
+ },
120
+ },
121
+ }
122
+ ```
123
+
111
124
  If no entry point is found (neither configured nor auto-detected), rules that require the design system (`no-unknown-classes`, `no-conflicting-classes`, `no-deprecated-classes`, `enforce-canonical`, `enforce-sort-order`, `no-unnecessary-arbitrary-value`, `consistent-variant-order`) are silently disabled. All other rules work without it.
112
125
 
113
126
  ## Custom class detection
@@ -140,6 +153,25 @@ You can extend these defaults via `settings.tailwindcss`. All values are **addit
140
153
 
141
154
  This applies to all 22 rules at once. For example, adding `"classNames"` to `attributes` makes every rule lint `<Input classNames={{ root: "..." }} />`.
142
155
 
156
+ To **remove** specific items from the built-in defaults, use `exclude`:
157
+
158
+ ```jsonc
159
+ {
160
+ "settings": {
161
+ "tailwindcss": {
162
+ "exclude": {
163
+ // Stop scanning variables named "style" / "styles"
164
+ "variablePatterns": ["^styles?$"],
165
+ // Stop scanning a specific callee
166
+ "callees": ["objstr"],
167
+ },
168
+ },
169
+ },
170
+ }
171
+ ```
172
+
173
+ For `variablePatterns`, exclusions match against the regex source (e.g. `"^styles?$"` removes the default `/^styles?$/` pattern).
174
+
143
175
  ## Supported patterns
144
176
 
145
177
  The plugin extracts Tailwind classes from:
package/dist/index.cjs CHANGED
@@ -42,9 +42,14 @@ const DEFAULT_EXTRACTOR_CONFIG = {
42
42
  };
43
43
  let _cachedConfig = null;
44
44
  let _settingsResolved = false;
45
- function mergeUnique(defaults, extras) {
46
- if (!extras || extras.length === 0) return defaults;
47
- const set = new Set(defaults);
45
+ function mergeUnique(defaults, extras, exclusions) {
46
+ let base = defaults;
47
+ if (exclusions && exclusions.length > 0) {
48
+ const excludeSet = new Set(exclusions);
49
+ base = base.filter((d) => !excludeSet.has(d));
50
+ }
51
+ if (!extras || extras.length === 0) return base;
52
+ const set = new Set(base);
48
53
  for (const e of extras) set.add(e);
49
54
  return [...set];
50
55
  }
@@ -67,11 +72,14 @@ function getExtractorConfig(context) {
67
72
  _cachedConfig = DEFAULT_EXTRACTOR_CONFIG;
68
73
  return _cachedConfig;
69
74
  }
75
+ const exclude = tw.exclude;
76
+ const excludedPatternSources = new Set(exclude?.variablePatterns ?? []);
77
+ const filteredPatterns = excludedPatternSources.size > 0 ? DEFAULT_EXTRACTOR_CONFIG.variablePatterns.filter((re) => !excludedPatternSources.has(re.source)) : DEFAULT_EXTRACTOR_CONFIG.variablePatterns;
70
78
  _cachedConfig = {
71
- attributes: mergeUnique(DEFAULT_EXTRACTOR_CONFIG.attributes, tw.attributes),
72
- callees: mergeUnique(DEFAULT_EXTRACTOR_CONFIG.callees, tw.callees),
73
- tags: mergeUnique(DEFAULT_EXTRACTOR_CONFIG.tags, tw.tags),
74
- variablePatterns: [...DEFAULT_EXTRACTOR_CONFIG.variablePatterns, ...(tw.variablePatterns ?? []).map((p) => new RegExp(p))]
79
+ attributes: mergeUnique(DEFAULT_EXTRACTOR_CONFIG.attributes, tw.attributes, exclude?.attributes),
80
+ callees: mergeUnique(DEFAULT_EXTRACTOR_CONFIG.callees, tw.callees, exclude?.callees),
81
+ tags: mergeUnique(DEFAULT_EXTRACTOR_CONFIG.tags, tw.tags, exclude?.tags),
82
+ variablePatterns: [...filteredPatterns, ...(tw.variablePatterns ?? []).map((p) => new RegExp(p))]
75
83
  };
76
84
  return _cachedConfig;
77
85
  }
@@ -864,6 +872,10 @@ async function main() {
864
872
  const entries = ds.getClassList();
865
873
  const classNames = entries.map(e => e[0]);
866
874
 
875
+ // Index for O(1) lookups by class name (avoids O(N) indexOf/includes in later phases)
876
+ const classNameIndex = new Map();
877
+ for (let i = 0; i < classNames.length; i++) classNameIndex.set(classNames[i], i);
878
+
867
879
  // Validity: which classes produce CSS
868
880
  const cssResults = ds.candidatesToCss(classNames);
869
881
  const validClasses = classNames.filter((_, i) => cssResults[i] != null);
@@ -925,7 +937,7 @@ async function main() {
925
937
  // Sort order — include extra candidates so bare utilities (rounded, blur, etc.) get order
926
938
  const allForOrder = [...classNames];
927
939
  for (const cls of validClasses) {
928
- if (!classNames.includes(cls)) allForOrder.push(cls);
940
+ if (!classNameIndex.has(cls)) allForOrder.push(cls);
929
941
  }
930
942
  const order = {};
931
943
  const orderResults = ds.getClassOrder(allForOrder);
@@ -1055,7 +1067,8 @@ async function main() {
1055
1067
  const candidates = [];
1056
1068
  for (const cls of validClasses) {
1057
1069
  if (cls.includes('[') || cls.includes('/')) continue;
1058
- const idx = classNames.indexOf(cls);
1070
+ const idx = classNameIndex.get(cls);
1071
+ if (idx === undefined) continue;
1059
1072
  const cssText = cssResults[idx];
1060
1073
  if (!cssText) continue;
1061
1074
  const pvMatch = cssText.match(/^\\s+([\\w-]+)\\s*:\\s*(.+?)\\s*;?\\s*$/m);
@@ -1088,21 +1101,61 @@ async function main() {
1088
1101
  main().catch(e => { process.stderr.write(e.message); process.exit(1); });
1089
1102
  `;
1090
1103
  const CACHE_DIR = (0, node_path.join)((0, node_os.tmpdir)(), "oxlint-tailwindcss");
1091
- const CACHE_VERSION = 10;
1092
- function getCachePath(cssPath, mtime) {
1093
- return (0, node_path.join)(CACHE_DIR, `${(0, node_crypto.createHash)("md5").update(`v${CACHE_VERSION}:${cssPath}:${mtime}`).digest("hex")}.json`);
1104
+ const CACHE_VERSION = 11;
1105
+ /**
1106
+ * Two-level disk cache for monorepo deduplication:
1107
+ *
1108
+ * Level 1 — mtime index (.idx): maps path+mtime → content hash (fast-path, avoids reading CSS)
1109
+ * Level 2 — content cache (.json): maps content hash → precomputed data (shared across packages)
1110
+ *
1111
+ * In monorepos, multiple packages with identical CSS (e.g. `@import 'tailwindcss'`) at different
1112
+ * paths share a single content cache entry, avoiding redundant child process spawns.
1113
+ */
1114
+ function getMtimeIndexPath(cssPath, mtime) {
1115
+ return (0, node_path.join)(CACHE_DIR, `${(0, node_crypto.createHash)("md5").update(`v${CACHE_VERSION}:${cssPath}:${mtime}`).digest("hex")}.idx`);
1116
+ }
1117
+ function computeContentHash(content) {
1118
+ return (0, node_crypto.createHash)("md5").update(`v${CACHE_VERSION}:${content}`).digest("hex");
1119
+ }
1120
+ function getContentCachePath(contentHash) {
1121
+ return (0, node_path.join)(CACHE_DIR, `${contentHash}.json`);
1122
+ }
1123
+ function tryReadCache(cachePath) {
1124
+ try {
1125
+ return JSON.parse((0, node_fs.readFileSync)(cachePath, "utf-8"));
1126
+ } catch {
1127
+ return null;
1128
+ }
1129
+ }
1130
+ function writeCacheFiles(contentCachePath, mtimeIndexPath, contentHash, data) {
1131
+ try {
1132
+ (0, node_fs.mkdirSync)(CACHE_DIR, { recursive: true });
1133
+ (0, node_fs.writeFileSync)(contentCachePath, data);
1134
+ (0, node_fs.writeFileSync)(mtimeIndexPath, contentHash);
1135
+ } catch {}
1094
1136
  }
1095
- function loadDesignSystemSync(cssPath) {
1137
+ function loadDesignSystemSync(cssPath, timeout) {
1096
1138
  const resolvedPath = (0, node_path.resolve)(cssPath);
1097
1139
  try {
1098
1140
  const mtime = (0, node_fs.statSync)(resolvedPath).mtimeMs;
1099
- const cachePath = getCachePath(resolvedPath, mtime);
1100
- if ((0, node_fs.existsSync)(cachePath)) try {
1101
- return JSON.parse((0, node_fs.readFileSync)(cachePath, "utf-8"));
1141
+ const mtimeIndexPath = getMtimeIndexPath(resolvedPath, mtime);
1142
+ if ((0, node_fs.existsSync)(mtimeIndexPath)) try {
1143
+ const cached = tryReadCache(getContentCachePath((0, node_fs.readFileSync)(mtimeIndexPath, "utf-8").trim()));
1144
+ if (cached) return cached;
1102
1145
  } catch {}
1146
+ const contentHash = computeContentHash((0, node_fs.readFileSync)(resolvedPath, "utf-8"));
1147
+ const contentCachePath = getContentCachePath(contentHash);
1148
+ const cached = tryReadCache(contentCachePath);
1149
+ if (cached) {
1150
+ try {
1151
+ (0, node_fs.mkdirSync)(CACHE_DIR, { recursive: true });
1152
+ (0, node_fs.writeFileSync)(mtimeIndexPath, contentHash);
1153
+ } catch {}
1154
+ return cached;
1155
+ }
1103
1156
  const stdout = (0, node_child_process.execFileSync)(process.execPath, ["-e", PRECOMPUTE_SCRIPT], {
1104
1157
  encoding: "utf-8",
1105
- timeout: 3e4,
1158
+ timeout: timeout ?? 3e4,
1106
1159
  maxBuffer: 50 * 1024 * 1024,
1107
1160
  env: {
1108
1161
  ...process.env,
@@ -1110,10 +1163,7 @@ function loadDesignSystemSync(cssPath) {
1110
1163
  },
1111
1164
  cwd: (0, node_path.dirname)(resolvedPath)
1112
1165
  });
1113
- try {
1114
- (0, node_fs.mkdirSync)(CACHE_DIR, { recursive: true });
1115
- (0, node_fs.writeFileSync)(cachePath, stdout);
1116
- } catch {}
1166
+ writeCacheFiles(contentCachePath, mtimeIndexPath, contentHash, stdout);
1117
1167
  return JSON.parse(stdout);
1118
1168
  } catch (error) {
1119
1169
  console.error(`[oxlint-tailwindcss] Failed to load design system from "${resolvedPath}":`, error instanceof Error ? error.message : error);
@@ -1122,6 +1172,44 @@ function loadDesignSystemSync(cssPath) {
1122
1172
  }
1123
1173
  //#endregion
1124
1174
  //#region src/design-system/auto-detect.ts
1175
+ const IMPORT_RE = /@import\s+['"]([^'"]+)['"]/g;
1176
+ /**
1177
+ * Resolves a CSS @import specifier to an absolute path.
1178
+ * Handles relative paths and package imports (walks up to node_modules).
1179
+ */
1180
+ function resolveImport(specifier, baseDir) {
1181
+ if (specifier.startsWith(".")) return (0, node_path.resolve)(baseDir, specifier);
1182
+ let dir = baseDir;
1183
+ while (true) {
1184
+ const candidate = (0, node_path.join)(dir, "node_modules", specifier);
1185
+ if ((0, node_fs.existsSync)(candidate)) return candidate;
1186
+ const parent = (0, node_path.dirname)(dir);
1187
+ if (parent === dir) break;
1188
+ dir = parent;
1189
+ }
1190
+ return null;
1191
+ }
1192
+ /**
1193
+ * Checks if any @import in the CSS content points to a file containing a Tailwind signal.
1194
+ * Only follows ONE level of imports — no recursion.
1195
+ */
1196
+ function hasTailwindSignalInImports(content, baseDir, signals) {
1197
+ IMPORT_RE.lastIndex = 0;
1198
+ let match;
1199
+ while ((match = IMPORT_RE.exec(content)) !== null) {
1200
+ const specifier = match[1];
1201
+ if (specifier === "tailwindcss") continue;
1202
+ const resolved = resolveImport(specifier, baseDir);
1203
+ if (!resolved) continue;
1204
+ try {
1205
+ const imported = (0, node_fs.readFileSync)(resolved, "utf-8");
1206
+ if (signals.some((signal) => imported.includes(signal))) return true;
1207
+ } catch {
1208
+ continue;
1209
+ }
1210
+ }
1211
+ return false;
1212
+ }
1125
1213
  const CANDIDATE_DIRS = [
1126
1214
  "src",
1127
1215
  ".",
@@ -1164,6 +1252,7 @@ function autoDetectEntryPoint(filePath) {
1164
1252
  try {
1165
1253
  const content = (0, node_fs.readFileSync)(fullPath, "utf-8");
1166
1254
  if (TAILWIND_SIGNALS.some((signal) => content.includes(signal))) return fullPath;
1255
+ if (hasTailwindSignalInImports(content, (0, node_path.dirname)(fullPath), TAILWIND_SIGNALS)) return fullPath;
1167
1256
  } catch {
1168
1257
  continue;
1169
1258
  }
@@ -1177,6 +1266,7 @@ function autoDetectEntryPoint(filePath) {
1177
1266
  try {
1178
1267
  const content = (0, node_fs.readFileSync)(fullPath, "utf-8");
1179
1268
  if (TAILWIND_SIGNALS.some((signal) => content.includes(signal))) return fullPath;
1269
+ if (hasTailwindSignalInImports(content, (0, node_path.dirname)(fullPath), TAILWIND_SIGNALS)) return fullPath;
1180
1270
  } catch {
1181
1271
  continue;
1182
1272
  }
@@ -1189,7 +1279,9 @@ function autoDetectEntryPoint(filePath) {
1189
1279
  }
1190
1280
  //#endregion
1191
1281
  //#region src/design-system/loader.ts
1192
- let singleton = null;
1282
+ const dsCache = /* @__PURE__ */ new Map();
1283
+ const autoDetectCache = /* @__PURE__ */ new Map();
1284
+ let lastLoadedPath = null;
1193
1285
  /**
1194
1286
  * Extracts `entryPoint` from `context.settings.tailwindcss`.
1195
1287
  */
@@ -1201,30 +1293,57 @@ function entryPointFromSettings(settings) {
1201
1293
  }
1202
1294
  }
1203
1295
  /**
1296
+ * Extracts `timeout` from `context.settings.tailwindcss`.
1297
+ */
1298
+ function timeoutFromSettings(settings) {
1299
+ const tw = settings?.tailwindcss;
1300
+ if (tw && typeof tw === "object" && "timeout" in tw) {
1301
+ const t = tw.timeout;
1302
+ if (typeof t === "number" && t > 0) return t;
1303
+ }
1304
+ }
1305
+ /**
1306
+ * Cached auto-detect: caches by directory since all files in the same directory
1307
+ * resolve to the same entry point. Avoids repeated filesystem walks.
1308
+ */
1309
+ function cachedAutoDetect(filePath) {
1310
+ if (!filePath) return autoDetectEntryPoint(filePath);
1311
+ const dir = (0, node_path.dirname)((0, node_path.resolve)(filePath));
1312
+ const cached = autoDetectCache.get(dir);
1313
+ if (cached !== void 0) return cached;
1314
+ const result = autoDetectEntryPoint(filePath);
1315
+ autoDetectCache.set(dir, result);
1316
+ return result;
1317
+ }
1318
+ /**
1204
1319
  * Returns the design system cache, loading synchronously on first call.
1205
1320
  * Uses execFileSync internally to bridge the async Tailwind API.
1206
- * All rules receive the SAME instance.
1321
+ *
1322
+ * Supports multiple design systems: each unique CSS entry point gets its own cache.
1323
+ * In monorepos, files in different packages auto-detect to different entry points
1324
+ * and each gets the correct DS.
1207
1325
  *
1208
1326
  * Resolution order: rule option `entryPoint` > `settings.tailwindcss.entryPoint` > auto-detect.
1209
1327
  */
1210
1328
  function getLoadedDesignSystem(entryPoint, settings, filePath) {
1211
- const cssPath = entryPoint ?? entryPointFromSettings(settings) ?? singleton?.path ?? autoDetectEntryPoint(filePath);
1329
+ const cssPath = entryPoint ?? entryPointFromSettings(settings) ?? cachedAutoDetect(filePath) ?? lastLoadedPath;
1212
1330
  if (!cssPath) return null;
1213
1331
  const resolvedPath = (0, node_path.resolve)(cssPath);
1214
1332
  try {
1215
1333
  const mtime = (0, node_fs.statSync)(resolvedPath).mtimeMs;
1216
- if (singleton !== null && singleton.path === resolvedPath && singleton.mtime === mtime) return {
1217
- cache: singleton.cache,
1334
+ const cached = dsCache.get(resolvedPath);
1335
+ if (cached && cached.mtime === mtime) return {
1336
+ cache: cached.cache,
1218
1337
  entryPoint: resolvedPath
1219
1338
  };
1220
- const data = loadDesignSystemSync(resolvedPath);
1339
+ const data = loadDesignSystemSync(resolvedPath, timeoutFromSettings(settings));
1221
1340
  if (!data) return null;
1222
1341
  const cache = DesignSystemCache.fromPrecomputed(data);
1223
- singleton = {
1342
+ dsCache.set(resolvedPath, {
1224
1343
  cache,
1225
- path: resolvedPath,
1226
1344
  mtime
1227
- };
1345
+ });
1346
+ lastLoadedPath = resolvedPath;
1228
1347
  return {
1229
1348
  cache,
1230
1349
  entryPoint: resolvedPath
@@ -1234,17 +1353,21 @@ function getLoadedDesignSystem(entryPoint, settings, filePath) {
1234
1353
  }
1235
1354
  }
1236
1355
  /**
1237
- * Creates a lazy DS loader that retries with more context as it becomes available.
1356
+ * Creates a lazy DS loader that resolves the correct design system per file.
1238
1357
  *
1239
1358
  * In `createOnce`, `context.settings` and `context.filename` throw. When visitors
1240
- * run, they become available. This function retries until it has the file path
1241
- * (meaning we're in a visitor with full context), then gives up.
1359
+ * run, they become available. The loader re-resolves when the filename changes,
1360
+ * supporting monorepos where different files need different design systems.
1361
+ *
1362
+ * When a fixed entryPoint is provided (rule option or settings), it's used for all
1363
+ * files and cached after first resolution.
1242
1364
  */
1243
1365
  function createLazyLoader(context) {
1244
- let result = null;
1245
1366
  let lastFilePath;
1367
+ let lastResult = null;
1368
+ let fixedEntryResolved = false;
1369
+ let fixedResult = null;
1246
1370
  return () => {
1247
- if (result) return result;
1248
1371
  let entryPoint;
1249
1372
  try {
1250
1373
  entryPoint = (context.options?.[0])?.entryPoint;
@@ -1257,10 +1380,17 @@ function createLazyLoader(context) {
1257
1380
  try {
1258
1381
  filePath = context.filename;
1259
1382
  } catch {}
1260
- if (filePath && filePath === lastFilePath) return null;
1383
+ const fixedEntry = entryPoint ?? entryPointFromSettings(settings);
1384
+ if (fixedEntry) {
1385
+ if (fixedEntryResolved) return fixedResult;
1386
+ fixedEntryResolved = true;
1387
+ fixedResult = getLoadedDesignSystem(fixedEntry, settings, filePath);
1388
+ return fixedResult;
1389
+ }
1390
+ if (filePath && filePath === lastFilePath) return lastResult;
1261
1391
  if (filePath) lastFilePath = filePath;
1262
- result = getLoadedDesignSystem(entryPoint, settings, filePath);
1263
- return result;
1392
+ lastResult = getLoadedDesignSystem(void 0, settings, filePath);
1393
+ return lastResult;
1264
1394
  };
1265
1395
  }
1266
1396
  //#endregion
@@ -1689,9 +1819,19 @@ let lengthView = null;
1689
1819
  let dataArea = null;
1690
1820
  let initialized = false;
1691
1821
  let available = true;
1822
+ let currentCssPath = null;
1692
1823
  function ensureService(cssPath) {
1693
- if (initialized) return available;
1824
+ if (initialized && currentCssPath === cssPath) return available;
1825
+ if (initialized) {
1826
+ cleanup();
1827
+ initialized = false;
1828
+ available = true;
1829
+ controlArray = null;
1830
+ lengthView = null;
1831
+ dataArea = null;
1832
+ }
1694
1833
  initialized = true;
1834
+ currentCssPath = cssPath;
1695
1835
  try {
1696
1836
  const tailwindNodePath = require.resolve("@tailwindcss/node");
1697
1837
  const sharedBuffer = new SharedArrayBuffer(BUFFER_SIZE);