You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Because it's valid - all ESLint parsers allow it for value imports.
Parsers that handle type-only imports (flow/hermes/typescript/babel) also allow an empty named import block: import type {} from 'mod', (flow only) import typeof {} from 'mod'.
I suggest we build a new rule, no-empty-named-blocks, which bans these empty import blocks entirely.
Examples
✅ Valid
import'mod';importDefaultfrom'mod';import{Named}from'mod';importDefault,{Named}from'mod';import*asNamespacefrom'mod';importtypeDefaultfrom'mod';importtype{Named}from'mod';importtypeDefault,{Named}from'mod';// note - invalid TS but valid Flowimporttype*asNamespacefrom'mod';// note - valid TS but invalid Flow// flow onlyimporttypeofDefaultfrom'mod';importtypeof{Named}from'mod';importtypeofDefault,{Named}from'mod';
❌ Invalid
import{}from'mod';importDefault,{}from'mod';importtype{}from'mod';importtypeDefault,{}from'mod';// note - invalid TS but valid Flow// flow onlyimporttypeof{}from'mod';importtypeofDefault,{}from'mod';
Fixer
There are 3 cases for us to consider here:
First, an import with a default and an empty named block (either value, type, or typeof).
This can always be safely fixed to remove the empty block:
- import Default, {} from 'mod';+ import Default from 'mod';
// flow only
- import type Default, {} from 'mod';+ import type Default from 'mod';
// flow only
- import typeof Default, {} from 'mod';+ import typeof Default from 'mod';
Second, a type-/typeof-only import with just an empty named block.
This can always be removed in its entirety as type-/typeof-only imports never have side-effects
- import type {} from 'mod';+
Finally a value import with just an empty block.
This cannot be safely deleted because even without any specifiers the import will still trigger side-effects. Instead of an autofixer, we can use a suggestion fixer with two suggestions:
Delete the import (fix by deleting the entire statement)
- import {} from 'mod';+
Convert to a side-effect import (fix by deleting the {} from)
- import {} from 'mod';+ import 'mod';
The text was updated successfully, but these errors were encountered:
Spinning this off of our discussion here #2473 (comment)
According to the ES6 spec an empty named import block
import {} from 'mod'
is valid syntax:Because it's valid - all ESLint parsers allow it for value imports.
Parsers that handle type-only imports (flow/hermes/typescript/babel) also allow an empty named import block:
import type {} from 'mod'
, (flow only)import typeof {} from 'mod'
.I suggest we build a new rule,
no-empty-named-blocks
, which bans these empty import blocks entirely.Examples
✅ Valid
❌ Invalid
Fixer
There are 3 cases for us to consider here:
First, an import with a default and an empty named block (either value, type, or typeof).
This can always be safely fixed to remove the empty block:
Second, a type-/typeof-only import with just an empty named block.
This can always be removed in its entirety as type-/typeof-only imports never have side-effects
Finally a value import with just an empty block.
This cannot be safely deleted because even without any specifiers the import will still trigger side-effects. Instead of an autofixer, we can use a suggestion fixer with two suggestions:
{} from
)The text was updated successfully, but these errors were encountered: