Documentation
¶
Overview ¶
Package imports normalizes and cleans import sets after rewrites.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Add ¶
Add ensures that the specified package path is imported in the AST file. It delegates to astutil.AddImport which handles creating the import declaration block if missing and deduplicating existing imports.
fset: The file set containing the file source positions. file: The AST file to modify. pkgPath: The import path to add (e.g., "fmt" or "github.com/pkg/errors").
Returns true if the import was added, false if it was already present.
func AddNamed ¶
AddNamed ensures that the specified package path is imported with the given alias name. This is useful for preventing shadowing issues or handling naming conflicts. It delegates to astutil.AddNamedImport.
fset: The file set containing the file source positions. file: The AST file to modify. name: The local package name (alias) to use. If empty, it works like Add. pkgPath: The import path to add.
Returns true if the import was added, false if it was already present with the same alias.
Types ¶
type ConflictResolver ¶
type ConflictResolver struct {
// contains filtered or unexported fields
}
ConflictResolver identifies naming collisions between intended package imports and existing identifiers in a file scope. It determines safe aliases for imports to ensure injected code does not break local variable references.
func NewConflictResolver ¶
func NewConflictResolver(pkg *packages.Package, file *ast.File) *ConflictResolver
NewConflictResolver creates a resolver for the given package and file.
pkg: The loaded package containing type information. file: The AST file to analyze.
func (*ConflictResolver) ResolveAlias ¶
func (c *ConflictResolver) ResolveAlias(requestedPkg, importPath string) (string, bool)
ResolveAlias checks if a requested package name (e.g. "errors") is shadowed by any local definition in the scope of the file. If shadowed, it generates a unique alias (e.g. "errors1") and ensures the import is added with that alias.
requestedPkg: The default package name (e.g. "fmt", "errors"). importPath: The full import path (e.g. "fmt", "github.com/pkg/errors").
Returns the name to use in the code. If no conflict, returns requestedPkg. If conflict, returns the alias.