Skip to content

Commit

Permalink
Wrap transpiled template strings in parens (#788)
Browse files Browse the repository at this point in the history
  • Loading branch information
TwitchBronBron committed Mar 31, 2023
1 parent f2885dc commit 3ab13f2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
4 changes: 3 additions & 1 deletion src/parser/Expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1133,7 +1133,7 @@ export class TemplateStringExpression extends Expression {
if (this.quasis.length === 1 && this.expressions.length === 0) {
return this.quasis[0].transpile(state);
}
let result = [];
let result = ['('];
let plus = '';
//helper function to figure out when to include the plus
function add(...items) {
Expand Down Expand Up @@ -1176,6 +1176,8 @@ export class TemplateStringExpression extends Expression {
}
}
}
//the expression should be wrapped in parens so it can be used line a single expression at runtime
result.push(')');

return result;
}
Expand Down
24 changes: 12 additions & 12 deletions src/parser/tests/expression/TemplateStringExpression.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ describe('TemplateStringExpression', () => {
end sub
`, `
sub main()
a = rokucommunity_bslib_toString(LINE_NUM) + "," + rokucommunity_bslib_toString(LINE_NUM)
a = (rokucommunity_bslib_toString(LINE_NUM) + "," + rokucommunity_bslib_toString(LINE_NUM))
end sub
`);
});
Expand All @@ -94,7 +94,7 @@ describe('TemplateStringExpression', () => {
end sub
`, `
sub main()
a = bslib_toString(LINE_NUM) + "," + bslib_toString(LINE_NUM)
a = (bslib_toString(LINE_NUM) + "," + bslib_toString(LINE_NUM))
end sub
`
);
Expand All @@ -119,7 +119,7 @@ describe('TemplateStringExpression', () => {
end sub
`, `
sub main()
a = "hello " + bslib_toString(LINE_NUM.text) + " world " + bslib_toString("template" + "".getChars()) + " test"
a = ("hello " + bslib_toString(LINE_NUM.text) + " world " + bslib_toString("template" + "".getChars()) + " test")
end sub
`);
});
Expand Down Expand Up @@ -191,7 +191,7 @@ describe('TemplateStringExpression', () => {
end sub
`, `
sub main()
a = "I am multiline" + chr(10) + bslib_toString(a.isRunning()) + chr(10) + "more"
a = ("I am multiline" + chr(10) + bslib_toString(a.isRunning()) + chr(10) + "more")
end sub
`);
});
Expand All @@ -210,11 +210,11 @@ describe('TemplateStringExpression', () => {
a = [
"one"
"two"
"I am a complex example" + bslib_toString(a.isRunning([
("I am a complex example" + bslib_toString(a.isRunning([
"a"
"b"
"c"
]))
])))
]
end sub
`);
Expand All @@ -239,25 +239,25 @@ describe('TemplateStringExpression', () => {
a = [
"one"
"two"
"I am a complex example " + bslib_toString(a.isRunning([
("I am a complex example " + bslib_toString(a.isRunning([
"a"
"b"
"c"
"d_open " + bslib_toString("inside" + m.items[1]) + " d_close"
]))
("d_open " + bslib_toString("inside" + m.items[1]) + " d_close")
])))
]
end sub
`);
});

it('properly transpiles two template strings side-by-side', () => {
it('properly transpiles two expressions side-by-side', () => {
testTranspile(`
sub main()
a = \`\${"hello"}\${"world"}\`
end sub
`, `
sub main()
a = "hello" + "world"
a = ("hello" + "world")
end sub
`);
});
Expand All @@ -269,7 +269,7 @@ describe('TemplateStringExpression', () => {
end sub
`, `
sub main()
text = "Hello " + "world"
text = ("Hello " + "world")
end sub
`);
});
Expand Down

0 comments on commit 3ab13f2

Please sign in to comment.