Skip to content

Commit

Permalink
fix: ensure that user-facing line numbers are 1-based, not 0-based, c…
Browse files Browse the repository at this point in the history
…loses #1051
  • Loading branch information
a-h committed Jan 22, 2025
1 parent 725a88f commit 45f4c46
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 5 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ toolchain go1.23.3
require (
github.com/PuerkitoBio/goquery v1.10.1
github.com/a-h/htmlformat v0.0.0-20231108124658-5bd994fe268e
github.com/a-h/parse v0.0.0-20240121214402-3caf7543159a
github.com/a-h/parse v0.0.0-20250122154542-74294addb73e
github.com/a-h/protocol v0.0.0-20240704131721-1e461c188041
github.com/andybalholm/brotli v1.1.0
github.com/cenkalti/backoff/v4 v4.3.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ github.com/a-h/htmlformat v0.0.0-20231108124658-5bd994fe268e h1:Eog54DQpku7NpPNf
github.com/a-h/htmlformat v0.0.0-20231108124658-5bd994fe268e/go.mod h1:FMIm5afKmEfarNbIXOaPHFY8X7fo+fRQB6I9MPG2nB0=
github.com/a-h/parse v0.0.0-20240121214402-3caf7543159a h1:vlmAfVwFK9sRpDlJyuHY8htP+KfGHB2VH02u0SoIufk=
github.com/a-h/parse v0.0.0-20240121214402-3caf7543159a/go.mod h1:3mnrkvGpurZ4ZrTDbYU84xhwXW2TjTKShSwjRi2ihfQ=
github.com/a-h/parse v0.0.0-20250122154542-74294addb73e h1:HjVbSQHy+dnlS6C3XajZ69NYAb5jbGNfHanvm1+iYlo=
github.com/a-h/parse v0.0.0-20250122154542-74294addb73e/go.mod h1:3mnrkvGpurZ4ZrTDbYU84xhwXW2TjTKShSwjRi2ihfQ=
github.com/a-h/protocol v0.0.0-20240704131721-1e461c188041 h1:2enlC41iOwWklx9ZUqpQygsNAG6KIm3uMMUXzBJw5jA=
github.com/a-h/protocol v0.0.0-20240704131721-1e461c188041/go.mod h1:Gm0KywveHnkiIhqFSMZglXwWZRQICg3KDWLYdglv/d8=
github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1U3M=
Expand Down
12 changes: 11 additions & 1 deletion parser/v2/ifexpressionparser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -622,9 +622,19 @@ func TestIncompleteIf(t *testing.T) {
t.Run("no opening brace", func(t *testing.T) {
input := parse.NewInput(`if a tree falls in the woods`)
_, _, err := ifExpression.Parse(input)
if err.Error() != "if: unterminated (missing closing '{\\n') - https://templ.guide/syntax-and-usage/statements#incomplete-statements: line 0, col 0" {
if err == nil {
t.Fatal("expected an error, got nil")
}
pe, isParseError := err.(parse.ParseError)
if !isParseError {
t.Fatalf("expected a parse error, got %T", err)
}
if pe.Msg != "if: unterminated (missing closing '{\\n') - https://templ.guide/syntax-and-usage/statements#incomplete-statements" {
t.Fatalf("unexpected error: %v", err)
}
if pe.Pos.Line != 0 {
t.Fatalf("unexpected line: %d", pe.Pos.Line)
}
})
t.Run("capitalised If", func(t *testing.T) {
input := parse.NewInput(`If a tree falls in the woods`)
Expand Down
14 changes: 12 additions & 2 deletions parser/v2/switchexpressionparser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,18 @@ func TestIncompleteSwitch(t *testing.T) {
t.Run("no opening brace", func(t *testing.T) {
input := parse.NewInput(`switch with no brace`)
_, _, err := switchExpression.Parse(input)
if err.Error() != "switch: unterminated (missing closing '{\\n') - https://templ.guide/syntax-and-usage/statements#incomplete-statements: line 0, col 0" {
t.Fatalf("unexpected error: %v", err)
if err == nil {
t.Fatal("expected an error, got nil")
}
pe, isParseError := err.(parse.ParseError)
if !isParseError {
t.Fatalf("expected a parse error, got %T", err)
}
if pe.Msg != "switch: unterminated (missing closing '{\\n') - https://templ.guide/syntax-and-usage/statements#incomplete-statements" {
t.Errorf("unexpected error: %v", err)
}
if pe.Pos.Line != 0 {
t.Errorf("unexpected line: %d", pe.Pos.Line)
}
})
t.Run("capitalised Switch", func(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion parser/v2/templateparser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -876,7 +876,7 @@ func TestTemplateParserErrors(t *testing.T) {
input: `templ Name(p Parameter) {
<span
}`,
expected: "<span>: malformed open element: line 2, col 0",
expected: "<span>: malformed open element: line 3, col 0",
},
}
for _, tt := range tests {
Expand Down

0 comments on commit 45f4c46

Please sign in to comment.