Skip to content

Commit

Permalink
Merge pull request #13 from buildkite/fix-literal-dollar-sign-escapes…
Browse files Browse the repository at this point in the history
…-again

Empty escaped interpolations should just be text, not interpolations
  • Loading branch information
DrJosh9000 authored Jun 26, 2024
2 parents 1466cba + 76d9f09 commit f208552
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
16 changes: 8 additions & 8 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}
}

Expand Down
10 changes: 7 additions & 3 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: "/"},
},
},
}
Expand Down

0 comments on commit f208552

Please sign in to comment.