JS: minor code style and formatting fixes
This commit is contained in:
@@ -53,7 +53,10 @@ abstract class AbstractCallInfo : CallInfo {
|
|||||||
// if value == null, it is get access
|
// if value == null, it is get access
|
||||||
class VariableAccessInfo(callInfo: CallInfo, val value: JsExpression? = null) : AbstractCallInfo(), CallInfo by callInfo
|
class VariableAccessInfo(callInfo: CallInfo, val value: JsExpression? = null) : AbstractCallInfo(), CallInfo by callInfo
|
||||||
|
|
||||||
class FunctionCallInfo(callInfo: CallInfo, val argumentsInfo: CallArgumentTranslator.ArgumentsInfo) : AbstractCallInfo(), CallInfo by callInfo
|
class FunctionCallInfo(
|
||||||
|
callInfo: CallInfo,
|
||||||
|
val argumentsInfo: CallArgumentTranslator.ArgumentsInfo
|
||||||
|
) : AbstractCallInfo(), CallInfo by callInfo
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -64,12 +67,18 @@ class FunctionCallInfo(callInfo: CallInfo, val argumentsInfo: CallArgumentTransl
|
|||||||
*/
|
*/
|
||||||
class ExplicitReceivers(val extensionOrDispatchReceiver: JsExpression?, val extensionReceiver: JsExpression? = null)
|
class ExplicitReceivers(val extensionOrDispatchReceiver: JsExpression?, val extensionReceiver: JsExpression? = null)
|
||||||
|
|
||||||
fun TranslationContext.getCallInfo(resolvedCall: ResolvedCall<out CallableDescriptor>, extensionOrDispatchReceiver: JsExpression?): CallInfo {
|
fun TranslationContext.getCallInfo(
|
||||||
|
resolvedCall: ResolvedCall<out CallableDescriptor>,
|
||||||
|
extensionOrDispatchReceiver: JsExpression?
|
||||||
|
): CallInfo {
|
||||||
return createCallInfo(resolvedCall, ExplicitReceivers(extensionOrDispatchReceiver))
|
return createCallInfo(resolvedCall, ExplicitReceivers(extensionOrDispatchReceiver))
|
||||||
}
|
}
|
||||||
|
|
||||||
// two receiver need only for FunctionCall in VariableAsFunctionResolvedCall
|
// two receiver need only for FunctionCall in VariableAsFunctionResolvedCall
|
||||||
fun TranslationContext.getCallInfo(resolvedCall: ResolvedCall<out FunctionDescriptor>, explicitReceivers: ExplicitReceivers): FunctionCallInfo {
|
fun TranslationContext.getCallInfo(
|
||||||
|
resolvedCall: ResolvedCall<out FunctionDescriptor>,
|
||||||
|
explicitReceivers: ExplicitReceivers
|
||||||
|
): FunctionCallInfo {
|
||||||
val argsBlock = JsBlock()
|
val argsBlock = JsBlock()
|
||||||
val argumentsInfo = CallArgumentTranslator.translate(resolvedCall, explicitReceivers.extensionOrDispatchReceiver, this, argsBlock)
|
val argumentsInfo = CallArgumentTranslator.translate(resolvedCall, explicitReceivers.extensionOrDispatchReceiver, this, argsBlock)
|
||||||
val explicitReceiversCorrected =
|
val explicitReceiversCorrected =
|
||||||
@@ -99,7 +108,10 @@ private fun TranslationContext.getDispatchReceiver(receiverValue: ReceiverValue)
|
|||||||
return getDispatchReceiver(getReceiverParameterForReceiver(receiverValue))
|
return getDispatchReceiver(getReceiverParameterForReceiver(receiverValue))
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun TranslationContext.createCallInfo(resolvedCall: ResolvedCall<out CallableDescriptor>, explicitReceivers: ExplicitReceivers): CallInfo {
|
private fun TranslationContext.createCallInfo(
|
||||||
|
resolvedCall: ResolvedCall<out CallableDescriptor>,
|
||||||
|
explicitReceivers: ExplicitReceivers
|
||||||
|
): CallInfo {
|
||||||
val receiverKind = resolvedCall.explicitReceiverKind
|
val receiverKind = resolvedCall.explicitReceiverKind
|
||||||
|
|
||||||
// I'm not sure if it's a proper code, and why it should work. Just copied similar logic from ExpressionCodegen.generateConstructorCall.
|
// I'm not sure if it's a proper code, and why it should work. Just copied similar logic from ExpressionCodegen.generateConstructorCall.
|
||||||
|
|||||||
+4
-1
@@ -48,7 +48,10 @@ fun CallArgumentTranslator.ArgumentsInfo.argsWithReceiver(receiver: JsExpression
|
|||||||
// call may be native and|or with spreadOperator
|
// call may be native and|or with spreadOperator
|
||||||
object DefaultFunctionCallCase : FunctionCallCase() {
|
object DefaultFunctionCallCase : FunctionCallCase() {
|
||||||
// TODO: refactor after fix ArgumentsInfo - duplicate code
|
// TODO: refactor after fix ArgumentsInfo - duplicate code
|
||||||
private fun nativeSpreadFunWithDispatchOrExtensionReceiver(argumentsInfo: CallArgumentTranslator.ArgumentsInfo, functionName: JsName): JsExpression {
|
private fun nativeSpreadFunWithDispatchOrExtensionReceiver(
|
||||||
|
argumentsInfo: CallArgumentTranslator.ArgumentsInfo,
|
||||||
|
functionName: JsName
|
||||||
|
): JsExpression {
|
||||||
val cachedReceiver = argumentsInfo.cachedReceiver!!
|
val cachedReceiver = argumentsInfo.cachedReceiver!!
|
||||||
val functionCallRef = Namer.getFunctionApplyRef(JsNameRef(functionName, cachedReceiver.assignmentExpression()))
|
val functionCallRef = Namer.getFunctionApplyRef(JsNameRef(functionName, cachedReceiver.assignmentExpression()))
|
||||||
return JsInvocation(functionCallRef, argumentsInfo.translateArguments)
|
return JsInvocation(functionCallRef, argumentsInfo.translateArguments)
|
||||||
|
|||||||
+9
-10
@@ -58,7 +58,8 @@ fun createWhile(doWhile: Boolean, expression: KtWhileExpressionBase, context: Tr
|
|||||||
jsCondition = JsLiteral.TRUE
|
jsCondition = JsLiteral.TRUE
|
||||||
|
|
||||||
if (doWhile) {
|
if (doWhile) {
|
||||||
// translate to: tmpSecondRun = false; do { if(tmpSecondRun) { <expr> if(!tmpExprVar) break; } else tmpSecondRun=true; <body> } while(true)
|
// translate to: tmpSecondRun = false;
|
||||||
|
// do { if(tmpSecondRun) { <expr> if(!tmpExprVar) break; } else tmpSecondRun=true; <body> } while(true)
|
||||||
val secondRun = context.defineTemporary(JsLiteral.FALSE)
|
val secondRun = context.defineTemporary(JsLiteral.FALSE)
|
||||||
conditionBlock.statements.add(breakIfConditionIsFalseStatement)
|
conditionBlock.statements.add(breakIfConditionIsFalseStatement)
|
||||||
val ifStatement = JsIf(secondRun, conditionBlock, assignment(secondRun, JsLiteral.TRUE).makeStmt())
|
val ifStatement = JsIf(secondRun, conditionBlock, assignment(secondRun, JsLiteral.TRUE).makeStmt())
|
||||||
@@ -100,16 +101,14 @@ fun translateForExpression(expression: KtForExpression, context: TranslationCont
|
|||||||
|
|
||||||
val destructuringParameter: KtDestructuringDeclaration? = expression.destructuringParameter
|
val destructuringParameter: KtDestructuringDeclaration? = expression.destructuringParameter
|
||||||
|
|
||||||
fun declareParameter(): Pair<JsName, Boolean> {
|
val loopParameter = getLoopParameter(expression)
|
||||||
val loopParameter = getLoopParameter(expression)
|
val (parameterName, parameterIsTemporary) = if (loopParameter != null) {
|
||||||
if (loopParameter != null) {
|
Pair(context.getNameForElement(loopParameter), false)
|
||||||
return Pair(context.getNameForElement(loopParameter), false)
|
}
|
||||||
}
|
else {
|
||||||
assert(destructuringParameter != null) { "If loopParameter is null, multi parameter must be not null ${expression.text}" }
|
assert(destructuringParameter != null) { "If loopParameter is null, multi parameter must be not null ${expression.text}" }
|
||||||
return Pair(context.scope().declareTemporary(), true)
|
Pair(context.scope().declareTemporary(), true)
|
||||||
}
|
}
|
||||||
|
|
||||||
val (parameterName, parameterIsTemporary) = declareParameter()
|
|
||||||
|
|
||||||
fun translateBody(itemValue: JsExpression?): JsStatement? {
|
fun translateBody(itemValue: JsExpression?): JsStatement? {
|
||||||
val realBody = expression.body?.let { Translation.translateAsStatementAndMergeInBlockIfNeeded(it, context) }
|
val realBody = expression.body?.let { Translation.translateAsStatementAndMergeInBlockIfNeeded(it, context) }
|
||||||
|
|||||||
+10
-8
@@ -53,12 +53,7 @@ public final class WhenTranslator extends AbstractTranslator {
|
|||||||
whenExpression = expression;
|
whenExpression = expression;
|
||||||
|
|
||||||
KtExpression subject = expression.getSubjectExpression();
|
KtExpression subject = expression.getSubjectExpression();
|
||||||
if (subject != null) {
|
expressionToMatch = subject != null ? context.defineTemporary(Translation.translateAsExpression(subject, context)) : null;
|
||||||
expressionToMatch = context.defineTemporary(Translation.translateAsExpression(subject, context));
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
expressionToMatch = null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private JsNode translate() {
|
private JsNode translate() {
|
||||||
@@ -122,7 +117,11 @@ public final class WhenTranslator extends AbstractTranslator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private JsExpression translateOrCondition(@NotNull JsExpression leftExpression, @NotNull KtWhenCondition condition, @NotNull TranslationContext context) {
|
private JsExpression translateOrCondition(
|
||||||
|
@NotNull JsExpression leftExpression,
|
||||||
|
@NotNull KtWhenCondition condition,
|
||||||
|
@NotNull TranslationContext context
|
||||||
|
) {
|
||||||
TranslationContext rightContext = context.innerBlock();
|
TranslationContext rightContext = context.innerBlock();
|
||||||
JsExpression rightExpression = translateCondition(condition, rightContext);
|
JsExpression rightExpression = translateCondition(condition, rightContext);
|
||||||
context.moveVarsFrom(rightContext);
|
context.moveVarsFrom(rightContext);
|
||||||
@@ -148,7 +147,10 @@ public final class WhenTranslator extends AbstractTranslator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private JsExpression translateWhenConditionToBooleanExpression(@NotNull KtWhenCondition condition, @NotNull TranslationContext context) {
|
private JsExpression translateWhenConditionToBooleanExpression(
|
||||||
|
@NotNull KtWhenCondition condition,
|
||||||
|
@NotNull TranslationContext context
|
||||||
|
) {
|
||||||
if (condition instanceof KtWhenConditionIsPattern) {
|
if (condition instanceof KtWhenConditionIsPattern) {
|
||||||
return translateIsCondition((KtWhenConditionIsPattern) condition, context);
|
return translateIsCondition((KtWhenConditionIsPattern) condition, context);
|
||||||
}
|
}
|
||||||
|
|||||||
+4
-7
@@ -204,15 +204,14 @@ class CallArgumentTranslator private constructor(
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
val list: MutableList<JsExpression>
|
val list: MutableList<JsExpression> = if (shouldWrapVarargInArray) {
|
||||||
if (shouldWrapVarargInArray) {
|
if (arguments.size == 1) SmartList<JsExpression>() else ArrayList<JsExpression>(arguments.size)
|
||||||
list = if (arguments.size == 1) SmartList<JsExpression>() else ArrayList<JsExpression>(arguments.size)
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
list = result
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
val resultKind = translateValueArguments(arguments, list, context)
|
translateValueArguments(arguments, list, context)
|
||||||
|
|
||||||
if (shouldWrapVarargInArray) {
|
if (shouldWrapVarargInArray) {
|
||||||
val concatArguments = prepareConcatArguments(arguments, list)
|
val concatArguments = prepareConcatArguments(arguments, list)
|
||||||
@@ -222,8 +221,6 @@ class CallArgumentTranslator private constructor(
|
|||||||
else if (result.size == 1) {
|
else if (result.size == 1) {
|
||||||
result[0] = JsAstUtils.invokeMethod(result[0], "slice")
|
result[0] = JsAstUtils.invokeMethod(result[0], "slice")
|
||||||
}
|
}
|
||||||
|
|
||||||
return resultKind
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun translateValueArguments(arguments: List<ValueArgument>, list: MutableList<JsExpression>,
|
private fun translateValueArguments(arguments: List<ValueArgument>, list: MutableList<JsExpression>,
|
||||||
|
|||||||
+2
-1
@@ -48,7 +48,8 @@ public final class CallExpressionTranslator extends AbstractCallExpressionTransl
|
|||||||
@Nullable JsExpression receiver,
|
@Nullable JsExpression receiver,
|
||||||
@NotNull TranslationContext context
|
@NotNull TranslationContext context
|
||||||
) {
|
) {
|
||||||
ResolvedCall<? extends FunctionDescriptor> resolvedCall = CallUtilKt.getFunctionResolvedCallWithAssert(expression, context.bindingContext());
|
ResolvedCall<? extends FunctionDescriptor> resolvedCall =
|
||||||
|
CallUtilKt.getFunctionResolvedCallWithAssert(expression, context.bindingContext());
|
||||||
|
|
||||||
if (isJsCall(resolvedCall)) {
|
if (isJsCall(resolvedCall)) {
|
||||||
return (new CallExpressionTranslator(expression, receiver, context)).translateJsCode();
|
return (new CallExpressionTranslator(expression, receiver, context)).translateJsCode();
|
||||||
|
|||||||
+14
-8
@@ -39,7 +39,7 @@ object CallableReferenceTranslator {
|
|||||||
is FunctionDescriptor ->
|
is FunctionDescriptor ->
|
||||||
translateForFunction(descriptor, context, expression)
|
translateForFunction(descriptor, context, expression)
|
||||||
else ->
|
else ->
|
||||||
throw IllegalArgumentException("Expected property or function: ${descriptor}, expression=${expression.text}")
|
throw IllegalArgumentException("Expected property or function: $descriptor, expression=${expression.text}")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -78,13 +78,13 @@ object CallableReferenceTranslator {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun isConstructor(descriptor: CallableDescriptor): Boolean = descriptor is ConstructorDescriptor
|
private fun isConstructor(descriptor: CallableDescriptor) = descriptor is ConstructorDescriptor
|
||||||
|
|
||||||
private fun isExtension(descriptor: CallableDescriptor): Boolean = DescriptorUtils.isExtension(descriptor)
|
private fun isExtension(descriptor: CallableDescriptor) = DescriptorUtils.isExtension(descriptor)
|
||||||
|
|
||||||
private fun isMember(descriptor: CallableDescriptor): Boolean = JsDescriptorUtils.getContainingDeclaration(descriptor) is ClassDescriptor
|
private fun isMember(descriptor: CallableDescriptor) = JsDescriptorUtils.getContainingDeclaration(descriptor) is ClassDescriptor
|
||||||
|
|
||||||
private fun isVar(descriptor: PropertyDescriptor): JsExpression = if (descriptor.isVar) JsLiteral.TRUE else JsLiteral.FALSE
|
private fun isVar(descriptor: PropertyDescriptor) = if (descriptor.isVar) JsLiteral.TRUE else JsLiteral.FALSE
|
||||||
|
|
||||||
private fun translateForTopLevelProperty(descriptor: PropertyDescriptor, context: TranslationContext): JsExpression {
|
private fun translateForTopLevelProperty(descriptor: PropertyDescriptor, context: TranslationContext): JsExpression {
|
||||||
val packageDescriptor = JsDescriptorUtils.getContainingDeclaration(descriptor)
|
val packageDescriptor = JsDescriptorUtils.getContainingDeclaration(descriptor)
|
||||||
@@ -94,7 +94,8 @@ object CallableReferenceTranslator {
|
|||||||
val jsPropertyName = context.getNameForDescriptor(descriptor)
|
val jsPropertyName = context.getNameForDescriptor(descriptor)
|
||||||
val jsPropertyNameAsString = context.program().getStringLiteral(jsPropertyName.toString())
|
val jsPropertyNameAsString = context.program().getStringLiteral(jsPropertyName.toString())
|
||||||
|
|
||||||
return JsInvocation(context.namer().callableRefForTopLevelPropertyReference(), jsPackageNameRef, jsPropertyNameAsString, isVar(descriptor))
|
return JsInvocation(context.namer().callableRefForTopLevelPropertyReference(), jsPackageNameRef, jsPropertyNameAsString,
|
||||||
|
isVar(descriptor))
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun translateForMemberProperty(descriptor: PropertyDescriptor, context: TranslationContext): JsExpression {
|
private fun translateForMemberProperty(descriptor: PropertyDescriptor, context: TranslationContext): JsExpression {
|
||||||
@@ -145,11 +146,16 @@ object CallableReferenceTranslator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun translateForMemberFunction(descriptor: FunctionDescriptor, context: TranslationContext): JsExpression {
|
private fun translateForMemberFunction(descriptor: FunctionDescriptor, context: TranslationContext): JsExpression {
|
||||||
val classDescriptor = JsDescriptorUtils.getContainingDeclaration(descriptor) as? ClassDescriptor ?: throw IllegalArgumentException("Expected ClassDescriptor: ${descriptor}")
|
val classDescriptor = JsDescriptorUtils.getContainingDeclaration(descriptor) as? ClassDescriptor ?:
|
||||||
|
throw IllegalArgumentException("Expected ClassDescriptor: $descriptor")
|
||||||
return translateAsMemberFunctionReference(descriptor, classDescriptor, context)
|
return translateAsMemberFunctionReference(descriptor, classDescriptor, context)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun translateAsMemberFunctionReference(descriptor: CallableDescriptor, classDescriptor: ClassDescriptor, context: TranslationContext): JsExpression {
|
private fun translateAsMemberFunctionReference(
|
||||||
|
descriptor: CallableDescriptor,
|
||||||
|
classDescriptor: ClassDescriptor,
|
||||||
|
context: TranslationContext
|
||||||
|
): JsExpression {
|
||||||
val jsClassNameRef = context.getQualifiedReference(classDescriptor)
|
val jsClassNameRef = context.getQualifiedReference(classDescriptor)
|
||||||
val funName = context.getNameForDescriptor(descriptor)
|
val funName = context.getNameForDescriptor(descriptor)
|
||||||
val funNameAsString = context.program().getStringLiteral(funName.toString())
|
val funNameAsString = context.program().getStringLiteral(funName.toString())
|
||||||
|
|||||||
@@ -35,7 +35,6 @@ import org.jetbrains.kotlin.resolve.DescriptorUtils;
|
|||||||
import org.jetbrains.kotlin.types.KotlinType;
|
import org.jetbrains.kotlin.types.KotlinType;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static com.google.dart.compiler.backend.js.ast.JsBinaryOperator.*;
|
import static com.google.dart.compiler.backend.js.ast.JsBinaryOperator.*;
|
||||||
@@ -84,7 +83,8 @@ public final class TranslationUtils {
|
|||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public static JsExpression translateExclForBinaryEqualLikeExpr(@NotNull JsBinaryOperation baseBinaryExpression) {
|
public static JsExpression translateExclForBinaryEqualLikeExpr(@NotNull JsBinaryOperation baseBinaryExpression) {
|
||||||
return new JsBinaryOperation(notOperator(baseBinaryExpression.getOperator()), baseBinaryExpression.getArg1(), baseBinaryExpression.getArg2());
|
return new JsBinaryOperation(notOperator(baseBinaryExpression.getOperator()), baseBinaryExpression.getArg1(),
|
||||||
|
baseBinaryExpression.getArg2());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isEqualLikeOperator(@NotNull JsBinaryOperator operator) {
|
public static boolean isEqualLikeOperator(@NotNull JsBinaryOperator operator) {
|
||||||
@@ -175,7 +175,7 @@ public final class TranslationUtils {
|
|||||||
@NotNull PropertyDescriptor descriptor,
|
@NotNull PropertyDescriptor descriptor,
|
||||||
@NotNull JsExpression assignTo) {
|
@NotNull JsExpression assignTo) {
|
||||||
JsNameRef backingFieldReference = backingFieldReference(context, descriptor);
|
JsNameRef backingFieldReference = backingFieldReference(context, descriptor);
|
||||||
return !JsAstUtils.isEmptyExpression(assignTo) ? assignment(backingFieldReference, assignTo) : assignTo;
|
return assignment(backingFieldReference, assignTo);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
|
|||||||
Reference in New Issue
Block a user