Skip to content

[Fix] avoid crashing on self-closing fragments #2113

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions lib/rules/jsx-fragments.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,18 @@ module.exports = {
function getFixerToShort(jsxElement) {
return function(fixer) {
let source = sourceCode.getText();
source = replaceNode(source, jsxElement.closingElement, closeFragShort);
source = replaceNode(source, jsxElement.openingElement, openFragShort);
const lengthDiff = sourceCode.getText(jsxElement.openingElement).length - openFragShort.length
+ sourceCode.getText(jsxElement.closingElement).length - closeFragShort.length;
let lengthDiff;
if (jsxElement.closingElement) {
source = replaceNode(source, jsxElement.closingElement, closeFragShort);
source = replaceNode(source, jsxElement.openingElement, openFragShort);
lengthDiff = sourceCode.getText(jsxElement.openingElement).length - openFragShort.length
+ sourceCode.getText(jsxElement.closingElement).length - closeFragShort.length;
} else {
source = replaceNode(source, jsxElement.openingElement, `${openFragShort}${closeFragShort}`);
lengthDiff = sourceCode.getText(jsxElement.openingElement).length - openFragShort.length
- closeFragShort.length;
}

const range = jsxElement.range;
return fixer.replaceTextRange(range, source.slice(range[0], range[1] - lengthDiff));
};
Expand Down
37 changes: 37 additions & 0 deletions tests/lib/rules/jsx-fragments.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ ruleTester.run('jsx-fragments', rule, {
code: '<Act.Frag><Foo /></Act.Frag>',
options: ['element'],
settings
}, {
code: '<Act.Frag />',
options: ['element'],
settings
}, {
code: `
import Act, { Frag as F } from 'react';
Expand Down Expand Up @@ -81,6 +85,10 @@ ruleTester.run('jsx-fragments', rule, {
code: '<Act.Frag key="key"><Foo /></Act.Frag>',
options: ['syntax'],
settings
}, {
code: '<Act.Frag key="key" />',
options: ['syntax'],
settings
}],

invalid: [{
Expand All @@ -98,6 +106,13 @@ ruleTester.run('jsx-fragments', rule, {
message: 'Fragments are only supported starting from React v16.2. '
+ 'Please disable the `react/jsx-fragments` rule in ESLint settings or upgrade your version of React.'
}]
}, {
code: '<Act.Frag />',
settings: settingsOld,
errors: [{
message: 'Fragments are only supported starting from React v16.2. '
+ 'Please disable the `react/jsx-fragments` rule in ESLint settings or upgrade your version of React.'
}]
}, {
code: '<><Foo /></>',
parser: 'babel-eslint',
Expand All @@ -115,6 +130,28 @@ ruleTester.run('jsx-fragments', rule, {
message: 'Prefer fragment shorthand over Act.Frag'
}],
output: '<><Foo /></>'
}, {
code: '<Act.Frag />',
options: ['syntax'],
settings,
errors: [{
message: 'Prefer fragment shorthand over Act.Frag'
}],
output: '<></>'
}, {
code: `
import Act, { Frag as F } from 'react';
<F />;
`,
options: ['syntax'],
settings,
errors: [{
message: 'Prefer fragment shorthand over Act.Frag'
}],
output: `
import Act, { Frag as F } from 'react';
<></>;
`
}, {
code: `
import Act, { Frag as F } from 'react';
Expand Down