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

[feat] Disallow default React import

Open NotWoods opened this issue 1 year ago • 2 comments

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 avatar Sep 10 '24 20:09 NotWoods

@NotWoods Use no-restricted-imports from eslint built-in rules:

  'no-restricted-imports': [
    'error',
    { name: 'react', importNames: ['default'], message: 'Use named import instead' },
  ],

SukkaW avatar Sep 14 '24 11:09 SukkaW

I mentioned in the alternatives section that I've considered using restricted imports, but it doesn't have an auto fixer.

NotWoods avatar Sep 15 '24 13:09 NotWoods