Skip to content
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

jsx-fragments: allow Fragment syntax option #3813

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
61 changes: 54 additions & 7 deletions lib/rules/jsx-fragments.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const messages = {
fragmentsNotSupported: '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.',
preferPragma: 'Prefer {{react}}.{{fragment}} over fragment shorthand',
preferPragmaShort: 'Prefer {{fragment}} over fragment shorthand',
preferFragment: 'Prefer fragment shorthand over {{react}}.{{fragment}}',
};

Expand All @@ -41,7 +42,7 @@ module.exports = {
messages,

schema: [{
enum: ['syntax', 'element'],
enum: ['syntax', 'element', 'elementShort'],
}],
},

Expand All @@ -53,6 +54,10 @@ module.exports = {
const closeFragShort = '</>';
const openFragLong = `<${reactPragma}.${fragmentPragma}>`;
const closeFragLong = `</${reactPragma}.${fragmentPragma}>`;
const openFragMedium = `<${fragmentPragma}>`;
const closeFragMedium = `</${fragmentPragma}>`;

const reactImports = [];

function reportOnReactVersion(node) {
if (!testReactVersion(context, '>= 16.2.0')) {
Expand All @@ -65,20 +70,49 @@ module.exports = {
return false;
}

function getFixerToLong(jsxFragment) {
function getFixerToLong(jsxFragment, withoutReactPragma) {
if (!jsxFragment.closingFragment || !jsxFragment.openingFragment) {
// the old TS parser crashes here
// TODO: FIXME: can we fake these two descriptors?
return null;
}
return function fix(fixer) {
const closeFrag = withoutReactPragma ? closeFragMedium : closeFragLong;
const openFrag = withoutReactPragma ? openFragMedium : openFragLong;
let source = getText(context);
source = replaceNode(source, jsxFragment.closingFragment, closeFragLong);
source = replaceNode(source, jsxFragment.openingFragment, openFragLong);
const lengthDiff = openFragLong.length - getText(context, jsxFragment.openingFragment).length
+ closeFragLong.length - getText(context, jsxFragment.closingFragment).length;
source = replaceNode(source, jsxFragment.closingFragment, closeFrag);
source = replaceNode(source, jsxFragment.openingFragment, openFrag);
const lengthDiff = openFrag.length - getText(context, jsxFragment.openingFragment).length
+ closeFrag.length - getText(context, jsxFragment.closingFragment).length;
const range = jsxFragment.range;
return fixer.replaceTextRange(range, source.slice(range[0], range[1] + lengthDiff));

const fixes = [];

// Insert the import statement at the top of the file if `withoutReactPragma` is true
if (withoutReactPragma) {
const ancestors = context.getAncestors();
const rootNode = ancestors.length > 0 ? ancestors[0] : jsxFragment;
const reactImport = reactImports.find(importNode => importNode.specifiers.some((spec) => spec.imported && spec.imported.name === fragmentPragma));

if (!reactImport) {
// No `Fragment` import found, so add it
const existingReactImport = reactImports.find(importNode => importNode.source.value === 'react');

if (existingReactImport) {
// If there's already an import from 'react', add `Fragment` to the existing specifiers
const lastSpecifier = existingReactImport.specifiers[existingReactImport.specifiers.length - 1];
fixes.push(fixer.insertTextAfter(lastSpecifier, `, ${fragmentPragma}`));
} else {
// Otherwise, add a new import statement at the top
// eslint-disable-next-line semi
fixes.push(fixer.insertTextBefore(rootNode.body[0], `import { Fragment } from 'react';\n\n`));
}
}
}

fixes.push(fixer.replaceTextRange(range, source.slice(range[0], range[1] + lengthDiff)));

return fixes;
};
}

Expand Down Expand Up @@ -165,10 +199,23 @@ module.exports = {
fix: getFixerToLong(node),
});
}

if (configuration === 'elementShort') {
report(context, messages.preferPragmaShort, 'preferPragmaShort', {
node,
data: {
react: reactPragma,
fragment: fragmentPragma,
},
fix: getFixerToLong(node, true),
});
}
},

ImportDeclaration(node) {
if (node.source && node.source.value === 'react') {
reactImports.push(node);

node.specifiers.forEach((spec) => {
if (spec.imported && spec.imported.name === fragmentPragma) {
if (spec.local) {
Expand Down
Loading