Angular Modernizer
    Preparing search index...

    Adds, removes and organizes imports in TypeScript source files.

    • ng-morph is used only to add entries to the imports: [] metadata arrays of @Component and @NgModule decorators.
    • ts-morph handles ES import statements: adding, removing, merging, aliases and non-Angular imports.
    Index

    Constructors

    • Constructor for ImportManager.

      Parameters

      • OptionalngMorphAdapter: NgMorphAdapter

        Optional ng-morph adapter for Angular-specific operations

        BACKWARD COMPATIBILITY: Constructor is optional - if not provided, falls back to pure ts-morph.

      Returns ImportManager

    Methods

    • Add a named import to a source file. Handles merging with existing imports from the same module.

      HYBRID APPROACH:

      • Angular imports (@angular/*): Try ng-morph for components/modules
      • Non-Angular/complex: Fall back to ts-morph

      Parameters

      • sourceFile: SourceFile

        The source file to modify

      • moduleSpecifier: string

        The module to import from (e.g., '@angular/common')

      • namedImport: string

        The named import to add (e.g., 'CommonModule')

      • Optionalalias: string

        Optional alias for the import (e.g., 'NgComponent')

      Returns void

      importManager.addNamedImport(sourceFile, '@angular/common', 'CommonModule');
      // Result: import { CommonModule } from '@angular/common';

      importManager.addNamedImport(sourceFile, '@angular/core', 'Component', 'NgComponent');
      // Result: import { Component as NgComponent } from '@angular/core';

      BEFORE (ts-morph): 30 lines with manual checking AFTER (hybrid): ~20 lines with ng-morph for Angular imports

    • Remove import declarations that have no imports.

      Cleans up empty import statements that may result from refactoring.

      Parameters

      • sourceFile: SourceFile

        The source file

      Returns void

      // Before: import {} from '@angular/core';

      importManager.cleanupEmptyImports(sourceFile);

      // After: (empty import removed)
    • Get the alias for a named import, if it exists.

      Parameters

      • sourceFile: SourceFile

        The source file

      • symbol: string

        The symbol name

      • Optionalmodule: string

        Optional module specifier

      Returns string | undefined

      The alias if found, undefined otherwise

      // import { Component as NgComponent } from '@angular/core';
      const alias = importManager.getImportAlias(sourceFile, 'Component', '@angular/core');
      // Returns 'NgComponent'
    • Get all imports from a source file.

      Parameters

      • sourceFile: SourceFile

        The source file

      Returns ImportInfo[]

      Array of import information

      Extracts:

      • All named imports with their module specifiers
      • Default imports and namespace imports
      • Import declaration positions for organization
      const imports = importManager.getImports(sourceFile);
      // Returns:
      // [
      // { moduleSpecifier: '@angular/core', namedImports: ['Component', 'OnInit'] },
      // { moduleSpecifier: 'rxjs', namedImports: ['Observable'], defaultImport: 'Rx' }
      // ]
    • Check if a named import exists in the source file.

      Parameters

      • sourceFile: SourceFile

        The source file

      • symbol: string

        The symbol name to check

      • Optionalmodule: string

        Optional module specifier to check specifically

      Returns boolean

      True if the import exists

      const exists = importManager.hasImport(sourceFile, 'Component', '@angular/core');
      // Returns true if 'import { Component } from '@angular/core';' exists
    • Merge duplicate import declarations.

      Combines multiple import statements from the same module into one.

      Parameters

      • sourceFile: SourceFile

        The source file

      Returns void

      // Before:
      // import { Component } from '@angular/core';
      // import { Injectable } from '@angular/core';

      importManager.mergeImports(sourceFile);

      // After:
      // import { Component, Injectable } from '@angular/core';
    • Organize imports (sort and remove duplicates).

      Parameters

      • sourceFile: SourceFile

        The source file

        Handles:

        • Sorting imports by module specifier (external libraries first, then internal)
        • Removing duplicate imports
        • Grouping related imports
        • Maintaining proper spacing and formatting

      Returns void

      // Before:
      // import { Component } from '@angular/core';
      // import { MyService } from './my-service';
      // import { Injectable } from '@angular/core';

      importManager.organizeImports(sourceFile);

      // After:
      // import { Component, Injectable } from '@angular/core';
      // import { MyService } from './my-service';
    • Remove a named import from a source file. Cleans up empty import declarations after removal.

      HYBRID APPROACH:

      • Always uses ts-morph for removal (simpler than ng-morph)
      • ng-morph doesn't provide significant benefits for removal operations

      Parameters

      • sourceFile: SourceFile

        The source file

      • namedImport: string

        The named import to remove

      • OptionalmoduleSpecifier: string

        Optional module specifier to target specific imports

        Handles:

        • Removing specific named imports from import declarations
        • Cleaning up empty import declarations
        • Preserving other imports from the same module

      Returns void

      // Before: import { Foo, Bar } from './module';
      importManager.removeNamedImport(sourceFile, 'Bar', './module');
      // After: import { Foo } from './module';