版博士V2.0程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

пре 1 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. import { MagicString, SFC } from '@vue-macros/common';
  2. export { MagicString, SFC, parseSFC } from '@vue-macros/common';
  3. import { Node, TSModuleBlock, CallExpression, TSType, LVal, VariableDeclaration, ExpressionStatement, TSCallSignatureDeclaration, TSFunctionType, TSTypeLiteral, TSIntersectionType, TSInterfaceDeclaration, StringLiteral, TSMethodSignature, TSPropertySignature, TSMappedType, TSUnionType, ObjectMethod, ObjectProperty, Expression } from '@babel/types';
  4. import { TSFile, TSResolvedType, TSExports } from './ts.js';
  5. export { ResolveTSFileIdImpl, TSDeclaration, TSProperties, TSScope, exportsSymbol, getTSFile, isTSDeclaration, isTSExports, mergeTSProperties, resolveTSEntityName, resolveTSExports, resolveTSFileId, resolveTSFileIdNode, resolveTSProperties, resolveTSReferencedType, resolveTSScope, resolveTypeElements, setResolveTSFileIdImpl, tsFileCache, tsFileExportsCache } from './ts.js';
  6. export { RollupResolve } from './resolve.js';
  7. export { keyToString } from './utils.js';
  8. import 'vite';
  9. import 'rollup';
  10. declare enum DefinitionKind {
  11. /**
  12. * Definition is a referenced variable.
  13. *
  14. * @example defineSomething(foo)
  15. */
  16. Reference = "Reference",
  17. /**
  18. * Definition is a `ObjectExpression`.
  19. *
  20. * @example defineSomething({ ... })
  21. */
  22. Object = "Object",
  23. /**
  24. * Definition is TypeScript interface.
  25. *
  26. * @example defineSomething<{ ... }>()
  27. */
  28. TS = "TS"
  29. }
  30. interface ASTDefinition<T extends Node> {
  31. code: string;
  32. scope: TSFile | TSResolvedType<TSModuleBlock> | undefined;
  33. ast: T;
  34. }
  35. declare function handleTSEmitsDefinition({ s, file, offset, defineEmitsAst, typeDeclRaw, declId, statement, }: {
  36. s: MagicString;
  37. file: TSFile;
  38. sfc: SFC;
  39. offset: number;
  40. defineEmitsAst: CallExpression;
  41. typeDeclRaw: TSType;
  42. statement: DefineEmitsStatement;
  43. declId?: LVal;
  44. }): Promise<TSEmits>;
  45. type Emits = TSEmits | undefined;
  46. type DefineEmitsStatement = VariableDeclaration | ExpressionStatement;
  47. interface EmitsBase {
  48. declId?: LVal;
  49. statementAst: DefineEmitsStatement;
  50. defineEmitsAst: CallExpression;
  51. }
  52. interface TSEmits extends EmitsBase {
  53. kind: DefinitionKind.TS;
  54. definitions: Record<string, ASTDefinition<TSCallSignatureDeclaration | TSFunctionType>[]>;
  55. definitionsAst: ASTDefinition<TSTypeLiteral | TSIntersectionType | TSInterfaceDeclaration | TSFunctionType>;
  56. /**
  57. * Adds a new emit to the definitions. `definitions` will updated after this call.
  58. *
  59. * Added definition cannot be set and removed again.
  60. *
  61. * @example add('change', '(evt: "change", value: string): void')
  62. */
  63. addEmit(name: string | StringLiteral, signature: string): void;
  64. /**
  65. * Modify a definition of a emit. `definitions` will updated after this call.
  66. *
  67. * @limitation Cannot set the emit added by `addEmit`.
  68. *
  69. * @example setEmit('foo', 0, '(evt: "change", value: string): void')
  70. *
  71. * @returns false if the definition does not exist.
  72. */
  73. setEmit(name: string | StringLiteral, index: number, signature: string): boolean;
  74. /**
  75. * Removes specified emit from TS interface. `definitions` will updated after this call.
  76. *
  77. * @limitation Cannot remove emit added by `addEmit`. (it will be removed in definitions though)
  78. *
  79. * @returns `true` if emit was removed, `false` if emit was not found.
  80. */
  81. removeEmit(name: string | StringLiteral, index: number): boolean;
  82. }
  83. declare function handleTSPropsDefinition({ s, file, offset, definePropsAst, typeDeclRaw, withDefaultsAst, defaultsDeclRaw, statement, declId, }: {
  84. s: MagicString;
  85. file: TSFile;
  86. sfc: SFC;
  87. offset: number;
  88. definePropsAst: CallExpression;
  89. typeDeclRaw: TSType;
  90. withDefaultsAst?: CallExpression;
  91. defaultsDeclRaw?: DefaultsASTRaw;
  92. statement: DefinePropsStatement;
  93. declId?: LVal;
  94. }): Promise<TSProps>;
  95. type Props = /* ReferenceProps | ObjectProps | */ TSProps | undefined;
  96. type DefinePropsStatement = VariableDeclaration | ExpressionStatement;
  97. type DefaultsASTRaw = CallExpression['arguments'][number];
  98. interface PropsBase {
  99. declId?: LVal;
  100. statementAst: DefinePropsStatement;
  101. definePropsAst: CallExpression;
  102. withDefaultsAst?: CallExpression;
  103. }
  104. interface TSPropsMethod {
  105. type: 'method';
  106. methods: ASTDefinition<TSMethodSignature>[];
  107. optional: boolean;
  108. }
  109. interface TSPropsProperty {
  110. type: 'property';
  111. value: ASTDefinition<TSResolvedType['type']> | undefined;
  112. optional: boolean;
  113. signature: ASTDefinition<TSPropertySignature | TSMappedType>;
  114. /** Whether added by `addProp` API */
  115. addByAPI: boolean;
  116. }
  117. interface RuntimePropDefinition {
  118. type: string[];
  119. required: boolean;
  120. default?: (key?: string) => string;
  121. }
  122. interface TSProps extends PropsBase {
  123. kind: DefinitionKind.TS;
  124. definitions: Record<string | number, TSPropsMethod | TSPropsProperty>;
  125. definitionsAst: ASTDefinition<TSInterfaceDeclaration | TSTypeLiteral | TSIntersectionType | TSUnionType | TSMappedType>;
  126. /**
  127. * Default value of props.
  128. *
  129. * `undefined` if not defined or it's not a static expression that cannot be analyzed statically.
  130. */
  131. defaults?: Record<string, ObjectMethod | ObjectProperty>;
  132. /**
  133. * `undefined` if not defined.
  134. */
  135. defaultsAst?: Expression;
  136. /**
  137. * Adds a new prop to the definitions. `definitions` will updated after this call.
  138. *
  139. * Added definition cannot be set and removed again.
  140. *
  141. * @example addProp('foo', 'string | boolean')
  142. *
  143. * @returns false if the definition already exists.
  144. */
  145. addProp(name: string | StringLiteral, type: string, optional?: boolean): boolean;
  146. /**
  147. * Modify a definition of a prop. `definitions` will updated after this call.
  148. *
  149. * @limitation Cannot set the prop added by `addProp`.
  150. *
  151. * @example setProp('foo', 'string | boolean')
  152. *
  153. * @returns false if the definition does not exist.
  154. */
  155. setProp(name: string | StringLiteral, type: string, optional?: boolean): boolean;
  156. /**
  157. * Removes specified prop from TS interface. `definitions` will updated after this call.
  158. *
  159. * @limitation Cannot remove prop added by `addProp`. (it will be removed in definitions though)
  160. *
  161. * @returns `true` if prop was removed, `false` if prop was not found.
  162. */
  163. removeProp(name: string | StringLiteral): boolean;
  164. /**
  165. * get runtime definitions.
  166. */
  167. getRuntimeDefinitions(): Promise<Record<string, RuntimePropDefinition>>;
  168. }
  169. interface AnalyzeResult {
  170. props: Props;
  171. emits: Emits;
  172. }
  173. declare function analyzeSFC(s: MagicString, sfc: SFC): Promise<AnalyzeResult>;
  174. declare function inferRuntimeType(node: TSResolvedType | TSExports): Promise<string[]>;
  175. declare function attachNodeLoc(node: Node, newNode: Node): void;
  176. declare function toRuntimeTypeString(types: string[]): string;
  177. export { ASTDefinition, AnalyzeResult, DefaultsASTRaw, DefineEmitsStatement, DefinePropsStatement, DefinitionKind, Emits, EmitsBase, Props, PropsBase, RuntimePropDefinition, TSEmits, TSExports, TSFile, TSProps, TSPropsMethod, TSPropsProperty, TSResolvedType, analyzeSFC, attachNodeLoc, handleTSEmitsDefinition, handleTSPropsDefinition, inferRuntimeType, toRuntimeTypeString };