版博士V2.0程序
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

77 строки
4.0 KiB

  1. import { TSDeclareFunction, TSInterfaceDeclaration, TSTypeAliasDeclaration, TSEnumDeclaration, TSModuleDeclaration, Statement, TSModuleBlock, TSCallSignatureDeclaration, TSFunctionType, TSConstructSignatureDeclaration, TSMethodSignature, TSType, TSPropertySignature, TSMappedType, TSInterfaceBody, TSTypeLiteral, TSIntersectionType, TSTypeElement, TSParenthesizedType, Identifier, TSEntityName } from '@babel/types';
  2. type TSDeclaration = TSDeclareFunction | TSInterfaceDeclaration | TSTypeAliasDeclaration | TSEnumDeclaration | TSModuleDeclaration;
  3. interface TSFile {
  4. filePath: string;
  5. content: string;
  6. ast: Statement[];
  7. }
  8. type TSScope = TSFile | TSResolvedType<TSModuleBlock>;
  9. interface TSProperties {
  10. callSignatures: Array<TSResolvedType<TSCallSignatureDeclaration | TSFunctionType>>;
  11. constructSignatures: Array<TSResolvedType<TSConstructSignatureDeclaration>>;
  12. methods: Record<string | number, Array<TSResolvedType<TSMethodSignature>>>;
  13. properties: Record<string | number, {
  14. value: TSResolvedType<TSType> | null;
  15. optional: boolean;
  16. signature: TSResolvedType<TSPropertySignature | TSMappedType>;
  17. }>;
  18. }
  19. declare const tsFileCache: Record<string, TSFile>;
  20. declare function getTSFile(filePath: string): Promise<TSFile>;
  21. declare function isTSDeclaration(node: any): node is TSDeclaration;
  22. declare function mergeTSProperties(a: TSProperties, b: TSProperties): TSProperties;
  23. /**
  24. * get properties of `interface` or `type` declaration
  25. *
  26. * @limitation don't support index signature
  27. */
  28. declare function resolveTSProperties({ type, scope, }: TSResolvedType<TSInterfaceDeclaration | TSInterfaceBody | TSTypeLiteral | TSIntersectionType | TSMappedType | TSFunctionType>): Promise<TSProperties>;
  29. /**
  30. * @limitation don't support index signature
  31. */
  32. declare function resolveTypeElements(scope: TSScope, elements: Array<TSTypeElement>): TSProperties;
  33. interface TSResolvedType<T = Exclude<TSType, TSParenthesizedType> | Exclude<TSDeclaration, TSTypeAliasDeclaration>> {
  34. scope: TSScope;
  35. type: T;
  36. }
  37. /**
  38. * Resolve a reference to a type.
  39. *
  40. * Supports `type` and `interface` only.
  41. *
  42. * @limitation don't support non-TS declaration (e.g. class, function...)
  43. */
  44. declare function resolveTSReferencedType(ref: TSResolvedType<TSType | Identifier | TSDeclaration>, stacks?: TSResolvedType<any>[]): Promise<TSResolvedType | TSExports | undefined>;
  45. declare function resolveTSScope(scope: TSScope): {
  46. isFile: boolean;
  47. file: TSFile;
  48. body: Statement[];
  49. };
  50. declare function resolveTSEntityName(node: TSEntityName): Identifier[];
  51. declare const exportsSymbol: unique symbol;
  52. type TSExports = {
  53. [K in string]: TSResolvedType | TSExports | undefined;
  54. } & {
  55. [exportsSymbol]: true;
  56. };
  57. declare const tsFileExportsCache: Map<TSScope, TSExports>;
  58. declare function isTSExports(val: unknown): val is TSExports;
  59. /**
  60. * Get exports of the TS file.
  61. *
  62. * @limitation don't support non-TS declaration (e.g. class, function...)
  63. * @limitation don't support `export default`, since TS don't support it currently.
  64. * @limitation don't support `export * as xxx from '...'` (aka namespace).
  65. */
  66. declare function resolveTSExports(scope: TSScope): Promise<TSExports>;
  67. type ResolveTSFileIdImpl = (id: string, importer: string) => Promise<string | undefined> | string | undefined;
  68. declare function resolveTSFileId(id: string, importer: string): string | Promise<string | undefined> | undefined;
  69. /**
  70. * @limitation don't node_modules and JavaScript file
  71. */
  72. declare function resolveTSFileIdNode(id: string, importer: string): string | undefined;
  73. declare function setResolveTSFileIdImpl(impl: ResolveTSFileIdImpl): void;
  74. export { ResolveTSFileIdImpl, TSDeclaration, TSExports, TSFile, TSProperties, TSResolvedType, TSScope, exportsSymbol, getTSFile, isTSDeclaration, isTSExports, mergeTSProperties, resolveTSEntityName, resolveTSExports, resolveTSFileId, resolveTSFileIdNode, resolveTSProperties, resolveTSReferencedType, resolveTSScope, resolveTypeElements, setResolveTSFileIdImpl, tsFileCache, tsFileExportsCache };