MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / extractVariable

Method extractVariable

src/extraction/tree-sitter.ts:4139–4581  ·  view source on GitHub ↗

* Extract a variable declaration (const, let, var, etc.) * * Extracts top-level and module-level variable declarations. * Captures the variable name and first 100 chars of initializer in signature for searchability. * * Returns the initializers it walked under the symbol they declare

(node: SyntaxNode)

Source from the content-addressed store, hash-verified

4137 * the file, as a second reference to the same function from the same spot.
4138 */
4139 private extractVariable(node: SyntaxNode): Set<number> | undefined {
4140 if (!this.extractor) return undefined;
4141 let walked: Set<number> | undefined;
4142
4143 // Different languages have different variable declaration structures
4144 // TypeScript/JavaScript: lexical_declaration contains variable_declarator children
4145 // Python: assignment has left (identifier) and right (value)
4146 // Go: var_declaration, short_var_declaration, const_declaration
4147
4148 const isConst = this.extractor.isConst?.(node) ?? false;
4149 const kind: NodeKind = isConst ? 'constant' : 'variable';
4150 const docstring = this.docstringFor(node);
4151 const isExported = this.extractor.isExported?.(node, this.source) ?? false;
4152
4153 // Extract variable declarators based on language
4154 if (this.language === 'typescript' || this.language === 'javascript' ||
4155 this.language === 'tsx' || this.language === 'jsx' || this.language === 'cfscript' ||
4156 this.language === 'arkts') {
4157 // Handle lexical_declaration and variable_declaration
4158 // These contain one or more variable_declarator children
4159 for (let i = 0; i < node.namedChildCount; i++) {
4160 const child = node.namedChild(i);
4161 if (child?.type === 'variable_declarator') {
4162 const nameNode = getChildByField(child, 'name');
4163 const valueNode = getChildByField(child, 'value');
4164
4165 if (nameNode) {
4166 // Skip destructured patterns (e.g., `let { x, y } = $props()` in Svelte)
4167 // These produce ugly multi-line names like "{ class: className }".
4168 // EXCEPT `export const { useGetXQuery } = someApi` — the RTK Query
4169 // generated hooks: real exported symbols destructured off a createApi
4170 // result. Mint a node per binding matching the hook convention (gated
4171 // on a bare-identifier RHS so ordinary destructures stay skipped).
4172 if (nameNode.type === 'object_pattern' || nameNode.type === 'array_pattern') {
4173 if (nameNode.type === 'object_pattern' && valueNode?.type === 'identifier') {
4174 this.extractRtkHookBindings(nameNode, isExported);
4175 }
4176 // A pattern declares no symbol of its own, but its initializer
4177 // (and any default value in the pattern) still runs. Walk the
4178 // declarator the way a function body walks it, with the
4179 // enclosing scope on the stack: the file for module code, which
4180 // a `<script setup>` block hands to its component. Without this
4181 // `const { data } = useFetch()` recorded no call at all (#2340).
4182 // Mirrored in the kernel (tsjs/extractors.rs extract_variable).
4183 this.visitFunctionBody(child, '');
4184 continue;
4185 }
4186 const name = getNodeText(nameNode, this.source);
4187 // Arrow functions / function expressions: extract as function instead of variable
4188 if (valueNode && (valueNode.type === 'arrow_function' || valueNode.type === 'function_expression' || valueNode.type === 'generator_function')) {
4189 this.extractFunction(valueNode);
4190 continue;
4191 }
4192
4193 // Capture first 100 chars of initializer for context (stored in signature for searchability)
4194 const initValue = valueNode ? getNodeText(valueNode, this.source).slice(0, 100) : undefined;
4195 const initSignature = initValue ? `= ${initValue}${initValue.length >= 100 ? '...' : ''}` : undefined;
4196

Callers 1

dispatchNodeMethod · 0.95

Calls 15

docstringForMethod · 0.95
visitFunctionBodyMethod · 0.95
extractFunctionMethod · 0.95
reactComponentHocMethod · 0.95
createNodeMethod · 0.95
ownedObjectValueMethod · 0.95

Tested by

no test coverage detected