ast-utils.d.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /**
  2. * @license
  3. * Copyright Google Inc. 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.io/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. * @return Change
  18. */
  19. export declare function insertImport(source: ts.SourceFile, fileToEdit: string, symbolName: string, fileName: string, isDefault?: boolean): Change;
  20. /**
  21. * Find all nodes from the AST in the subtree of node of SyntaxKind kind.
  22. * @param node
  23. * @param kind
  24. * @param max The maximum number of items to return.
  25. * @param recursive Continue looking for nodes of kind recursive until end
  26. * the last child even when node of kind has been found.
  27. * @return all nodes of kind, or [] if none is found
  28. */
  29. export declare function findNodes(node: ts.Node, kind: ts.SyntaxKind, max?: number, recursive?: boolean): ts.Node[];
  30. /**
  31. * Get all the nodes from a source.
  32. * @param sourceFile The source file object.
  33. * @returns {Observable<ts.Node>} An observable of all the nodes in the source.
  34. */
  35. export declare function getSourceNodes(sourceFile: ts.SourceFile): ts.Node[];
  36. export declare function findNode(node: ts.Node, kind: ts.SyntaxKind, text: string): ts.Node | null;
  37. /**
  38. * Insert `toInsert` after the last occurence of `ts.SyntaxKind[nodes[i].kind]`
  39. * or after the last of occurence of `syntaxKind` if the last occurence is a sub child
  40. * of ts.SyntaxKind[nodes[i].kind] and save the changes in file.
  41. *
  42. * @param nodes insert after the last occurence of nodes
  43. * @param toInsert string to insert
  44. * @param file file to insert changes into
  45. * @param fallbackPos position to insert if toInsert happens to be the first occurence
  46. * @param syntaxKind the ts.SyntaxKind of the subchildren to insert after
  47. * @return Change instance
  48. * @throw Error if toInsert is first occurence but fall back is not set
  49. */
  50. export declare function insertAfterLastOccurrence(nodes: ts.Node[], toInsert: string, file: string, fallbackPos: number, syntaxKind?: ts.SyntaxKind): Change;
  51. export declare function getContentOfKeyLiteral(_source: ts.SourceFile, node: ts.Node): string | null;
  52. export declare function getDecoratorMetadata(source: ts.SourceFile, identifier: string, module: string): ts.Node[];
  53. /**
  54. * Given a source file with @NgModule class(es), find the name of the first @NgModule class.
  55. *
  56. * @param source source file containing one or more @NgModule
  57. * @returns the name of the first @NgModule, or `undefined` if none is found
  58. */
  59. export declare function getFirstNgModuleName(source: ts.SourceFile): string | undefined;
  60. export declare function getMetadataField(node: ts.ObjectLiteralExpression, metadataField: string): ts.ObjectLiteralElement[];
  61. export declare function addSymbolToNgModuleMetadata(source: ts.SourceFile, ngModulePath: string, metadataField: string, symbolName: string, importPath?: string | null): Change[];
  62. /**
  63. * Custom function to insert a declaration (component, pipe, directive)
  64. * into NgModule declarations. It also imports the component.
  65. */
  66. export declare function addDeclarationToModule(source: ts.SourceFile, modulePath: string, classifiedName: string, importPath: string): Change[];
  67. /**
  68. * Custom function to insert an NgModule into NgModule imports. It also imports the module.
  69. */
  70. export declare function addImportToModule(source: ts.SourceFile, modulePath: string, classifiedName: string, importPath: string): Change[];
  71. /**
  72. * Custom function to insert a provider into NgModule. It also imports it.
  73. */
  74. export declare function addProviderToModule(source: ts.SourceFile, modulePath: string, classifiedName: string, importPath: string): Change[];
  75. /**
  76. * Custom function to insert an export into NgModule. It also imports it.
  77. */
  78. export declare function addExportToModule(source: ts.SourceFile, modulePath: string, classifiedName: string, importPath: string): Change[];
  79. /**
  80. * Custom function to insert an export into NgModule. It also imports it.
  81. */
  82. export declare function addBootstrapToModule(source: ts.SourceFile, modulePath: string, classifiedName: string, importPath: string): Change[];
  83. /**
  84. * Custom function to insert an entryComponent into NgModule. It also imports it.
  85. * @deprecated - Since version 9.0.0 with Ivy, entryComponents is no longer necessary.
  86. */
  87. export declare function addEntryComponentToModule(source: ts.SourceFile, modulePath: string, classifiedName: string, importPath: string): Change[];
  88. /**
  89. * Determine if an import already exists.
  90. */
  91. export declare function isImported(source: ts.SourceFile, classifiedName: string, importPath: string): boolean;
  92. /**
  93. * This function returns the name of the environment export
  94. * whether this export is aliased or not. If the environment file
  95. * is not imported, then it will return `null`.
  96. */
  97. export declare function getEnvironmentExportName(source: ts.SourceFile): string | null;
  98. /**
  99. * Returns the RouterModule declaration from NgModule metadata, if any.
  100. */
  101. export declare function getRouterModuleDeclaration(source: ts.SourceFile): ts.Expression | undefined;
  102. /**
  103. * Adds a new route declaration to a router module (i.e. has a RouterModule declaration)
  104. */
  105. export declare function addRouteDeclarationToModule(source: ts.SourceFile, fileToAdd: string, routeLiteral: string): Change;