Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Wrap transpiled template strings in parens #788

Merged
merged 1 commit into from
Mar 31, 2023
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
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