javascript.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { getNodeText, getChildByField } from '../tree-sitter-helpers';
  2. import type { LanguageExtractor } from '../tree-sitter-types';
  3. import { classifyTsClassMember } from './typescript';
  4. export const javascriptExtractor: LanguageExtractor = {
  5. functionTypes: ['function_declaration', 'arrow_function', 'function_expression'],
  6. classTypes: ['class_declaration'],
  7. methodTypes: ['method_definition', 'field_definition'],
  8. // JS `field_definition` ≙ TS `public_field_definition`: plain fields are
  9. // properties, function-valued fields are methods (#808).
  10. classifyMethodNode: classifyTsClassMember,
  11. interfaceTypes: [],
  12. structTypes: [],
  13. enumTypes: [],
  14. typeAliasTypes: [],
  15. importTypes: ['import_statement'],
  16. callTypes: ['call_expression'],
  17. variableTypes: ['lexical_declaration', 'variable_declaration'],
  18. nameField: 'name',
  19. // JS `field_definition` names its key the `property` field (TS's
  20. // public_field_definition uses `name`). Without this, JS class fields —
  21. // including arrow-function handler fields — extracted no name and produced
  22. // no node at all (#808).
  23. resolveName: (node, source) => {
  24. if (node.type === 'field_definition') {
  25. const prop = getChildByField(node, 'property');
  26. if (prop) return getNodeText(prop, source);
  27. }
  28. return undefined;
  29. },
  30. bodyField: 'body',
  31. resolveBody: (node, bodyField) => {
  32. // field_definition (arrow function class fields) nest the body inside
  33. // an arrow_function or function_expression child:
  34. // field_definition → arrow_function → body (statement_block)
  35. // Also handles wrapper patterns like: field = throttle((e) => { ... })
  36. // field_definition → call_expression → arguments → arrow_function → body
  37. if (node.type === 'field_definition') {
  38. for (let i = 0; i < node.namedChildCount; i++) {
  39. const child = node.namedChild(i);
  40. if (!child) continue;
  41. if (child.type === 'arrow_function' || child.type === 'function_expression') {
  42. return getChildByField(child, bodyField);
  43. }
  44. if (child.type === 'call_expression') {
  45. const args = getChildByField(child, 'arguments');
  46. if (args) {
  47. for (let j = 0; j < args.namedChildCount; j++) {
  48. const arg = args.namedChild(j);
  49. if (arg && (arg.type === 'arrow_function' || arg.type === 'function_expression')) {
  50. return getChildByField(arg, bodyField);
  51. }
  52. }
  53. }
  54. }
  55. }
  56. }
  57. return null;
  58. },
  59. paramsField: 'parameters',
  60. getSignature: (node, source) => {
  61. const params = getChildByField(node, 'parameters');
  62. return params ? getNodeText(params, source) : undefined;
  63. },
  64. isExported: (node, _source) => {
  65. let current = node.parent;
  66. while (current) {
  67. if (current.type === 'export_statement') return true;
  68. current = current.parent;
  69. }
  70. return false;
  71. },
  72. isAsync: (node) => {
  73. for (let i = 0; i < node.childCount; i++) {
  74. const child = node.child(i);
  75. if (child?.type === 'async') return true;
  76. }
  77. return false;
  78. },
  79. isConst: (node) => {
  80. if (node.type === 'lexical_declaration') {
  81. for (let i = 0; i < node.childCount; i++) {
  82. const child = node.child(i);
  83. if (child?.type === 'const') return true;
  84. }
  85. }
  86. return false;
  87. },
  88. extractImport: (node, source) => {
  89. const sourceField = node.childForFieldName('source');
  90. if (sourceField) {
  91. const moduleName = source.substring(sourceField.startIndex, sourceField.endIndex).replace(/['"]/g, '');
  92. if (moduleName) {
  93. return { moduleName, signature: source.substring(node.startIndex, node.endIndex).trim() };
  94. }
  95. }
  96. return null;
  97. },
  98. };