Skip to content

Commit 9960a47

Browse files
Fix bug with ternary transpile for indexed set (#1357)
* Fix bug with ternary transpile for indexed set * Update src/parser/tests/expression/TernaryExpression.spec.ts Co-authored-by: Luis Jacobetty Soares <57358121+luis-soares-sky@users.noreply.github.com> --------- Co-authored-by: Luis Jacobetty Soares <57358121+luis-soares-sky@users.noreply.github.com>
1 parent f022574 commit 9960a47

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/bscPlugin/transpile/BrsFilePreTranspileProcessor.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@ export class BrsFilePreTranspileProcessor {
131131
}),
132132
endIf: createToken(TokenKind.EndIf, 'end if', ternaryExpression.questionMarkToken.range)
133133
});
134-
} else if (isIndexedSetStatement(parent)) {
134+
//if this is an indexedSetStatement, and the ternary expression is NOT an index
135+
} else if (isIndexedSetStatement(parent) && parent.index !== ternaryExpression && !parent.additionalIndexes?.includes(ternaryExpression)) {
135136
ifStatement = createIfStatement({
136137
if: createToken(TokenKind.If, 'if', ternaryExpression.questionMarkToken.range),
137138
condition: ternaryExpression.test,

src/parser/tests/expression/TernaryExpression.spec.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -827,6 +827,29 @@ describe('ternary expressions', () => {
827827
);
828828
});
829829

830+
it('supports ternary in indexedSet key', () => {
831+
testTranspile(
832+
`
833+
sub main()
834+
m[m.isShiftPressed ? "a" : "b"] = 0
835+
m[m.useAltKey ? m.altKey : m.key] = 1
836+
end sub
837+
`,
838+
`
839+
sub main()
840+
m[bslib_ternary(m.isShiftPressed, "a", "b")] = 0
841+
m[(function(__bsCondition, m)
842+
if __bsCondition then
843+
return m.altKey
844+
else
845+
return m.key
846+
end if
847+
end function)(m.useAltKey, m)] = 1
848+
end sub
849+
`
850+
);
851+
});
852+
830853
it('supports scope-captured outer, and simple inner', () => {
831854
testTranspile(
832855
`

0 commit comments

Comments
 (0)