Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Treat bare dollar sign as literal and dont interpolate #16

Merged
merged 4 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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