Skip to content

Commit

Permalink
[Fix] jsx-sort-props: avoid a crash with spread props
Browse files Browse the repository at this point in the history
Fixes #3376
  • Loading branch information
ljharb committed Aug 31, 2022
1 parent 3a237b0 commit d3aa050
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Expand Up @@ -7,11 +7,12 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange

### Fixed
* [`jsx-key`]: avoid a crash with optional chaining ([#3371][] @ljharb)

* [`jsx-sort-props`]: avoid a crash with spread props ([#3376][] @ljharb)

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

[#3376]: https://github.com/jsx-eslint/eslint-plugin-react/issues/3376
[#3375]: https://github.com/jsx-eslint/eslint-plugin-react/issues/3375
[#3371]: https://github.com/jsx-eslint/eslint-plugin-react/issues/3371

Expand Down
6 changes: 4 additions & 2 deletions lib/rules/jsx-sort-props.js
Expand Up @@ -258,8 +258,10 @@ function generateFixerFunction(node, context, reservedList) {

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

const rangeStart = fixers[fixers.length - 1].range[0];
const rangeEnd = fixers[0].range[1];
const firstFixer = fixers[0];
const lastFixer = fixers[fixers.length - 1];
const rangeStart = lastFixer ? lastFixer.range[0] : 0;
const rangeEnd = firstFixer ? firstFixer.range[1] : -0;

fixers.forEach((fix) => {
source = `${source.substr(0, fix.range[0])}${fix.text}${source.substr(fix.range[1])}`;
Expand Down
30 changes: 29 additions & 1 deletion tests/lib/rules/jsx-sort-props.js
Expand Up @@ -1073,6 +1073,34 @@ ruleTester.run('jsx-sort-props', rule, {
line: 2,
},
],
} : []
} : [],
{
code: `
<Page
// Pass all the props to the Page component.
{...props}
// Use the platform specific props from the doc.ts file.
{...TemplatePageProps[platform]}
// Use the getSubTitle helper function to get the page header subtitle from the active platform.
subTitle={getSubTitle(platform)}
// You can define custom sections using the \`otherSections\` prop.
// Here it is using a method that takes the platform as an argument to return the correct array of section props.
otherSections={_otherSections(platform) as IPageSectionProps[]}
// You can hide the side rail by setting \`showSideRail\` to false.
// showSideRail={false}
// You can pass a custom className to the page wrapper if needed.
// className="customPageClassName"
/>
`,
features: ['ts', 'no-babel-old'],
errors: [
{
messageId: 'sortPropsByAlpha',
line: 11,
},
],
}
)),
});

0 comments on commit d3aa050

Please sign in to comment.