Skip to content

Commit cb70af6

Browse files
committed
Fix imported functions evaluated wrongly if not used as an expression (#68)
1 parent c6ef7a7 commit cb70af6

4 files changed

Lines changed: 35 additions & 0 deletions

File tree

parser/parser.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,14 @@ func (p *Parser) eat() lexer.Token {
561561
return token
562562
}
563563

564+
func (p *Parser) vomit(amount uint) lexer.Token {
565+
for amount > 0 && p.index > 0 {
566+
p.index--
567+
amount--
568+
}
569+
return p.peek()
570+
}
571+
564572
func (p *Parser) isShortVarInit() bool {
565573
_, err := p.findAllowed(lexer.SHORT_INIT_OPERATOR, lexer.IDENTIFIER, lexer.COMMA)
566574

@@ -3144,6 +3152,14 @@ func (p *Parser) evaluateStatement(ctx context) (Statement, error) {
31443152
}
31453153

31463154
if err == nil && stmt == nil {
3155+
// If importAlias has already been evaluated, but no case was hit, reset
3156+
// the token pointer by 2 to set it back to the import-alias token. This
3157+
// is kinda ugly but the importAlias does not belong into the context
3158+
// and this way it's not necessary to propagate it from function to
3159+
// function.
3160+
if len(importAlias) > 0 {
3161+
p.vomit(2)
3162+
}
31473163
stmt, err = p.evaluateExpression(ctx)
31483164
}
31493165
}

tests/import.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,17 @@ func testImportsFromExternalSourceSuccess(t *testing.T, transpilerFunc transpile
8585
})
8686
}
8787

88+
func testCallImportedFunctionAsStatementSuccess(t *testing.T, transpilerFunc transpilerCalloutFunc) {
89+
transpilerFunc(t, func(dir string) (string, error) {
90+
return `
91+
import "strings"
92+
strings.Contains("Hello World", "Wor")
93+
`, nil
94+
}, func(output string, err error) {
95+
require.Nil(t, err)
96+
})
97+
}
98+
8899
func testImportVariableSuccess(t *testing.T, transpilerFunc transpilerCalloutFunc) {
89100
value := "Hello World"
90101

tests/import_linux_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ func TestImportsFromExternalSourceSuccess(t *testing.T) {
2424
testImportsFromExternalSourceSuccess(t, transpileBashFunc)
2525
}
2626

27+
func TestCallImportedFunctionAsStatementSuccess(t *testing.T) {
28+
testCallImportedFunctionAsStatementSuccess(t, transpileBashFunc)
29+
}
30+
2731
func TestImportVariableSuccess(t *testing.T) {
2832
testImportVariableSuccess(t, transpileBashFunc)
2933
}

tests/import_windows_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ func TestImportsFromExternalSourceSuccess(t *testing.T) {
2424
testImportsFromExternalSourceSuccess(t, transpileBatchFunc)
2525
}
2626

27+
func TestCallImportedFunctionAsStatementSuccess(t *testing.T) {
28+
testCallImportedFunctionAsStatementSuccess(t, transpileBatchFunc)
29+
}
30+
2731
func TestImportVariableSuccess(t *testing.T) {
2832
testImportVariableSuccess(t, transpileBatchFunc)
2933
}

0 commit comments

Comments
 (0)