Skip to content

Commit

Permalink
feat(react): sort JSX props in a-z order
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamie Mason committed May 9, 2016
1 parent d0811d8 commit 01a2944
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions transforms/sort-jsx-props.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module.exports = function (file, api) {
var root = api.jscodeshift(file.source);

root.find(api.jscodeshift.JSXOpeningElement)
.forEach(sortJSXProps);

return root.toSource();
};

function sortJSXProps (node) {
node.value.attributes = node.value.attributes.sort(sortJSXPropsByName);
}

function sortJSXPropsByName (a, b) {
return applySort(getPropNameForSort(a), getPropNameForSort(b));
}

function getPropNameForSort (attr) {
return attr.name ? attr.name.name : '...spread';
}

function applySort (a, b) {
if (a < b) {
return -1;
}
if (a > b) {
return 1;
}
return 0;
}

0 comments on commit 01a2944

Please sign in to comment.