All files / migrations/standalone 0004-migrate-import-statements.ts

80% Statements 36/45
80% Branches 4/5
100% Functions 1/1
80% Lines 36/45

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 461x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x                   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
import type { Project } from "ts-morph";
import type { CliOptions } from "../../../types/cli-options";
 
import { saveFileChanges } from "../../utils/log-utils";
import { removeImportFromNgModuleDecorator } from "../../utils/angular-utils";
 
export const migrateImportStatements = async (
  project: Project,
  cliOptions: CliOptions,
) => {
  // Get all typescript source files in the project and update any @ionic/angular imports to @ionic/angular/standalone
  for (const sourceFile of project.getSourceFiles()) {
    let hasChanges = false;
 
    const importDeclarations = sourceFile.getImportDeclarations();
    importDeclarations.forEach((importDeclaration) => {
      const moduleSpecifier = importDeclaration.getModuleSpecifierValue();
      if (moduleSpecifier === "@ionic/angular") {
        importDeclaration.setModuleSpecifier("@ionic/angular/standalone");
 
        const namedImports = importDeclaration.getNamedImports();
        const importSpecifier = namedImports.find(
          (n) => n.getName() === "IonicModule",
        );
 
        if (importSpecifier) {
          if (namedImports.length > 1) {
            // Remove the IonicModule import specifier.
            importSpecifier.remove();
          } else {
            // If this is the only import specifier, remove the entire import declaration.
            importDeclaration.remove();
          }
          removeImportFromNgModuleDecorator(sourceFile, "IonicModule");
        }
 
        hasChanges = true;
      }
    });
 
    if (hasChanges) {
      await saveFileChanges(sourceFile, cliOptions);
    }
  }
};