Repository: rokucommunity/brighterscript (ts)

Issue:
Ternaries don't work properly when used within an IndexedGetExpression's index
Using brighterscript@0.68.0, this code:

```
sub main(args as dynamic)
    someObj = {}
    overrides[someObj["abc"] = true ? someObj["abc"] : "abc"] = "ha"
end sub
```

Results in a compile error:

```
[07:20:43:013 PM] Transpiling
[07:20:43:023 PM] Error when calling plugin BscPlugin.beforeFileTranspile: RangeError: Maximum call stack size exceeded
    at walk (/Users/lse16/dev/brighterscript-test-bench/node_modules/brighterscript/src/astUtils/visitors.ts:30:21)
    at IndexedSetStatement.walk (/Users/lse16/dev/brighterscript-test-bench/node_modules/brighterscript/src/parser/Statement.ts:1349:17)
    at walk (/Users/lse16/dev/brighterscript-test-bench/node_modules/brighterscript/src/astUtils/visitors.ts:84:13)
    at walkArray (/Users/lse16/dev/brighterscript-test-bench/node_modules/brighterscript/src/astUtils/visitors.ts:110:29)
    at Block.walk (/Users/lse16/dev/brighterscript-test-bench/node_modules/brighterscript/src/parser/Statement.ts:233:22)
    at walk (/Users/lse16/dev/brighterscript-test-bench/node_modules/brighterscript/src/astUtils/visitors.ts:84:13)
    at IfStatement.walk (/Users/lse16/dev/brighterscript-test-bench/node_modules/brighterscript/src/parser/Statement.ts:578:17)
    at BrsFilePreTranspileProcessor.processTernaryExpression (/Users/lse16/dev/brighterscript-test-bench/node_modules/brighterscript/src/bscPlugin/transpile/BrsFilePreTranspileProcessor.ts:176:25)
    at Object.TernaryExpression (/Users/lse16/dev/brighterscript-test-bench/node_modules/brighterscript/src/bscPlugin/transpile/BrsFilePreTranspileProcessor.ts:43:22)
    at /Users/lse16/dev/brighterscript-test-bench/node_modules/brighterscript/src/astUtils/visitors.ts:215:51
    at walk (/Users/lse16/dev/brighterscript-test-bench/node_modules/brighterscript/src/astUtils/visitors.ts:51:23)
    at IndexedSetStatement.walk (/Users/lse16/dev/brighterscript-test-bench/node_modules/brighterscript/src/parser/Statement.ts:1350:17)
    at walk (/Users/lse16/dev/brighterscript-test-bench/node_modules/brighterscript/src/astUtils/visitors.ts:84:13)
    at walkArray (/Users/lse16/dev/brighterscript-test-bench/node_modules/brighterscript/src/astUtils/visitors.ts:110:29)
    at Block.walk (/Users/lse16/dev/brighterscript-test-bench/node_modules/brighterscript/src/parser/Statement.ts:233:22)
    at walk (/Users/lse16/dev/brighterscript-test-bench/node_modules/brighterscript/src/astUtils/visitors.ts:84:13)
[07:20:43:077 PM] Transpiling finished. (63.561ms)
```

And a broken file:

![Image](https://github.com/user-attachments/assets/acd65867-342f-41b1-ab3d-778e8308a6aa)
![Image](https://github.com/user-attachments/assets/d72552f2-4243-441f-a3f0-71356f9bb8a3)


Pull request description:
Fix bug with ternary transpile for indexed set
Fixes #1356 . 

The issue was that we were only checking if the parent of the ternaryExpression was an IndexedSet, but we currently only support lifting the VALUE of those expressions, not their indexes. So this now guards against matching on the indexes.

Patch:
diff --git a/src/bscPlugin/transpile/BrsFilePreTranspileProcessor.ts b/src/bscPlugin/transpile/BrsFilePreTranspileProcessor.ts
index 287f2d9c..d61657ad 100644
--- a/src/bscPlugin/transpile/BrsFilePreTranspileProcessor.ts
+++ b/src/bscPlugin/transpile/BrsFilePreTranspileProcessor.ts
@@ -131,7 +131,8 @@ export class BrsFilePreTranspileProcessor {
                 }),
                 endIf: createToken(TokenKind.EndIf, 'end if', ternaryExpression.questionMarkToken.range)
             });
-        } else if (isIndexedSetStatement(parent)) {
+            //if this is an indexedSetStatement, and the ternary expression is NOT an index
+        } else if (isIndexedSetStatement(parent) && parent.index !== ternaryExpression && !parent.additionalIndexes?.includes(ternaryExpression)) {
             ifStatement = createIfStatement({
                 if: createToken(TokenKind.If, 'if', ternaryExpression.questionMarkToken.range),
                 condition: ternaryExpression.test,


Test patch:
diff --git a/src/parser/tests/expression/TernaryExpression.spec.ts b/src/parser/tests/expression/TernaryExpression.spec.ts
index d1f5f0f3..3903d25c 100644
--- a/src/parser/tests/expression/TernaryExpression.spec.ts
+++ b/src/parser/tests/expression/TernaryExpression.spec.ts
@@ -827,6 +827,29 @@ describe('ternary expressions', () => {
             );
         });
 
+        it('supports ternary in indexedSet key', () => {
+            testTranspile(
+                `
+                    sub main()
+                        m[m.isShiftPressed ? "a" : "b"] = 0
+                        m[m.useAltKey ? m.altKey : m.key] = 1
+                    end sub
+                `,
+                `
+                    sub main()
+                        m[bslib_ternary(m.isShiftPressed, "a", "b")] = 0
+                        m[(function(__bsCondition, m)
+                                if __bsCondition then
+                                    return m.altKey
+                                else
+                                    return m.key
+                                end if
+                            end function)(m.useAltKey, m)] = 1
+                    end sub
+                `
+            );
+        });
+
         it('supports scope-captured outer, and simple inner', () => {
             testTranspile(
                 `