react-native-navigation的迁移库

ClassParser.ts 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import * as Typedoc from 'typedoc';
  2. export interface PropertyContext {
  3. name: string;
  4. type: string;
  5. }
  6. export interface ArgumentContext {
  7. name: string;
  8. type: string;
  9. }
  10. export interface MethodContext {
  11. name: string;
  12. arguments: ArgumentContext[];
  13. returnType: string;
  14. source: string;
  15. line: number;
  16. comment?: string;
  17. }
  18. export interface ClassContext {
  19. name: string;
  20. properties: PropertyContext[];
  21. methods: MethodContext[];
  22. }
  23. export class ClassParser {
  24. constructor(private sourceLinkPrefix: string) { }
  25. public parseClasses(classReflections: Typedoc.DeclarationReflection[]): ClassContext[] {
  26. return classReflections.map((r) => this.parseClass(r));
  27. }
  28. private parseClass(classReflection: Typedoc.DeclarationReflection): ClassContext {
  29. return {
  30. name: classReflection.name,
  31. properties: this.parseProperties(classReflection),
  32. methods: this.parseMethods(classReflection)
  33. };
  34. }
  35. private parseMethods(reflection: Typedoc.DeclarationReflection): MethodContext[] {
  36. const methodReflections = reflection.getChildrenByKind(Typedoc.ReflectionKind.Method);
  37. const methods = methodReflections.map((m) => this.parseMethod(m))
  38. .filter((m) => !m.source.includes('/node_modules/'))
  39. .sort((a, b) => a.line - b.line);
  40. return methods;
  41. }
  42. private parseMethod(methodReflection: Typedoc.DeclarationReflection) {
  43. const name = methodReflection.name;
  44. const line = methodReflection.sources[0].line;
  45. const fileName = methodReflection.sources[0].fileName;
  46. const source = `${this.sourceLinkPrefix}/${fileName}#L${line}`;
  47. const comment = methodReflection.signatures[0].comment ? methodReflection.signatures[0].comment.shortText : '';
  48. const args = this.parseArguments(methodReflection.signatures[0].parameters || []);
  49. const returnType = methodReflection.signatures[0].type.toString();
  50. return {
  51. name,
  52. arguments: args,
  53. returnType,
  54. source,
  55. line,
  56. comment
  57. };
  58. }
  59. private parseArguments(parameters: Typedoc.ParameterReflection[]): ArgumentContext[] {
  60. return parameters.map((parameter) => ({
  61. name: parameter.name,
  62. type: parameter.type.toString()
  63. }));
  64. }
  65. private parseProperties(reflection: Typedoc.DeclarationReflection): PropertyContext[] {
  66. const propsReflections = reflection.getChildrenByKind(Typedoc.ReflectionKind.Property);
  67. return propsReflections.map((propReflection) => ({
  68. name: propReflection.name,
  69. type: propReflection.type.toString()
  70. }));
  71. }
  72. }