-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(react): sort JSX props in a-z order
- Loading branch information
Jamie Mason
committed
May 9, 2016
1 parent
d0811d8
commit 01a2944
Showing
1 changed file
with
30 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |