Skip to content

Commit

Permalink
Merge pull request #16 from buildkite/ps-215-treat-bare-dollar-sign-a…
Browse files Browse the repository at this point in the history
…s-literal

Treat bare dollar sign as literal and dont interpolate
  • Loading branch information
jordandcarter authored Dec 16, 2024
2 parents d30645a + d8a9ea6 commit 74a2a5f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 25 deletions.
17 changes: 1 addition & 16 deletions interpolate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,22 +114,6 @@ func TestIgnoresParentheses(t *testing.T) {
}
}

func TestVariablesMustStartWithLetters(t *testing.T) {
t.Parallel()

for _, str := range []string{
`$1 burgers`,
`$99bottles`,
} {
_, err := interpolate.Interpolate(nil, str)
if err == nil {
t.Fatalf("Test %q should have resulted in an error", str)
} else {
t.Log(err)
}
}
}

func TestMissingParameterValuesReturnEmptyStrings(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -342,6 +326,7 @@ func TestExtractingIdentifiers(t *testing.T) {
{`${LLAMAS:-${ROCK:-true}}`, []string{`LLAMAS`, `ROCK`}},
{`${BUILDKITE_COMMIT:0}`, []string{`BUILDKITE_COMMIT`}},
{`$BUILDKITE_COMMIT hello there $$DOUBLE_DOLLAR \$ESCAPED_DOLLAR`, []string{`BUILDKITE_COMMIT`, `$DOUBLE_DOLLAR`, `$ESCAPED_DOLLAR`}},
{`This $ is not a variable`, []string{}},
} {
id, err := interpolate.Identifiers(tc.Str)
if err != nil {
Expand Down
33 changes: 24 additions & 9 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,12 @@ func (p *Parser) parseExpression(stop ...rune) (Expression, error) {

// If we run into a dollar sign and it's not the last char, it's an expansion
if c == '$' && p.pos < (len(p.input)-1) {
expansion, err := p.parseExpansion()
expressionItem, err := p.parseExpansion()
if err != nil {
return nil, err
}
expr = append(expr, ExpressionItem{Expansion: expansion})

expr = append(expr, expressionItem)
continue
}

Expand Down Expand Up @@ -158,24 +159,38 @@ func (p *Parser) parseEscapedExpansion() (EscapedExpansion, error) {
}
}

func (p *Parser) parseExpansion() (Expansion, error) {
func (p *Parser) parseExpansion() (ExpressionItem, error) {
var empty ExpressionItem

if c := p.nextRune(); c != '$' {
return nil, fmt.Errorf("Expected expansion to start with $, got %c", c)
return empty, fmt.Errorf("Expected expansion to start with $, got %c", c)
}

c := p.peekRune()

// if we have an open brace, this is a brace expansion
if c := p.peekRune(); c == '{' {
return p.parseBraceExpansion()
if c == '{' {
expansion, err := p.parseBraceExpansion()
if err != nil {
return empty, err
}
return ExpressionItem{Expansion: expansion}, nil
}

// if not a letter, it's a literal dollar sign
if !unicode.IsLetter(c) {
return ExpressionItem{Text: "$"}, nil
}

// otherwise, it's a variable expansion
identifier, err := p.scanIdentifier()
if err != nil {
return nil, err
return empty, err
}

return VariableExpansion{
return ExpressionItem{Expansion: VariableExpansion{
Identifier: identifier,
}, nil
}}, nil
}

func (p *Parser) parseBraceExpansion() (Expansion, error) {
Expand Down

0 comments on commit 74a2a5f

Please sign in to comment.