Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 | /**
* JavaScript Parser
*
* This module provides functionality to traverse a Tree-sitter AST
* for JavaScript/TypeScript and extract code entities and relationships.
*/
/**
* Extract code entities and relationships from a JavaScript/TypeScript AST
* @param {Object} astRootNode - The root node of the Tree-sitter AST
* @param {String} fileContentString - The full content of the file
* @returns {Object} Object containing arrays of extracted entities and relationships
*/
export function parseJavaScript(astRootNode, fileContentString) {
// Initialize empty arrays to store the entities and relationships
const entities = [];
const relationships = [];
// Track current parent entity for establishing relationships
let currentParentEntity = null;
/**
* Helper function to create a code entity object
* @param {Object} node - The Tree-sitter AST node
* @param {String} entityType - Type of the entity (function_declaration, etc.)
* @param {String} name - Name of the entity
* @param {Object} customMetadata - Additional metadata for the entity
* @returns {Object} A code entity object
*/
function createEntity(node, entityType, name, customMetadata = {}) {
// Extract the raw content from the file using node positions
const startByte = node.startIndex;
const endByte = node.endIndex;
const rawContent = fileContentString.substring(startByte, endByte);
// Extract position information
const startLine = node.startPosition.row;
const startColumn = node.startPosition.column;
const endLine = node.endPosition.row;
const endColumn = node.endPosition.column;
// Determine language - in the future, this could be more sophisticated
// based on file extension or TypeScript-specific constructs
const language =
entityType.includes("interface") ||
entityType.includes("type_alias") ||
entityType.includes("enum")
? "typescript"
: "javascript";
// Create entity with a temporary ID that will be replaced by the database
// This ID is just for establishing relationships within the same file
const entity = {
id: `temp_${entities.length + 1}`,
entity_type: entityType,
name: name,
start_line: startLine,
start_column: startColumn,
end_line: endLine,
end_column: endColumn,
raw_content: rawContent,
language: language,
parent_entity_id: currentParentEntity ? currentParentEntity.id : null,
custom_metadata: customMetadata,
};
// If this entity has a parent, create a parent-child relationship
if (currentParentEntity) {
createRelationship(
currentParentEntity.id,
entity.id,
entity.name,
"DEFINES_CHILD_ENTITY"
);
}
return entity;
}
/**
* Helper function to create a code relationship object
* @param {String} sourceEntityId - ID of the source entity
* @param {String|null} targetEntityId - ID of the target entity, if available
* @param {String} targetSymbolName - Name of the target symbol
* @param {String} relationshipType - Type of relationship (e.g., CALLS_FUNCTION)
* @param {Object} customMetadata - Additional metadata for the relationship
* @returns {Object} A code relationship object
*/
function createRelationship(
sourceEntityId,
targetEntityId,
targetSymbolName,
relationshipType,
customMetadata = {}
) {
const relationship = {
source_entity_id: sourceEntityId,
target_entity_id: targetEntityId,
target_symbol_name: targetSymbolName,
relationship_type: relationshipType,
custom_metadata: customMetadata,
};
relationships.push(relationship);
return relationship;
}
/**
* Find an entity by name in the current entities array
* @param {String} name - The name of the entity to find
* @param {String} type - Optional type to filter by
* @returns {Object|null} The found entity or null
*/
function findEntityByName(name, type = null) {
for (const entity of entities) {
if (entity.name === name && (!type || entity.entity_type === type)) {
return entity;
}
}
return null;
}
/**
* Process function calls within a scope
* @param {Object} node - The node to check for function calls
* @param {Object} scopeEntity - The entity representing the current scope
*/
function processFunctionCalls(node, scopeEntity) {
if (!node || !scopeEntity) return;
// Process call expressions
if (node.type === "call_expression") {
let functionName = "";
let metadata = {};
// Extract function name
const functionNode = node.namedChild(0);
if (functionNode) {
if (functionNode.type === "identifier") {
// Simple function call: foo()
functionName = functionNode.text;
} else if (functionNode.type === "member_expression") {
// Method call: obj.method()
for (let i = 0; i < functionNode.namedChildCount; i++) {
const child = functionNode.namedChild(i);
if (child.type === "property_identifier") {
functionName = child.text;
// Get the object name for more context
const objectNode = functionNode.namedChild(0);
if (objectNode && objectNode.type === "identifier") {
metadata.objectName = objectNode.text;
}
break;
}
}
}
if (functionName) {
// Check if this is calling a function we've already defined
const targetEntity = findEntityByName(functionName);
createRelationship(
scopeEntity.id,
targetEntity ? targetEntity.id : null,
functionName,
"CALLS_FUNCTION",
{
callLocation: {
line: node.startPosition.row,
column: node.startPosition.column,
},
...metadata,
}
);
}
}
}
// Recursively process children
for (let i = 0; i < node.namedChildCount; i++) {
const child = node.namedChild(i);
processFunctionCalls(child, scopeEntity);
}
}
/**
* Process class inheritance relationships
* @param {Object} node - Class declaration node
* @param {Object} classEntity - The entity representing the class
*/
function processClassInheritance(node, classEntity) {
// Check for an extends clause
for (let i = 0; i < node.namedChildCount; i++) {
const child = node.namedChild(i);
if (child.type === "extends_clause") {
for (let j = 0; j < child.namedChildCount; j++) {
const baseClassNode = child.namedChild(j);
if (
baseClassNode.type === "identifier" ||
baseClassNode.type === "nested_identifier"
) {
const baseClassName = baseClassNode.text;
// Check if the base class is defined in this file
const targetEntity = findEntityByName(
baseClassName,
"class_declaration"
);
createRelationship(
classEntity.id,
targetEntity ? targetEntity.id : null,
baseClassName,
"EXTENDS_CLASS"
);
break;
}
}
}
}
}
/**
* Process TypeScript interface implementation
* @param {Object} node - Class declaration node
* @param {Object} classEntity - The entity representing the class
*/
function processInterfaceImplementation(node, classEntity) {
// Check for an implements clause
for (let i = 0; i < node.namedChildCount; i++) {
const child = node.namedChild(i);
if (child.type === "implements_clause") {
for (let j = 0; j < child.namedChildCount; j++) {
const interfaceNode = child.namedChild(j);
if (
interfaceNode.type === "identifier" ||
interfaceNode.type === "nested_identifier"
) {
const interfaceName = interfaceNode.text;
// Check if the interface is defined in this file
const targetEntity = findEntityByName(
interfaceName,
"interface_declaration"
);
createRelationship(
classEntity.id,
targetEntity ? targetEntity.id : null,
interfaceName,
"IMPLEMENTS_INTERFACE"
);
}
}
}
}
}
/**
* Process variable references within a scope
* @param {Object} node - The node to check for variable references
* @param {Object} scopeEntity - The entity representing the current scope
*/
function processVariableReferences(node, scopeEntity) {
if (!node || !scopeEntity) return;
// Process identifiers that might be variable references
if (
node.type === "identifier" &&
// Skip if part of declaration, function name, etc.
node.parent &&
![
"variable_declarator",
"function_declaration",
"method_definition",
].includes(node.parent.type)
) {
const varName = node.text;
// Try to find the variable in our entities
const targetEntity = findEntityByName(varName, "variable_declarator");
createRelationship(
scopeEntity.id,
targetEntity ? targetEntity.id : null,
varName,
"REFERENCES_VARIABLE",
{
referenceLocation: {
line: node.startPosition.row,
column: node.startPosition.column,
},
}
);
}
// Recursively process children
for (let i = 0; i < node.namedChildCount; i++) {
const child = node.namedChild(i);
processVariableReferences(child, scopeEntity);
}
}
/**
* Process import/export relationships
* @param {Object} entity - The entity representing the import or export statement
*/
function processImportExportRelationships(entity) {
if (entity.entity_type === "import_statement") {
const modulePath = entity.custom_metadata.source;
const importNames = entity.custom_metadata.importNames || [];
// Create relationship for each imported symbol
for (const importName of importNames) {
createRelationship(
entity.id,
null, // We don't know the target entity ID since it's in another file
importName,
"IMPORTS_MODULE",
{ modulePath }
);
}
} else if (entity.entity_type === "export_statement") {
const exportedNames = entity.custom_metadata.exportedNames || [];
const isDefault = entity.custom_metadata.isDefault;
// For each exported symbol, create a relationship
for (const exportName of exportedNames) {
// Try to find the exported entity in our list
const targetEntity = findEntityByName(exportName);
createRelationship(
entity.id,
targetEntity ? targetEntity.id : null,
exportName,
"EXPORTS_SYMBOL",
{ isDefault }
);
}
}
}
/**
* Process a function declaration node
* @param {Object} node - Function declaration node
*/
function processFunctionDeclaration(node) {
// Find the identifier (function name) child node
let nameNode = null;
for (let i = 0; i < node.namedChildCount; i++) {
const child = node.namedChild(i);
if (child.type === "identifier") {
nameNode = child;
break;
}
}
if (nameNode) {
const functionName = nameNode.text;
const customMetadata = {
isAsync:
node.type === "function_declaration" &&
node.firstChild &&
node.firstChild.type === "async",
};
const entity = createEntity(
node,
"function_declaration",
functionName,
customMetadata
);
entities.push(entity);
// Store the previous parent and set this entity as the new parent
const prevParent = currentParentEntity;
currentParentEntity = entity;
// Process the function body to extract nested entities
for (let i = 0; i < node.namedChildCount; i++) {
const child = node.namedChild(i);
if (child.type === "statement_block") {
traverseNode(child);
// Process function calls and variable references in the function body
processFunctionCalls(child, entity);
processVariableReferences(child, entity);
}
}
// Restore the previous parent
currentParentEntity = prevParent;
}
}
/**
* Process a function expression node (including arrow functions)
* @param {Object} node - Function expression node
* @param {Object} parentNode - The parent node (e.g., variable declaration)
*/
function processFunctionExpression(node, parentNode) {
let functionName = "anonymous";
let customMetadata = {
isArrow: node.type === "arrow_function",
isAsync: false,
};
// Check if this is an async function
if (node.firstChild && node.firstChild.type === "async") {
customMetadata.isAsync = true;
}
// If parent is a variable declaration, use that as the function name
if (parentNode && parentNode.type === "variable_declarator") {
for (let i = 0; i < parentNode.namedChildCount; i++) {
const child = parentNode.namedChild(i);
if (child.type === "identifier") {
functionName = child.text;
break;
}
}
}
// If parent is a pair (object property), use that as the function name
if (parentNode && parentNode.type === "pair") {
for (let i = 0; i < parentNode.namedChildCount; i++) {
const child = parentNode.namedChild(i);
if (child.type === "property_identifier") {
functionName = child.text;
break;
}
}
}
// If parent is a member expression assignment, use the property name
if (
parentNode &&
(parentNode.type === "assignment_expression" ||
parentNode.type === "pair") &&
parentNode.namedChild(0) &&
parentNode.namedChild(0).type === "member_expression"
) {
const memberExpr = parentNode.namedChild(0);
for (let i = 0; i < memberExpr.namedChildCount; i++) {
const child = memberExpr.namedChild(i);
if (child.type === "property_identifier") {
functionName = child.text;
break;
}
}
}
const entityType =
node.type === "arrow_function"
? "arrow_function_expression"
: "function_expression";
const entity = createEntity(node, entityType, functionName, customMetadata);
entities.push(entity);
// Store the previous parent and set this entity as the new parent
const prevParent = currentParentEntity;
currentParentEntity = entity;
// Process the function body to extract nested entities
let bodyNode = null;
for (let i = 0; i < node.namedChildCount; i++) {
const child = node.namedChild(i);
if (child.type === "statement_block") {
bodyNode = child;
traverseNode(child);
}
}
// For arrow functions, the expression might be the body
if (!bodyNode && node.type === "arrow_function") {
for (let i = 0; i < node.namedChildCount; i++) {
const child = node.namedChild(i);
if (child.type !== "formal_parameters") {
bodyNode = child;
break;
}
}
}
// Process function calls and variable references in the function body
if (bodyNode) {
processFunctionCalls(bodyNode, entity);
processVariableReferences(bodyNode, entity);
}
// Restore the previous parent
currentParentEntity = prevParent;
}
/**
* Process a class declaration node
* @param {Object} node - Class declaration node
*/
function processClassDeclaration(node) {
// Find the identifier (class name) child node
let nameNode = null;
for (let i = 0; i < node.namedChildCount; i++) {
const child = node.namedChild(i);
if (child.type === "identifier") {
nameNode = child;
break;
}
}
if (nameNode) {
const className = nameNode.text;
const entity = createEntity(node, "class_declaration", className);
entities.push(entity);
// Process inheritance and interface implementation
processClassInheritance(node, entity);
processInterfaceImplementation(node, entity);
// Store the previous parent and set this entity as the new parent
const prevParent = currentParentEntity;
currentParentEntity = entity;
// Process the class body to extract methods
for (let i = 0; i < node.namedChildCount; i++) {
const child = node.namedChild(i);
if (child.type === "class_body") {
traverseNode(child);
}
}
// Restore the previous parent
currentParentEntity = prevParent;
}
}
/**
* Process a method definition node
* @param {Object} node - Method definition node
*/
function processMethodDefinition(node) {
// Find the method name
let nameNode = null;
for (let i = 0; i < node.namedChildCount; i++) {
const child = node.namedChild(i);
if (
child.type === "property_identifier" ||
child.type === "computed_property_name"
) {
nameNode = child;
break;
}
}
if (nameNode) {
const methodName = nameNode.text;
const customMetadata = {
isAsync: node.firstChild && node.firstChild.type === "async",
isStatic: node.firstChild && node.firstChild.type === "static",
isGetter: node.firstChild && node.firstChild.type === "get",
isSetter: node.firstChild && node.firstChild.type === "set",
};
const entity = createEntity(
node,
"method_definition",
methodName,
customMetadata
);
entities.push(entity);
// Store the previous parent and set this entity as the new parent
const prevParent = currentParentEntity;
currentParentEntity = entity;
// Process the method body to extract nested entities
let bodyNode = null;
for (let i = 0; i < node.namedChildCount; i++) {
const child = node.namedChild(i);
if (child.type === "statement_block") {
bodyNode = child;
traverseNode(child);
}
}
// Process function calls and variable references in the method body
if (bodyNode) {
processFunctionCalls(bodyNode, entity);
processVariableReferences(bodyNode, entity);
}
// Restore the previous parent
currentParentEntity = prevParent;
}
}
/**
* Process a variable declaration node
* @param {Object} node - Variable declaration node
*/
function processVariableDeclaration(node) {
const kind = node.firstChild ? node.firstChild.type : null; // var, let, const
for (let i = 0; i < node.namedChildCount; i++) {
const declarator = node.namedChild(i);
if (declarator.type === "variable_declarator") {
// Get the variable name
let nameNode = null;
for (let j = 0; j < declarator.namedChildCount; j++) {
const child = declarator.namedChild(j);
if (child.type === "identifier") {
nameNode = child;
break;
}
}
if (nameNode) {
const variableName = nameNode.text;
// Check if this is a function assignment
let valueNode = null;
for (let j = 0; j < declarator.namedChildCount; j++) {
const child = declarator.namedChild(j);
if (child.type === "function" || child.type === "arrow_function") {
valueNode = child;
processFunctionExpression(valueNode, declarator);
break;
}
}
// If not a function, create a variable entity
if (
!valueNode ||
(valueNode.type !== "function" &&
valueNode.type !== "arrow_function")
) {
const customMetadata = { kind: kind };
const entity = createEntity(
declarator,
"variable_declarator",
variableName,
customMetadata
);
entities.push(entity);
// If there's a value assigned, check for references
for (let j = 0; j < declarator.namedChildCount; j++) {
const child = declarator.namedChild(j);
if (child.type !== "identifier") {
// Process any variable references in the initialization
if (currentParentEntity) {
processVariableReferences(child, currentParentEntity);
}
break;
}
}
}
}
}
}
}
/**
* Process an interface declaration node (TypeScript)
* @param {Object} node - Interface declaration node
*/
function processInterfaceDeclaration(node) {
// Find the identifier (interface name) child node
let nameNode = null;
for (let i = 0; i < node.namedChildCount; i++) {
const child = node.namedChild(i);
if (child.type === "identifier") {
nameNode = child;
break;
}
}
if (nameNode) {
const interfaceName = nameNode.text;
const entity = createEntity(node, "interface_declaration", interfaceName);
entity.language = "typescript";
entities.push(entity);
// Check for extends clauses to establish relationships
for (let i = 0; i < node.namedChildCount; i++) {
const child = node.namedChild(i);
if (child.type === "extends_clause") {
for (let j = 0; j < child.namedChildCount; j++) {
const baseInterfaceNode = child.namedChild(j);
if (
baseInterfaceNode.type === "identifier" ||
baseInterfaceNode.type === "nested_identifier"
) {
const baseInterfaceName = baseInterfaceNode.text;
// Check if the base interface is defined in this file
const targetEntity = findEntityByName(
baseInterfaceName,
"interface_declaration"
);
createRelationship(
entity.id,
targetEntity ? targetEntity.id : null,
baseInterfaceName,
"EXTENDS_INTERFACE"
);
}
}
}
}
}
}
/**
* Process a type alias declaration node (TypeScript)
* @param {Object} node - Type alias declaration node
*/
function processTypeAliasDeclaration(node) {
// Find the identifier (type name) child node
let nameNode = null;
for (let i = 0; i < node.namedChildCount; i++) {
const child = node.namedChild(i);
if (child.type === "identifier") {
nameNode = child;
break;
}
}
if (nameNode) {
const typeName = nameNode.text;
const entity = createEntity(node, "type_alias_declaration", typeName);
entity.language = "typescript";
entities.push(entity);
}
}
/**
* Process an enum declaration node (TypeScript)
* @param {Object} node - Enum declaration node
*/
function processEnumDeclaration(node) {
// Find the identifier (enum name) child node
let nameNode = null;
for (let i = 0; i < node.namedChildCount; i++) {
const child = node.namedChild(i);
if (child.type === "identifier") {
nameNode = child;
break;
}
}
if (nameNode) {
const enumName = nameNode.text;
const entity = createEntity(node, "enum_declaration", enumName);
entity.language = "typescript";
entities.push(entity);
}
}
/**
* Process an import statement
* @param {Object} node - Import statement node
*/
function processImportStatement(node) {
// Gather the import specifiers or default import name
let importNames = [];
let importSource = null;
for (let i = 0; i < node.namedChildCount; i++) {
const child = node.namedChild(i);
if (child.type === "import_specifier") {
// Named import: import { name } from 'source'
for (let j = 0; j < child.namedChildCount; j++) {
const specifier = child.namedChild(j);
if (specifier.type === "identifier") {
importNames.push(specifier.text);
break;
}
}
} else if (child.type === "identifier") {
// Default import: import name from 'source'
importNames.push(child.text);
} else if (child.type === "namespace_import") {
// Namespace import: import * as name from 'source'
for (let j = 0; j < child.namedChildCount; j++) {
const specifier = child.namedChild(j);
if (specifier.type === "identifier") {
importNames.push(`* as ${specifier.text}`);
break;
}
}
} else if (child.type === "string") {
// Import source: from 'source'
importSource = child.text;
}
}
// Create an entity for the import statement
if (importNames.length > 0 && importSource) {
const importName = importNames.join(", ");
const customMetadata = {
importNames: importNames,
source: importSource,
};
const entity = createEntity(
node,
"import_statement",
importName,
customMetadata
);
entities.push(entity);
// Process import relationships
processImportExportRelationships(entity);
}
}
/**
* Process an export statement
* @param {Object} node - Export statement node
*/
function processExportStatement(node) {
let exportName = "default";
let customMetadata = {
isDefault: false,
};
// Check if this is a default export
if (node.type === "export_statement") {
for (let i = 0; i < node.childCount; i++) {
if (node.child(i).type === "default") {
customMetadata.isDefault = true;
break;
}
}
}
// Extract the exported name(s)
let exportedNames = [];
for (let i = 0; i < node.namedChildCount; i++) {
const child = node.namedChild(i);
if (child.type === "identifier") {
exportedNames.push(child.text);
} else if (child.type === "export_specifier") {
// Named export specifier: export { name }
for (let j = 0; j < child.namedChildCount; j++) {
const specifier = child.namedChild(j);
if (specifier.type === "identifier") {
exportedNames.push(specifier.text);
break;
}
}
} else if (child.type === "export_clause") {
// Get names from export clause: export { name1, name2 }
for (let j = 0; j < child.namedChildCount; j++) {
const specifier = child.namedChild(j);
if (specifier.type === "export_specifier") {
for (let k = 0; k < specifier.namedChildCount; k++) {
const identifier = specifier.namedChild(k);
if (identifier.type === "identifier") {
exportedNames.push(identifier.text);
break;
}
}
}
}
} else if (
child.type === "function_declaration" ||
child.type === "class_declaration" ||
child.type === "variable_declaration"
) {
// Traverse the child node to process the exported declaration
traverseNode(child);
}
}
if (exportedNames.length > 0) {
exportName = exportedNames.join(", ");
customMetadata.exportedNames = exportedNames;
}
// Create an entity for the export statement
const entity = createEntity(
node,
"export_statement",
exportName,
customMetadata
);
entities.push(entity);
// Process export relationships
processImportExportRelationships(entity);
}
/**
* Process a JSX/TSX element (React component)
* @param {Object} node - JSX element node
*/
function processJsxElement(node) {
// Find the opening tag name
let tagName = "unknown";
for (let i = 0; i < node.namedChildCount; i++) {
const child = node.namedChild(i);
if (child.type === "jsx_opening_element") {
for (let j = 0; j < child.namedChildCount; j++) {
const nameNode = child.namedChild(j);
if (
nameNode.type === "identifier" ||
nameNode.type === "nested_identifier" ||
nameNode.type === "jsx_identifier"
) {
tagName = nameNode.text;
break;
}
}
break;
}
}
// Create a custom component entity if the tag name starts with a capital letter
// (following React's convention for custom components)
if (tagName.match(/^[A-Z]/)) {
const customMetadata = {
isComponent: true,
};
const entity = createEntity(node, "jsx_element", tagName, customMetadata);
entities.push(entity);
// If we're in a component and this is a custom component usage,
// create a "USES_COMPONENT" relationship
if (currentParentEntity) {
// Try to find a component declaration in our entities
const targetEntity = findEntityByName(tagName);
createRelationship(
currentParentEntity.id,
targetEntity ? targetEntity.id : null,
tagName,
"USES_COMPONENT"
);
}
}
}
/**
* Process a comment node
* @param {Object} node - Comment node
*/
function processComment(node) {
// Extract the comment text, removing comment markers
let commentText = node.text;
// For block comments
if (node.type === "comment" && commentText.startsWith("/*")) {
commentText = commentText.substring(2, commentText.length - 2).trim();
}
// For line comments
else if (node.type === "comment" && commentText.startsWith("//")) {
commentText = commentText.substring(2).trim();
}
// Only create entities for significant comments (more than a few words)
const words = commentText.split(/\s+/).filter(Boolean);
if (words.length >= 3) {
const customMetadata = {
isBlock: node.type === "comment" && node.text.startsWith("/*"),
isJSDoc: node.type === "comment" && node.text.startsWith("/**"),
};
// Use first few words as a name
const name =
words.slice(0, 3).join(" ") + (words.length > 3 ? "..." : "");
const entity = createEntity(node, "comment", name, customMetadata);
entities.push(entity);
}
}
/**
* Recursively traverse the AST node
* @param {Object} node - Current node in traversal
*/
function traverseNode(node) {
switch (node.type) {
case "function_declaration":
processFunctionDeclaration(node);
break;
case "function":
case "arrow_function":
processFunctionExpression(node, node.parent);
break;
case "class_declaration":
processClassDeclaration(node);
break;
case "method_definition":
processMethodDefinition(node);
break;
case "lexical_declaration":
case "variable_declaration":
processVariableDeclaration(node);
break;
case "interface_declaration":
processInterfaceDeclaration(node);
break;
case "type_alias_declaration":
processTypeAliasDeclaration(node);
break;
case "enum_declaration":
processEnumDeclaration(node);
break;
case "import_statement":
processImportStatement(node);
break;
case "export_statement":
processExportStatement(node);
break;
case "jsx_element":
case "jsx_self_closing_element":
processJsxElement(node);
break;
case "comment":
processComment(node);
break;
default:
// Continue traversing for other node types
for (let i = 0; i < node.namedChildCount; i++) {
traverseNode(node.namedChild(i));
}
break;
}
}
// Start traversal from the root node
traverseNode(astRootNode);
// Return the extracted entities and relationships
return { entities, relationships };
}
/**
* Utility function to convert a node object to a plain JavaScript object
* This can be useful for debugging or serializing the node
* @param {Object} node - Tree-sitter node
* @returns {Object} Plain object representation
*/
export function nodeToObject(node) {
if (!node) return null;
const obj = {
type: node.type,
text: node.text,
startPosition: {
row: node.startPosition.row,
column: node.startPosition.column,
},
endPosition: {
row: node.endPosition.row,
column: node.endPosition.column,
},
childCount: node.childCount,
namedChildCount: node.namedChildCount,
children: [],
};
// Add children recursively
for (let i = 0; i < node.namedChildCount; i++) {
obj.children.push(nodeToObject(node.namedChild(i)));
}
return obj;
}
export default {
parseJavaScript,
nodeToObject,
};
|