Constructor for ImportManager.
OptionalngMorphAdapter: NgMorphAdapterOptional ng-morph adapter for Angular-specific operations
BACKWARD COMPATIBILITY: Constructor is optional - if not provided, falls back to pure ts-morph.
Add a named import to a source file. Handles merging with existing imports from the same module.
HYBRID APPROACH:
The source file to modify
The module to import from (e.g., '@angular/common')
The named import to add (e.g., 'CommonModule')
Optionalalias: stringOptional alias for the import (e.g., 'NgComponent')
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.
The source file
Get the alias for a named import, if it exists.
The source file
The symbol name
Optionalmodule: stringOptional module specifier
The alias if found, undefined otherwise
Get all imports from a source file.
The source file
Array of import information
Extracts:
Check if a named import exists in the source file.
The source file
The symbol name to check
Optionalmodule: stringOptional module specifier to check specifically
True if the import exists
Merge duplicate import declarations.
Combines multiple import statements from the same module into one.
The source file
Organize imports (sort and remove duplicates).
The source file
Handles:
// 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:
The source file
The named import to remove
OptionalmoduleSpecifier: stringOptional module specifier to target specific imports
Handles:
Adds, removes and organizes imports in TypeScript source files.
imports: []metadata arrays of@Componentand@NgModuledecorators.