Fix AST parsing issues with function scope, generics, and test packages #12
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR fixes several parsing issues that were causing errors when analyzing large codebases:
Issues Fixed
1. Function-scoped declarations
Problem: Function-local variables were being tracked as file-level declarations, causing "duplicate declaration" errors when the same variable name was used in multiple functions.
Solution: Modified the visitor to return
nilafter emittingFuncDeclnodes, preventing descent into function bodies. Import cycle detection only needs package-level declarations.2. File emission order
Problem: All files were emitted when processing a
Packagenode, butImportSpecnodes were processed later during the AST walk. This caused all imports to be assigned to the last file in the package, leading to "duplicate import" errors.Solution: Added package context tracking (
currentPackage,currentDirName) to the visitor. File nodes are now emitted individually as they're encountered, with proper filename lookup from the package context.3. Blank identifier
Problem: The blank identifier
_was being tracked like a regular declaration, but it's used for compile-time checks and can appear multiple times.Solution: Skip tracking declarations named
_in theaddGenDeclfunction.4. External test packages
Problem: Packages ending in
_test(external test packages) were being processed, but they can coexist with regular packages in the same directory and cannot be imported.Solution: Added filtering in
main.goto skip packages with names ending in_test.5. Generic type receivers
Problem: Methods on generic types like
*Foo[T]orFoo[T, U]were causing errors because the receiver name extraction didn't handle generic type syntax.Solution: Added
extractTypeNamefunction that recursively extracts base type names fromast.IndexExprandast.IndexListExprnodes. Updated receiver type handling to support generic types.6. Error messages
Enhancement: Added file paths to all error messages for easier debugging.
Testing
These fixes were tested on a large codebase with:
The tool now successfully generates import cycle graphs for such codebases.