sortier
Version:
An opinionated code sorter
30 lines (29 loc) • 1.39 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.sortJsxElement = sortJsxElement;
const sort_utils_js_1 = require("../../utilities/sort-utils.js");
const sort_utils_js_2 = require("../utilities/sort-utils.js");
function sortJsxElement(jsxElement, comments, fileContents,
// Left in for consistency with other sort functions
// eslint-disable-next-line @typescript-eslint/no-unused-vars
options) {
if (jsxElement.openingElement == null) {
// Fragment element
return fileContents;
}
let newFileContents = fileContents.slice();
const allNodes = jsxElement.openingElement.attributes;
// Any time there is a spread operator, we need to sort around it... moving it could cause functionality changes
const spreadGroups = (0, sort_utils_js_2.getSpreadGroups)(allNodes);
for (const nodes of spreadGroups) {
const groupings = (0, sort_utils_js_1.getContextGroups)(nodes, comments, fileContents);
groupings.forEach((element) => {
const unsorted = element.nodes;
const sorted = element.nodes.slice().sort((a, b) => {
return (0, sort_utils_js_1.compare)(a.name.name, b.name.name);
});
newFileContents = (0, sort_utils_js_1.reorderValues)(newFileContents, element.comments, unsorted, sorted);
});
}
return newFileContents;
}