Skip to content

Don't remove unaliased hyphenated imports if they're used#1342

Merged
a-h merged 2 commits intoa-h:mainfrom
whalenrp:fix-imports
Feb 15, 2026
Merged

Don't remove unaliased hyphenated imports if they're used#1342
a-h merged 2 commits intoa-h:mainfrom
whalenrp:fix-imports

Conversation

@whalenrp
Copy link
Copy Markdown
Contributor

Hyphenated imports, when aliased, are preserved if they're in use. However, it's supported in Go to also reference these by the package name directly without an alias if that package name doesn't have hyphens. I'm seeing issues where running templ fmt against files without manually-added package aliases causes these imports to get stripped away incorrectly. This change passes back the go AST and uses it to validate whether the unhyphenated package name is used for a hyphenated import. If so, we skip the deletion logic and leave that import around.

Before:
- import "github.com/foo/css-classes" + cssclasses.Header → import removed ❌
- import cssclasses "github.com/foo/css-classes" + cssclasses.Header → import kept ✓

After:
- import "github.com/foo/css-classes" + cssclasses.Header → import kept ✓
- import cssclasses "github.com/foo/css-classes" + cssclasses.Header → import kept ✓

whalenrp and others added 2 commits February 14, 2026 14:36
Hyphenated imports, when aliased, are preserved if they're in use.
However, it's supported in Go to also reference these by the package
name directly without an alias if that package name doesn't have
hyphens. I'm seeing issues where running `templ fmt` against files
without manually-added package aliases causes these imports to get
stripped away incorrectly. This change passes back the go AST and uses
it to validate whether the unhyphenated package name is used for a
hyphenated import. If so, we skip the deletion logic and leave that
import around.
@a-h
Copy link
Copy Markdown
Owner

a-h commented Feb 15, 2026

Nice work, thanks.

@a-h a-h merged commit bdda41e into a-h:main Feb 15, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants