Repository: rokucommunity/brighterscript (ts)

Issue:
template strings are not a single expression
When the template strings are converted to pure brightscript they became a complex expression.
this means that you cannot directly call "methods" attached to strings (like `encodeUri()`) because it will be applied only to the last segment.

minimal example:
```brighterscript
sub s()
    example = `${1}${2}`.encodeUri()
    example = `${1}-`.encodeUri()
end sub
```

translate to:
```brightscript
sub s()
    example = bslib_toString(1) + bslib_toString(2).encodeUri()
    example = bslib_toString(1) + "-".encodeUri()
end sub
```

As a workaround, for now, I can just "wrap" the template string with brackets (as in
```
example = (`...`).encodeUri()
```
) , but it should be `bsc` job, IMHO

Pull request:
Wrap transpiled template strings in parens
Wraps transpiled template strings in parentheses so they can be interacted with as a single expression. 

Fixes #787 

Patch:
diff --git a/src/parser/Expression.ts b/src/parser/Expression.ts
index bf35d51c..6f049db4 100644
--- a/src/parser/Expression.ts
+++ b/src/parser/Expression.ts
@@ -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) {
@@ -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;
     }


Test patch:
diff --git a/src/parser/tests/expression/TemplateStringExpression.spec.ts b/src/parser/tests/expression/TemplateStringExpression.spec.ts
index 12a08200..1b1a6884 100644
--- a/src/parser/tests/expression/TemplateStringExpression.spec.ts
+++ b/src/parser/tests/expression/TemplateStringExpression.spec.ts
@@ -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
             `);
         });
@@ -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
                 `
             );
@@ -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
             `);
         });
@@ -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
             `);
         });
@@ -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
             `);
@@ -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
             `);
         });
@@ -269,7 +269,7 @@ describe('TemplateStringExpression', () => {
                 end sub
             `, `
                 sub main()
-                    text = "Hello " + "world"
+                    text = ("Hello " + "world")
                 end sub
             `);
         });