eslint-plugin-react icon indicating copy to clipboard operation
eslint-plugin-react copied to clipboard

[react/jsx-fragments] Enforce <Fragment>

Open thany opened this issue 6 years ago • 7 comments

I would like to enforce using <Fragment>...</Fragment> instead of <React.Fragment>...</React.Fragment>. The fix option would mean to also add it to the imports if it hasn't been already.

So these would be a warning:

import React from 'react';

export default Component = () => (
    <>Foo</>
);
import React from 'react';

export default Component = () => (
    <React.Fragment>Foo</React.Fragment>
);

This would be the fix: (and should be fixable with --fix)

import React, { Fragment } from 'react';

export default Component = () => (
    <Fragment>Foo</Fragment>
);

Is this achievable?

thany avatar Sep 26 '19 14:09 thany

That would be the same kind of rule as Component vs React.Component, and also for everything else chained off of React including hooks. I think there’d need to be a new rule for this, that had an option for each property independently.

ljharb avatar Sep 26 '19 15:09 ljharb

Yes, I agree a more generic rule for this would be preferable. I do think some magic has to happen to make that new rule's fixes process after this one's. Because this rule processes <> into <React.Fragment> and that new rule would then further process it into <Fragment> and an added import.

thany avatar Sep 27 '19 08:09 thany

That's not a problem; eslint often requires multiple passes (editor integrations often don't do this, but the command line does).

ljharb avatar Sep 27 '19 18:09 ljharb

Did anyone ever start on this? Or is there a way to currently achieve this behavior?

dbrxnds avatar Aug 30 '22 12:08 dbrxnds

Nope, the issue is open and it has a help wanted label, so it’s just waiting for a PR.

ljharb avatar Aug 30 '22 14:08 ljharb