migration.d.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /**
  2. * @license
  3. * Copyright Google LLC All Rights Reserved.
  4. *
  5. * Use of this source code is governed by an MIT-style license that can be
  6. * found in the LICENSE file at https://angular.dev/license
  7. */
  8. import * as ts from 'typescript';
  9. import { ResolvedResource } from './component-resource-collector';
  10. import { FileSystem, WorkspacePath } from './file-system';
  11. import { UpdateLogger } from './logger';
  12. import { TargetVersion } from './target-version';
  13. import { LineAndCharacter } from './utils/line-mappings';
  14. export interface MigrationFailure {
  15. filePath: WorkspacePath;
  16. message: string;
  17. position?: LineAndCharacter;
  18. }
  19. export type PostMigrationAction = void | {
  20. /** Whether the package manager should run upon migration completion. */
  21. runPackageManager: boolean;
  22. };
  23. /** Creates a constructor type for the specified type. */
  24. export type Constructor<T> = new (...args: any[]) => T;
  25. /** Gets a constructor type for the passed migration data. */
  26. export type MigrationCtor<Data, Context = any> = Constructor<Migration<Data, Context>>;
  27. export declare abstract class Migration<Data, Context = any> {
  28. /** TypeScript program for the migration. */
  29. program: ts.Program;
  30. /** TypeChecker instance for the analysis program. */
  31. typeChecker: ts.TypeChecker;
  32. /**
  33. * Version for which the migration rule should run. Null if the migration
  34. * is invoked manually.
  35. */
  36. targetVersion: TargetVersion | null;
  37. /** Context data for the migration. */
  38. context: Context;
  39. /** Upgrade data passed to the migration. */
  40. upgradeData: Data;
  41. /** File system that can be used for modifying files. */
  42. fileSystem: FileSystem;
  43. /** Logger that can be used to print messages as part of the migration. */
  44. logger: UpdateLogger;
  45. /** List of migration failures that need to be reported. */
  46. failures: MigrationFailure[];
  47. /** Whether the migration is enabled or not. */
  48. abstract enabled: boolean;
  49. constructor(
  50. /** TypeScript program for the migration. */
  51. program: ts.Program,
  52. /** TypeChecker instance for the analysis program. */
  53. typeChecker: ts.TypeChecker,
  54. /**
  55. * Version for which the migration rule should run. Null if the migration
  56. * is invoked manually.
  57. */
  58. targetVersion: TargetVersion | null,
  59. /** Context data for the migration. */
  60. context: Context,
  61. /** Upgrade data passed to the migration. */
  62. upgradeData: Data,
  63. /** File system that can be used for modifying files. */
  64. fileSystem: FileSystem,
  65. /** Logger that can be used to print messages as part of the migration. */
  66. logger: UpdateLogger);
  67. /** Method can be used to perform global analysis of the program. */
  68. init(): void;
  69. /**
  70. * Method that will be called once all nodes, templates and stylesheets
  71. * have been visited.
  72. */
  73. postAnalysis(): void;
  74. /**
  75. * Method that will be called for each node in a given source file. Unlike tslint, this
  76. * function will only retrieve TypeScript nodes that need to be casted manually. This
  77. * allows us to only walk the program source files once per program and not per
  78. * migration rule (significant performance boost).
  79. */
  80. visitNode(node: ts.Node): void;
  81. /** Method that will be called for each Angular template in the program. */
  82. visitTemplate(template: ResolvedResource): void;
  83. /** Method that will be called for each stylesheet in the program. */
  84. visitStylesheet(stylesheet: ResolvedResource): void;
  85. /** Creates a failure with a specified message at the given node location. */
  86. protected createFailureAtNode(node: ts.Node, message: string): void;
  87. }