Skip to content

Commit

Permalink
chore: fix errcheck warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
a-h committed Mar 6, 2023
1 parent c1606d6 commit a054d8c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
30 changes: 21 additions & 9 deletions generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -511,15 +511,19 @@ func (g *generator) writeIfExpression(indentLevel int, n parser.IfExpression) (e
return err
}
indentLevel++
g.writeNodes(indentLevel, n, stripLeadingAndTrailingWhitespace(n.Then))
if err = g.writeNodes(indentLevel, n, stripLeadingAndTrailingWhitespace(n.Then)); err != nil {
return err
}
indentLevel--
if len(n.Else) > 0 {
// } else {
if _, err = g.w.WriteIndent(indentLevel, `} else {`+"\n"); err != nil {
return err
}
indentLevel++
g.writeNodes(indentLevel, n, stripLeadingAndTrailingWhitespace(n.Else))
if err = g.writeNodes(indentLevel, n, stripLeadingAndTrailingWhitespace(n.Else)); err != nil {
return err
}
indentLevel--
}
// }
Expand Down Expand Up @@ -557,7 +561,9 @@ func (g *generator) writeSwitchExpression(indentLevel int, n parser.SwitchExpres
}
g.sourceMap.Add(c.Expression, r)
indentLevel++
g.writeNodes(indentLevel, n, stripLeadingAndTrailingWhitespace(c.Children))
if err = g.writeNodes(indentLevel, n, stripLeadingAndTrailingWhitespace(c.Children)); err != nil {
return err
}
indentLevel--
}
}
Expand Down Expand Up @@ -598,7 +604,9 @@ func (g *generator) writeBlockTemplElementExpression(indentLevel int, n parser.T
return err
}
indentLevel++
g.writeNodes(indentLevel, n, stripLeadingAndTrailingWhitespace(n.Children))
if err = g.writeNodes(indentLevel, n, stripLeadingAndTrailingWhitespace(n.Children)); err != nil {
return err
}
// return nil
if _, err = g.w.WriteIndent(indentLevel, "return err\n"); err != nil {
return err
Expand Down Expand Up @@ -687,7 +695,9 @@ func (g *generator) writeForExpression(indentLevel int, n parser.ForExpression)
}
// Children.
indentLevel++
g.writeNodes(indentLevel, n, stripLeadingAndTrailingWhitespace(n.Children))
if err = g.writeNodes(indentLevel, n, stripLeadingAndTrailingWhitespace(n.Children)); err != nil {
return err
}
indentLevel--
// }
if _, err = g.w.WriteIndent(indentLevel, `}`+"\n"); err != nil {
Expand Down Expand Up @@ -798,7 +808,9 @@ func (g *generator) writeStandardElement(indentLevel int, n parser.Element) (err
}
}
// Children.
g.writeNodes(indentLevel, n, stripNonCriticalElementWhitespace(n.Children))
if err = g.writeNodes(indentLevel, n, stripNonCriticalElementWhitespace(n.Children)); err != nil {
return err
}
// </div>
if _, err = g.w.WriteIndent(indentLevel, fmt.Sprintf(`_, err = templBuffer.WriteString("</%s>")`+"\n", html.EscapeString(n.Name))); err != nil {
return err
Expand Down Expand Up @@ -1056,9 +1068,9 @@ func (g *generator) writeRawElement(indentLevel int, n parser.RawElement) (err e
}
}
// Contents.
g.writeText(0, parser.Text{
Value: n.Contents,
})
if err = g.writeText(0, parser.Text{Value: n.Contents}); err != nil {
return err
}
// </div>
if _, err = g.w.WriteIndent(indentLevel, fmt.Sprintf(`_, err = templBuffer.WriteString("</%s>")`+"\n", html.EscapeString(n.Name))); err != nil {
return err
Expand Down
16 changes: 12 additions & 4 deletions generator/rangewriter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,33 @@ func TestRangeWriter(t *testing.T) {
}
})
t.Run("writing characters increases the col position", func(t *testing.T) {
rw.Write("abc")
if _, err := rw.Write("abc"); err != nil {
t.Fatalf("failed to write: %v", err)
}
if diff := cmp.Diff(parser.NewPositionFromValues(3, 0, 3), rw.Current); diff != "" {
t.Error(diff)
}
})
t.Run("newline characters implement carriage return", func(t *testing.T) {
rw.Write("\n1")
if _, err := rw.Write("\n1"); err != nil {
t.Fatalf("failed to write: %v", err)
}
if diff := cmp.Diff(parser.NewPositionFromValues(5, 1, 1), rw.Current); diff != "" {
t.Error(diff)
}
})
t.Run("multi-byte characters count as a single column position", func(t *testing.T) {
rw.Write("\n你")
if _, err := rw.Write("\n你"); err != nil {
t.Fatalf("failed to write: %v", err)
}
if diff := cmp.Diff(parser.NewPositionFromValues(9, 2, 1), rw.Current); diff != "" {
t.Error(diff)
}
})
t.Run("a range is returned from each write", func(t *testing.T) {
rw.Write("\n")
if _, err := rw.Write("\n"); err != nil {
t.Fatalf("failed to write: %v", err)
}
r, err := rw.Write("test")
if err != nil {
t.Fatalf("expected successful write, got error: %v", err)
Expand Down

0 comments on commit a054d8c

Please sign in to comment.