Fix name clashing between local variables
This commit is contained in:
+4
-4
@@ -37,18 +37,18 @@ fun prototypeOf(classNameRef: JsExpression) = JsNameRef(Namer.PROTOTYPE_NAME, cl
|
|||||||
fun translateFunction(declaration: IrFunction, name: JsName?, context: JsGenerationContext): JsFunction {
|
fun translateFunction(declaration: IrFunction, name: JsName?, context: JsGenerationContext): JsFunction {
|
||||||
val functionScope = JsFunctionScope(context.currentScope, "scope for ${name ?: "annon"}")
|
val functionScope = JsFunctionScope(context.currentScope, "scope for ${name ?: "annon"}")
|
||||||
val functionContext = context.newDeclaration(functionScope, declaration)
|
val functionContext = context.newDeclaration(functionScope, declaration)
|
||||||
|
val functionParams = declaration.valueParameters.map { functionContext.getNameForSymbol(it.symbol) }
|
||||||
val body = declaration.body?.accept(IrElementToJsStatementTransformer(), functionContext) as? JsBlock ?: JsBlock()
|
val body = declaration.body?.accept(IrElementToJsStatementTransformer(), functionContext) as? JsBlock ?: JsBlock()
|
||||||
val function = JsFunction(functionScope, body, "member function ${name ?: "annon"}")
|
val function = JsFunction(functionScope, body, "member function ${name ?: "annon"}")
|
||||||
|
|
||||||
function.name = name
|
function.name = name
|
||||||
|
|
||||||
fun JsFunction.addParameter(parameterName: String) {
|
fun JsFunction.addParameter(parameter: JsName) {
|
||||||
val parameter = function.scope.declareName(parameterName)
|
|
||||||
parameters.add(JsParameter(parameter))
|
parameters.add(JsParameter(parameter))
|
||||||
}
|
}
|
||||||
|
|
||||||
declaration.extensionReceiverParameter?.let { function.addParameter(context.getNameForSymbol(it.symbol).ident) }
|
declaration.extensionReceiverParameter?.let { function.addParameter(functionContext.getNameForSymbol(it.symbol)) }
|
||||||
declaration.valueParameters.forEach { function.addParameter(context.getNameForSymbol(it.symbol).ident) }
|
functionParams.forEach { function.addParameter(it) }
|
||||||
|
|
||||||
return function
|
return function
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-1
@@ -23,6 +23,7 @@ class SimpleNameGenerator : NameGenerator {
|
|||||||
|
|
||||||
private fun getNameForDescriptor(descriptor: DeclarationDescriptor, context: JsGenerationContext): JsName =
|
private fun getNameForDescriptor(descriptor: DeclarationDescriptor, context: JsGenerationContext): JsName =
|
||||||
nameCache.getOrPut(descriptor) {
|
nameCache.getOrPut(descriptor) {
|
||||||
|
var nameDeclarator: (String) -> JsName = context.currentScope::declareName
|
||||||
val nameBuilder = StringBuilder()
|
val nameBuilder = StringBuilder()
|
||||||
when (descriptor) {
|
when (descriptor) {
|
||||||
is ReceiverParameterDescriptor -> {
|
is ReceiverParameterDescriptor -> {
|
||||||
@@ -71,6 +72,7 @@ class SimpleNameGenerator : NameGenerator {
|
|||||||
}
|
}
|
||||||
is LocalVariableDescriptor -> {
|
is LocalVariableDescriptor -> {
|
||||||
nameBuilder.append(descriptor.name.identifier)
|
nameBuilder.append(descriptor.name.identifier)
|
||||||
|
nameDeclarator = context.currentScope::declareFreshName
|
||||||
}
|
}
|
||||||
is CallableDescriptor -> {
|
is CallableDescriptor -> {
|
||||||
nameBuilder.append(descriptor.name.asString())
|
nameBuilder.append(descriptor.name.asString())
|
||||||
@@ -79,7 +81,7 @@ class SimpleNameGenerator : NameGenerator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
context.currentScope.declareName(sanitizeName(nameBuilder.toString()))
|
nameDeclarator(sanitizeName(nameBuilder.toString()))
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun sanitizeName(name: String): String {
|
private fun sanitizeName(name: String): String {
|
||||||
|
|||||||
Reference in New Issue
Block a user