X Tutup
Skip to content

Commit 20a5d92

Browse files
TheLartiansPerryvw
authored andcommitted
Add parentheses to ternary conditional (#715)
* add broken ternary test case * add parentheses to ternary condition
1 parent f54cdef commit 20a5d92

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

src/LuaTransformer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3829,7 +3829,7 @@ export class LuaTransformer {
38293829
}
38303830

38313831
protected transformProtectedConditionalExpression(expression: ts.ConditionalExpression): tstl.CallExpression {
3832-
const condition = this.transformExpression(expression.condition);
3832+
const condition = tstl.createParenthesizedExpression(this.transformExpression(expression.condition));
38333833
const val1 = this.transformExpression(expression.whenTrue);
38343834
const val2 = this.transformExpression(expression.whenFalse);
38353835

@@ -3847,7 +3847,7 @@ export class LuaTransformer {
38473847
if (tsHelper.isFalsible(this.checker.getTypeAtLocation(expression.whenTrue), isStrict)) {
38483848
return this.transformProtectedConditionalExpression(expression);
38493849
}
3850-
const condition = this.transformExpression(expression.condition);
3850+
const condition = tstl.createParenthesizedExpression(this.transformExpression(expression.condition));
38513851
const val1 = this.transformExpression(expression.whenTrue);
38523852
const val2 = this.transformExpression(expression.whenFalse);
38533853

test/unit/conditionals.spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,10 @@ test.each([
288288
{ input: "false ? 'a' : 'b'" },
289289
{ input: "true ? false : true" },
290290
{ input: "false ? false : true" },
291+
{ input: "true || true ? 'a' : 'b'" },
292+
{ input: "true || false ? 'a' : 'b'" },
293+
{ input: "false || true ? 'a' : 'b'" },
294+
{ input: "false || false ? 'a' : 'b'" },
291295
{ input: "true ? literalValue : true" },
292296
{ input: "true ? variableValue : true" },
293297
{ input: "true ? maybeUndefinedValue : true" },

0 commit comments

Comments
 (0)
X Tutup