react-native-navigation的迁移库

ClassParser.ts 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import * as Handlebars from 'handlebars';
  2. import * as Typedoc from 'typedoc';
  3. export interface PropertyContext {
  4. name: string;
  5. type: string;
  6. }
  7. export interface ArgumentContext {
  8. name: string;
  9. type: string;
  10. }
  11. export interface MethodContext {
  12. name: string;
  13. arguments: ArgumentContext[];
  14. returnType: string;
  15. source: string;
  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 parseClass(reflection: Typedoc.DeclarationReflection): ClassContext {
  26. return {
  27. name: reflection.name,
  28. properties: this.parseProperties(reflection),
  29. methods: this.parseMethods(reflection)
  30. };
  31. }
  32. private parseMethods(reflection: Typedoc.DeclarationReflection): MethodContext[] {
  33. const methodReflections = reflection.getChildrenByKind(Typedoc.ReflectionKind.Method);
  34. methodReflections.sort((a, b) => a.sources[0].line - b.sources[0].line);
  35. return methodReflections.map((methodReflection) => ({
  36. name: methodReflection.name,
  37. arguments: this.parseArguments(methodReflection.signatures[0].parameters || []),
  38. returnType: methodReflection.signatures[0].type.toString(),
  39. source: `${this.sourceLinkPrefix}/${methodReflection.sources[0].fileName}#L${methodReflection.sources[0].line}`,
  40. comment: methodReflection.signatures[0].comment ? methodReflection.signatures[0].comment.shortText : ''
  41. }));
  42. }
  43. private parseArguments(parameters: Typedoc.ParameterReflection[]): ArgumentContext[] {
  44. return parameters.map((parameter) => ({
  45. name: parameter.name,
  46. type: parameter.type.toString()
  47. }));
  48. }
  49. private parseProperties(reflection: Typedoc.DeclarationReflection): PropertyContext[] {
  50. const propsReflections = reflection.getChildrenByKind(Typedoc.ReflectionKind.Property);
  51. return propsReflections.map((propReflection) => ({
  52. name: propReflection.name,
  53. type: propReflection.type.toString()
  54. }));
  55. }
  56. }