eslint-react
eslint-react copied to clipboard
[feat] Disallow default React import
Describe the problem
Bundlers can't tree-shake well when React is imported using:
import React from 'react'
Replacing it with a star import works better:
import * as React from 'react'
Describe the solution you'd like
Introduce a linter rule, such as 'no-react-default-import', that autofixes the default import to a star import.
Alternatives considered
This can also be banned using no-restricted-imports but then there is no autofix
Additional context
I've written this rule for internal use at Microsoft and would like to upstream it
@NotWoods Use no-restricted-imports from eslint built-in rules:
'no-restricted-imports': [
'error',
{ name: 'react', importNames: ['default'], message: 'Use named import instead' },
],
I mentioned in the alternatives section that I've considered using restricted imports, but it doesn't have an auto fixer.