oxlint-tailwindcss 1.7.1 → 1.8.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 +164 -40
- package/dist/index.mjs +164 -40
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -514,24 +514,58 @@ function splitClassesWithSeparators(classString) {
|
|
|
514
514
|
};
|
|
515
515
|
}
|
|
516
516
|
/**
|
|
517
|
+
* The separator to place before an output class whose source class sits at
|
|
518
|
+
* original index `si`, given the previous output class came from `sPrev`.
|
|
519
|
+
*
|
|
520
|
+
* - **forward move** (`si > sPrev`): the classes between them were dropped, so
|
|
521
|
+
* the effective separator is the LAST newline-bearing separator in that gap
|
|
522
|
+
* (it carries the correct indent), else the separator immediately preceding
|
|
523
|
+
* `si`. This transfers a line break forward when the class that opened a line
|
|
524
|
+
* is removed, so the next survivor keeps its own line.
|
|
525
|
+
* - **non-forward move** (append / reorder — e.g. `enforce-shorthand` pushes the
|
|
526
|
+
* merged replacement to the end): reuse `si`'s own preceding separator if it
|
|
527
|
+
* carries a newline, else a single space — never the (possibly empty) leader,
|
|
528
|
+
* so output classes never glue together.
|
|
529
|
+
*/
|
|
530
|
+
function pickSeparator(separators, sPrev, si) {
|
|
531
|
+
if (si > sPrev) {
|
|
532
|
+
let chosen = separators[si] ?? " ";
|
|
533
|
+
for (let k = sPrev + 1; k <= si; k++) if (separators[k]?.includes("\n")) chosen = separators[k];
|
|
534
|
+
return chosen;
|
|
535
|
+
}
|
|
536
|
+
const own = separators[si];
|
|
537
|
+
return own && own.includes("\n") ? own : " ";
|
|
538
|
+
}
|
|
539
|
+
/**
|
|
517
540
|
* Reconstruct a class string from a `ClassSplit` and a (possibly transformed)
|
|
518
541
|
* array of classes.
|
|
519
542
|
*
|
|
520
|
-
* - If `newClasses.length === split.classes.length`
|
|
521
|
-
* canonicalize, sort, variant-order, etc.)
|
|
522
|
-
* verbatim. This keeps multiline indentation
|
|
523
|
-
*
|
|
524
|
-
*
|
|
525
|
-
*
|
|
526
|
-
*
|
|
543
|
+
* - If `newClasses.length === split.classes.length` and no `sourceIndices` are
|
|
544
|
+
* given (1-to-1 transformation: canonicalize, sort, variant-order, etc.)
|
|
545
|
+
* every separator is preserved verbatim. This keeps multiline indentation
|
|
546
|
+
* intact.
|
|
547
|
+
* - If `sourceIndices` is given (count-changing transforms that know where each
|
|
548
|
+
* output class came from: no-duplicate removes dupes, shorthand collapses
|
|
549
|
+
* pairs) the layout is preserved structurally — leader and trailing stay
|
|
550
|
+
* verbatim (edge-safe) and each interior separator is recovered from the
|
|
551
|
+
* source via `pickSeparator`, so a multiline block keeps its per-line grouping
|
|
552
|
+
* instead of reflowing to one class per line.
|
|
553
|
+
* - Otherwise (count differs, no mapping) it degrades gracefully: leading and
|
|
554
|
+
* trailing whitespace are preserved; classes are joined by the first internal
|
|
555
|
+
* separator that contains a newline, or a single space if none does.
|
|
527
556
|
*/
|
|
528
|
-
function rebuildClassString(split, newClasses) {
|
|
557
|
+
function rebuildClassString(split, newClasses, sourceIndices) {
|
|
529
558
|
const { classes, separators } = split;
|
|
530
|
-
if (newClasses.length === classes.length) {
|
|
559
|
+
if (!sourceIndices && newClasses.length === classes.length) {
|
|
531
560
|
let result = separators[0];
|
|
532
561
|
for (let i = 0; i < newClasses.length; i++) result += newClasses[i] + separators[i + 1];
|
|
533
562
|
return result;
|
|
534
563
|
}
|
|
564
|
+
if (sourceIndices && sourceIndices.length === newClasses.length && newClasses.length > 0) {
|
|
565
|
+
let result = separators[0] + newClasses[0];
|
|
566
|
+
for (let i = 1; i < newClasses.length; i++) result += pickSeparator(separators, sourceIndices[i - 1], sourceIndices[i]) + newClasses[i];
|
|
567
|
+
return result + separators[separators.length - 1];
|
|
568
|
+
}
|
|
535
569
|
const join = separators.slice(1, -1).find((s) => s.includes("\n")) ?? " ";
|
|
536
570
|
return separators[0] + newClasses.join(join) + separators[separators.length - 1];
|
|
537
571
|
}
|
|
@@ -555,7 +589,12 @@ const noDuplicateClasses = defineRule({
|
|
|
555
589
|
for (const cls of classes) if (seen.has(cls)) duplicates.push(cls);
|
|
556
590
|
else seen.add(cls);
|
|
557
591
|
if (duplicates.length > 0) {
|
|
558
|
-
const
|
|
592
|
+
const firstIdx = /* @__PURE__ */ new Map();
|
|
593
|
+
classes.forEach((c, i) => {
|
|
594
|
+
if (!firstIdx.has(c)) firstIdx.set(c, i);
|
|
595
|
+
});
|
|
596
|
+
const unique = [...firstIdx.keys()];
|
|
597
|
+
const fixed = rebuildClassString(split, unique, unique.map((c) => firstIdx.get(c)));
|
|
559
598
|
for (const dup of duplicates) context.report({
|
|
560
599
|
node: loc.node,
|
|
561
600
|
messageId: "duplicate",
|
|
@@ -590,7 +629,9 @@ const noUnnecessaryWhitespace = defineRule({
|
|
|
590
629
|
return m[1] + m[2].replace(/[ \t]+/g, " ");
|
|
591
630
|
}).join("\n");
|
|
592
631
|
if (normalized.startsWith(" ") && !loc.preserveLeadingSpace) normalized = normalized.slice(1);
|
|
593
|
-
|
|
632
|
+
const lastNl = normalized.lastIndexOf("\n");
|
|
633
|
+
const trailingIsClosingIndent = lastNl !== -1 && /^[ \t]*$/.test(normalized.slice(lastNl + 1));
|
|
634
|
+
if (normalized.endsWith(" ") && !loc.preserveTrailingSpace && !trailingIsClosingIndent) normalized = normalized.slice(0, -1);
|
|
594
635
|
if (normalized !== loc.value) context.report({
|
|
595
636
|
node: loc.node,
|
|
596
637
|
messageId: "unnecessaryWhitespace",
|
|
@@ -2926,6 +2967,22 @@ function safeFilename(context) {
|
|
|
2926
2967
|
}
|
|
2927
2968
|
}
|
|
2928
2969
|
/**
|
|
2970
|
+
* Safely read `context.sourceCode`. Same try/catch pattern as the other
|
|
2971
|
+
* accessors (the getter may throw inside `createOnce`). Used by
|
|
2972
|
+
* `enforce-consistent-line-wrapping` to derive a statement's real base
|
|
2973
|
+
* indentation (the line's leading whitespace) when wrapping a single-line
|
|
2974
|
+
* template into the block convention — the backtick column
|
|
2975
|
+
* (`node.loc.start.column`) is NOT the base indent. Returns `undefined` if
|
|
2976
|
+
* unavailable; callers fall back to a zero base indent.
|
|
2977
|
+
*/
|
|
2978
|
+
function safeSourceCode(context) {
|
|
2979
|
+
try {
|
|
2980
|
+
return context.sourceCode ?? void 0;
|
|
2981
|
+
} catch {
|
|
2982
|
+
return;
|
|
2983
|
+
}
|
|
2984
|
+
}
|
|
2985
|
+
/**
|
|
2929
2986
|
* Safely read `context.cwd`, falling back to `process.cwd()`. Same try/catch
|
|
2930
2987
|
* pattern as the other accessors (the getter throws in `createOnce` on older
|
|
2931
2988
|
* oxlint). The fallback keeps relative-path resolution working even when the
|
|
@@ -5208,8 +5265,22 @@ const enforceShorthand = defineRule({
|
|
|
5208
5265
|
if (!mergeIsSafe(family, group.value, cache)) continue;
|
|
5209
5266
|
const matched = family.parts.map((part) => group.byPart.get(part));
|
|
5210
5267
|
const replacement = group.variant + reattachImportant(`${family.to}-${group.value}`, group.position);
|
|
5211
|
-
const
|
|
5212
|
-
|
|
5268
|
+
const matchedSet = new Set(matched);
|
|
5269
|
+
const remaining = [];
|
|
5270
|
+
const sourceIndices = [];
|
|
5271
|
+
let firstMatchedIdx = -1;
|
|
5272
|
+
classes.forEach((cls, i) => {
|
|
5273
|
+
if (matchedSet.has(cls)) {
|
|
5274
|
+
if (firstMatchedIdx === -1) firstMatchedIdx = i;
|
|
5275
|
+
return;
|
|
5276
|
+
}
|
|
5277
|
+
remaining.push(cls);
|
|
5278
|
+
sourceIndices.push(i);
|
|
5279
|
+
});
|
|
5280
|
+
if (!remaining.includes(replacement)) {
|
|
5281
|
+
remaining.push(replacement);
|
|
5282
|
+
sourceIndices.push(firstMatchedIdx);
|
|
5283
|
+
}
|
|
5213
5284
|
for (const part of family.parts) consumed.add(part);
|
|
5214
5285
|
context.report({
|
|
5215
5286
|
node: loc.node,
|
|
@@ -5219,7 +5290,7 @@ const enforceShorthand = defineRule({
|
|
|
5219
5290
|
replacement
|
|
5220
5291
|
},
|
|
5221
5292
|
fix(fixer) {
|
|
5222
|
-
return fixer.replaceTextRange(loc.range, preserveSpaces(loc, rebuildClassString(split, remaining)));
|
|
5293
|
+
return fixer.replaceTextRange(loc.range, preserveSpaces(loc, rebuildClassString(split, remaining, sourceIndices)));
|
|
5223
5294
|
}
|
|
5224
5295
|
});
|
|
5225
5296
|
}
|
|
@@ -6448,6 +6519,14 @@ const consistentVariantOrder = defineRule({
|
|
|
6448
6519
|
//#endregion
|
|
6449
6520
|
//#region src/rules/enforce-consistent-line-wrapping.ts
|
|
6450
6521
|
const DEFAULT_PRINT_WIDTH = 80;
|
|
6522
|
+
/** One indentation level added below the statement's base indent for wrapped lines. */
|
|
6523
|
+
const INDENT_UNIT = " ";
|
|
6524
|
+
/** Group `classes` into chunks of at most `n`, each chunk space-joined. */
|
|
6525
|
+
function chunkList(classes, n) {
|
|
6526
|
+
const chunks = [];
|
|
6527
|
+
for (let i = 0; i < classes.length; i += n) chunks.push(classes.slice(i, i + n).join(" "));
|
|
6528
|
+
return chunks;
|
|
6529
|
+
}
|
|
6451
6530
|
const enforceConsistentLineWrapping = defineRule({
|
|
6452
6531
|
meta: {
|
|
6453
6532
|
type: "suggestion",
|
|
@@ -6463,52 +6542,97 @@ const enforceConsistentLineWrapping = defineRule({
|
|
|
6463
6542
|
}],
|
|
6464
6543
|
defaultOptions: [{ printWidth: DEFAULT_PRINT_WIDTH }],
|
|
6465
6544
|
messages: {
|
|
6466
|
-
tooLong: "Class string
|
|
6545
|
+
tooLong: "Class string exceeds the print width of {{printWidth}} (longest line is {{length}} characters). Consider splitting into multiple lines or extracting into a component.",
|
|
6467
6546
|
tooManyPerLine: "Too many classes on a single line ({{count}}). Maximum allowed per line is {{max}}."
|
|
6468
6547
|
}
|
|
6469
6548
|
},
|
|
6470
6549
|
createOnce(context) {
|
|
6471
6550
|
const getPrintWidth = createLazyOptions(context, (o) => o?.printWidth ?? DEFAULT_PRINT_WIDTH);
|
|
6472
6551
|
const getClassesPerLine = createLazyOptions(context, (o) => o?.classesPerLine);
|
|
6552
|
+
/**
|
|
6553
|
+
* The statement's real base indentation — the leading whitespace of the
|
|
6554
|
+
* source line the opening backtick sits on. NOT `node.loc.start.column`,
|
|
6555
|
+
* which is the backtick's column (e.g. 18 for `const className = ` and
|
|
6556
|
+
* deeper still inside nested JSX). Falls back to `''` when `sourceCode`
|
|
6557
|
+
* is unavailable (block still lands correctly for top-level statements).
|
|
6558
|
+
*/
|
|
6559
|
+
function deriveBaseIndent(loc) {
|
|
6560
|
+
const sc = safeSourceCode(context);
|
|
6561
|
+
const line = loc.node.loc?.start.line;
|
|
6562
|
+
if (sc?.lines && typeof line === "number" && line >= 1 && line <= sc.lines.length) {
|
|
6563
|
+
const src = sc.lines[line - 1] ?? "";
|
|
6564
|
+
const m = /^[ \t]*/.exec(src);
|
|
6565
|
+
return m ? m[0] : "";
|
|
6566
|
+
}
|
|
6567
|
+
return "";
|
|
6568
|
+
}
|
|
6569
|
+
/**
|
|
6570
|
+
* Re-wrap a template literal's classes into the block convention,
|
|
6571
|
+
* non-destructively:
|
|
6572
|
+
* - single-line → convert to a `\n` + indent block (unless the quasi is
|
|
6573
|
+
* glued to a `${}`, in which case keep it inline/hanging so adjacency
|
|
6574
|
+
* survives);
|
|
6575
|
+
* - already multiline → re-wrap ONLY the lines that exceed the budget,
|
|
6576
|
+
* reusing each line's own indent; conforming lines are left verbatim.
|
|
6577
|
+
*/
|
|
6578
|
+
function rewrapTemplate(loc, lines, classesPerLine) {
|
|
6579
|
+
const baseIndent = deriveBaseIndent(loc);
|
|
6580
|
+
const interiorIndent = baseIndent + INDENT_UNIT;
|
|
6581
|
+
if (lines.length === 1) {
|
|
6582
|
+
const chunks = chunkList(splitClasses(loc.value), classesPerLine);
|
|
6583
|
+
if (!loc.preserveLeadingSpace && !loc.preserveTrailingSpace) return "\n" + chunks.map((c) => interiorIndent + c).join("\n") + "\n" + baseIndent;
|
|
6584
|
+
return chunks.join("\n" + interiorIndent);
|
|
6585
|
+
}
|
|
6586
|
+
return lines.map((line) => {
|
|
6587
|
+
const classes = splitClasses(line);
|
|
6588
|
+
if (classes.length <= classesPerLine) return line;
|
|
6589
|
+
const m = /^[ \t]*/.exec(line);
|
|
6590
|
+
const lineIndent = m && m[0].length > 0 ? m[0] : interiorIndent;
|
|
6591
|
+
return chunkList(classes, classesPerLine).map((c) => lineIndent + c).join("\n");
|
|
6592
|
+
}).join("\n");
|
|
6593
|
+
}
|
|
6473
6594
|
function check(locations) {
|
|
6474
6595
|
const printWidth = getPrintWidth();
|
|
6475
6596
|
const classesPerLine = getClassesPerLine();
|
|
6476
6597
|
for (const loc of locations) {
|
|
6477
|
-
|
|
6598
|
+
let maxLine = 0;
|
|
6599
|
+
for (const line of loc.value.split("\n")) if (line.length > maxLine) maxLine = line.length;
|
|
6600
|
+
if (maxLine > printWidth) context.report({
|
|
6478
6601
|
node: loc.node,
|
|
6479
6602
|
messageId: "tooLong",
|
|
6480
6603
|
data: {
|
|
6481
|
-
length: String(
|
|
6604
|
+
length: String(maxLine),
|
|
6482
6605
|
printWidth: String(printWidth)
|
|
6483
6606
|
}
|
|
6484
6607
|
});
|
|
6485
6608
|
if (classesPerLine !== void 0) {
|
|
6486
|
-
const
|
|
6487
|
-
|
|
6488
|
-
|
|
6489
|
-
|
|
6490
|
-
|
|
6491
|
-
|
|
6492
|
-
|
|
6493
|
-
|
|
6609
|
+
const lines = loc.value.split("\n");
|
|
6610
|
+
let maxPerLine = 0;
|
|
6611
|
+
for (const line of lines) {
|
|
6612
|
+
const n = splitClasses(line).length;
|
|
6613
|
+
if (n > maxPerLine) maxPerLine = n;
|
|
6614
|
+
}
|
|
6615
|
+
if (maxPerLine > classesPerLine) {
|
|
6616
|
+
const data = {
|
|
6617
|
+
count: String(maxPerLine),
|
|
6618
|
+
max: String(classesPerLine)
|
|
6619
|
+
};
|
|
6620
|
+
if (loc.node.type === "TemplateElement") {
|
|
6621
|
+
const fixedValue = rewrapTemplate(loc, lines, classesPerLine);
|
|
6622
|
+
context.report({
|
|
6623
|
+
node: loc.node,
|
|
6624
|
+
messageId: "tooManyPerLine",
|
|
6625
|
+
data,
|
|
6626
|
+
fix(fixer) {
|
|
6627
|
+
return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
|
|
6628
|
+
}
|
|
6629
|
+
});
|
|
6630
|
+
} else context.report({
|
|
6494
6631
|
node: loc.node,
|
|
6495
6632
|
messageId: "tooManyPerLine",
|
|
6496
|
-
data
|
|
6497
|
-
count: String(classes.length),
|
|
6498
|
-
max: String(classesPerLine)
|
|
6499
|
-
},
|
|
6500
|
-
fix(fixer) {
|
|
6501
|
-
return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
|
|
6502
|
-
}
|
|
6633
|
+
data
|
|
6503
6634
|
});
|
|
6504
|
-
}
|
|
6505
|
-
node: loc.node,
|
|
6506
|
-
messageId: "tooManyPerLine",
|
|
6507
|
-
data: {
|
|
6508
|
-
count: String(classes.length),
|
|
6509
|
-
max: String(classesPerLine)
|
|
6510
|
-
}
|
|
6511
|
-
});
|
|
6635
|
+
}
|
|
6512
6636
|
}
|
|
6513
6637
|
}
|
|
6514
6638
|
}
|
package/dist/index.mjs
CHANGED
|
@@ -518,24 +518,58 @@ function splitClassesWithSeparators(classString) {
|
|
|
518
518
|
};
|
|
519
519
|
}
|
|
520
520
|
/**
|
|
521
|
+
* The separator to place before an output class whose source class sits at
|
|
522
|
+
* original index `si`, given the previous output class came from `sPrev`.
|
|
523
|
+
*
|
|
524
|
+
* - **forward move** (`si > sPrev`): the classes between them were dropped, so
|
|
525
|
+
* the effective separator is the LAST newline-bearing separator in that gap
|
|
526
|
+
* (it carries the correct indent), else the separator immediately preceding
|
|
527
|
+
* `si`. This transfers a line break forward when the class that opened a line
|
|
528
|
+
* is removed, so the next survivor keeps its own line.
|
|
529
|
+
* - **non-forward move** (append / reorder — e.g. `enforce-shorthand` pushes the
|
|
530
|
+
* merged replacement to the end): reuse `si`'s own preceding separator if it
|
|
531
|
+
* carries a newline, else a single space — never the (possibly empty) leader,
|
|
532
|
+
* so output classes never glue together.
|
|
533
|
+
*/
|
|
534
|
+
function pickSeparator(separators, sPrev, si) {
|
|
535
|
+
if (si > sPrev) {
|
|
536
|
+
let chosen = separators[si] ?? " ";
|
|
537
|
+
for (let k = sPrev + 1; k <= si; k++) if (separators[k]?.includes("\n")) chosen = separators[k];
|
|
538
|
+
return chosen;
|
|
539
|
+
}
|
|
540
|
+
const own = separators[si];
|
|
541
|
+
return own && own.includes("\n") ? own : " ";
|
|
542
|
+
}
|
|
543
|
+
/**
|
|
521
544
|
* Reconstruct a class string from a `ClassSplit` and a (possibly transformed)
|
|
522
545
|
* array of classes.
|
|
523
546
|
*
|
|
524
|
-
* - If `newClasses.length === split.classes.length`
|
|
525
|
-
* canonicalize, sort, variant-order, etc.)
|
|
526
|
-
* verbatim. This keeps multiline indentation
|
|
527
|
-
*
|
|
528
|
-
*
|
|
529
|
-
*
|
|
530
|
-
*
|
|
547
|
+
* - If `newClasses.length === split.classes.length` and no `sourceIndices` are
|
|
548
|
+
* given (1-to-1 transformation: canonicalize, sort, variant-order, etc.)
|
|
549
|
+
* every separator is preserved verbatim. This keeps multiline indentation
|
|
550
|
+
* intact.
|
|
551
|
+
* - If `sourceIndices` is given (count-changing transforms that know where each
|
|
552
|
+
* output class came from: no-duplicate removes dupes, shorthand collapses
|
|
553
|
+
* pairs) the layout is preserved structurally — leader and trailing stay
|
|
554
|
+
* verbatim (edge-safe) and each interior separator is recovered from the
|
|
555
|
+
* source via `pickSeparator`, so a multiline block keeps its per-line grouping
|
|
556
|
+
* instead of reflowing to one class per line.
|
|
557
|
+
* - Otherwise (count differs, no mapping) it degrades gracefully: leading and
|
|
558
|
+
* trailing whitespace are preserved; classes are joined by the first internal
|
|
559
|
+
* separator that contains a newline, or a single space if none does.
|
|
531
560
|
*/
|
|
532
|
-
function rebuildClassString(split, newClasses) {
|
|
561
|
+
function rebuildClassString(split, newClasses, sourceIndices) {
|
|
533
562
|
const { classes, separators } = split;
|
|
534
|
-
if (newClasses.length === classes.length) {
|
|
563
|
+
if (!sourceIndices && newClasses.length === classes.length) {
|
|
535
564
|
let result = separators[0];
|
|
536
565
|
for (let i = 0; i < newClasses.length; i++) result += newClasses[i] + separators[i + 1];
|
|
537
566
|
return result;
|
|
538
567
|
}
|
|
568
|
+
if (sourceIndices && sourceIndices.length === newClasses.length && newClasses.length > 0) {
|
|
569
|
+
let result = separators[0] + newClasses[0];
|
|
570
|
+
for (let i = 1; i < newClasses.length; i++) result += pickSeparator(separators, sourceIndices[i - 1], sourceIndices[i]) + newClasses[i];
|
|
571
|
+
return result + separators[separators.length - 1];
|
|
572
|
+
}
|
|
539
573
|
const join = separators.slice(1, -1).find((s) => s.includes("\n")) ?? " ";
|
|
540
574
|
return separators[0] + newClasses.join(join) + separators[separators.length - 1];
|
|
541
575
|
}
|
|
@@ -559,7 +593,12 @@ const noDuplicateClasses = defineRule({
|
|
|
559
593
|
for (const cls of classes) if (seen.has(cls)) duplicates.push(cls);
|
|
560
594
|
else seen.add(cls);
|
|
561
595
|
if (duplicates.length > 0) {
|
|
562
|
-
const
|
|
596
|
+
const firstIdx = /* @__PURE__ */ new Map();
|
|
597
|
+
classes.forEach((c, i) => {
|
|
598
|
+
if (!firstIdx.has(c)) firstIdx.set(c, i);
|
|
599
|
+
});
|
|
600
|
+
const unique = [...firstIdx.keys()];
|
|
601
|
+
const fixed = rebuildClassString(split, unique, unique.map((c) => firstIdx.get(c)));
|
|
563
602
|
for (const dup of duplicates) context.report({
|
|
564
603
|
node: loc.node,
|
|
565
604
|
messageId: "duplicate",
|
|
@@ -594,7 +633,9 @@ const noUnnecessaryWhitespace = defineRule({
|
|
|
594
633
|
return m[1] + m[2].replace(/[ \t]+/g, " ");
|
|
595
634
|
}).join("\n");
|
|
596
635
|
if (normalized.startsWith(" ") && !loc.preserveLeadingSpace) normalized = normalized.slice(1);
|
|
597
|
-
|
|
636
|
+
const lastNl = normalized.lastIndexOf("\n");
|
|
637
|
+
const trailingIsClosingIndent = lastNl !== -1 && /^[ \t]*$/.test(normalized.slice(lastNl + 1));
|
|
638
|
+
if (normalized.endsWith(" ") && !loc.preserveTrailingSpace && !trailingIsClosingIndent) normalized = normalized.slice(0, -1);
|
|
598
639
|
if (normalized !== loc.value) context.report({
|
|
599
640
|
node: loc.node,
|
|
600
641
|
messageId: "unnecessaryWhitespace",
|
|
@@ -2930,6 +2971,22 @@ function safeFilename(context) {
|
|
|
2930
2971
|
}
|
|
2931
2972
|
}
|
|
2932
2973
|
/**
|
|
2974
|
+
* Safely read `context.sourceCode`. Same try/catch pattern as the other
|
|
2975
|
+
* accessors (the getter may throw inside `createOnce`). Used by
|
|
2976
|
+
* `enforce-consistent-line-wrapping` to derive a statement's real base
|
|
2977
|
+
* indentation (the line's leading whitespace) when wrapping a single-line
|
|
2978
|
+
* template into the block convention — the backtick column
|
|
2979
|
+
* (`node.loc.start.column`) is NOT the base indent. Returns `undefined` if
|
|
2980
|
+
* unavailable; callers fall back to a zero base indent.
|
|
2981
|
+
*/
|
|
2982
|
+
function safeSourceCode(context) {
|
|
2983
|
+
try {
|
|
2984
|
+
return context.sourceCode ?? void 0;
|
|
2985
|
+
} catch {
|
|
2986
|
+
return;
|
|
2987
|
+
}
|
|
2988
|
+
}
|
|
2989
|
+
/**
|
|
2933
2990
|
* Safely read `context.cwd`, falling back to `process.cwd()`. Same try/catch
|
|
2934
2991
|
* pattern as the other accessors (the getter throws in `createOnce` on older
|
|
2935
2992
|
* oxlint). The fallback keeps relative-path resolution working even when the
|
|
@@ -5212,8 +5269,22 @@ const enforceShorthand = defineRule({
|
|
|
5212
5269
|
if (!mergeIsSafe(family, group.value, cache)) continue;
|
|
5213
5270
|
const matched = family.parts.map((part) => group.byPart.get(part));
|
|
5214
5271
|
const replacement = group.variant + reattachImportant(`${family.to}-${group.value}`, group.position);
|
|
5215
|
-
const
|
|
5216
|
-
|
|
5272
|
+
const matchedSet = new Set(matched);
|
|
5273
|
+
const remaining = [];
|
|
5274
|
+
const sourceIndices = [];
|
|
5275
|
+
let firstMatchedIdx = -1;
|
|
5276
|
+
classes.forEach((cls, i) => {
|
|
5277
|
+
if (matchedSet.has(cls)) {
|
|
5278
|
+
if (firstMatchedIdx === -1) firstMatchedIdx = i;
|
|
5279
|
+
return;
|
|
5280
|
+
}
|
|
5281
|
+
remaining.push(cls);
|
|
5282
|
+
sourceIndices.push(i);
|
|
5283
|
+
});
|
|
5284
|
+
if (!remaining.includes(replacement)) {
|
|
5285
|
+
remaining.push(replacement);
|
|
5286
|
+
sourceIndices.push(firstMatchedIdx);
|
|
5287
|
+
}
|
|
5217
5288
|
for (const part of family.parts) consumed.add(part);
|
|
5218
5289
|
context.report({
|
|
5219
5290
|
node: loc.node,
|
|
@@ -5223,7 +5294,7 @@ const enforceShorthand = defineRule({
|
|
|
5223
5294
|
replacement
|
|
5224
5295
|
},
|
|
5225
5296
|
fix(fixer) {
|
|
5226
|
-
return fixer.replaceTextRange(loc.range, preserveSpaces(loc, rebuildClassString(split, remaining)));
|
|
5297
|
+
return fixer.replaceTextRange(loc.range, preserveSpaces(loc, rebuildClassString(split, remaining, sourceIndices)));
|
|
5227
5298
|
}
|
|
5228
5299
|
});
|
|
5229
5300
|
}
|
|
@@ -6452,6 +6523,14 @@ const consistentVariantOrder = defineRule({
|
|
|
6452
6523
|
//#endregion
|
|
6453
6524
|
//#region src/rules/enforce-consistent-line-wrapping.ts
|
|
6454
6525
|
const DEFAULT_PRINT_WIDTH = 80;
|
|
6526
|
+
/** One indentation level added below the statement's base indent for wrapped lines. */
|
|
6527
|
+
const INDENT_UNIT = " ";
|
|
6528
|
+
/** Group `classes` into chunks of at most `n`, each chunk space-joined. */
|
|
6529
|
+
function chunkList(classes, n) {
|
|
6530
|
+
const chunks = [];
|
|
6531
|
+
for (let i = 0; i < classes.length; i += n) chunks.push(classes.slice(i, i + n).join(" "));
|
|
6532
|
+
return chunks;
|
|
6533
|
+
}
|
|
6455
6534
|
const enforceConsistentLineWrapping = defineRule({
|
|
6456
6535
|
meta: {
|
|
6457
6536
|
type: "suggestion",
|
|
@@ -6467,52 +6546,97 @@ const enforceConsistentLineWrapping = defineRule({
|
|
|
6467
6546
|
}],
|
|
6468
6547
|
defaultOptions: [{ printWidth: DEFAULT_PRINT_WIDTH }],
|
|
6469
6548
|
messages: {
|
|
6470
|
-
tooLong: "Class string
|
|
6549
|
+
tooLong: "Class string exceeds the print width of {{printWidth}} (longest line is {{length}} characters). Consider splitting into multiple lines or extracting into a component.",
|
|
6471
6550
|
tooManyPerLine: "Too many classes on a single line ({{count}}). Maximum allowed per line is {{max}}."
|
|
6472
6551
|
}
|
|
6473
6552
|
},
|
|
6474
6553
|
createOnce(context) {
|
|
6475
6554
|
const getPrintWidth = createLazyOptions(context, (o) => o?.printWidth ?? DEFAULT_PRINT_WIDTH);
|
|
6476
6555
|
const getClassesPerLine = createLazyOptions(context, (o) => o?.classesPerLine);
|
|
6556
|
+
/**
|
|
6557
|
+
* The statement's real base indentation — the leading whitespace of the
|
|
6558
|
+
* source line the opening backtick sits on. NOT `node.loc.start.column`,
|
|
6559
|
+
* which is the backtick's column (e.g. 18 for `const className = ` and
|
|
6560
|
+
* deeper still inside nested JSX). Falls back to `''` when `sourceCode`
|
|
6561
|
+
* is unavailable (block still lands correctly for top-level statements).
|
|
6562
|
+
*/
|
|
6563
|
+
function deriveBaseIndent(loc) {
|
|
6564
|
+
const sc = safeSourceCode(context);
|
|
6565
|
+
const line = loc.node.loc?.start.line;
|
|
6566
|
+
if (sc?.lines && typeof line === "number" && line >= 1 && line <= sc.lines.length) {
|
|
6567
|
+
const src = sc.lines[line - 1] ?? "";
|
|
6568
|
+
const m = /^[ \t]*/.exec(src);
|
|
6569
|
+
return m ? m[0] : "";
|
|
6570
|
+
}
|
|
6571
|
+
return "";
|
|
6572
|
+
}
|
|
6573
|
+
/**
|
|
6574
|
+
* Re-wrap a template literal's classes into the block convention,
|
|
6575
|
+
* non-destructively:
|
|
6576
|
+
* - single-line → convert to a `\n` + indent block (unless the quasi is
|
|
6577
|
+
* glued to a `${}`, in which case keep it inline/hanging so adjacency
|
|
6578
|
+
* survives);
|
|
6579
|
+
* - already multiline → re-wrap ONLY the lines that exceed the budget,
|
|
6580
|
+
* reusing each line's own indent; conforming lines are left verbatim.
|
|
6581
|
+
*/
|
|
6582
|
+
function rewrapTemplate(loc, lines, classesPerLine) {
|
|
6583
|
+
const baseIndent = deriveBaseIndent(loc);
|
|
6584
|
+
const interiorIndent = baseIndent + INDENT_UNIT;
|
|
6585
|
+
if (lines.length === 1) {
|
|
6586
|
+
const chunks = chunkList(splitClasses(loc.value), classesPerLine);
|
|
6587
|
+
if (!loc.preserveLeadingSpace && !loc.preserveTrailingSpace) return "\n" + chunks.map((c) => interiorIndent + c).join("\n") + "\n" + baseIndent;
|
|
6588
|
+
return chunks.join("\n" + interiorIndent);
|
|
6589
|
+
}
|
|
6590
|
+
return lines.map((line) => {
|
|
6591
|
+
const classes = splitClasses(line);
|
|
6592
|
+
if (classes.length <= classesPerLine) return line;
|
|
6593
|
+
const m = /^[ \t]*/.exec(line);
|
|
6594
|
+
const lineIndent = m && m[0].length > 0 ? m[0] : interiorIndent;
|
|
6595
|
+
return chunkList(classes, classesPerLine).map((c) => lineIndent + c).join("\n");
|
|
6596
|
+
}).join("\n");
|
|
6597
|
+
}
|
|
6477
6598
|
function check(locations) {
|
|
6478
6599
|
const printWidth = getPrintWidth();
|
|
6479
6600
|
const classesPerLine = getClassesPerLine();
|
|
6480
6601
|
for (const loc of locations) {
|
|
6481
|
-
|
|
6602
|
+
let maxLine = 0;
|
|
6603
|
+
for (const line of loc.value.split("\n")) if (line.length > maxLine) maxLine = line.length;
|
|
6604
|
+
if (maxLine > printWidth) context.report({
|
|
6482
6605
|
node: loc.node,
|
|
6483
6606
|
messageId: "tooLong",
|
|
6484
6607
|
data: {
|
|
6485
|
-
length: String(
|
|
6608
|
+
length: String(maxLine),
|
|
6486
6609
|
printWidth: String(printWidth)
|
|
6487
6610
|
}
|
|
6488
6611
|
});
|
|
6489
6612
|
if (classesPerLine !== void 0) {
|
|
6490
|
-
const
|
|
6491
|
-
|
|
6492
|
-
|
|
6493
|
-
|
|
6494
|
-
|
|
6495
|
-
|
|
6496
|
-
|
|
6497
|
-
|
|
6613
|
+
const lines = loc.value.split("\n");
|
|
6614
|
+
let maxPerLine = 0;
|
|
6615
|
+
for (const line of lines) {
|
|
6616
|
+
const n = splitClasses(line).length;
|
|
6617
|
+
if (n > maxPerLine) maxPerLine = n;
|
|
6618
|
+
}
|
|
6619
|
+
if (maxPerLine > classesPerLine) {
|
|
6620
|
+
const data = {
|
|
6621
|
+
count: String(maxPerLine),
|
|
6622
|
+
max: String(classesPerLine)
|
|
6623
|
+
};
|
|
6624
|
+
if (loc.node.type === "TemplateElement") {
|
|
6625
|
+
const fixedValue = rewrapTemplate(loc, lines, classesPerLine);
|
|
6626
|
+
context.report({
|
|
6627
|
+
node: loc.node,
|
|
6628
|
+
messageId: "tooManyPerLine",
|
|
6629
|
+
data,
|
|
6630
|
+
fix(fixer) {
|
|
6631
|
+
return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
|
|
6632
|
+
}
|
|
6633
|
+
});
|
|
6634
|
+
} else context.report({
|
|
6498
6635
|
node: loc.node,
|
|
6499
6636
|
messageId: "tooManyPerLine",
|
|
6500
|
-
data
|
|
6501
|
-
count: String(classes.length),
|
|
6502
|
-
max: String(classesPerLine)
|
|
6503
|
-
},
|
|
6504
|
-
fix(fixer) {
|
|
6505
|
-
return fixer.replaceTextRange(loc.range, preserveSpaces(loc, fixedValue));
|
|
6506
|
-
}
|
|
6637
|
+
data
|
|
6507
6638
|
});
|
|
6508
|
-
}
|
|
6509
|
-
node: loc.node,
|
|
6510
|
-
messageId: "tooManyPerLine",
|
|
6511
|
-
data: {
|
|
6512
|
-
count: String(classes.length),
|
|
6513
|
-
max: String(classesPerLine)
|
|
6514
|
-
}
|
|
6515
|
-
});
|
|
6639
|
+
}
|
|
6516
6640
|
}
|
|
6517
6641
|
}
|
|
6518
6642
|
}
|