ast-utils.d.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 '../third_party/github.com/Microsoft/TypeScript/lib/typescript';
  9. import { Change } from './change';
  10. /**
  11. * Add Import `import { symbolName } from fileName` if the import doesn't exit
  12. * already. Assumes fileToEdit can be resolved and accessed.
  13. * @param fileToEdit File we want to add import to.
  14. * @param symbolName Item to import.
  15. * @param fileName Path to the file.
  16. * @param isDefault If true, import follows style for importing default exports.
  17. * @param alias Alias that the symbol should be inserted under.
  18. * @return Change
  19. */
  20. export declare function insertImport(source: ts.SourceFile, fileToEdit: string, symbolName: string, fileName: string, isDefault?: boolean, alias?: string): Change;
  21. /**
  22. * Find all nodes from the AST in the subtree of node of SyntaxKind kind.
  23. * @param node
  24. * @param kind
  25. * @param max The maximum number of items to return.
  26. * @param recursive Continue looking for nodes of kind recursive until end
  27. * the last child even when node of kind has been found.
  28. * @return all nodes of kind, or [] if none is found
  29. */
  30. export declare function findNodes(node: ts.Node, kind: ts.SyntaxKind, max?: number, recursive?: boolean): ts.Node[];
  31. /**
  32. * Find all nodes from the AST in the subtree that satisfy a type guard.
  33. * @param node
  34. * @param guard
  35. * @param max The maximum number of items to return.
  36. * @param recursive Continue looking for nodes of kind recursive until end
  37. * the last child even when node of kind has been found.
  38. * @return all nodes that satisfy the type guard, or [] if none is found
  39. */
  40. export declare function findNodes<T extends ts.Node>(node: ts.Node, guard: (node: ts.Node) => node is T, max?: number, recursive?: boolean): T[];
  41. /**
  42. * Get all the nodes from a source.
  43. * @param sourceFile The source file object.
  44. * @returns {Array<ts.Node>} An array of all the nodes in the source.
  45. */
  46. export declare function getSourceNodes(sourceFile: ts.SourceFile): ts.Node[];
  47. export declare function findNode(node: ts.Node, kind: ts.SyntaxKind, text: string): ts.Node | null;
  48. /**
  49. * Insert `toInsert` after the last occurence of `ts.SyntaxKind[nodes[i].kind]`
  50. * or after the last of occurence of `syntaxKind` if the last occurence is a sub child
  51. * of ts.SyntaxKind[nodes[i].kind] and save the changes in file.
  52. *
  53. * @param nodes insert after the last occurence of nodes
  54. * @param toInsert string to insert
  55. * @param file file to insert changes into
  56. * @param fallbackPos position to insert if toInsert happens to be the first occurence
  57. * @param syntaxKind the ts.SyntaxKind of the subchildren to insert after
  58. * @return Change instance
  59. * @throw Error if toInsert is first occurence but fall back is not set
  60. */
  61. export declare function insertAfterLastOccurrence(nodes: ts.Node[] | ts.NodeArray<ts.Node>, toInsert: string, file: string, fallbackPos: number, syntaxKind?: ts.SyntaxKind): Change;
  62. export declare function getDecoratorMetadata(source: ts.SourceFile, identifier: string, module: string): ts.Node[];
  63. export declare function getMetadataField(node: ts.ObjectLiteralExpression, metadataField: string): ts.ObjectLiteralElement[];
  64. export declare function addSymbolToNgModuleMetadata(source: ts.SourceFile, ngModulePath: string, metadataField: string, symbolName: string, importPath?: string | null): Change[];
  65. /**
  66. * Custom function to insert a declaration (component, pipe, directive)
  67. * into NgModule declarations. It also imports the component.
  68. */
  69. export declare function addDeclarationToModule(source: ts.SourceFile, modulePath: string, classifiedName: string, importPath: string): Change[];
  70. /**
  71. * Custom function to insert an NgModule into NgModule imports. It also imports the module.
  72. */
  73. export declare function addImportToModule(source: ts.SourceFile, modulePath: string, classifiedName: string, importPath: string): Change[];
  74. /**
  75. * Custom function to insert a provider into NgModule. It also imports it.
  76. */
  77. export declare function addProviderToModule(source: ts.SourceFile, modulePath: string, classifiedName: string, importPath: string): Change[];
  78. /**
  79. * Custom function to insert an export into NgModule. It also imports it.
  80. */
  81. export declare function addExportToModule(source: ts.SourceFile, modulePath: string, classifiedName: string, importPath: string): Change[];
  82. /**
  83. * Custom function to insert an export into NgModule. It also imports it.
  84. */
  85. export declare function addBootstrapToModule(source: ts.SourceFile, modulePath: string, classifiedName: string, importPath: string): Change[];
  86. /**
  87. * Determine if an import already exists.
  88. */
  89. export declare function isImported(source: ts.SourceFile, classifiedName: string, importPath: string): boolean;
  90. /**
  91. * Returns the RouterModule declaration from NgModule metadata, if any.
  92. */
  93. export declare function getRouterModuleDeclaration(source: ts.SourceFile): ts.Expression | undefined;
  94. /**
  95. * Adds a new route declaration to a router module (i.e. has a RouterModule declaration)
  96. */
  97. export declare function addRouteDeclarationToModule(source: ts.SourceFile, fileToAdd: string, routeLiteral: string): Change;
  98. /**
  99. * Determines if a SourceFile has a top-level declaration whose name matches a specific symbol.
  100. * Can be used to avoid conflicts when inserting new imports into a file.
  101. * @param sourceFile File in which to search.
  102. * @param symbolName Name of the symbol to search for.
  103. * @param skipModule Path of the module that the symbol may have been imported from. Used to
  104. * avoid false positives where the same symbol we're looking for may have been imported.
  105. */
  106. export declare function hasTopLevelIdentifier(sourceFile: ts.SourceFile, symbolName: string, skipModule?: string | null): boolean;