Repository navigation
fix(lint): keep templ fixtures as templ generates them - #46
Merged
Merged
Conversation
structpages-lint maps a .templ attribute to its generated code through templ's source map only when the _templ.go is byte-for-byte what the templ generator produces. The repo's pre-commit hook ran goimports and gofmt -s over every .go file but gsx's *.x.go, so it regrouped the imports of the committed *_templ.go fixtures. Each then read as stale, and the .templ half of the region and keyed checks went untested: TestRegion_Integration and TestKeyed_Integration were missing all their .templ findings. CI never ran the tools/lint tests, so nothing caught it. The linter itself was right: `templ generate` output is exactly what it reproduces. The hook now skips *_templ.go as it skips *.x.go, and the five fixtures are regenerated with templ v0.3.1020 and keep their deliberate differences (another version header in pages_templ.go, none in noversion_templ.go, an edited comment that keeps stale_templ.go stale). A templscan test checks every fixture is accepted, except stale.templ, and says how to regenerate one that isn't. A new CI job runs the tools/lint tests. docs/lint.md now names a formatter run over a _templ.go as one of the differences that make its attributes go unchecked.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #46 +/- ##
=======================================
Coverage 96.09% 96.09%
=======================================
Files 20 20
Lines 1869 1869
=======================================
Hits 1796 1796
Misses 43 43
Partials 30 30 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
TestRegion_IntegrationandTestKeyed_Integrationintools/lintwere missing every.templfinding: 8 for region and 4 for keyed. The.templhalf of both checks was effectively untested, and CI never runs thetools/linttests, so it went unnoticed.Root cause
The linter maps a
.templattribute to its generated code through templ's source map only when the_templ.gois byte-for-byte what the templ generator produces. That check was correct:templ generatev0.3.1020 isgenerator.Generatefollowed byformat.Source, which is exactly what the linter reproduces.The fixtures were the problem.
.githooks/pre-commitrunsgoimports -wandgofmt -w -sover every tracked.gofile except gsx's*.x.go, then re-stages them. goimports regrouped each committed*_templ.go's threeimportlines into one block, so every fixture read as stale and its attributes were skipped. #44's new fixture passed before its commit and broke when the hook rewrote it.Fix
Hook:
.githooks/pre-commitskips*_templ.goas it already skips*.x.go.Fixtures: all five templ fixtures are regenerated with templ v0.3.1020, each from the directory it was originally generated in, so
FileNamedoesn't change. Their deliberate differences are kept:pages_templ.gohas another version header;noversion_templ.gohas none;stale_templ.gohas an edited comment that keeps it stale.Each file's diff is the import block only.
Guard: a new test,
templscan.TestExprAttrs_fixturesAreTemplOutput, checks that every fixture is accepted exceptstale.templ. Its failure message says how to regenerate a fixture.CI: a new
Test tools/lintjob runsgo test ./...intools/linton Go 1.26.Docs:
docs/lint.mdnow names a formatter run over a_templ.go(goimports regroups its imports) as one of the differences that leave its attributes unchecked, and advises leaving*_templ.goout ofgoimports/gofmt -s.Impact on users
None in the linter:
templ generateoutput was always checked. Only a project that reformats its_templ.goafter generating gets its templ attributes skipped, and the docs now say so.Testing
cd tools/lint && go test -count=1 ./...passes,TestRegion_IntegrationandTestKeyed_Integrationincluded, for the first time in a while.