code_block.d.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 { Rule } from '@angular-devkit/schematics';
  9. /** Generated code that hasn't been interpolated yet. */
  10. export interface PendingCode {
  11. /** Code that will be inserted. */
  12. expression: string;
  13. /** Imports that need to be added to the file in which the code is inserted. */
  14. imports: PendingImports;
  15. }
  16. /** Map keeping track of imports and aliases under which they're referred to in an expression. */
  17. type PendingImports = Map<string, Map<string, string>>;
  18. /**
  19. * Callback invoked by a Rule that produces the code
  20. * that needs to be inserted somewhere in the app.
  21. */
  22. export type CodeBlockCallback = (block: CodeBlock) => PendingCode;
  23. /**
  24. * Utility class used to generate blocks of code that
  25. * can be inserted by the devkit into a user's app.
  26. */
  27. export declare class CodeBlock {
  28. private _imports;
  29. /** Function used to tag a code block in order to produce a `PendingCode` object. */
  30. code: (strings: TemplateStringsArray, ...params: unknown[]) => PendingCode;
  31. /**
  32. * Used inside of a code block to mark external symbols and which module they should be imported
  33. * from. When the code is inserted, the required import statements will be produced automatically.
  34. * @param symbolName Name of the external symbol.
  35. * @param moduleName Module from which the symbol should be imported.
  36. */
  37. external: (symbolName: string, moduleName: string) => string;
  38. /**
  39. * Produces the necessary rules to transform a `PendingCode` object into valid code.
  40. * @param initialCode Code pending transformed.
  41. * @param filePath Path of the file in which the code will be inserted.
  42. */
  43. static transformPendingCode(initialCode: PendingCode, filePath: string): {
  44. code: {
  45. /** Code that will be inserted. */
  46. expression: string;
  47. /** Imports that need to be added to the file in which the code is inserted. */
  48. imports: PendingImports;
  49. };
  50. rules: Rule[];
  51. };
  52. }
  53. export {};