buildImportMap creates a map of package names to import information. It handles standard imports, named imports, and excludes blank/dot imports.
(file *ast.File)
| 22 | // buildImportMap creates a map of package names to import information. |
| 23 | // It handles standard imports, named imports, and excludes blank/dot imports. |
| 24 | func buildImportMap(file *ast.File) map[string]*importInfo { |
| 25 | imports := make(map[string]*importInfo) |
| 26 | |
| 27 | for _, impDecl := range file.Imports { |
| 28 | path := strings.Trim(impDecl.Path.Value, `"`) |
| 29 | |
| 30 | // Handle imports with explicit names |
| 31 | if impDecl.Name != nil { |
| 32 | switch impDecl.Name.Name { |
| 33 | case "_", ".": |
| 34 | // Blank imports (for side effects) and dot imports are always kept |
| 35 | continue |
| 36 | default: |
| 37 | // Named import: use the alias as the local name |
| 38 | imports[impDecl.Name.Name] = &importInfo{ |
| 39 | spec: impDecl, |
| 40 | path: path, |
| 41 | localName: impDecl.Name.Name, |
| 42 | used: false, |
| 43 | } |
| 44 | } |
| 45 | } else { |
| 46 | // Standard import: infer package name from path |
| 47 | localName := inferPackageName(path) |
| 48 | imports[localName] = &importInfo{ |
| 49 | spec: impDecl, |
| 50 | path: path, |
| 51 | localName: localName, |
| 52 | used: false, |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | return imports |
| 58 | } |
| 59 | |
| 60 | // detectUsedImports performs a single AST walk to mark which imports are used. |
| 61 | // It looks for qualified identifiers (pkg.Name). |
no test coverage detected