eslint-plugin-react: jsx-uses-react rule does not detect jsxFrag rule
In babel, if one does not use jsxFrag, fragment syntax always requires React import (to use React.Fragment).
However, jsx-uses-react rule does not check this pragma and does not require React import when jsx pragma is set.
I think the rule should check jsxFrag pragma too and mark React as used if there is no jsxFrag pragma.
About this issue
- Original URL
- State: open
- Created 5 years ago
- Comments: 16 (7 by maintainers)
If this is still causing anyone pain (especially https://github.com/emotion-js/emotion/issues/1549 folks, since #2158 appears to be stalled), I threw together a plugin that re-implements
jsx-uses-reactandreact-in-jsx-scopefor Fragments specifically. This could be useful as a reference implementation for this issue, but I didn’t feel like I understood the other ecosystem implications of howjsxFragis used. @justingrantIt could be possible to implement something more general for
jsxFragsupport injsx-uses-reactandreact-in-jsx-scope, but I’m not sure how this fits into everything else.@Ailrun @ljharb - I ended up here after running into https://github.com/emotion-js/emotion/issues/1549. If I’m understanding that issue, the
<>shorthand forFragmentis broken with the current versions of emotion and eslint-plugin-react, because:import React from 'react'and don’t otherwise useReact, then eslint will complain that'React' is defined but never used. (no-unused-vars)(example: https://codesandbox.io/s/pensive-edison-bdkpp)import React from 'react', then you’ll get a runtime errorReact is not definedbecauseReact.Fragmentis emitted into the final transpiled code withoutReactbeing imported.In this comment, @Andarist says that emotion can’t solve (2), and recommends that
eslint-plugin-reactcould help . Is there already aneslint-plugin-reactrule that can solve (2) instead, presumably by not triggering theno-unused-varsif the unused var isReactand the file contains<>? Or is that solution what #2158 is trying to address? If the latter, then what’s needed to get #2158 over the finish line?I suppose that makes sense, but it’s a bunch of extra complexity.