@@ -4,70 +4,31 @@ import { FunctionVisitor, TransformationContext } from "../context";
44import { performHoisting , popScope , pushScope , ScopeType } from "../utils/scope" ;
55import { transformBlockOrStatement } from "./block" ;
66
7- function canBeFalsy ( context : TransformationContext , type : ts . Type ) : boolean {
8- const strictNullChecks = context . options . strict === true || context . options . strictNullChecks === true ;
9-
10- const falsyFlags =
11- ts . TypeFlags . Boolean |
12- ts . TypeFlags . BooleanLiteral |
13- ts . TypeFlags . Undefined |
14- ts . TypeFlags . Null |
15- ts . TypeFlags . Never |
16- ts . TypeFlags . Void |
17- ts . TypeFlags . Any ;
18-
19- if ( type . flags & falsyFlags ) {
20- return true ;
21- } else if ( ! strictNullChecks && ! type . isLiteral ( ) ) {
22- return true ;
23- } else if ( type . isUnion ( ) ) {
24- return type . types . some ( subType => canBeFalsy ( context , subType ) ) ;
25- } else {
26- return false ;
27- }
28- }
29-
30- function wrapInFunctionCall ( expression : lua . Expression ) : lua . FunctionExpression {
31- const returnStatement = lua . createReturnStatement ( [ expression ] ) ;
32-
33- return lua . createFunctionExpression (
34- lua . createBlock ( [ returnStatement ] ) ,
35- undefined ,
36- undefined ,
37- undefined ,
38- lua . FunctionExpressionFlags . Inline
39- ) ;
40- }
41-
42- function transformProtectedConditionalExpression (
43- context : TransformationContext ,
44- expression : ts . ConditionalExpression
45- ) : lua . CallExpression {
46- const condition = lua . createParenthesizedExpression ( context . transformExpression ( expression . condition ) ) ;
47- const val1 = context . transformExpression ( expression . whenTrue ) ;
48- const val2 = context . transformExpression ( expression . whenFalse ) ;
49-
50- const val1Function = wrapInFunctionCall ( val1 ) ;
51- const val2Function = wrapInFunctionCall ( val2 ) ;
52-
53- // (condition and (() => v1) or (() => v2))()
54- const conditionAnd = lua . createBinaryExpression ( condition , val1Function , lua . SyntaxKind . AndOperator ) ;
55- const orExpression = lua . createBinaryExpression ( conditionAnd , val2Function , lua . SyntaxKind . OrOperator ) ;
56- return lua . createCallExpression ( lua . createParenthesizedExpression ( orExpression ) , [ ] , expression ) ;
57- }
58-
597export const transformConditionalExpression : FunctionVisitor < ts . ConditionalExpression > = ( expression , context ) => {
60- if ( canBeFalsy ( context , context . checker . getTypeAtLocation ( expression . whenTrue ) ) ) {
61- return transformProtectedConditionalExpression ( context , expression ) ;
62- }
8+ // local ____conditional;
9+ const tempVariable = context . createUniqueIdentifier ( "ternary_conditional" ) ;
10+ const tempVariableDeclaration = lua . createVariableDeclarationStatement ( tempVariable ) ;
11+
12+ const createTempVariableAssignment = ( value : lua . Expression ) =>
13+ lua . createBlock ( [
14+ ...context . popPrecedingStatements ( ) ,
15+ lua . createAssignmentStatement ( lua . cloneIdentifier ( tempVariable ) , value ) ,
16+ ] ) ;
17+
18+ // if expression.condition
19+ const ifStatement = lua . createIfStatement (
20+ context . transformExpression ( expression . condition ) ,
21+ // then ____conditional = expression.whenTrue
22+ createTempVariableAssignment ( context . transformExpression ( expression . whenTrue ) ) ,
23+ // else ____conditional = expression.whenFalse
24+ createTempVariableAssignment ( context . transformExpression ( expression . whenFalse ) )
25+ ) ;
6326
64- const condition = lua . createParenthesizedExpression ( context . transformExpression ( expression . condition ) ) ;
65- const val1 = context . transformExpression ( expression . whenTrue ) ;
66- const val2 = context . transformExpression ( expression . whenFalse ) ;
27+ // Use temp variable declaration and if statement as preceding statements
28+ context . pushPrecedingStatement ( tempVariableDeclaration , ifStatement ) ;
6729
68- // condition and v1 or v2
69- const conditionAnd = lua . createBinaryExpression ( condition , val1 , lua . SyntaxKind . AndOperator ) ;
70- return lua . createBinaryExpression ( conditionAnd , val2 , lua . SyntaxKind . OrOperator , expression ) ;
30+ // ____conditional
31+ return lua . cloneIdentifier ( tempVariable ) ;
7132} ;
7233
7334export function transformIfStatement ( statement : ts . IfStatement , context : TransformationContext ) : lua . IfStatement {
0 commit comments