Skip to content

Commit a8bfa5a

Browse files
committed
Extract duplicate code that converts ordered classNames back to attribute value
1 parent 10470a4 commit a8bfa5a

File tree

1 file changed

+13
-19
lines changed

1 file changed

+13
-19
lines changed

lib/rules/classnames-order.js

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -305,20 +305,11 @@ module.exports = {
305305
}
306306

307307
let orderedClassNames;
308-
let validatedClassNamesValue = '';
309308

309+
// Sorting
310310
if (officialSorting) {
311311
orderedClassNames = order(classNames, contextFallback);
312-
for (let i = 0; i < orderedClassNames.length; i++) {
313-
const w = whitespaces[i] ?? '';
314-
const cls = orderedClassNames[i];
315-
validatedClassNamesValue += headSpace ? `${w}${cls}` : `${cls}${w}`;
316-
if (headSpace && tailSpace && i === orderedClassNames.length - 1) {
317-
validatedClassNamesValue += whitespaces[whitespaces.length - 1] ?? '';
318-
}
319-
}
320312
} else {
321-
// Sorting
322313
const mergedSorted = [];
323314
const mergedExtras = [];
324315
if (groupByResponsive) {
@@ -334,18 +325,21 @@ module.exports = {
334325
mergedExtras.push(...extras);
335326
}
336327

337-
// Generates the validated/sorted attribute value
338328
const flatted = mergedSorted.flat();
339-
const union = prependCustom ? [...mergedExtras, ...flatted] : [...flatted, ...mergedExtras];
340-
for (let i = 0; i < union.length; i++) {
341-
const w = whitespaces[i] ?? '';
342-
const cls = union[i];
343-
validatedClassNamesValue += headSpace ? `${w}${cls}` : `${cls}${w}`;
344-
if (headSpace && tailSpace && i === union.length - 1) {
345-
validatedClassNamesValue += whitespaces[whitespaces.length - 1] ?? '';
346-
}
329+
orderedClassNames = prependCustom ? [...mergedExtras, ...flatted] : [...flatted, ...mergedExtras];
330+
}
331+
332+
// Generates the validated/sorted attribute value
333+
let validatedClassNamesValue = '';
334+
for (let i = 0; i < orderedClassNames.length; i++) {
335+
const w = whitespaces[i] ?? '';
336+
const cls = orderedClassNames[i];
337+
validatedClassNamesValue += headSpace ? `${w}${cls}` : `${cls}${w}`;
338+
if (headSpace && tailSpace && i === orderedClassNames.length - 1) {
339+
validatedClassNamesValue += whitespaces[whitespaces.length - 1] ?? '';
347340
}
348341
}
342+
349343
if (originalClassNamesValue !== validatedClassNamesValue) {
350344
validatedClassNamesValue = prefix + validatedClassNamesValue + suffix;
351345
context.report({

0 commit comments

Comments
 (0)