Skip to content

Commit 8e9dd0f

Browse files
committed
Fix noSortAlphabetically not respected in sort-prop-types
1 parent 1a3a17a commit 8e9dd0f

File tree

3 files changed

+53
-11
lines changed

3 files changed

+53
-11
lines changed

lib/rules/sort-prop-types.js

+1
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ module.exports = {
114114
ignoreCase,
115115
requiredFirst,
116116
callbacksLast,
117+
noSortAlphabetically,
117118
sortShapeProp
118119
);
119120
}

lib/util/propTypesSort.js

+23-11
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,10 @@ function getShapeProperties(node) {
6969
* @param {Boolean=} ignoreCase whether or not to ignore case when comparing the two elements.
7070
* @param {Boolean=} requiredFirst whether or not to sort required elements first.
7171
* @param {Boolean=} callbacksLast whether or not to sort callbacks after everything else.
72+
* @param {Boolean=} noSortAlphabetically whether or not to disable alphabetical sorting of the elements.
7273
* @returns {Number} the sort order of the two elements.
7374
*/
74-
function sorter(a, b, context, ignoreCase, requiredFirst, callbacksLast) {
75+
function sorter(a, b, context, ignoreCase, requiredFirst, callbacksLast, noSortAlphabetically) {
7576
const aKey = String(astUtil.getKeyValue(context, a));
7677
const bKey = String(astUtil.getKeyValue(context, b));
7778

@@ -93,15 +94,17 @@ function sorter(a, b, context, ignoreCase, requiredFirst, callbacksLast) {
9394
}
9495
}
9596

96-
if (ignoreCase) {
97-
return aKey.localeCompare(bKey);
98-
}
97+
if (!noSortAlphabetically) {
98+
if (ignoreCase) {
99+
return aKey.localeCompare(bKey);
100+
}
99101

100-
if (aKey < bKey) {
101-
return -1;
102-
}
103-
if (aKey > bKey) {
104-
return 1;
102+
if (aKey < bKey) {
103+
return -1;
104+
}
105+
if (aKey > bKey) {
106+
return 1;
107+
}
105108
}
106109
return 0;
107110
}
@@ -119,7 +122,16 @@ function sorter(a, b, context, ignoreCase, requiredFirst, callbacksLast) {
119122
* @returns {Object|*|{range, text}} the sort order of the two elements.
120123
*/
121124
const commentnodeMap = new WeakMap(); // all nodes reference WeakMap for start and end range
122-
function fixPropTypesSort(fixer, context, declarations, ignoreCase, requiredFirst, callbacksLast, sortShapeProp) {
125+
function fixPropTypesSort(
126+
fixer,
127+
context,
128+
declarations,
129+
ignoreCase,
130+
requiredFirst,
131+
callbacksLast,
132+
noSortAlphabetically,
133+
sortShapeProp
134+
) {
123135
function sortInSource(allNodes, source) {
124136
const originalSource = source;
125137
const sourceCode = context.getSourceCode();
@@ -161,7 +173,7 @@ function fixPropTypesSort(fixer, context, declarations, ignoreCase, requiredFirs
161173
nodeGroups.forEach((nodes) => {
162174
const sortedAttributes = toSorted(
163175
nodes,
164-
(a, b) => sorter(a, b, context, ignoreCase, requiredFirst, callbacksLast)
176+
(a, b) => sorter(a, b, context, ignoreCase, requiredFirst, callbacksLast, noSortAlphabetically)
165177
);
166178

167179
source = nodes.reduceRight((acc, attr, index) => {

tests/lib/rules/sort-prop-types.js

+29
Original file line numberDiff line numberDiff line change
@@ -1886,6 +1886,35 @@ ruleTester.run('sort-prop-types', rule, {
18861886
},
18871887
],
18881888
},
1889+
{
1890+
code: `
1891+
var Component = React.createClass({
1892+
propTypes: {
1893+
onChange: React.PropTypes.func,
1894+
a: React.PropTypes.string,
1895+
c: React.PropTypes.string,
1896+
b: React.PropTypes.string,
1897+
}
1898+
});
1899+
`,
1900+
output: `
1901+
var Component = React.createClass({
1902+
propTypes: {
1903+
a: React.PropTypes.string,
1904+
c: React.PropTypes.string,
1905+
b: React.PropTypes.string,
1906+
onChange: React.PropTypes.func,
1907+
}
1908+
});
1909+
`,
1910+
options: [{ callbacksLast: true, noSortAlphabetically: true }],
1911+
errors: [
1912+
{
1913+
messageId: 'callbackPropsLast',
1914+
line: 4,
1915+
},
1916+
],
1917+
},
18891918
semver.satisfies(eslintPkg.version, '> 3') ? {
18901919
code: `
18911920
var First = createReactClass({

0 commit comments

Comments
 (0)