diff --git a/CHANGELOG.md b/CHANGELOG.md index 3f0f0df401..d38bc17c8a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/rules/jsx-sort-props.js b/lib/rules/jsx-sort-props.js index 9a55d32af7..74929cc60e 100644 --- a/lib/rules/jsx-sort-props.js +++ b/lib/rules/jsx-sort-props.js @@ -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])}`; diff --git a/tests/lib/rules/jsx-sort-props.js b/tests/lib/rules/jsx-sort-props.js index 3a7391e24d..1dc9d270a6 100644 --- a/tests/lib/rules/jsx-sort-props.js +++ b/tests/lib/rules/jsx-sort-props.js @@ -1073,6 +1073,34 @@ ruleTester.run('jsx-sort-props', rule, { line: 2, }, ], - } : [] + } : [], + { + code: ` + + `, + features: ['ts', 'no-babel-old'], + errors: [ + { + messageId: 'sortPropsByAlpha', + line: 11, + }, + ], + } )), });