oxlint-tailwindcss 1.0.1 → 1.2.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/dist/index.cjs CHANGED
@@ -718,6 +718,8 @@ var DesignSystemCache = class DesignSystemCache {
718
718
  _validClasses = [];
719
719
  _knownPrefixes = null;
720
720
  _maxOrder = 0n;
721
+ _prefix = "";
722
+ componentSet = /* @__PURE__ */ new Set();
721
723
  static fromPrecomputed(data) {
722
724
  const cache = new DesignSystemCache();
723
725
  cache._validClasses = data.validClasses;
@@ -730,7 +732,11 @@ var DesignSystemCache = class DesignSystemCache {
730
732
  }
731
733
  for (const [cls, props] of Object.entries(data.cssProps)) cache.cssPropsMap.set(cls, props);
732
734
  if (data.variantOrder) for (const [name, index] of Object.entries(data.variantOrder)) cache.variantOrderMap.set(name, index);
733
- if (data.componentClasses) for (const cls of data.componentClasses) cache.validitySet.add(cls);
735
+ if (data.componentClasses) for (const cls of data.componentClasses) {
736
+ cache.validitySet.add(cls);
737
+ cache.componentSet.add(cls);
738
+ }
739
+ cache._prefix = data.prefix ?? "";
734
740
  if (data.arbitraryEquivalents) for (const [arb, named] of Object.entries(data.arbitraryEquivalents)) cache.arbitraryEquivMap.set(arb, named);
735
741
  return cache;
736
742
  }
@@ -740,6 +746,10 @@ var DesignSystemCache = class DesignSystemCache {
740
746
  get maxOrder() {
741
747
  return this._maxOrder;
742
748
  }
749
+ /** Tailwind v4 project prefix (e.g. 'tw'); '' when none is configured. */
750
+ get prefix() {
751
+ return this._prefix;
752
+ }
743
753
  canonicalize(className) {
744
754
  const cached = this.canonicalMap.get(className);
745
755
  if (cached !== void 0) return cached;
@@ -838,7 +848,32 @@ var DesignSystemCache = class DesignSystemCache {
838
848
  if (bare.includes("(") && bare.includes(")")) return true;
839
849
  return false;
840
850
  }
851
+ /**
852
+ * Strict validity for `no-unknown-classes`, prefix-aware.
853
+ *
854
+ * - `'valid'` — usable as written.
855
+ * - `'missing-prefix'`— a real Tailwind utility written WITHOUT the required
856
+ * project prefix (`flex` under `prefix(tw)`); generates no CSS.
857
+ * - `'unknown'` — not a recognized class.
858
+ *
859
+ * When no prefix is configured this collapses to the tolerant `isValid`
860
+ * behavior. `isValid` itself stays tolerant (other rules call it with
861
+ * prefix-free candidates), so the strict check lives here.
862
+ */
863
+ classValidity(className) {
864
+ if (!this._prefix) return this.isValid(className) ? "valid" : "unknown";
865
+ const tag = this._prefix + ":";
866
+ const hadPrefix = className.startsWith(tag);
867
+ const body = hadPrefix ? className.slice(tag.length) : className;
868
+ let baseUtility = splitImportant(extractUtility(body)).bare;
869
+ const slashIdx = baseUtility.lastIndexOf("/");
870
+ if (slashIdx > 0) baseUtility = baseUtility.slice(0, slashIdx);
871
+ if (this.componentSet.has(baseUtility)) return "valid";
872
+ if (this.isValid(body)) return hadPrefix ? "valid" : "missing-prefix";
873
+ return "unknown";
874
+ }
841
875
  getOrder(className) {
876
+ if (this._prefix && className.startsWith(this._prefix + ":")) className = className.slice(this._prefix.length + 1);
842
877
  const cached = this.orderMap.get(className);
843
878
  if (cached !== void 0) return cached;
844
879
  const utility = extractUtility(className);
@@ -1169,6 +1204,16 @@ async function main() {
1169
1204
  const base = dirname(cssPath);
1170
1205
  const ds = await __unstable__loadDesignSystem(css, { base });
1171
1206
 
1207
+ // Tailwind v4 project prefix (\`@import "tailwindcss" prefix(tw)\`). getClassList()
1208
+ // returns names WITHOUT the prefix, but candidatesToCss/getClassOrder/
1209
+ // canonicalizeCandidates only resolve the PREFIXED form (\`tw:flex\`). We apply
1210
+ // the prefix only when calling the DS and strip it back off before storing, so
1211
+ // every structure stays prefix-free. The prefix is alphanumeric (no ':' '-' '['
1212
+ // ']' '/'), so \`prefix + ':'\` is an unambiguous separator.
1213
+ const prefix = (ds.theme && ds.theme.prefix) || '';
1214
+ const pfx = (c) => prefix ? prefix + ':' + c : c;
1215
+ const unpfx = (c) => (prefix && c.startsWith(prefix + ':')) ? c.slice(prefix.length + 1) : c;
1216
+
1172
1217
  const entries = ds.getClassList();
1173
1218
  const classNames = entries.map(e => e[0]);
1174
1219
 
@@ -1176,8 +1221,10 @@ async function main() {
1176
1221
  const classNameIndex = new Map();
1177
1222
  for (let i = 0; i < classNames.length; i++) classNameIndex.set(classNames[i], i);
1178
1223
 
1179
- // Validity: which classes produce CSS
1180
- const cssResults = ds.candidatesToCss(classNames);
1224
+ // Validity: which classes produce CSS. Validate the prefixed form, but keep
1225
+ // cssResults aligned positionally to the unprefixed classNames so the later
1226
+ // cssProps / arbitraryEquivalents phases keep indexing by position.
1227
+ const cssResults = ds.candidatesToCss(classNames.map(pfx));
1181
1228
  const validClasses = classNames.filter((_, i) => cssResults[i] != null);
1182
1229
 
1183
1230
  // Expand: validate extra candidates not in getClassList() but valid in v4
@@ -1199,7 +1246,7 @@ async function main() {
1199
1246
  }
1200
1247
  }
1201
1248
  if (extraCandidates.length > 0) {
1202
- const extraResults = ds.candidatesToCss(extraCandidates);
1249
+ const extraResults = ds.candidatesToCss(extraCandidates.map(pfx));
1203
1250
  for (let i = 0; i < extraCandidates.length; i++) {
1204
1251
  if (extraResults[i] != null) {
1205
1252
  validClasses.push(extraCandidates[i]);
@@ -1228,9 +1275,11 @@ async function main() {
1228
1275
  // NOTE: canonicalizeCandidates deduplicates, so we must call it one class at a time
1229
1276
  const canonical = {};
1230
1277
  for (const cls of classNames) {
1231
- const result = ds.canonicalizeCandidates([cls]);
1232
- if (result[0] && result[0] !== cls) {
1233
- canonical[cls] = result[0];
1278
+ const result = ds.canonicalizeCandidates([pfx(cls)]);
1279
+ const canon = result[0] ? unpfx(result[0]) : null;
1280
+ // Compare unprefixed-vs-unprefixed so the prefix itself never reads as a change.
1281
+ if (canon && canon !== cls) {
1282
+ canonical[cls] = canon;
1234
1283
  }
1235
1284
  }
1236
1285
 
@@ -1261,13 +1310,14 @@ async function main() {
1261
1310
  }
1262
1311
  const legacyToProcess = legacyCandidates.filter(cls => !validSet.has(cls));
1263
1312
  if (legacyToProcess.length > 0) {
1264
- const legacyCssResults = ds.candidatesToCss(legacyToProcess);
1313
+ const legacyCssResults = ds.candidatesToCss(legacyToProcess.map(pfx));
1265
1314
  for (let i = 0; i < legacyToProcess.length; i++) {
1266
1315
  if (legacyCssResults[i] == null) continue;
1267
1316
  const cls = legacyToProcess[i];
1268
- const result = ds.canonicalizeCandidates([cls]);
1269
- if (result[0] && result[0] !== cls) {
1270
- canonical[cls] = result[0];
1317
+ const result = ds.canonicalizeCandidates([pfx(cls)]);
1318
+ const canon = result[0] ? unpfx(result[0]) : null;
1319
+ if (canon && canon !== cls) {
1320
+ canonical[cls] = canon;
1271
1321
  }
1272
1322
  // Mark as valid so no-unknown-classes doesn't flag legacy spellings.
1273
1323
  validClasses.push(cls);
@@ -1281,9 +1331,9 @@ async function main() {
1281
1331
  if (!classNameIndex.has(cls)) allForOrder.push(cls);
1282
1332
  }
1283
1333
  const order = {};
1284
- const orderResults = ds.getClassOrder(allForOrder);
1334
+ const orderResults = ds.getClassOrder(allForOrder.map(pfx));
1285
1335
  for (const [name, val] of orderResults) {
1286
- if (val !== null) order[name] = val.toString();
1336
+ if (val !== null) order[unpfx(name)] = val.toString();
1287
1337
  }
1288
1338
 
1289
1339
  // CSS properties per class — extract only from the ROOT selector, not descendant selectors.
@@ -1372,7 +1422,9 @@ async function main() {
1372
1422
 
1373
1423
  for (let i = 0; i < classNames.length; i++) {
1374
1424
  if (cssResults[i]) {
1375
- const props = extractRootCssProps(cssResults[i], classNames[i]);
1425
+ // Match against the prefixed selector (the CSS emits \`.tw\\:flex\`), but
1426
+ // store under the unprefixed key. extractRootCssProps CSS-escapes the ':'.
1427
+ const props = extractRootCssProps(cssResults[i], pfx(classNames[i]));
1376
1428
  if (props.length > 0) cssProps[classNames[i]] = props;
1377
1429
  }
1378
1430
  }
@@ -1398,7 +1450,8 @@ async function main() {
1398
1450
  let acm;
1399
1451
  attrClassRe.lastIndex = 0;
1400
1452
  while ((acm = attrClassRe.exec(cssResults[i])) !== null) {
1401
- componentClasses.push(acm[1]);
1453
+ // Typography-style plugins emit \`[class~="tw:not-prose"]\` under a prefix.
1454
+ componentClasses.push(unpfx(acm[1]));
1402
1455
  }
1403
1456
  }
1404
1457
  }
@@ -1433,7 +1486,10 @@ async function main() {
1433
1486
  }
1434
1487
  if (candidates.length > 0) {
1435
1488
  const arbForms = candidates.map(c => c.arbitraryForm);
1436
- const arbResults = ds.candidatesToCss(arbForms);
1489
+ // Prefix only the validation; keys/values stored stay prefix-free. The
1490
+ // declaration block extractDeclarations compares is identical with or
1491
+ // without the prefix (the prefix only affects the selector).
1492
+ const arbResults = ds.candidatesToCss(arbForms.map(pfx));
1437
1493
  for (let i = 0; i < candidates.length; i++) {
1438
1494
  if (!arbResults[i]) continue;
1439
1495
  if (extractDeclarations(arbResults[i]) === extractDeclarations(candidates[i].namedCss)) {
@@ -1442,7 +1498,7 @@ async function main() {
1442
1498
  }
1443
1499
  }
1444
1500
 
1445
- const json = JSON.stringify({ validClasses, canonical, order, cssProps, variantOrder, componentClasses, arbitraryEquivalents });
1501
+ const json = JSON.stringify({ validClasses, canonical, order, cssProps, variantOrder, componentClasses, arbitraryEquivalents, prefix });
1446
1502
  // Atomic write: write to a unique temp path then rename, so a peer isolate
1447
1503
  // busy-waiting on the cache file never observes a half-written JSON.
1448
1504
  writeFileSync(WD_TMP_PATH, json);
@@ -2104,6 +2160,7 @@ const noUnknownClasses = (0, _oxlint_plugins.defineRule)({
2104
2160
  messages: {
2105
2161
  unknown: "\"{{className}}\" is not a valid Tailwind class.",
2106
2162
  unknownWithSuggestion: "\"{{className}}\" is not a valid Tailwind class. Did you mean \"{{suggestion}}\"?",
2163
+ missingPrefix: "\"{{className}}\" needs the \"{{prefix}}:\" prefix to produce CSS. Did you mean \"{{suggestion}}\"?",
2107
2164
  suggestReplace: "Replace \"{{className}}\" with \"{{replacement}}\".",
2108
2165
  ...DS_UNAVAILABLE_MESSAGE
2109
2166
  }
@@ -2132,9 +2189,34 @@ const noUnknownClasses = (0, _oxlint_plugins.defineRule)({
2132
2189
  const classes = split.classes;
2133
2190
  for (const cls of classes) {
2134
2191
  if (shouldIgnore(cls)) continue;
2192
+ const validity = cache.classValidity(cls);
2193
+ if (validity === "valid") continue;
2135
2194
  const stripped = stripModifiers(cls);
2136
- if (cache.isValid(stripped)) continue;
2137
2195
  if (DEPRECATED_MAP[stripped]) continue;
2196
+ if (validity === "missing-prefix") {
2197
+ const fixed = `${cache.prefix}:${cls}`;
2198
+ const fixedValue = rebuildClassString(split, classes.map((c) => c === cls ? fixed : c));
2199
+ context.report({
2200
+ node: loc.node,
2201
+ messageId: "missingPrefix",
2202
+ data: {
2203
+ className: cls,
2204
+ prefix: cache.prefix,
2205
+ suggestion: fixed
2206
+ },
2207
+ suggest: [{
2208
+ messageId: "suggestReplace",
2209
+ data: {
2210
+ className: cls,
2211
+ replacement: fixed
2212
+ },
2213
+ fix(fixer) {
2214
+ return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
2215
+ }
2216
+ }]
2217
+ });
2218
+ continue;
2219
+ }
2138
2220
  const suggestion = findBestSuggestion(stripped, cache.validClasses);
2139
2221
  if (suggestion) {
2140
2222
  const fixedValue = rebuildClassString(split, classes.map((c) => c === cls ? suggestion : c));
@@ -2826,7 +2908,10 @@ const enforceSortOrder = (0, _oxlint_plugins.defineRule)({
2826
2908
  groupClasses.length = 0;
2827
2909
  for (const [name] of ordered) groupClasses.push(name);
2828
2910
  }
2829
- const sortedGroupKeys = [...groups.keys()].sort((a, b) => {
2911
+ const stripKey = (k) => cache.prefix && k.startsWith(cache.prefix + ":") ? k.slice(cache.prefix.length + 1) : k;
2912
+ const sortedGroupKeys = [...groups.keys()].sort((ka, kb) => {
2913
+ const a = stripKey(ka);
2914
+ const b = stripKey(kb);
2830
2915
  if (a === "" && b !== "") return -1;
2831
2916
  if (a !== "" && b === "") return 1;
2832
2917
  if (a === "" && b === "") return 0;
@@ -2861,7 +2946,7 @@ const enforceSortOrder = (0, _oxlint_plugins.defineRule)({
2861
2946
  });
2862
2947
  //#endregion
2863
2948
  //#region src/rules/enforce-shorthand.ts
2864
- const VALUE_RE = /^(?:m[trbl]|p[trbl]|[wh]|rounded-t[lr]|rounded-b[lr]|min-[wh]|max-[wh])-(.+)$/;
2949
+ const VALUE_RE = /^(?:m[trbl]|mx|my|p[trbl]|px|py|[wh]|rounded-t[lr]|rounded-b[lr]|min-[wh]|max-[wh])-(.+)$/;
2865
2950
  const INVALID_SIZE_VALUES = new Set([
2866
2951
  "screen",
2867
2952
  "dvw",
@@ -2894,6 +2979,10 @@ function createShorthandRules(value) {
2894
2979
  parts: [`ml-${value}`, `mr-${value}`],
2895
2980
  replacement: `mx-${value}`
2896
2981
  },
2982
+ {
2983
+ parts: [`mx-${value}`, `my-${value}`],
2984
+ replacement: `m-${value}`
2985
+ },
2897
2986
  {
2898
2987
  parts: [
2899
2988
  `pt-${value}`,
@@ -2911,6 +3000,10 @@ function createShorthandRules(value) {
2911
3000
  parts: [`pl-${value}`, `pr-${value}`],
2912
3001
  replacement: `px-${value}`
2913
3002
  },
3003
+ {
3004
+ parts: [`px-${value}`, `py-${value}`],
3005
+ replacement: `p-${value}`
3006
+ },
2914
3007
  ...!INVALID_SIZE_VALUES.has(value) ? [{
2915
3008
  parts: [`w-${value}`, `h-${value}`],
2916
3009
  replacement: `size-${value}`
@@ -2941,29 +3034,33 @@ const enforceShorthand = (0, _oxlint_plugins.defineRule)({
2941
3034
  const classes = split.classes;
2942
3035
  if (classes.length < 2) continue;
2943
3036
  const classSet = new Set(classes);
2944
- const values = /* @__PURE__ */ new Set();
3037
+ const variantValues = /* @__PURE__ */ new Set();
2945
3038
  for (const cls of classes) {
2946
- const match = VALUE_RE.exec(splitImportant(cls).bare);
2947
- if (match) values.add(match[1]);
3039
+ const { utility, variant } = splitUtilityAndVariant(cls);
3040
+ const match = VALUE_RE.exec(splitImportant(utility).bare);
3041
+ if (match) variantValues.add(`${variant}\0${match[1]}`);
2948
3042
  }
2949
- for (const value of values) {
2950
- const rules = createShorthandRules(value);
3043
+ for (const variantValue of variantValues) {
3044
+ const nul = variantValue.indexOf("\0");
3045
+ const variant = variantValue.slice(0, nul);
3046
+ const rules = createShorthandRules(variantValue.slice(nul + 1));
2951
3047
  for (const rule of rules) {
2952
- const hasImportantPrefix = rule.parts.every((p) => classSet.has(`!${p}`));
2953
- const hasImportantSuffix = !hasImportantPrefix && rule.parts.every((p) => classSet.has(`${p}!`));
2954
- const hasPlain = rule.parts.every((p) => classSet.has(p));
3048
+ const withVariant = (p) => `${variant}${p}`;
3049
+ const hasImportantPrefix = rule.parts.every((p) => classSet.has(withVariant(`!${p}`)));
3050
+ const hasImportantSuffix = !hasImportantPrefix && rule.parts.every((p) => classSet.has(withVariant(`${p}!`)));
3051
+ const hasPlain = rule.parts.every((p) => classSet.has(withVariant(p)));
2955
3052
  if (!hasImportantPrefix && !hasImportantSuffix && !hasPlain) continue;
2956
3053
  const importantStart = hasImportantPrefix ? "!" : "";
2957
3054
  const importantEnd = hasImportantSuffix ? "!" : "";
2958
- const matchParts = rule.parts.map((p) => `${importantStart}${p}${importantEnd}`);
3055
+ const matchParts = rule.parts.map((p) => withVariant(`${importantStart}${p}${importantEnd}`));
2959
3056
  const remaining = classes.filter((c) => !matchParts.includes(c));
2960
- remaining.push(`${importantStart}${rule.replacement}${importantEnd}`);
3057
+ remaining.push(withVariant(`${importantStart}${rule.replacement}${importantEnd}`));
2961
3058
  context.report({
2962
3059
  node: loc.node,
2963
3060
  messageId: "shorthand",
2964
3061
  data: {
2965
3062
  parts: matchParts.map((p) => `"${p}"`).join(", "),
2966
- replacement: `${importantStart}${rule.replacement}${importantEnd}`
3063
+ replacement: withVariant(`${importantStart}${rule.replacement}${importantEnd}`)
2967
3064
  },
2968
3065
  fix(fixer) {
2969
3066
  return fixer.replaceTextRange(loc.range, preserveSpaces(loc, rebuildClassString(split, remaining)));
@@ -3896,8 +3993,23 @@ const consistentVariantOrder = (0, _oxlint_plugins.defineRule)({
3896
3993
  const fallback = DEFAULT_VARIANT_ORDER.length;
3897
3994
  return (v) => map.get(v) ?? fallback;
3898
3995
  });
3996
+ const getPrefix = createLazyOptions(context, () => {
3997
+ try {
3998
+ return getDS().cache.prefix;
3999
+ } catch (err) {
4000
+ if (!isFatalError(err)) throw err;
4001
+ return "";
4002
+ }
4003
+ });
3899
4004
  function reorderClass(cls) {
3900
- const variants = extractVariants(cls);
4005
+ const prefix = getPrefix();
4006
+ let pfx = "";
4007
+ let body = cls;
4008
+ if (prefix && cls.startsWith(prefix + ":")) {
4009
+ pfx = prefix + ":";
4010
+ body = cls.slice(prefix.length + 1);
4011
+ }
4012
+ const variants = extractVariants(body);
3901
4013
  if (variants.length < 2) return null;
3902
4014
  const priorityOf = getPriority();
3903
4015
  const sorted = [...variants].sort((a, b) => priorityOf(a) - priorityOf(b));
@@ -3907,8 +4019,8 @@ const consistentVariantOrder = (0, _oxlint_plugins.defineRule)({
3907
4019
  else nonPseudo.push(v);
3908
4020
  const final = nonPseudo.length > 0 && pseudo.length > 0 ? [...nonPseudo, ...pseudo] : sorted;
3909
4021
  if (variants.every((v, i) => v === final[i])) return null;
3910
- const utility = extractUtility(cls);
3911
- return final.join(":") + ":" + utility;
4022
+ const utility = extractUtility(body);
4023
+ return pfx + final.join(":") + ":" + utility;
3912
4024
  }
3913
4025
  function check(locations) {
3914
4026
  for (const loc of locations) {