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 {
|
||||
val functionScope = JsFunctionScope(context.currentScope, "scope for ${name ?: "annon"}")
|
||||
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 function = JsFunction(functionScope, body, "member function ${name ?: "annon"}")
|
||||
|
||||
function.name = name
|
||||
|
||||
fun JsFunction.addParameter(parameterName: String) {
|
||||
val parameter = function.scope.declareName(parameterName)
|
||||
fun JsFunction.addParameter(parameter: JsName) {
|
||||
parameters.add(JsParameter(parameter))
|
||||
}
|
||||
|
||||
declaration.extensionReceiverParameter?.let { function.addParameter(context.getNameForSymbol(it.symbol).ident) }
|
||||
declaration.valueParameters.forEach { function.addParameter(context.getNameForSymbol(it.symbol).ident) }
|
||||
declaration.extensionReceiverParameter?.let { function.addParameter(functionContext.getNameForSymbol(it.symbol)) }
|
||||
functionParams.forEach { function.addParameter(it) }
|
||||
|
||||
return function
|
||||
}
|
||||
|
||||
+3
-1
@@ -23,6 +23,7 @@ class SimpleNameGenerator : NameGenerator {
|
||||
|
||||
private fun getNameForDescriptor(descriptor: DeclarationDescriptor, context: JsGenerationContext): JsName =
|
||||
nameCache.getOrPut(descriptor) {
|
||||
var nameDeclarator: (String) -> JsName = context.currentScope::declareName
|
||||
val nameBuilder = StringBuilder()
|
||||
when (descriptor) {
|
||||
is ReceiverParameterDescriptor -> {
|
||||
@@ -71,6 +72,7 @@ class SimpleNameGenerator : NameGenerator {
|
||||
}
|
||||
is LocalVariableDescriptor -> {
|
||||
nameBuilder.append(descriptor.name.identifier)
|
||||
nameDeclarator = context.currentScope::declareFreshName
|
||||
}
|
||||
is CallableDescriptor -> {
|
||||
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 {
|
||||
|
||||
Reference in New Issue
Block a user