Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Extract duplicate code that converts ordered classNames back to attri…
…bute value
  • Loading branch information
mpsijm committed Jun 13, 2022
commit 9a979647b2abf3256ab10008ede167e9b1035e1c
32 changes: 13 additions & 19 deletions lib/rules/classnames-order.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,20 +314,11 @@ module.exports = {
}

let orderedClassNames;
let validatedClassNamesValue = '';

// Sorting
if (officialSorting) {
orderedClassNames = order(classNames, contextFallback);
for (let i = 0; i < orderedClassNames.length; i++) {
const w = whitespaces[i] ?? '';
const cls = orderedClassNames[i];
validatedClassNamesValue += headSpace ? `${w}${cls}` : `${cls}${w}`;
if (headSpace && tailSpace && i === orderedClassNames.length - 1) {
validatedClassNamesValue += whitespaces[whitespaces.length - 1] ?? '';
}
}
} else {
// Sorting
const mergedSorted = [];
const mergedExtras = [];
if (groupByResponsive) {
Expand All @@ -343,18 +334,21 @@ module.exports = {
mergedExtras.push(...extras);
}

// Generates the validated/sorted attribute value
const flatted = mergedSorted.flat();
const union = prependCustom ? [...mergedExtras, ...flatted] : [...flatted, ...mergedExtras];
for (let i = 0; i < union.length; i++) {
const w = whitespaces[i] ?? '';
const cls = union[i];
validatedClassNamesValue += headSpace ? `${w}${cls}` : `${cls}${w}`;
if (headSpace && tailSpace && i === union.length - 1) {
validatedClassNamesValue += whitespaces[whitespaces.length - 1] ?? '';
}
orderedClassNames = prependCustom ? [...mergedExtras, ...flatted] : [...flatted, ...mergedExtras];
}

// Generates the validated/sorted attribute value
let validatedClassNamesValue = '';
for (let i = 0; i < orderedClassNames.length; i++) {
const w = whitespaces[i] ?? '';
const cls = orderedClassNames[i];
validatedClassNamesValue += headSpace ? `${w}${cls}` : `${cls}${w}`;
if (headSpace && tailSpace && i === orderedClassNames.length - 1) {
validatedClassNamesValue += whitespaces[whitespaces.length - 1] ?? '';
}
}

if (originalClassNamesValue !== validatedClassNamesValue) {
validatedClassNamesValue = prefix + validatedClassNamesValue + suffix;
context.report({
Expand Down