[K/JS] chore: stop unnecessary defineProperties generating.
This commit is contained in:
+1
-1
@@ -115,7 +115,7 @@ class IrElementToJsExpressionTransformer : BaseIrElementToJsNodeTransformer<JsEx
|
||||
return expression.receiver!!.accept(this, context).withSource(expression, context)
|
||||
}
|
||||
val fieldName = context.getNameForField(field)
|
||||
return JsNameRef(fieldName, expression.receiver?.accept(this, context)).withSource(expression, context)
|
||||
return jsElementAccess(fieldName, expression.receiver?.accept(this, context)).withSource(expression, context)
|
||||
}
|
||||
|
||||
override fun visitGetValue(expression: IrGetValue, context: JsGenerationContext): JsExpression {
|
||||
|
||||
+1
-1
@@ -101,7 +101,7 @@ class IrElementToJsStatementTransformer : BaseIrElementToJsNodeTransformer<JsSta
|
||||
override fun visitSetField(expression: IrSetField, context: JsGenerationContext): JsStatement {
|
||||
val fieldName = context.getNameForField(expression.symbol.owner)
|
||||
val expressionTransformer = IrElementToJsExpressionTransformer()
|
||||
val dest = JsNameRef(fieldName, expression.receiver?.accept(expressionTransformer, context))
|
||||
val dest = jsElementAccess(fieldName, expression.receiver?.accept(expressionTransformer, context))
|
||||
return expression.value.maybeOptimizeIntoSwitch(context) { jsAssignment(dest, it).withSource(expression, context).makeStmt() }
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -111,7 +111,7 @@ class JsClassGenerator(private val irClass: IrClass, val context: JsGenerationCo
|
||||
if (property.getter?.extensionReceiverParameter != null || property.setter?.extensionReceiverParameter != null)
|
||||
continue
|
||||
|
||||
if (!property.visibility.isPublicAPI)
|
||||
if (!property.visibility.isPublicAPI || property.isSimpleProperty)
|
||||
continue
|
||||
|
||||
if (
|
||||
|
||||
+19
-8
@@ -13,6 +13,7 @@ import org.jetbrains.kotlin.ir.symbols.IrClassSymbol
|
||||
import org.jetbrains.kotlin.ir.types.IrSimpleType
|
||||
import org.jetbrains.kotlin.ir.util.fqNameWhenAvailable
|
||||
import org.jetbrains.kotlin.ir.util.isEffectivelyExternal
|
||||
import org.jetbrains.kotlin.ir.util.isSimpleProperty
|
||||
import org.jetbrains.kotlin.ir.util.parentAsClass
|
||||
import org.jetbrains.kotlin.js.backend.ast.*
|
||||
import org.jetbrains.kotlin.utils.DFS
|
||||
@@ -117,7 +118,7 @@ class JsNameLinkingNamer(private val context: JsIrBackendContext, private val mi
|
||||
declaration is IrFunction && declaration.dispatchReceiverParameter != null -> {
|
||||
val property = (declaration as? IrSimpleFunction)?.correspondingPropertySymbol?.owner
|
||||
if (property?.isExported(context) == true || property?.isEffectivelyExternal() == true) {
|
||||
context.minimizedNameGenerator.reserveName(property.name.asString())
|
||||
context.minimizedNameGenerator.reserveName(property.getJsNameOrKotlinName().identifier)
|
||||
}
|
||||
if (declaration.hasStableJsName(context)) {
|
||||
val signature = jsFunctionSignature(declaration, context)
|
||||
@@ -126,7 +127,7 @@ class JsNameLinkingNamer(private val context: JsIrBackendContext, private val mi
|
||||
}
|
||||
declaration is IrProperty -> {
|
||||
if (declaration.isExported(context)) {
|
||||
context.minimizedNameGenerator.reserveName(declaration.name.asString())
|
||||
context.minimizedNameGenerator.reserveName(declaration.getJsNameOrKotlinName().identifier)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -138,12 +139,22 @@ class JsNameLinkingNamer(private val context: JsIrBackendContext, private val mi
|
||||
it.declarations.forEach {
|
||||
when {
|
||||
it is IrField -> {
|
||||
val safeName = if (minimizedMemberNames) {
|
||||
context.minimizedNameGenerator.generateNextName()
|
||||
} else it.safeName()
|
||||
val suffix = nameCnt.getOrDefault(safeName, 0) + 1
|
||||
nameCnt[safeName] = suffix
|
||||
result[it] = safeName + "_$suffix"
|
||||
val correspondingProperty = it.correspondingPropertySymbol?.owner
|
||||
val hasStableName = correspondingProperty != null &&
|
||||
correspondingProperty.visibility.isPublicAPI &&
|
||||
(correspondingProperty.isExported(context) || correspondingProperty.getJsName() != null) &&
|
||||
correspondingProperty.isSimpleProperty
|
||||
val safeName = when {
|
||||
hasStableName -> (correspondingProperty ?: it).getJsNameOrKotlinName().identifier
|
||||
minimizedMemberNames -> context.minimizedNameGenerator.generateNextName()
|
||||
else -> it.safeName()
|
||||
}
|
||||
val resultName = if (!hasStableName) {
|
||||
val suffix = nameCnt.getOrDefault(safeName, 0) + 1
|
||||
nameCnt[safeName] = suffix
|
||||
safeName + "_$suffix"
|
||||
} else safeName
|
||||
result[it] = resultName
|
||||
}
|
||||
it is IrFunction && it.dispatchReceiverParameter != null -> {
|
||||
nameCnt[jsFunctionSignature(it, context)] = 1 // avoid clashes with member functions
|
||||
|
||||
+6
-3
@@ -52,10 +52,13 @@ fun <T : JsNode> IrWhen.toJsNode(
|
||||
}
|
||||
|
||||
fun jsElementAccess(name: String, receiver: JsExpression?): JsExpression =
|
||||
if (receiver == null || name.isValidES5Identifier()) {
|
||||
JsNameRef(JsName(name, false), receiver)
|
||||
jsElementAccess(JsName(name, false), receiver)
|
||||
|
||||
fun jsElementAccess(name: JsName, receiver: JsExpression?): JsExpression =
|
||||
if (receiver == null || name.ident.isValidES5Identifier()) {
|
||||
JsNameRef(name, receiver)
|
||||
} else {
|
||||
JsArrayAccess(receiver, JsStringLiteral(name))
|
||||
JsArrayAccess(receiver, JsStringLiteral(name.ident))
|
||||
}
|
||||
|
||||
fun jsAssignment(left: JsExpression, right: JsExpression) = JsBinaryOperation(JsBinaryOperator.ASG, left, right)
|
||||
|
||||
@@ -192,6 +192,16 @@ fun IrValueParameter.createStubDefaultValue(): IrExpressionBody =
|
||||
IrErrorExpressionImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, type, "Stub expression for default value of $name")
|
||||
)
|
||||
|
||||
val IrProperty.isSimpleProperty: Boolean
|
||||
get() {
|
||||
val getterFun = getter
|
||||
val setterFun = setter
|
||||
return !isFakeOverride &&
|
||||
modality === Modality.FINAL &&
|
||||
(getterFun == null || getterFun.origin == IrDeclarationOrigin.DEFAULT_PROPERTY_ACCESSOR) &&
|
||||
(setterFun == null || setterFun.origin == IrDeclarationOrigin.DEFAULT_PROPERTY_ACCESSOR)
|
||||
}
|
||||
|
||||
val IrClass.functions: Sequence<IrSimpleFunction>
|
||||
get() = declarations.asSequence().filterIsInstance<IrSimpleFunction>()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user