[K/JS] Don't generate TypeScript definitions from ExportModel if the -Xgenerate-dts flag was not provided ^Fixed KT-53940
This commit is contained in:
@@ -114,7 +114,7 @@ class K2JsIrCompiler : CLICompiler<K2JSCompilerArguments>() {
|
||||
return K2JSCompilerArguments()
|
||||
}
|
||||
|
||||
private data class TransformResult(val out: CompilationOutputs, val dts: String)
|
||||
private data class TransformResult(val out: CompilationOutputs, val dts: String?)
|
||||
|
||||
private class Ir2JsTransformer(
|
||||
val arguments: K2JSCompilerArguments,
|
||||
@@ -145,7 +145,7 @@ class K2JsIrCompiler : CLICompiler<K2JSCompilerArguments>() {
|
||||
)
|
||||
}
|
||||
|
||||
private fun makeJsCodeGeneratorAndDts(): Pair<JsCodeGenerator, String> {
|
||||
private fun makeJsCodeGeneratorAndDts(): Pair<JsCodeGenerator, String?> {
|
||||
val ir = lowerIr()
|
||||
val transformer = IrModuleToJsTransformer(ir.context, mainCallArguments, ir.moduleFragmentToUniqueName)
|
||||
|
||||
@@ -225,6 +225,7 @@ class K2JsIrCompiler : CLICompiler<K2JSCompilerArguments>() {
|
||||
configurationJs.put(CLIConfigurationKeys.RENDER_DIAGNOSTIC_INTERNAL_NAME, arguments.renderInternalDiagnosticNames)
|
||||
configurationJs.put(JSConfigurationKeys.PROPERTY_LAZY_INITIALIZATION, arguments.irPropertyLazyInitialization)
|
||||
configurationJs.put(JSConfigurationKeys.GENERATE_POLYFILLS, arguments.generatePolyfills)
|
||||
configurationJs.put(JSConfigurationKeys.GENERATE_DTS, arguments.generateDts)
|
||||
configurationJs.put(JSConfigurationKeys.GENERATE_INLINE_ANONYMOUS_FUNCTIONS, arguments.irGenerateInlineAnonymousFunctions)
|
||||
|
||||
if (!checkKotlinPackageUsage(environmentForJS.configuration, sourcesFiles)) return COMPILATION_ERROR
|
||||
@@ -382,7 +383,7 @@ class K2JsIrCompiler : CLICompiler<K2JSCompilerArguments>() {
|
||||
|
||||
outputs.write(outputDir, outputName)
|
||||
|
||||
if (arguments.generateDts) {
|
||||
if (tsDefinitions != null) {
|
||||
val dtsFile = outputDir.resolve("$outputName.d.ts")
|
||||
dtsFile.writeText(tsDefinitions)
|
||||
}
|
||||
|
||||
+10
-4
@@ -25,6 +25,7 @@ import org.jetbrains.kotlin.js.sourceMap.SourceMap3Builder
|
||||
import org.jetbrains.kotlin.js.sourceMap.SourceMapBuilderConsumer
|
||||
import org.jetbrains.kotlin.js.util.TextOutputImpl
|
||||
import org.jetbrains.kotlin.serialization.js.ModuleKind
|
||||
import org.jetbrains.kotlin.utils.addToStdlib.runIf
|
||||
import java.io.ByteArrayOutputStream
|
||||
import java.io.File
|
||||
import java.util.*
|
||||
@@ -99,6 +100,7 @@ class IrModuleToJsTransformer(
|
||||
) {
|
||||
private val shouldGeneratePolyfills = backendContext.configuration.getBoolean(JSConfigurationKeys.GENERATE_POLYFILLS)
|
||||
private val generateRegionComments = backendContext.configuration.getBoolean(JSConfigurationKeys.GENERATE_REGION_COMMENTS)
|
||||
private val shouldGenerateTypeScriptDefinitions = backendContext.configuration.getBoolean(JSConfigurationKeys.GENERATE_DTS)
|
||||
|
||||
private val mainModuleName = backendContext.configuration[CommonConfigurationKeys.MODULE_NAME]!!
|
||||
private val moduleKind = backendContext.configuration[JSConfigurationKeys.MODULE_KIND]!!
|
||||
@@ -142,7 +144,9 @@ class IrModuleToJsTransformer(
|
||||
|
||||
fun generateModule(modules: Iterable<IrModuleFragment>, modes: Set<TranslationMode>, relativeRequirePath: Boolean): CompilerResult {
|
||||
val exportData = associateIrAndExport(modules)
|
||||
val dts = ExportedModule(mainModuleName, moduleKind, exportData.flatExportedDeclarations()).toTypeScript()
|
||||
val dts = runIf(shouldGenerateTypeScriptDefinitions) {
|
||||
ExportedModule(mainModuleName, moduleKind, exportData.flatExportedDeclarations()).toTypeScript()
|
||||
}
|
||||
doStaticMembersLowering(modules)
|
||||
|
||||
val result = EnumMap<TranslationMode, CompilationOutputs>(TranslationMode::class.java)
|
||||
@@ -162,9 +166,11 @@ class IrModuleToJsTransformer(
|
||||
return CompilerResult(result, dts)
|
||||
}
|
||||
|
||||
fun makeJsCodeGeneratorAndDts(modules: Iterable<IrModuleFragment>, mode: TranslationMode): Pair<JsCodeGenerator, String> {
|
||||
fun makeJsCodeGeneratorAndDts(modules: Iterable<IrModuleFragment>, mode: TranslationMode): Pair<JsCodeGenerator, String?> {
|
||||
val exportData = associateIrAndExport(modules)
|
||||
val dts = ExportedModule(mainModuleName, moduleKind, exportData.flatExportedDeclarations()).toTypeScript()
|
||||
val dts = runIf(shouldGenerateTypeScriptDefinitions) {
|
||||
ExportedModule(mainModuleName, moduleKind, exportData.flatExportedDeclarations()).toTypeScript()
|
||||
}
|
||||
doStaticMembersLowering(modules)
|
||||
|
||||
if (mode.dce) {
|
||||
@@ -252,7 +258,7 @@ class IrModuleToJsTransformer(
|
||||
|
||||
result.exports.statements += exportStatements
|
||||
|
||||
if (exports.isNotEmpty()) {
|
||||
if (shouldGenerateTypeScriptDefinitions && exports.isNotEmpty()) {
|
||||
result.dts = exports.toTypeScript(moduleKind)
|
||||
}
|
||||
|
||||
|
||||
+4
@@ -131,6 +131,10 @@ fun createCompilerConfiguration(module: TestModule, configurators: List<Abstract
|
||||
configuration.put(JSConfigurationKeys.GENERATE_STRICT_IMPLICIT_EXPORT, true)
|
||||
}
|
||||
|
||||
if (JsEnvironmentConfigurationDirectives.GENERATE_DTS in module.directives) {
|
||||
configuration.put(JSConfigurationKeys.GENERATE_DTS, true)
|
||||
}
|
||||
|
||||
if (module.frontendKind == FrontendKinds.FIR) {
|
||||
configuration[CommonConfigurationKeys.USE_FIR] = true
|
||||
}
|
||||
|
||||
@@ -80,6 +80,9 @@ public class JSConfigurationKeys {
|
||||
public static final CompilerConfigurationKey<Boolean> GENERATE_POLYFILLS =
|
||||
CompilerConfigurationKey.create("generate polyfills for newest properties, methods and classes from ES6+");
|
||||
|
||||
public static final CompilerConfigurationKey<Boolean> GENERATE_DTS =
|
||||
CompilerConfigurationKey.create("generate TypeScript definition file");
|
||||
|
||||
public static final CompilerConfigurationKey<Boolean> GENERATE_REGION_COMMENTS =
|
||||
CompilerConfigurationKey.create("generate special comments at the start and the end of each file block, " +
|
||||
"it allows to fold them and navigate to them in the IDEA");
|
||||
|
||||
Reference in New Issue
Block a user