Skip to content

Commit

Permalink
[Fix] sort-prop-types: ensure sort-prop-types respects noSortAlphab…
Browse files Browse the repository at this point in the history
…etically
  • Loading branch information
caesar1030 committed Dec 3, 2023
1 parent 2525079 commit 6feac8c
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
### Fixed
* [`require-default-props`]: fix config schema ([#3605][] @controversial)
* [`jsx-curly-brace-presence`]: Revert [#3538][] due to issues with intended string type casting usage ([#3611][] @taozhou-glean)
* [`sort-prop-types`]: ensure sort-prop-types respects noSortAlphabetically ([#3610][] @caesar1030)

[#3611]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3611
[#3610]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3610
[#3605]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3605

## [7.33.0] - 2023.07.19
Expand Down
4 changes: 3 additions & 1 deletion lib/util/propTypesSort.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ function sorter(a, b, context, ignoreCase, requiredFirst, callbacksLast, noSortA
return 0;
}

const commentnodeMap = new WeakMap(); // all nodes reference WeakMap for start and end range

/**
* Fixes sort order of prop types.
*
Expand All @@ -118,10 +120,10 @@ function sorter(a, b, context, ignoreCase, requiredFirst, callbacksLast, noSortA
* @param {Boolean=} ignoreCase whether or not to ignore case when comparing the two elements.
* @param {Boolean=} requiredFirst whether or not to sort required elements first.
* @param {Boolean=} callbacksLast whether or not to sort callbacks after everything else.
* @param {Boolean=} noSortAlphabetically whether or not to disable alphabetical sorting of the elements.
* @param {Boolean=} sortShapeProp whether or not to sort propTypes defined in PropTypes.shape.
* @returns {Object|*|{range, text}} the sort order of the two elements.
*/
const commentnodeMap = new WeakMap(); // all nodes reference WeakMap for start and end range
function fixPropTypesSort(
fixer,
context,
Expand Down
77 changes: 75 additions & 2 deletions tests/lib/rules/sort-prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,19 @@ ruleTester.run('sort-prop-types', rule, {
};
`,
options: [{ sortShapeProp: true }],
},
{
code: `
var Component = createReactClass({
propTypes: {
a: React.PropTypes.string,
c: React.PropTypes.string,
b: React.PropTypes.string,
onChange: React.PropTypes.func,
}
});
`,
options: [{ callbacksLast: true, noSortAlphabetically: true }],
}
)),
invalid: parsers.all([].concat(
Expand Down Expand Up @@ -1888,7 +1901,7 @@ ruleTester.run('sort-prop-types', rule, {
},
{
code: `
var Component = React.createClass({
var Component = createReactClass({
propTypes: {
onChange: React.PropTypes.func,
a: React.PropTypes.string,
Expand All @@ -1898,7 +1911,7 @@ ruleTester.run('sort-prop-types', rule, {
});
`,
output: `
var Component = React.createClass({
var Component = createReactClass({
propTypes: {
a: React.PropTypes.string,
c: React.PropTypes.string,
Expand All @@ -1912,6 +1925,8 @@ ruleTester.run('sort-prop-types', rule, {
{
messageId: 'callbackPropsLast',
line: 4,
column: 13,
type: 'Property',
},
],
},
Expand Down Expand Up @@ -2177,6 +2192,64 @@ ruleTester.run('sort-prop-types', rule, {
type: 'Property',
},
],
} : [],
semver.satisfies(eslintPkg.version, '> 3') ? {
code: `
var Component = createReactClass({
propTypes: {
/* onChange */ onChange: React.PropTypes.func,
/* a */ a: React.PropTypes.string,
/* c */ c: React.PropTypes.string,
/* b */ b: React.PropTypes.string,
}
});
`,
output: `
var Component = createReactClass({
propTypes: {
/* a */ a: React.PropTypes.string,
/* c */ c: React.PropTypes.string,
/* b */ b: React.PropTypes.string,
/* onChange */ onChange: React.PropTypes.func,
}
});
`,
options: [{ callbacksLast: true, noSortAlphabetically: true }],
errors: [
{
messageId: 'callbackPropsLast',
line: 4,
},
],
} : [],
semver.satisfies(eslintPkg.version, '> 3') ? {
code: `
var Component = createReactClass({
propTypes: {
/* onChange */ onChange: React.PropTypes.func /* onChange */,
/* a */ a: React.PropTypes.string /* a */,
/* c */ c: React.PropTypes.string /* c */,
/* b */ b: React.PropTypes.string /* b */,
}
});
`,
output: `
var Component = createReactClass({
propTypes: {
/* a */ a: React.PropTypes.string /* a */,
/* c */ c: React.PropTypes.string /* c */,
/* b */ b: React.PropTypes.string /* b */,
/* onChange */ onChange: React.PropTypes.func /* onChange */,
}
});
`,
options: [{ callbacksLast: true, noSortAlphabetically: true }],
errors: [
{
messageId: 'callbackPropsLast',
line: 4,
},
],
} : []
)),
});

0 comments on commit 6feac8c

Please sign in to comment.