[JS] Prevent default class constructors in d.ts files.
Generate private TypeScript constructor for classes without public primary Kotlin constructor.
This commit is contained in:
+10
-1
@@ -66,6 +66,13 @@ fun ExportedDeclaration.toTypeScript(indent: String): String = indent + when (th
|
|||||||
|
|
||||||
val membersString = members.joinToString("") { it.toTypeScript("$indent ") + "\n" }
|
val membersString = members.joinToString("") { it.toTypeScript("$indent ") + "\n" }
|
||||||
|
|
||||||
|
// If there are no exported constructors, add a private constructor to disable default one
|
||||||
|
val privateCtorString =
|
||||||
|
if (!isInterface && !isAbstract && members.none { it is ExportedConstructor })
|
||||||
|
"$indent private constructor();\n"
|
||||||
|
else
|
||||||
|
""
|
||||||
|
|
||||||
val renderedTypeParameters =
|
val renderedTypeParameters =
|
||||||
if (typeParameters.isNotEmpty())
|
if (typeParameters.isNotEmpty())
|
||||||
"<" + typeParameters.joinToString(", ") + ">"
|
"<" + typeParameters.joinToString(", ") + ">"
|
||||||
@@ -74,7 +81,9 @@ fun ExportedDeclaration.toTypeScript(indent: String): String = indent + when (th
|
|||||||
|
|
||||||
val modifiers = if (isAbstract && !isInterface) "abstract " else ""
|
val modifiers = if (isAbstract && !isInterface) "abstract " else ""
|
||||||
|
|
||||||
val klassExport = "$modifiers$keyword $name$renderedTypeParameters$superClassClause$superInterfacesClause {\n$membersString$indent}"
|
val bodyString = privateCtorString + membersString + indent
|
||||||
|
|
||||||
|
val klassExport = "$modifiers$keyword $name$renderedTypeParameters$superClassClause$superInterfacesClause {\n$bodyString}"
|
||||||
val staticsExport = if (nestedClasses.isNotEmpty()) "\n" + ExportedNamespace(name, nestedClasses).toTypeScript(indent) else ""
|
val staticsExport = if (nestedClasses.isNotEmpty()) "\n" + ExportedNamespace(name, nestedClasses).toTypeScript(indent) else ""
|
||||||
klassExport + staticsExport
|
klassExport + staticsExport
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,10 +9,12 @@ declare namespace JS_TESTS {
|
|||||||
readonly x: string;
|
readonly x: string;
|
||||||
}
|
}
|
||||||
class ClassWithSecondaryCtor {
|
class ClassWithSecondaryCtor {
|
||||||
|
private constructor();
|
||||||
readonly x: string;
|
readonly x: string;
|
||||||
static create(y: string): ClassWithSecondaryCtor
|
static create(y: string): ClassWithSecondaryCtor
|
||||||
}
|
}
|
||||||
class ClassWithMultipleSecondaryCtors {
|
class ClassWithMultipleSecondaryCtors {
|
||||||
|
private constructor();
|
||||||
readonly x: string;
|
readonly x: string;
|
||||||
static createFromString(y: string): ClassWithMultipleSecondaryCtors
|
static createFromString(y: string): ClassWithMultipleSecondaryCtors
|
||||||
static createFromInts(y: number, z: number): ClassWithMultipleSecondaryCtors
|
static createFromInts(y: number, z: number): ClassWithMultipleSecondaryCtors
|
||||||
@@ -24,6 +26,7 @@ declare namespace JS_TESTS {
|
|||||||
static createFromInts(y: number, z: number): OpenClassWithMixedConstructors
|
static createFromInts(y: number, z: number): OpenClassWithMixedConstructors
|
||||||
}
|
}
|
||||||
class DerivedClassWithSecondaryCtor extends OpenClassWithMixedConstructors {
|
class DerivedClassWithSecondaryCtor extends OpenClassWithMixedConstructors {
|
||||||
|
private constructor();
|
||||||
static delegateToPrimary(y: string): DerivedClassWithSecondaryCtor
|
static delegateToPrimary(y: string): DerivedClassWithSecondaryCtor
|
||||||
static delegateToCreateFromInts(y: number, z: number): DerivedClassWithSecondaryCtor
|
static delegateToCreateFromInts(y: number, z: number): DerivedClassWithSecondaryCtor
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user