|
@@ -1499,13 +1499,14 @@ export class TreeSitterExtractor {
|
|
|
|
|
|
|
|
for (const spec of specs) {
|
|
for (const spec of specs) {
|
|
|
const nameNode = spec.namedChild(0);
|
|
const nameNode = spec.namedChild(0);
|
|
|
|
|
+ let varNode: Node | null = null;
|
|
|
if (nameNode && nameNode.type === 'identifier') {
|
|
if (nameNode && nameNode.type === 'identifier') {
|
|
|
const name = getNodeText(nameNode, this.source);
|
|
const name = getNodeText(nameNode, this.source);
|
|
|
const valueNode = spec.namedChildCount > 1 ? spec.namedChild(spec.namedChildCount - 1) : null;
|
|
const valueNode = spec.namedChildCount > 1 ? spec.namedChild(spec.namedChildCount - 1) : null;
|
|
|
const initValue = valueNode ? getNodeText(valueNode, this.source).slice(0, 100) : undefined;
|
|
const initValue = valueNode ? getNodeText(valueNode, this.source).slice(0, 100) : undefined;
|
|
|
const initSignature = initValue ? `= ${initValue}${initValue.length >= 100 ? '...' : ''}` : undefined;
|
|
const initSignature = initValue ? `= ${initValue}${initValue.length >= 100 ? '...' : ''}` : undefined;
|
|
|
|
|
|
|
|
- this.createNode(node.type === 'const_declaration' ? 'constant' : 'variable', name, spec, {
|
|
|
|
|
|
|
+ varNode = this.createNode(node.type === 'const_declaration' ? 'constant' : 'variable', name, spec, {
|
|
|
docstring,
|
|
docstring,
|
|
|
signature: initSignature,
|
|
signature: initSignature,
|
|
|
});
|
|
});
|
|
@@ -1515,8 +1516,16 @@ export class TreeSitterExtractor {
|
|
|
// implementations) or `var c = pkg.New()` are extracted as
|
|
// implementations) or `var c = pkg.New()` are extracted as
|
|
|
// instantiates/calls dependencies — the body walker only covers
|
|
// instantiates/calls dependencies — the body walker only covers
|
|
|
// initializers inside functions, not these top-level declarations.
|
|
// initializers inside functions, not these top-level declarations.
|
|
|
|
|
+ // Scope the walk to the declared symbol so a call inside an anonymous
|
|
|
|
|
+ // func_literal initializer — a cobra `RunE: func(){…}` handler, a
|
|
|
|
|
+ // goroutine or callback closure — attributes to the var instead of
|
|
|
|
|
+ // leaking to the file node (which reads as "no caller"), issue #693.
|
|
|
const valueField = getChildByField(spec, 'value');
|
|
const valueField = getChildByField(spec, 'value');
|
|
|
- if (valueField) this.visitFunctionBody(valueField, '');
|
|
|
|
|
|
|
+ if (valueField) {
|
|
|
|
|
+ if (varNode) this.nodeStack.push(varNode.id);
|
|
|
|
|
+ this.visitFunctionBody(valueField, varNode?.id ?? '');
|
|
|
|
|
+ if (varNode) this.nodeStack.pop();
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Handle short_var_declaration (:=)
|
|
// Handle short_var_declaration (:=)
|