oxlint-tailwindcss 0.6.2 → 0.6.3

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
@@ -554,6 +554,20 @@ function hasArbitraryValue(cls) {
554
554
  return utility.includes("[") && utility.includes("]");
555
555
  }
556
556
  /**
557
+ * Checks if a class's utility part contains dynamic value syntax: either
558
+ * arbitrary brackets (`w-[200px]`) or CSS variable shorthand (`bg-(--color)`).
559
+ *
560
+ * Unlike `hasArbitraryValue`, this also catches `(--var)` shorthand — both
561
+ * forms are what require the async Tailwind DS to canonicalize, because
562
+ * their value space is unbounded. Simple named classes (`flex`, `px-4`,
563
+ * even with variants like `[&>svg]:hover:flex`) can be canonicalized from
564
+ * the precomputed cache without touching the worker.
565
+ */
566
+ function utilityHasDynamicValue(cls) {
567
+ const utility = extractUtility(cls);
568
+ return utility.includes("[") || utility.includes("(");
569
+ }
570
+ /**
557
571
  * Gets the arbitrary value from a class, or null if none.
558
572
  * e.g. "w-[200px]" → "200px"
559
573
  * "bg-[#ff0000]" → "#ff0000"
@@ -1806,12 +1820,24 @@ const noConflictingClasses = (0, _oxlint_plugins.defineRule)({
1806
1820
  /**
1807
1821
  * Persistent canonicalize service using worker_threads + SharedArrayBuffer.
1808
1822
  *
1809
- * Calls `ds.canonicalizeCandidates(classes, { rem })` dynamically via a worker
1823
+ * Calls `ds.canonicalizeCandidates([cls], { rem })` dynamically via a worker
1810
1824
  * thread. Same pattern as sort-service.ts — loads the DS once, then accepts
1811
1825
  * requests synchronously via Atomics.wait().
1812
1826
  *
1813
1827
  * This enables canonicalization of arbitrary user classes (e.g. `p-[2px]` → `p-0.5`)
1814
1828
  * which can't be precomputed because the input space is infinite.
1829
+ *
1830
+ * ## Per-class cache (process-wide)
1831
+ *
1832
+ * Results are cached by `cssPath\0rem\0class`. The worker is invoked only for
1833
+ * cache misses. In practice the cache converges quickly — after the first few
1834
+ * files any given class has been seen, and every subsequent lookup is O(1).
1835
+ *
1836
+ * ## Per-class worker calls
1837
+ *
1838
+ * `ds.canonicalizeCandidates` deduplicates its input (see sync-loader.ts note),
1839
+ * so the worker MUST call it one class at a time to preserve input order/length.
1840
+ * The cache amortizes the per-call overhead.
1815
1841
  */
1816
1842
  const BUFFER_SIZE$1 = 4 * 1024 * 1024;
1817
1843
  const HEADER_INTS$1 = 4;
@@ -1856,7 +1882,12 @@ async function main() {
1856
1882
  try {
1857
1883
  const { classes, rem } = JSON.parse(requestStr);
1858
1884
  const options = rem ? { rem } : undefined;
1859
- const result = ds.canonicalizeCandidates(classes, options);
1885
+ // canonicalizeCandidates deduplicates its input, so we must call it
1886
+ // one class at a time to preserve order/length. See sync-loader.ts.
1887
+ const result = classes.map((cls) => {
1888
+ const r = ds.canonicalizeCandidates([cls], options);
1889
+ return r[0] ?? cls;
1890
+ });
1860
1891
  response = Buffer.from(JSON.stringify(result), 'utf-8');
1861
1892
  } catch {
1862
1893
  response = Buffer.from('null', 'utf-8');
@@ -1878,6 +1909,7 @@ let dataArea$1 = null;
1878
1909
  let initialized$1 = false;
1879
1910
  let available$1 = true;
1880
1911
  let currentCssPath$1 = null;
1912
+ const canonCache = /* @__PURE__ */ new Map();
1881
1913
  function ensureService$1(cssPath) {
1882
1914
  if (initialized$1 && currentCssPath$1 === cssPath) return available$1;
1883
1915
  if (initialized$1) {
@@ -1933,11 +1965,11 @@ function cleanup$1() {
1933
1965
  }
1934
1966
  }
1935
1967
  /**
1936
- * Canonicalize classes using the Tailwind CSS design system via worker thread.
1937
- * Returns the canonicalized class array, or null if the service is unavailable.
1968
+ * Round-trip raw worker call for a batch of classes (no caching).
1969
+ * Returns the canonicalized class array (same length/order as input),
1970
+ * or null if the service is unavailable.
1938
1971
  */
1939
- function canonicalizeClassesSync(cssPath, classes, rem) {
1940
- if (!ensureService$1(cssPath)) return null;
1972
+ function callWorker(classes, rem) {
1941
1973
  if (!controlArray$1 || !dataArea$1 || !lengthView$1) return null;
1942
1974
  try {
1943
1975
  const request = Buffer.from(JSON.stringify({
@@ -1964,6 +1996,43 @@ function canonicalizeClassesSync(cssPath, classes, rem) {
1964
1996
  return null;
1965
1997
  }
1966
1998
  }
1999
+ /**
2000
+ * Canonicalize classes using the Tailwind CSS design system via worker thread.
2001
+ * Returns the canonicalized class array (same length/order as input), or null
2002
+ * if the service is unavailable.
2003
+ *
2004
+ * Uses a process-wide per-class cache: the worker is invoked only for classes
2005
+ * not already seen with this (cssPath, rem) combination.
2006
+ */
2007
+ function canonicalizeClassesSync(cssPath, classes, rem) {
2008
+ if (!ensureService$1(cssPath)) return null;
2009
+ const out = Array.from({ length: classes.length });
2010
+ const missingIdx = [];
2011
+ const missing = [];
2012
+ const cachePrefix = `${cssPath}\0${rem ?? ""}\0`;
2013
+ for (let i = 0; i < classes.length; i++) {
2014
+ const key = cachePrefix + classes[i];
2015
+ const hit = canonCache.get(key);
2016
+ if (hit !== void 0) out[i] = hit;
2017
+ else {
2018
+ missingIdx.push(i);
2019
+ missing.push(classes[i]);
2020
+ }
2021
+ }
2022
+ if (missing.length === 0) return out;
2023
+ const uniqueMissing = [...new Set(missing)];
2024
+ const fresh = callWorker(uniqueMissing, rem);
2025
+ if (!fresh || fresh.length !== uniqueMissing.length) return null;
2026
+ const freshByClass = /* @__PURE__ */ new Map();
2027
+ for (let k = 0; k < uniqueMissing.length; k++) freshByClass.set(uniqueMissing[k], fresh[k]);
2028
+ for (let j = 0; j < missing.length; j++) {
2029
+ const cls = missing[j];
2030
+ const value = freshByClass.get(cls) ?? cls;
2031
+ canonCache.set(cachePrefix + cls, value);
2032
+ out[missingIdx[j]] = value;
2033
+ }
2034
+ return out;
2035
+ }
1967
2036
  //#endregion
1968
2037
  //#region src/rules/enforce-canonical.ts
1969
2038
  /**
@@ -2011,10 +2080,18 @@ const enforceCanonical = (0, _oxlint_plugins.defineRule)({
2011
2080
  const { cache, entryPoint } = ds;
2012
2081
  for (const loc of locations) {
2013
2082
  const classes = splitClasses(loc.value);
2014
- const dynamic = canonicalizeClassesSync(entryPoint, classes, getRem());
2015
- let canonicals;
2016
- if (dynamic) canonicals = classes.map((cls, i) => preserveImportantPosition(cls, dynamic[i]));
2017
- else canonicals = classes.map((cls) => cache.canonicalize(cls));
2083
+ const canonicals = Array.from({ length: classes.length });
2084
+ const arbitraryIdx = [];
2085
+ const arbitrary = [];
2086
+ for (let i = 0; i < classes.length; i++) if (utilityHasDynamicValue(classes[i])) {
2087
+ arbitraryIdx.push(i);
2088
+ arbitrary.push(classes[i]);
2089
+ } else canonicals[i] = cache.canonicalize(classes[i]);
2090
+ if (arbitrary.length > 0) {
2091
+ const dynamic = canonicalizeClassesSync(entryPoint, arbitrary, getRem());
2092
+ if (dynamic) for (let k = 0; k < arbitrary.length; k++) canonicals[arbitraryIdx[k]] = preserveImportantPosition(arbitrary[k], dynamic[k]);
2093
+ else for (let k = 0; k < arbitrary.length; k++) canonicals[arbitraryIdx[k]] = cache.canonicalize(arbitrary[k]);
2094
+ }
2018
2095
  let firstNonCanonical = true;
2019
2096
  for (let i = 0; i < classes.length; i++) {
2020
2097
  const cls = classes[i];
package/dist/index.mjs CHANGED
@@ -558,6 +558,20 @@ function hasArbitraryValue(cls) {
558
558
  return utility.includes("[") && utility.includes("]");
559
559
  }
560
560
  /**
561
+ * Checks if a class's utility part contains dynamic value syntax: either
562
+ * arbitrary brackets (`w-[200px]`) or CSS variable shorthand (`bg-(--color)`).
563
+ *
564
+ * Unlike `hasArbitraryValue`, this also catches `(--var)` shorthand — both
565
+ * forms are what require the async Tailwind DS to canonicalize, because
566
+ * their value space is unbounded. Simple named classes (`flex`, `px-4`,
567
+ * even with variants like `[&>svg]:hover:flex`) can be canonicalized from
568
+ * the precomputed cache without touching the worker.
569
+ */
570
+ function utilityHasDynamicValue(cls) {
571
+ const utility = extractUtility(cls);
572
+ return utility.includes("[") || utility.includes("(");
573
+ }
574
+ /**
561
575
  * Gets the arbitrary value from a class, or null if none.
562
576
  * e.g. "w-[200px]" → "200px"
563
577
  * "bg-[#ff0000]" → "#ff0000"
@@ -1810,12 +1824,24 @@ const noConflictingClasses = defineRule({
1810
1824
  /**
1811
1825
  * Persistent canonicalize service using worker_threads + SharedArrayBuffer.
1812
1826
  *
1813
- * Calls `ds.canonicalizeCandidates(classes, { rem })` dynamically via a worker
1827
+ * Calls `ds.canonicalizeCandidates([cls], { rem })` dynamically via a worker
1814
1828
  * thread. Same pattern as sort-service.ts — loads the DS once, then accepts
1815
1829
  * requests synchronously via Atomics.wait().
1816
1830
  *
1817
1831
  * This enables canonicalization of arbitrary user classes (e.g. `p-[2px]` → `p-0.5`)
1818
1832
  * which can't be precomputed because the input space is infinite.
1833
+ *
1834
+ * ## Per-class cache (process-wide)
1835
+ *
1836
+ * Results are cached by `cssPath\0rem\0class`. The worker is invoked only for
1837
+ * cache misses. In practice the cache converges quickly — after the first few
1838
+ * files any given class has been seen, and every subsequent lookup is O(1).
1839
+ *
1840
+ * ## Per-class worker calls
1841
+ *
1842
+ * `ds.canonicalizeCandidates` deduplicates its input (see sync-loader.ts note),
1843
+ * so the worker MUST call it one class at a time to preserve input order/length.
1844
+ * The cache amortizes the per-call overhead.
1819
1845
  */
1820
1846
  const BUFFER_SIZE$1 = 4 * 1024 * 1024;
1821
1847
  const HEADER_INTS$1 = 4;
@@ -1860,7 +1886,12 @@ async function main() {
1860
1886
  try {
1861
1887
  const { classes, rem } = JSON.parse(requestStr);
1862
1888
  const options = rem ? { rem } : undefined;
1863
- const result = ds.canonicalizeCandidates(classes, options);
1889
+ // canonicalizeCandidates deduplicates its input, so we must call it
1890
+ // one class at a time to preserve order/length. See sync-loader.ts.
1891
+ const result = classes.map((cls) => {
1892
+ const r = ds.canonicalizeCandidates([cls], options);
1893
+ return r[0] ?? cls;
1894
+ });
1864
1895
  response = Buffer.from(JSON.stringify(result), 'utf-8');
1865
1896
  } catch {
1866
1897
  response = Buffer.from('null', 'utf-8');
@@ -1882,6 +1913,7 @@ let dataArea$1 = null;
1882
1913
  let initialized$1 = false;
1883
1914
  let available$1 = true;
1884
1915
  let currentCssPath$1 = null;
1916
+ const canonCache = /* @__PURE__ */ new Map();
1885
1917
  function ensureService$1(cssPath) {
1886
1918
  if (initialized$1 && currentCssPath$1 === cssPath) return available$1;
1887
1919
  if (initialized$1) {
@@ -1937,11 +1969,11 @@ function cleanup$1() {
1937
1969
  }
1938
1970
  }
1939
1971
  /**
1940
- * Canonicalize classes using the Tailwind CSS design system via worker thread.
1941
- * Returns the canonicalized class array, or null if the service is unavailable.
1972
+ * Round-trip raw worker call for a batch of classes (no caching).
1973
+ * Returns the canonicalized class array (same length/order as input),
1974
+ * or null if the service is unavailable.
1942
1975
  */
1943
- function canonicalizeClassesSync(cssPath, classes, rem) {
1944
- if (!ensureService$1(cssPath)) return null;
1976
+ function callWorker(classes, rem) {
1945
1977
  if (!controlArray$1 || !dataArea$1 || !lengthView$1) return null;
1946
1978
  try {
1947
1979
  const request = Buffer.from(JSON.stringify({
@@ -1968,6 +2000,43 @@ function canonicalizeClassesSync(cssPath, classes, rem) {
1968
2000
  return null;
1969
2001
  }
1970
2002
  }
2003
+ /**
2004
+ * Canonicalize classes using the Tailwind CSS design system via worker thread.
2005
+ * Returns the canonicalized class array (same length/order as input), or null
2006
+ * if the service is unavailable.
2007
+ *
2008
+ * Uses a process-wide per-class cache: the worker is invoked only for classes
2009
+ * not already seen with this (cssPath, rem) combination.
2010
+ */
2011
+ function canonicalizeClassesSync(cssPath, classes, rem) {
2012
+ if (!ensureService$1(cssPath)) return null;
2013
+ const out = Array.from({ length: classes.length });
2014
+ const missingIdx = [];
2015
+ const missing = [];
2016
+ const cachePrefix = `${cssPath}\0${rem ?? ""}\0`;
2017
+ for (let i = 0; i < classes.length; i++) {
2018
+ const key = cachePrefix + classes[i];
2019
+ const hit = canonCache.get(key);
2020
+ if (hit !== void 0) out[i] = hit;
2021
+ else {
2022
+ missingIdx.push(i);
2023
+ missing.push(classes[i]);
2024
+ }
2025
+ }
2026
+ if (missing.length === 0) return out;
2027
+ const uniqueMissing = [...new Set(missing)];
2028
+ const fresh = callWorker(uniqueMissing, rem);
2029
+ if (!fresh || fresh.length !== uniqueMissing.length) return null;
2030
+ const freshByClass = /* @__PURE__ */ new Map();
2031
+ for (let k = 0; k < uniqueMissing.length; k++) freshByClass.set(uniqueMissing[k], fresh[k]);
2032
+ for (let j = 0; j < missing.length; j++) {
2033
+ const cls = missing[j];
2034
+ const value = freshByClass.get(cls) ?? cls;
2035
+ canonCache.set(cachePrefix + cls, value);
2036
+ out[missingIdx[j]] = value;
2037
+ }
2038
+ return out;
2039
+ }
1971
2040
  //#endregion
1972
2041
  //#region src/rules/enforce-canonical.ts
1973
2042
  /**
@@ -2015,10 +2084,18 @@ const enforceCanonical = defineRule({
2015
2084
  const { cache, entryPoint } = ds;
2016
2085
  for (const loc of locations) {
2017
2086
  const classes = splitClasses(loc.value);
2018
- const dynamic = canonicalizeClassesSync(entryPoint, classes, getRem());
2019
- let canonicals;
2020
- if (dynamic) canonicals = classes.map((cls, i) => preserveImportantPosition(cls, dynamic[i]));
2021
- else canonicals = classes.map((cls) => cache.canonicalize(cls));
2087
+ const canonicals = Array.from({ length: classes.length });
2088
+ const arbitraryIdx = [];
2089
+ const arbitrary = [];
2090
+ for (let i = 0; i < classes.length; i++) if (utilityHasDynamicValue(classes[i])) {
2091
+ arbitraryIdx.push(i);
2092
+ arbitrary.push(classes[i]);
2093
+ } else canonicals[i] = cache.canonicalize(classes[i]);
2094
+ if (arbitrary.length > 0) {
2095
+ const dynamic = canonicalizeClassesSync(entryPoint, arbitrary, getRem());
2096
+ if (dynamic) for (let k = 0; k < arbitrary.length; k++) canonicals[arbitraryIdx[k]] = preserveImportantPosition(arbitrary[k], dynamic[k]);
2097
+ else for (let k = 0; k < arbitrary.length; k++) canonicals[arbitraryIdx[k]] = cache.canonicalize(arbitrary[k]);
2098
+ }
2022
2099
  let firstNonCanonical = true;
2023
2100
  for (let i = 0; i < classes.length; i++) {
2024
2101
  const cls = classes[i];