Skip to content

x/tools/internal/refactor/inline: choose better local names for renaming imports #74965

@gavinkflam

Description

@gavinkflam

Part of #63352

The current mechanism takes the last segment of the package path and appends a numeric suffix (if needed) to ensure uniqueness. This produces low-quality local names such path2, format3 and utils5.

I'm proposing potential strategies (2), (3) and (4), as illustrated below, to generate better names. These strategies follow common practices for package aliasing and result in more meaningful names.

  1. Try PkgName (unchanged).
  2. Try the local alias of the callee. (There is a todo comment in inline.go#525)
  3. Try PkgName + "pkg". For example, path becomes pathpkg.
  4. Prepend the preceding path segment if available. For example, golang.org/x/tools/internal/astutil becomes internalastutil.
  5. Appending a numeric suffix (unchanged). For example, path becomes path1, path2, ...

I'd love to implement these myself.


Alias examples in OSS repositories:

Strategy 3

  1. path as pathpkg: 14 usages in golang/tools, 11 usages in golang/go
  2. urlpkg, tracepkg, errorspkg, stringspkg, logpkg, htmlpkg, etc. in golang/go and golang/tools
  3. nodepkg, volumepkg, auditpkg, controllerpkg, etc. in kubernetes/kubernetes

Strategy 4

  1. internalastutil: 11 usages in golang/tools
  2. metav1: 2700+ usages in kubernetes/kubernetes
  3. apierrors: 564 usages in kubernetes/kubernetes
  4. utilerrors: 165 usages in kubernetes/kubernetes

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.ToolProposalIssues describing a requested change to a Go tool or command-line program.ToolsThis label describes issues relating to any tools in the x/tools repository.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions