// generateShortCircuitedBinaryOperator generates the expression source for a short circuiting binary operator. func (eg *expressionGenerator) generateShortCircuitedBinaryOperator(binaryOp *codedom.BinaryOperationNode, context generationContext) esbuilder.ExpressionBuilder { compareValue := codedom.LiteralValue("false", binaryOp.BasisNode()) if binaryOp.Operator == "&&" { compareValue = codedom.LiteralValue("true", binaryOp.BasisNode()) } return eg.generateShortCircuiter( binaryOp.LeftExpr, compareValue, binaryOp.RightExpr, context, func(resultName string, rightSide esbuilder.ExpressionBuilder) esbuilder.ExpressionBuilder { return esbuilder.Binary(esbuilder.Identifier(resultName), binaryOp.Operator, rightSide) }) }
// generateAwaitPromise generates the expression source for waiting for a promise. func (eg *expressionGenerator) generateAwaitPromise(awaitPromise *codedom.AwaitPromiseNode, context generationContext) esbuilder.ExpressionBuilder { resultName := eg.generateUniqueName("$result") childExpr := eg.generateExpression(awaitPromise.ChildExpression, context) // Add an asynchronous wrapper for the await that executes the child expression as a promise and then // waits for it to return (via a call to then), at which point the wrapped expression is executed. if context.shortCircuiter != nil { eg.addAsyncWrapper(esbuilder.Binary(context.shortCircuiter, "||", childExpr), resultName) } else { eg.addAsyncWrapper(childExpr, resultName) } // An await expression is a reference to the "result" after waiting on the child expression // and then executing the parent expression inside the await callback. return esbuilder.Identifier(resultName) }
// generateNormalBinaryOperator generates the expression source for a non-short circuiting binary operator. func (eg *expressionGenerator) generateNormalBinaryOperator(binaryOp *codedom.BinaryOperationNode, context generationContext) esbuilder.ExpressionBuilder { binaryLeftExpr := eg.generateExpression(binaryOp.LeftExpr, context) binaryRightExpr := eg.generateExpression(binaryOp.RightExpr, context) return esbuilder.Binary(binaryLeftExpr, binaryOp.Operator, binaryRightExpr) }