From 76d9f0984ac670fe77605ed119f47314a34a2d0d Mon Sep 17 00:00:00 2001 From: Ben Moskovitz Date: Fri, 21 Jun 2024 11:13:31 +1000 Subject: [PATCH] Empty escaped interpolations should just be text, not interpolations --- parser.go | 16 ++++++++-------- parser_test.go | 10 +++++++--- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/parser.go b/parser.go index c69b8bf..e02cc7d 100644 --- a/parser.go +++ b/parser.go @@ -87,7 +87,7 @@ func (p *Parser) parseExpression(stop ...rune) (Expression, error) { return nil, err } - expr = append(expr, ExpressionItem{Expansion: ee}) + expr = append(expr, ee) continue } @@ -122,28 +122,28 @@ func (p *Parser) parseExpression(stop ...rune) (Expression, error) { return expr, nil } -func (p *Parser) parseEscapedExpansion() (Expansion, error) { +func (p *Parser) parseEscapedExpansion() (ExpressionItem, error) { next := p.peekRune() switch { case next == '{': // if it's an escaped brace expansion, (eg $${MY_COOL_VAR:-5}) consume text until the close brace id := p.scanUntil(func(r rune) bool { return r == '}' }) id = id + string(p.nextRune()) // we know that the next rune is a close brace, chuck it on the end - return EscapedExpansion{Identifier: id}, nil + return ExpressionItem{Expansion: EscapedExpansion{Identifier: id}}, nil case unicode.IsLetter(next): // it's an escaped identifier (eg $$MY_COOL_VAR) id, err := p.scanIdentifier() if err != nil { - return nil, err + return ExpressionItem{}, err } - return EscapedExpansion{Identifier: id}, nil + return ExpressionItem{Expansion: EscapedExpansion{Identifier: id}}, nil default: - // there's no identifier or brace afterward, so it's probably a literal escaped dollar sign, so return an empty identifier - // that will be expanded to a single dollar sign - return EscapedExpansion{Identifier: ""}, nil + // there's no identifier or brace afterward, so it's probably a literal escaped dollar sign + // just return a text item with the dollar sign + return ExpressionItem{Text: "$"}, nil } } diff --git a/parser_test.go b/parser_test.go index a223f93..466a37a 100644 --- a/parser_test.go +++ b/parser_test.go @@ -174,9 +174,13 @@ func TestParser(t *testing.T) { { String: "this is a regex! /^start.*end$$/", // the dollar sign at the end of the regex has to be escaped to be treated as a literal dollar sign by this library Expected: []interpolate.ExpressionItem{ - {Text: "this is a regex! /^start.*end"}, - {Expansion: interpolate.EscapedExpansion{Identifier: ""}}, - {Text: "/"}, + {Text: "this is a regex! /^start.*end"}, {Text: "$"}, {Text: "/"}, + }, + }, + { + String: `this is a more different regex! /^start.*end\$/`, // the dollar sign at the end of the regex has to be escaped to be treated as a literal dollar sign by this library + Expected: []interpolate.ExpressionItem{ + {Text: "this is a more different regex! /^start.*end"}, {Text: "$"}, {Text: "/"}, }, }, }