Remap source maps in JS DCE. Improve JS DCE error logging

See KT-19821
This commit is contained in:
Alexey Andreev
2017-08-25 15:38:01 +03:00
parent 1350e3c4ac
commit c66bc0b0e9
28 changed files with 196 additions and 42 deletions
@@ -22,10 +22,7 @@ import org.jetbrains.kotlin.cli.common.arguments.K2JSDceArguments
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
import org.jetbrains.kotlin.config.Services
import org.jetbrains.kotlin.js.dce.DeadCodeElimination
import org.jetbrains.kotlin.js.dce.InputFile
import org.jetbrains.kotlin.js.dce.extractRoots
import org.jetbrains.kotlin.js.dce.printTree
import org.jetbrains.kotlin.js.dce.*
import java.io.File
class K2JSDce : CLITool<K2JSDceArguments>() {
@@ -38,7 +35,9 @@ class K2JSDce : CLITool<K2JSDceArguments>() {
val inputName = parts[0]
val moduleName = parts.getOrNull(1) ?: ""
val resolvedModuleName = if (!moduleName.isEmpty()) moduleName else File(inputName).nameWithoutExtension
InputFile(inputName, File(baseDir, resolvedModuleName + ".js").absolutePath, resolvedModuleName)
val pathToSourceMapCandidate = "$inputName.map"
val pathToSourceMap = if (File(pathToSourceMapCandidate).exists()) pathToSourceMapCandidate else null
InputFile(inputName, pathToSourceMap, File(baseDir, resolvedModuleName + ".js").absolutePath, resolvedModuleName)
}
if (files.isEmpty() && !arguments.version) {
@@ -51,9 +50,16 @@ class K2JSDce : CLITool<K2JSDceArguments>() {
val includedDeclarations = arguments.declarationsToKeep.orEmpty().toSet()
val dceResult = DeadCodeElimination.run(files, includedDeclarations) {
messageCollector.report(CompilerMessageSeverity.LOGGING, it)
val logConsumer = { level: DCELogLevel, message: String ->
val severity = when (level) {
DCELogLevel.ERROR -> CompilerMessageSeverity.ERROR
DCELogLevel.WARN -> CompilerMessageSeverity.WARNING
DCELogLevel.INFO -> CompilerMessageSeverity.LOGGING
}
messageCollector.report(severity, message)
}
val dceResult = DeadCodeElimination.run(files, includedDeclarations, logConsumer)
if (dceResult.status == DeadCodeEliminationStatus.FAILED) return ExitCode.COMPILATION_ERROR
val nodes = dceResult.reachableNodes
val reachabilitySeverity = if (arguments.printReachabilityInfo) CompilerMessageSeverity.INFO else CompilerMessageSeverity.LOGGING
+3
View File
@@ -0,0 +1,3 @@
$TESTDATA_DIR$/parseError.js
-output-dir
$TEMP_DIR$/min
+1
View File
@@ -0,0 +1 @@
..
+2
View File
@@ -0,0 +1,2 @@
error: at compiler/testData/cli/js-dce/parseError.js (1, 1): syntax error
COMPILATION_ERROR
+1
View File
@@ -0,0 +1 @@
// ABSENT: min/parseError.js
+3
View File
@@ -0,0 +1,3 @@
$TESTDATA_DIR$/withSourceMap.js
-output-dir
$TEMP_DIR$/min
+24
View File
@@ -0,0 +1,24 @@
if (typeof kotlin === 'undefined') {
throw new Error("Error loading module 'sample'. Its dependency 'kotlin' was not found. Please, check whether 'kotlin' is loaded prior to 'sample'.");
}
var sample = function (_, Kotlin) {
'use strict';
var println = Kotlin.kotlin.io.println_s8jyv4$;
function foo() {
println('foo');
}
function bar() {
println('bar');
}
function main(args) {
foo();
}
_.foo = foo;
_.bar = bar;
_.main_kand9s$ = main;
main([]);
Kotlin.defineModule('sample', _);
return _;
}(typeof sample === 'undefined' ? {} : sample, kotlin);
//# sourceMappingURL=sample.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"sample.js","sources":["sample.kt"],"sourcesContent":[null],"names":[],"mappings":";;;;;;;IACI,QAAQ,KAAR,C;EACJ,C;;IAGI,QAAQ,KAAR,C;EACJ,C;;IAGI,K;EACJ,C;;;;;;;;"}
+1
View File
@@ -0,0 +1 @@
OK
+2
View File
@@ -0,0 +1,2 @@
// EXISTS: min/withSourceMap.js
// EXISTS: min/withSourceMap.js.map
@@ -674,6 +674,12 @@ public class CliTestGenerated extends AbstractCliTest {
doJsDceTest(fileName);
}
@TestMetadata("parseError.args")
public void testParseError() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/cli/js-dce/parseError.args");
doJsDceTest(fileName);
}
@TestMetadata("printReachability.args")
public void testPrintReachability() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/cli/js-dce/printReachability.args");
@@ -685,5 +691,11 @@ public class CliTestGenerated extends AbstractCliTest {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/cli/js-dce/simple.args");
doJsDceTest(fileName);
}
@TestMetadata("withSourceMap.args")
public void testWithSourceMap() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/cli/js-dce/withSourceMap.args");
doJsDceTest(fileName);
}
}
}