typescript.ts 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import { getNodeText, getChildByField } from '../tree-sitter-helpers';
  2. import type { LanguageExtractor } from '../tree-sitter-types';
  3. export const typescriptExtractor: LanguageExtractor = {
  4. functionTypes: ['function_declaration', 'arrow_function', 'function_expression'],
  5. classTypes: ['class_declaration'],
  6. methodTypes: ['method_definition', 'public_field_definition'],
  7. interfaceTypes: ['interface_declaration'],
  8. structTypes: [],
  9. enumTypes: ['enum_declaration'],
  10. typeAliasTypes: ['type_alias_declaration'],
  11. importTypes: ['import_statement'],
  12. callTypes: ['call_expression'],
  13. variableTypes: ['lexical_declaration', 'variable_declaration'],
  14. nameField: 'name',
  15. bodyField: 'body',
  16. paramsField: 'parameters',
  17. returnField: 'return_type',
  18. getSignature: (node, source) => {
  19. const params = getChildByField(node, 'parameters');
  20. const returnType = getChildByField(node, 'return_type');
  21. if (!params) return undefined;
  22. let sig = getNodeText(params, source);
  23. if (returnType) {
  24. sig += ': ' + getNodeText(returnType, source).replace(/^:\s*/, '');
  25. }
  26. return sig;
  27. },
  28. getVisibility: (node) => {
  29. for (let i = 0; i < node.childCount; i++) {
  30. const child = node.child(i);
  31. if (child?.type === 'accessibility_modifier') {
  32. const text = child.text;
  33. if (text === 'public') return 'public';
  34. if (text === 'private') return 'private';
  35. if (text === 'protected') return 'protected';
  36. }
  37. }
  38. return undefined;
  39. },
  40. isExported: (node, _source) => {
  41. // Walk the parent chain to find an export_statement ancestor.
  42. // This correctly handles deeply nested nodes like arrow functions
  43. // inside variable declarations: `export const X = () => { ... }`
  44. // where the arrow_function is 3 levels deep under export_statement.
  45. let current = node.parent;
  46. while (current) {
  47. if (current.type === 'export_statement') return true;
  48. current = current.parent;
  49. }
  50. return false;
  51. },
  52. isAsync: (node) => {
  53. for (let i = 0; i < node.childCount; i++) {
  54. const child = node.child(i);
  55. if (child?.type === 'async') return true;
  56. }
  57. return false;
  58. },
  59. isStatic: (node) => {
  60. for (let i = 0; i < node.childCount; i++) {
  61. const child = node.child(i);
  62. if (child?.type === 'static') return true;
  63. }
  64. return false;
  65. },
  66. isConst: (node) => {
  67. // For lexical_declaration, check if it's 'const' or 'let'
  68. // For variable_declaration, it's always 'var'
  69. if (node.type === 'lexical_declaration') {
  70. for (let i = 0; i < node.childCount; i++) {
  71. const child = node.child(i);
  72. if (child?.type === 'const') return true;
  73. }
  74. }
  75. return false;
  76. },
  77. extractImport: (node, source) => {
  78. const sourceField = node.childForFieldName('source');
  79. if (sourceField) {
  80. const moduleName = source.substring(sourceField.startIndex, sourceField.endIndex).replace(/['"]/g, '');
  81. if (moduleName) {
  82. return { moduleName, signature: source.substring(node.startIndex, node.endIndex).trim() };
  83. }
  84. }
  85. return null;
  86. },
  87. };