Skip to content
Merged
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
chore: removeunused code
  • Loading branch information
francoismassart committed Jun 23, 2022
commit 9daf66ea5e1eaf615e7a1aadf23d0c257c45ebab
124 changes: 0 additions & 124 deletions lib/rules/classnames-order.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,130 +82,6 @@ module.exports = {
//----------------------------------------------------------------------
// Helpers
//----------------------------------------------------------------------
/**
* Get the index of a variant within a className
* @param {String} str The input string (haystack)
* @param {Array} arr The list of possible variants (needle)
* @param {Boolean} beginning Optional, starts from the beginning
* @returns {Number}
*/
const getPrefixIndex = (str, arr, beginning = false) => {
const start = beginning ? '^' : '';
let idx = arr.findIndex((el) => {
const separator = '\\' + mergedConfig.separator;
const pattern = `${start}${el}${separator}.*`;
const re = new RegExp(pattern);
return re.test(str);
}, str);
return idx;
};

/**
* Add left '0' padding to given number
* @param {Number} num The input number
* @param {Number} size Optional, the desired length
* @returns {String}
*/
const pad = (num, size = 2) => {
let str = '' + num;
while (str.length < size) {
str = '0' + str;
}
return str;
};
/**
* Generate an Array of Array, grouping class by responsive variants
* @param {Array} classNames
* @returns {Array} an Array (one entry per responsive variant), each entry is an array of classnames
*/
const getResponsiveGroups = (classNames) => {
const responsiveVariants = Object.keys(mergedConfig.theme.screens);
const classnamesByResponsive = [[]];
responsiveVariants.forEach((prefix) => {
classnamesByResponsive.push([]);
});
classNames.forEach((cls) => {
const idx = parseInt(getSpecificity(cls, responsiveVariants, true), 10);
classnamesByResponsive[idx].push(cls);
});
return classnamesByResponsive;
};

/**
* Parse each classname and populate the `sorted` and `extra` arrays
* @param {Array} classNames
* @returns {Object} An object with `sorted` and `extra` arrays
*/
const getSortedGroups = (classNames) => {
// Init assets before sorting
const groups = groupUtil.getGroups(groupsConfig, mergedConfig);
const sorted = groupUtil.initGroupSlots(groups);
const extras = [];

// Move each classname inside its dedicated group
classNames.forEach((className) => {
const trimmed = className.replace(/\s{1,}/g, '');
const idx = groupUtil.getGroupIndex(trimmed, groups, mergedConfig.separator);
if (idx > -1) {
sorted[idx].push(className);
} else {
extras.push(className);
}
});

// Sorts each groups' classnames
sorted.forEach((slot) => {
slot.sort(sortTailwindClasses);
});
return {
sorted,
extras,
};
};

/**
* Get a padded string version of the sorting key
* @param {String} classname
* @param {Array} variants
* @param {Boolean} beginning
* @returns {String}
*/
const getSpecificity = (classname, variants, beginning = false) => {
// Index can be -1, 0... Adding 1 for better readability... -1 becomes 0
return pad(getPrefixIndex(classname, variants, beginning) + 1, 2);
};

/**
* Sort classnames by using a sorting key
* @param {String} a A classname
* @param {String} b Another classname
* @returns {Number} -1, 0 or +1
*/
const sortTailwindClasses = (a, b) => {
const responsiveVariants = Object.keys(mergedConfig.theme.screens);
const themeVariants = ['dark'];
// motion-safe/reduce are not present...
// TODO Check if already present due to custom config overwiting the default `variantOrder`
const stateVariants = [...mergedConfig.variantOrder, 'motion-safe', 'motion-reduce'];
const aIdxStr = `${getSpecificity(a, responsiveVariants, true)}${getSpecificity(
a,
themeVariants
)}${getSpecificity(a, stateVariants)}`;
const bIdxStr = `${getSpecificity(b, responsiveVariants, true)}${getSpecificity(
b,
themeVariants
)}${getSpecificity(b, stateVariants)}`;
const aIdx = parseInt(aIdxStr, 10);
const bIdx = parseInt(bIdxStr, 10);
if (aIdx < bIdx) {
return -1;
}
if (aIdx > bIdx) {
return 1;
}
return 0;
};

/**
* Recursive function crawling into child nodes
* @param {ASTNode} node The root node of the current parsing
Expand Down