Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-authored-by: Josh Goldberg <git@joshuakgoldberg.com>
  • Loading branch information
bradzacher and JoshuaKGoldberg committed Jan 31, 2023
1 parent fb41743 commit 3848375
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
Expand Up @@ -6,7 +6,8 @@ description: 'Enforce the use of top-level import type qualifier when an import
>
> See **https://typescript-eslint.io/rules/no-type-imports-with-verbatim-side-effects** for documentation.
The [`--verbatimModuleSyntax`](https://www.typescriptlang.org/tsconfig#verbatimModuleSyntax) compiler option causes TypeScript to do simple and predictable transpilation on import declarations, namely it completely removes import declarations with a top-level `type` qualifier, and it removes any import specifiers with an inline `type` qualifier.
The [`--verbatimModuleSyntax`](https://www.typescriptlang.org/tsconfig#verbatimModuleSyntax) compiler option causes TypeScript to do simple and predictable transpilation on import declarations.
Namely, it completely removes import declarations with a top-level `type` qualifier, and it removes any import specifiers with an inline `type` qualifier.

The latter behavior does have one potentially surprising effect in that in certain cases TS can leave behind a "side effect" import at runtime:

Expand All @@ -20,7 +21,7 @@ import {} from 'mod';
import 'mod';
```

For some rare cases, this may be desirable - but for many cases you will not want to leave behind an unnecessary side effect import.
For the rare case of needing to import for side effects, this may be desirable - but for most cases you will not want to leave behind an unnecessary side effect import.

## Examples

Expand Down
Expand Up @@ -26,19 +26,16 @@ export default util.createRule<Options, MessageIds>({
create(context) {
const sourceCode = context.getSourceCode();
return {
ImportDeclaration(node): void {
if (node.importKind === 'type') {
return;
}
'ImportDeclaration[importKind!="type"]'(
node: TSESTree.ImportDeclaration,
): void {

const specifiers: TSESTree.ImportSpecifier[] = [];
for (const specifier of node.specifiers) {
if (specifier.type !== AST_NODE_TYPES.ImportSpecifier) {
return;
}
if (specifier.importKind !== 'type') {
return;
}
if (
specifier.type !== AST_NODE_TYPES.ImportSpecifier ||
specifier.importKind !== 'type'
) {
specifiers.push(specifier);
}

Expand Down

0 comments on commit 3848375

Please sign in to comment.