Skip to content

Commit

Permalink
fix: boolean expression format, fixes #571
Browse files Browse the repository at this point in the history
  • Loading branch information
a-h committed Feb 29, 2024
1 parent 6c2f65f commit 339ce2c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.2.593
0.2.594
19 changes: 16 additions & 3 deletions parser/v2/goexpression/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"go/ast"
"go/parser"
"go/scanner"
"go/token"
"strings"
"unicode"
Expand Down Expand Up @@ -100,16 +101,28 @@ func Switch(content string) (start, end int, err error) {
})
}

func isExpectedEOF(err error) bool {
errList, ok := err.(scanner.ErrorList)
if !ok {
return false
}
if len(errList) == 0 {
return false
}
return errList[0].Msg == `expected 'EOF', found '}'`
}

func Expression(content string) (start, end int, err error) {
expr, err := parser.ParseExpr(content)
if expr == nil {
return 0, 0, ErrExpectedNodeNotFound
}
switch expr := expr.(type) {
case *ast.BinaryExpr:
// An expression might be followed by a `</div>`.
// Go interprets `<` as an (invalid) binary expression, so read up to the first binary operator.
if err != nil {
end = int(expr.End()) - 1
if err != nil && !isExpectedEOF(err) {
// An expression might be followed by a `</div>`.
// Go interprets `<` as an (invalid) binary expression, so read up to the first binary operator.
end = int(expr.OpPos) - 1
}
case *ast.BadExpr:
Expand Down
8 changes: 8 additions & 0 deletions parser/v2/goexpression/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,14 @@ var expressionTests = []testInput{
name: "bare variable",
input: `component`,
},
{
name: "boolean expression",
input: `direction == "newest"`,
},
{
name: "string concat",
input: `direction + "newest"`,
},
}

func TestExpression(t *testing.T) {
Expand Down

0 comments on commit 339ce2c

Please sign in to comment.