Skip to content

Commit d3aa050

Browse files
committed
[Fix] jsx-sort-props: avoid a crash with spread props
Fixes #3376
1 parent 3a237b0 commit d3aa050

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
77

88
### Fixed
99
* [`jsx-key`]: avoid a crash with optional chaining ([#3371][] @ljharb)
10-
10+
* [`jsx-sort-props`]: avoid a crash with spread props ([#3376][] @ljharb)
1111

1212
### Changed
1313
* [Docs] [`jsx-sort-propts`]: replace ref string with ref variable ([#3375][] @Luccasoli)
1414

15+
[#3376]: https://github.com/jsx-eslint/eslint-plugin-react/issues/3376
1516
[#3375]: https://github.com/jsx-eslint/eslint-plugin-react/issues/3375
1617
[#3371]: https://github.com/jsx-eslint/eslint-plugin-react/issues/3371
1718

lib/rules/jsx-sort-props.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,8 +258,10 @@ function generateFixerFunction(node, context, reservedList) {
258258

259259
fixers.sort((a, b) => b.range[0] - a.range[0]);
260260

261-
const rangeStart = fixers[fixers.length - 1].range[0];
262-
const rangeEnd = fixers[0].range[1];
261+
const firstFixer = fixers[0];
262+
const lastFixer = fixers[fixers.length - 1];
263+
const rangeStart = lastFixer ? lastFixer.range[0] : 0;
264+
const rangeEnd = firstFixer ? firstFixer.range[1] : -0;
263265

264266
fixers.forEach((fix) => {
265267
source = `${source.substr(0, fix.range[0])}${fix.text}${source.substr(fix.range[1])}`;

tests/lib/rules/jsx-sort-props.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1073,6 +1073,34 @@ ruleTester.run('jsx-sort-props', rule, {
10731073
line: 2,
10741074
},
10751075
],
1076-
} : []
1076+
} : [],
1077+
{
1078+
code: `
1079+
<Page
1080+
// Pass all the props to the Page component.
1081+
{...props}
1082+
// Use the platform specific props from the doc.ts file.
1083+
{...TemplatePageProps[platform]}
1084+
// Use the getSubTitle helper function to get the page header subtitle from the active platform.
1085+
subTitle={getSubTitle(platform)}
1086+
// You can define custom sections using the \`otherSections\` prop.
1087+
// Here it is using a method that takes the platform as an argument to return the correct array of section props.
1088+
otherSections={_otherSections(platform) as IPageSectionProps[]}
1089+
1090+
// You can hide the side rail by setting \`showSideRail\` to false.
1091+
// showSideRail={false}
1092+
1093+
// You can pass a custom className to the page wrapper if needed.
1094+
// className="customPageClassName"
1095+
/>
1096+
`,
1097+
features: ['ts', 'no-babel-old'],
1098+
errors: [
1099+
{
1100+
messageId: 'sortPropsByAlpha',
1101+
line: 11,
1102+
},
1103+
],
1104+
}
10771105
)),
10781106
});

0 commit comments

Comments
 (0)