JS: de-duplicate temporary names in some cases to allow name resolution to make its work better
This commit is contained in:
committed by
Alexey Andreev
parent
9e0a06a4aa
commit
7d3e3c0299
+1
-1
@@ -98,7 +98,7 @@ abstract class AbstractDeclarationVisitor : TranslatorVisitor<Unit>() {
|
||||
context: TranslationContext
|
||||
): JsExpression {
|
||||
val function = context.getFunctionObject(descriptor)
|
||||
val innerContext = context.newDeclaration(descriptor).translateAndAliasParameters(descriptor, function.parameters)
|
||||
val innerContext = context.newDeclaration(descriptor).translateAndAliasParameters(descriptor, function.parameters, false)
|
||||
|
||||
if (descriptor.isSuspend) {
|
||||
if (descriptor.requiresStateMachineTransformation(context)) {
|
||||
|
||||
+1
-1
@@ -214,7 +214,7 @@ class ClassTranslator private constructor(
|
||||
context.addDeclarationStatement(constructorInitializer.makeStmt())
|
||||
|
||||
context = context.contextWithScope(constructorInitializer)
|
||||
context.translateAndAliasParameters(constructorDescriptor, constructorInitializer.parameters)
|
||||
context.translateAndAliasParameters(constructorDescriptor, constructorInitializer.parameters, false)
|
||||
.translateFunction(constructor, constructorInitializer)
|
||||
|
||||
// Translate super/this call
|
||||
|
||||
+1
-1
@@ -115,7 +115,7 @@ class DeclarationBodyVisitor(
|
||||
val caller = JsFunction(context.getScopeForDescriptor(containingClass), JsBlock(), "")
|
||||
val callerContext = context
|
||||
.newDeclaration(descriptor)
|
||||
.translateAndAliasParameters(descriptor, caller.parameters)
|
||||
.translateAndAliasParameters(descriptor, caller.parameters, true)
|
||||
.innerBlock(caller.body)
|
||||
|
||||
val callbackName = JsScope.declareTemporaryName("callback" + Namer.DEFAULT_PARAMETER_IMPLEMENTOR_SUFFIX)
|
||||
|
||||
+1
-1
@@ -72,7 +72,7 @@ class JsDataClassGenerator extends DataClassMethodGenerator {
|
||||
context.bindingContext(), BindingContext.VALUE_PARAMETER_AS_PROPERTY, parameterDescriptor);
|
||||
|
||||
JsName fieldName = context.getNameForDescriptor(propertyDescriptor);
|
||||
JsName paramName = context.getNameForDescriptor(parameterDescriptor);
|
||||
JsName paramName = JsScope.declareTemporaryName(context.getNameForDescriptor(parameterDescriptor).getIdent());
|
||||
|
||||
functionObj.getParameters().add(new JsParameter(paramName));
|
||||
|
||||
|
||||
+1
-1
@@ -240,7 +240,7 @@ private class PropertyTranslator(
|
||||
private fun translateCustomAccessor(expression: KtPropertyAccessor): JsPropertyInitializer {
|
||||
val descriptor = BindingUtils.getFunctionDescriptor(bindingContext(), expression)
|
||||
val function = JsFunction(context().getScopeForDescriptor(descriptor), JsBlock(), descriptor.toString())
|
||||
context().translateAndAliasParameters(descriptor, function.parameters).translateFunction(expression, function)
|
||||
context().translateAndAliasParameters(descriptor, function.parameters, false).translateFunction(expression, function)
|
||||
return translateFunctionAsEcma5PropertyDescriptor(function, descriptor, context())
|
||||
}
|
||||
|
||||
|
||||
+6
-2
@@ -39,7 +39,8 @@ import org.jetbrains.kotlin.resolve.descriptorUtil.isEffectivelyPublicApi
|
||||
|
||||
fun TranslationContext.translateAndAliasParameters(
|
||||
descriptor: FunctionDescriptor,
|
||||
targetList: MutableList<JsParameter>
|
||||
targetList: MutableList<JsParameter>,
|
||||
aliasValueParams: Boolean
|
||||
): TranslationContext {
|
||||
val aliases = mutableMapOf<DeclarationDescriptor, JsExpression>()
|
||||
|
||||
@@ -62,7 +63,10 @@ fun TranslationContext.translateAndAliasParameters(
|
||||
}
|
||||
|
||||
for (valueParameter in descriptor.valueParameters) {
|
||||
targetList += JsParameter(getNameForDescriptor(valueParameter)).apply { hasDefaultValue = valueParameter.hasDefaultValue() }
|
||||
val name = getNameForDescriptor(valueParameter)
|
||||
val tmpName = if (aliasValueParams) JsScope.declareTemporaryName(name.ident) else name
|
||||
aliases[valueParameter] = JsAstUtils.pureFqn(tmpName, null)
|
||||
targetList += JsParameter(tmpName).apply { hasDefaultValue = valueParameter.hasDefaultValue() }
|
||||
}
|
||||
|
||||
val continuationDescriptor = continuationParameterDescriptor
|
||||
|
||||
+1
-1
@@ -47,7 +47,7 @@ class LiteralFunctionTranslator(context: TranslationContext) : AbstractTranslato
|
||||
|
||||
val functionContext = invokingContext
|
||||
.newFunctionBodyWithUsageTracker(lambda, descriptor)
|
||||
.translateAndAliasParameters(descriptor, lambda.parameters)
|
||||
.translateAndAliasParameters(descriptor, lambda.parameters, false)
|
||||
|
||||
descriptor.valueParameters.forEach {
|
||||
if (it is ValueParameterDescriptorImpl.WithDestructuringDeclaration) {
|
||||
|
||||
+2
-1
@@ -29,6 +29,7 @@ import org.jetbrains.kotlin.js.translate.context.TranslationContext;
|
||||
import org.jetbrains.kotlin.js.translate.expression.LocalFunctionCollector;
|
||||
import org.jetbrains.kotlin.js.translate.general.AbstractTranslator;
|
||||
import org.jetbrains.kotlin.js.translate.general.Translation;
|
||||
import org.jetbrains.kotlin.js.translate.reference.ReferenceTranslator;
|
||||
import org.jetbrains.kotlin.psi.KtDeclarationWithBody;
|
||||
import org.jetbrains.kotlin.psi.KtExpression;
|
||||
import org.jetbrains.kotlin.types.KotlinType;
|
||||
@@ -78,7 +79,7 @@ public final class FunctionBodyTranslator extends AbstractTranslator {
|
||||
for (ValueParameterDescriptor valueParameter : valueParameters) {
|
||||
if (!valueParameter.declaresDefaultValue()) continue;
|
||||
|
||||
JsNameRef jsNameRef = functionBodyContext.getNameForDescriptor(valueParameter).makeRef();
|
||||
JsExpression jsNameRef = ReferenceTranslator.translateAsValueReference(valueParameter, functionBodyContext);
|
||||
KtExpression defaultArgument = getDefaultArgument(valueParameter);
|
||||
JsBlock defaultArgBlock = new JsBlock();
|
||||
JsExpression defaultValue = Translation.translateAsExpression(defaultArgument, functionBodyContext, defaultArgBlock);
|
||||
|
||||
Reference in New Issue
Block a user