Skip to content

Commit 6feac8c

Browse files
committed
[Fix] sort-prop-types: ensure sort-prop-types respects noSortAlphabetically
1 parent 2525079 commit 6feac8c

File tree

3 files changed

+80
-3
lines changed

3 files changed

+80
-3
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
88
### Fixed
99
* [`require-default-props`]: fix config schema ([#3605][] @controversial)
1010
* [`jsx-curly-brace-presence`]: Revert [#3538][] due to issues with intended string type casting usage ([#3611][] @taozhou-glean)
11+
* [`sort-prop-types`]: ensure sort-prop-types respects noSortAlphabetically ([#3610][] @caesar1030)
1112

1213
[#3611]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3611
14+
[#3610]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3610
1315
[#3605]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3605
1416

1517
## [7.33.0] - 2023.07.19

lib/util/propTypesSort.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ function sorter(a, b, context, ignoreCase, requiredFirst, callbacksLast, noSortA
109109
return 0;
110110
}
111111

112+
const commentnodeMap = new WeakMap(); // all nodes reference WeakMap for start and end range
113+
112114
/**
113115
* Fixes sort order of prop types.
114116
*
@@ -118,10 +120,10 @@ function sorter(a, b, context, ignoreCase, requiredFirst, callbacksLast, noSortA
118120
* @param {Boolean=} ignoreCase whether or not to ignore case when comparing the two elements.
119121
* @param {Boolean=} requiredFirst whether or not to sort required elements first.
120122
* @param {Boolean=} callbacksLast whether or not to sort callbacks after everything else.
123+
* @param {Boolean=} noSortAlphabetically whether or not to disable alphabetical sorting of the elements.
121124
* @param {Boolean=} sortShapeProp whether or not to sort propTypes defined in PropTypes.shape.
122125
* @returns {Object|*|{range, text}} the sort order of the two elements.
123126
*/
124-
const commentnodeMap = new WeakMap(); // all nodes reference WeakMap for start and end range
125127
function fixPropTypesSort(
126128
fixer,
127129
context,

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

+75-2
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,19 @@ ruleTester.run('sort-prop-types', rule, {
467467
};
468468
`,
469469
options: [{ sortShapeProp: true }],
470+
},
471+
{
472+
code: `
473+
var Component = createReactClass({
474+
propTypes: {
475+
a: React.PropTypes.string,
476+
c: React.PropTypes.string,
477+
b: React.PropTypes.string,
478+
onChange: React.PropTypes.func,
479+
}
480+
});
481+
`,
482+
options: [{ callbacksLast: true, noSortAlphabetically: true }],
470483
}
471484
)),
472485
invalid: parsers.all([].concat(
@@ -1888,7 +1901,7 @@ ruleTester.run('sort-prop-types', rule, {
18881901
},
18891902
{
18901903
code: `
1891-
var Component = React.createClass({
1904+
var Component = createReactClass({
18921905
propTypes: {
18931906
onChange: React.PropTypes.func,
18941907
a: React.PropTypes.string,
@@ -1898,7 +1911,7 @@ ruleTester.run('sort-prop-types', rule, {
18981911
});
18991912
`,
19001913
output: `
1901-
var Component = React.createClass({
1914+
var Component = createReactClass({
19021915
propTypes: {
19031916
a: React.PropTypes.string,
19041917
c: React.PropTypes.string,
@@ -1912,6 +1925,8 @@ ruleTester.run('sort-prop-types', rule, {
19121925
{
19131926
messageId: 'callbackPropsLast',
19141927
line: 4,
1928+
column: 13,
1929+
type: 'Property',
19151930
},
19161931
],
19171932
},
@@ -2177,6 +2192,64 @@ ruleTester.run('sort-prop-types', rule, {
21772192
type: 'Property',
21782193
},
21792194
],
2195+
} : [],
2196+
semver.satisfies(eslintPkg.version, '> 3') ? {
2197+
code: `
2198+
var Component = createReactClass({
2199+
propTypes: {
2200+
/* onChange */ onChange: React.PropTypes.func,
2201+
/* a */ a: React.PropTypes.string,
2202+
/* c */ c: React.PropTypes.string,
2203+
/* b */ b: React.PropTypes.string,
2204+
}
2205+
});
2206+
`,
2207+
output: `
2208+
var Component = createReactClass({
2209+
propTypes: {
2210+
/* a */ a: React.PropTypes.string,
2211+
/* c */ c: React.PropTypes.string,
2212+
/* b */ b: React.PropTypes.string,
2213+
/* onChange */ onChange: React.PropTypes.func,
2214+
}
2215+
});
2216+
`,
2217+
options: [{ callbacksLast: true, noSortAlphabetically: true }],
2218+
errors: [
2219+
{
2220+
messageId: 'callbackPropsLast',
2221+
line: 4,
2222+
},
2223+
],
2224+
} : [],
2225+
semver.satisfies(eslintPkg.version, '> 3') ? {
2226+
code: `
2227+
var Component = createReactClass({
2228+
propTypes: {
2229+
/* onChange */ onChange: React.PropTypes.func /* onChange */,
2230+
/* a */ a: React.PropTypes.string /* a */,
2231+
/* c */ c: React.PropTypes.string /* c */,
2232+
/* b */ b: React.PropTypes.string /* b */,
2233+
}
2234+
});
2235+
`,
2236+
output: `
2237+
var Component = createReactClass({
2238+
propTypes: {
2239+
/* a */ a: React.PropTypes.string /* a */,
2240+
/* c */ c: React.PropTypes.string /* c */,
2241+
/* b */ b: React.PropTypes.string /* b */,
2242+
/* onChange */ onChange: React.PropTypes.func /* onChange */,
2243+
}
2244+
});
2245+
`,
2246+
options: [{ callbacksLast: true, noSortAlphabetically: true }],
2247+
errors: [
2248+
{
2249+
messageId: 'callbackPropsLast',
2250+
line: 4,
2251+
},
2252+
],
21802253
} : []
21812254
)),
21822255
});

0 commit comments

Comments
 (0)