-
-
Notifications
You must be signed in to change notification settings - Fork 274
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: map errors from formatting generated files back to their locations in the templ file #737
Merged
Merged
Changes from 7 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
4968031
fix: attempt to remap formatting errors to the templ file
Robert-Kolmos a59284e
fix: update error message when mapping fails
Robert-Kolmos 0066dc5
fix: update formatter error parsing to use the error type
Robert-Kolmos 112de91
fix: refactor error to use scanner.ErrorList instead of a custom string
Robert-Kolmos 0b3ce78
fix: add tests for error mapping behavior
Robert-Kolmos 6d4e562
fix: fix issue where offset was still in generated file
Robert-Kolmos 18c5d2f
fix: add .error suffixes to test files that fail to generate
Robert-Kolmos 290a477
fix: wrap formatter error with context
Robert-Kolmos 8a45023
fix: remove extra newline
Robert-Kolmos 4604221
fix: make some test failures fatal
Robert-Kolmos 97b6b80
fix: include test name in errors
Robert-Kolmos 26e431e
fix: make test run on the HandleEvent method instead of generate
Robert-Kolmos 5a76b3f
fix: update comment
Robert-Kolmos e29ce62
refactor: simplify remap
a-h File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package generatecmd | ||
|
||
import ( | ||
"context" | ||
"go/scanner" | ||
"go/token" | ||
"io" | ||
"log/slog" | ||
"testing" | ||
|
||
"github.com/a-h/templ/generator" | ||
) | ||
|
||
func TestEventHandler(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
fileName string | ||
errorPositions []token.Position | ||
}{ | ||
{ | ||
name: "single error outputs location in srcFile", | ||
fileName: "single_error.templ.error", | ||
errorPositions: []token.Position{ | ||
{Filename: "single_error.templ.error", Offset: 41, Line: 3, Column: 20}, | ||
}, | ||
}, | ||
{ | ||
name: "multiple errors all output locations in srcFile", | ||
fileName: "multiple_errors.templ.error", | ||
errorPositions: []token.Position{ | ||
{Filename: "multiple_errors.templ.error", Offset: 36, Line: 3, Column: 15}, | ||
{Filename: "multiple_errors.templ.error", Offset: 96, Line: 7, Column: 22}, | ||
}, | ||
}, | ||
} | ||
|
||
slog := slog.New(slog.NewTextHandler(io.Discard, &slog.HandlerOptions{})) | ||
fseh := NewFSEventHandler(slog, ".", false, []generator.GenerateOpt{}, false, false, true) | ||
for _, test := range tests { | ||
_, _, _, err := fseh.generate(context.Background(), test.fileName) | ||
if err == nil { | ||
t.Errorf("No error was thrown for file %s", test.fileName) | ||
} | ||
|
||
list, ok := err.(scanner.ErrorList) | ||
if !ok { | ||
t.Errorf("Error is not of type scanner.ErrorList") | ||
} | ||
|
||
if len(list) != len(test.errorPositions) { | ||
t.Errorf("Expected %d errors but got %d", len(test.errorPositions), len(list)) | ||
} | ||
|
||
for i, err := range list { | ||
if err.Pos != test.errorPositions[i] { | ||
t.Errorf("Got error %s at pos %v, expected this error at pos %v", err.Msg, err.Pos, test.errorPositions[i]) | ||
} | ||
} | ||
|
||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package generatecmd | ||
|
||
func invalid(a: string) string { | ||
return "foo" | ||
} | ||
|
||
templ multipleError(a: string) { | ||
<div/> | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package generatecmd | ||
|
||
templ singleError(a: string) { | ||
<div/> | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
.error
suffix was necessary to prevent the test files from breaking the build because they would get pulled into anytempl generate
running from the root.