Remove redundant code and deduplicate indexFile
- Remove extractFunctionVariable() and its dispatch (already handled by extractVariable) - Remove dead getGrammar() export (zero callers) - Deduplicate indexFile by delegating to indexFileWithContent - Remove redundant arrow function variable extraction tests (covered by existing suite)
This commit is contained in:
@@ -524,45 +524,6 @@ export function bar() {}
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Arrow Function Variable Extraction', () => {
|
|
||||||
it('should extract const arrow functions as function nodes', () => {
|
|
||||||
const code = `
|
|
||||||
const handleClick = () => {
|
|
||||||
console.log('clicked');
|
|
||||||
};
|
|
||||||
`;
|
|
||||||
const result = extractFromSource('handler.ts', code);
|
|
||||||
|
|
||||||
const funcNode = result.nodes.find((n) => n.kind === 'function' && n.name === 'handleClick');
|
|
||||||
expect(funcNode).toBeDefined();
|
|
||||||
expect(funcNode?.kind).toBe('function');
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should detect async arrow functions', () => {
|
|
||||||
const code = `
|
|
||||||
export const fetchUser = async (id: string) => {
|
|
||||||
return await db.find(id);
|
|
||||||
};
|
|
||||||
`;
|
|
||||||
const result = extractFromSource('api.ts', code);
|
|
||||||
|
|
||||||
const funcNode = result.nodes.find((n) => n.kind === 'function' && n.name === 'fetchUser');
|
|
||||||
expect(funcNode).toBeDefined();
|
|
||||||
expect(funcNode?.isExported).toBe(true);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should not create duplicate nodes for arrow functions in export statements', () => {
|
|
||||||
const code = `
|
|
||||||
export const compute = (x: number) => x * 2;
|
|
||||||
`;
|
|
||||||
const result = extractFromSource('math.ts', code);
|
|
||||||
|
|
||||||
const funcNodes = result.nodes.filter((n) => n.kind === 'function' && n.name === 'compute');
|
|
||||||
// Should appear only once, not duplicated between extractFunctionVariable and extractFunction
|
|
||||||
expect(funcNodes).toHaveLength(1);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('Python Extraction', () => {
|
describe('Python Extraction', () => {
|
||||||
it('should extract function definitions', () => {
|
it('should extract function definitions', () => {
|
||||||
const code = `
|
const code = `
|
||||||
|
|||||||
@@ -156,14 +156,6 @@ function loadGrammar(language: Language): unknown | null {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Get a grammar by language, loading it lazily if needed.
|
|
||||||
* Exported for direct grammar access without parser initialization.
|
|
||||||
*/
|
|
||||||
export function getGrammar(language: string): unknown | null {
|
|
||||||
return loadGrammar(language as Language);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get a parser for the specified language
|
* Get a parser for the specified language
|
||||||
*/
|
*/
|
||||||
|
|||||||
+2
-38
@@ -487,7 +487,7 @@ export class ExtractionOrchestrator {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check file exists and is readable
|
// Read file content and stats
|
||||||
let content: string;
|
let content: string;
|
||||||
let stats: fs.Stats;
|
let stats: fs.Stats;
|
||||||
try {
|
try {
|
||||||
@@ -509,43 +509,7 @@ export class ExtractionOrchestrator {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check file size
|
return this.indexFileWithContent(relativePath, content, stats);
|
||||||
if (stats.size > this.config.maxFileSize) {
|
|
||||||
return {
|
|
||||||
nodes: [],
|
|
||||||
edges: [],
|
|
||||||
unresolvedReferences: [],
|
|
||||||
errors: [
|
|
||||||
{
|
|
||||||
message: `File exceeds max size (${stats.size} > ${this.config.maxFileSize})`,
|
|
||||||
severity: 'warning',
|
|
||||||
},
|
|
||||||
],
|
|
||||||
durationMs: 0,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
// Detect language
|
|
||||||
const language = detectLanguage(relativePath);
|
|
||||||
if (!isLanguageSupported(language)) {
|
|
||||||
return {
|
|
||||||
nodes: [],
|
|
||||||
edges: [],
|
|
||||||
unresolvedReferences: [],
|
|
||||||
errors: [],
|
|
||||||
durationMs: 0,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
// Extract from source
|
|
||||||
const result = extractFromSource(relativePath, content, language);
|
|
||||||
|
|
||||||
// Store in database
|
|
||||||
if (result.nodes.length > 0 || result.errors.length === 0) {
|
|
||||||
this.storeExtractionResult(relativePath, content, language, stats, result);
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -978,14 +978,6 @@ export class TreeSitterExtractor {
|
|||||||
else if (this.extractor.typeAliasTypes.includes(nodeType)) {
|
else if (this.extractor.typeAliasTypes.includes(nodeType)) {
|
||||||
this.extractTypeAlias(node);
|
this.extractTypeAlias(node);
|
||||||
}
|
}
|
||||||
// Check for arrow functions / function expressions assigned to variables (JS/TS)
|
|
||||||
else if (nodeType === 'variable_declarator') {
|
|
||||||
const valueNode = getChildByField(node, 'value');
|
|
||||||
if (valueNode && (valueNode.type === 'arrow_function' || valueNode.type === 'function_expression')) {
|
|
||||||
this.extractFunctionVariable(node);
|
|
||||||
skipChildren = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Check for variable declarations (const, let, var, etc.)
|
// Check for variable declarations (const, let, var, etc.)
|
||||||
// Only extract top-level variables (not inside functions/methods)
|
// Only extract top-level variables (not inside functions/methods)
|
||||||
else if (this.extractor.variableTypes.includes(nodeType) && !this.isInsideClassLikeNode()) {
|
else if (this.extractor.variableTypes.includes(nodeType) && !this.isInsideClassLikeNode()) {
|
||||||
@@ -1109,62 +1101,6 @@ export class TreeSitterExtractor {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Extract an arrow function or function expression assigned to a variable.
|
|
||||||
* Handles patterns like: const foo = () => {} or const bar = function() {}
|
|
||||||
*/
|
|
||||||
private extractFunctionVariable(node: SyntaxNode): void {
|
|
||||||
if (!this.extractor) return;
|
|
||||||
|
|
||||||
// Only handle variable_declarator where value is arrow_function or function_expression
|
|
||||||
if (node.type !== 'variable_declarator') return;
|
|
||||||
|
|
||||||
const nameNode = getChildByField(node, 'name');
|
|
||||||
const valueNode = getChildByField(node, 'value');
|
|
||||||
|
|
||||||
if (!nameNode || !valueNode) return;
|
|
||||||
if (valueNode.type !== 'arrow_function' && valueNode.type !== 'function_expression') return;
|
|
||||||
|
|
||||||
const name = getNodeText(nameNode, this.source);
|
|
||||||
if (!name) return;
|
|
||||||
|
|
||||||
// Check if exported by walking parents
|
|
||||||
let isExported = false;
|
|
||||||
let current = node.parent;
|
|
||||||
while (current) {
|
|
||||||
if (current.type === 'export_statement') {
|
|
||||||
isExported = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (current.type === 'program' || current.type === 'module') break;
|
|
||||||
current = current.parent;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Build signature from the arrow function parameters
|
|
||||||
let signature: string | undefined;
|
|
||||||
const params = getChildByField(valueNode, 'parameters');
|
|
||||||
if (params) {
|
|
||||||
signature = `${name}${getNodeText(params, this.source)}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if async
|
|
||||||
const isAsync = this.extractor.isAsync?.(valueNode);
|
|
||||||
|
|
||||||
const funcNode = this.createNode('function', name, node, {
|
|
||||||
isExported,
|
|
||||||
signature: signature || undefined,
|
|
||||||
isAsync,
|
|
||||||
});
|
|
||||||
|
|
||||||
// Push to stack and visit body for call extraction
|
|
||||||
this.nodeStack.push(funcNode.id);
|
|
||||||
const body = getChildByField(valueNode, this.extractor.bodyField);
|
|
||||||
if (body) {
|
|
||||||
this.visitFunctionBody(body, funcNode.id);
|
|
||||||
}
|
|
||||||
this.nodeStack.pop();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Extract a function
|
* Extract a function
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user