Skip to content

⬅️ Back to Table of Contents

📄 no-import-type-side-effects

📊 Analysis Summary

Metric Count
🔧 Functions 1
📦 Imports 8
📑 Type Aliases 2

📚 Table of Contents

🛠️ File Location:

📂 packages/eslint-plugin/src/rules/no-import-type-side-effects.ts

📤 Default Export

export default createRule<Options, MessageIds>({ ... })
Property Value
name 'no-import-type-side-effects'
meta.type 'problem'
meta.docs.description 'Enforce the use of top-level import type qualifier when an import only has specifiers with inline type qualifiers'
meta.fixable 'code'
meta.messages.useTopLevelQualifier 'TypeScript will only remove the inline type specifiers which will leave behind a side effect import at runtime. Conv...
meta.schema []
defaultOptions []

Entry point: create — documented under Functions.


📦 Imports

Name Source
TSESLint @typescript-eslint/utils
TSESTree @typescript-eslint/utils
AST_NODE_TYPES @typescript-eslint/utils
createRule ../util
isImportKeyword ../util
isTypeKeyword ../util
nullThrows ../util
NullThrowsReasons ../util

Functions

create(context: any): { 'ImportDeclaration[importKind!="type"]'(node: TSESTree.Im…

Parameters:

  • context any

Returns: { 'ImportDeclaration[importKind!="type"]'(node: TSESTree.ImportDeclaration): void; }

Calls:

  • specifiers.push
  • context.report
  • nullThrows (from ../util)
  • context.sourceCode.getFirstToken
  • NullThrowsReasons.MissingToken
  • fixes.push
  • fixer.removeRange
  • fixer.insertTextAfter
Code
create(context) {
    return {
      'ImportDeclaration[importKind!="type"]'(
        node: TSESTree.ImportDeclaration,
      ): void {
        if (node.specifiers.length === 0) {
          return;
        }

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

        context.report({
          node,
          messageId: 'useTopLevelQualifier',
          fix(fixer) {
            const fixes: TSESLint.RuleFix[] = [];
            for (const specifier of specifiers) {
              const qualifier = nullThrows(
                context.sourceCode.getFirstToken(specifier, isTypeKeyword),
                NullThrowsReasons.MissingToken(
                  'type keyword',
                  'import specifier',
                ),
              );
              fixes.push(
                fixer.removeRange([
                  qualifier.range[0],
                  specifier.imported.range[0],
                ]),
              );
            }

            const importKeyword = nullThrows(
              context.sourceCode.getFirstToken(node, isImportKeyword),
              NullThrowsReasons.MissingToken('import keyword', 'import'),
            );
            fixes.push(fixer.insertTextAfter(importKeyword, ' type'));

            return fixes;
          },
        });
      },
    };
  }

Type Aliases

Options

type Options = [];

MessageIds

type MessageIds = 'useTopLevelQualifier';

Generated by Syntax Scribe