diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/js/K2JSCompiler.java b/compiler/cli/src/org/jetbrains/kotlin/cli/js/K2JSCompiler.java index 4a7ee43cb12..1e9da5645a5 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/js/K2JSCompiler.java +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/js/K2JSCompiler.java @@ -135,7 +135,7 @@ public class K2JSCompiler extends CLICompiler { configuration.put(CommonConfigurationKeys.MODULE_NAME, FileUtil.getNameWithoutExtension(outputFile)); JsConfig config = new JsConfig(project, configuration); - if (config.checkLibFilesAndReportErrors(new JsConfig.Reporter() { + JsConfig.Reporter reporter = new JsConfig.Reporter() { @Override public void error(@NotNull String message) { messageCollector.report(ERROR, message, null); @@ -145,7 +145,8 @@ public class K2JSCompiler extends CLICompiler { public void warning(@NotNull String message) { messageCollector.report(STRONG_WARNING, message, null); } - })) { + }; + if (config.checkLibFilesAndReportErrors(reporter)) { return COMPILATION_ERROR; } @@ -184,7 +185,7 @@ public class K2JSCompiler extends CLICompiler { K2JSTranslator translator = new K2JSTranslator(config); try { //noinspection unchecked - translationResult = translator.translate(sourcesFiles, mainCallParameters, jsAnalysisResult); + translationResult = translator.translate(reporter, sourcesFiles, mainCallParameters, jsAnalysisResult); } catch (Exception e) { throw ExceptionUtilsKt.rethrow(e); diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/AbstractDiagnosticsTestWithJsStdLibAndBackendCompilation.kt b/compiler/tests/org/jetbrains/kotlin/checkers/AbstractDiagnosticsTestWithJsStdLibAndBackendCompilation.kt index 4a5f160e4c5..c09ee9490e2 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/AbstractDiagnosticsTestWithJsStdLibAndBackendCompilation.kt +++ b/compiler/tests/org/jetbrains/kotlin/checkers/AbstractDiagnosticsTestWithJsStdLibAndBackendCompilation.kt @@ -20,6 +20,7 @@ import org.jetbrains.kotlin.config.LanguageVersionSettings import org.jetbrains.kotlin.context.ModuleContext import org.jetbrains.kotlin.diagnostics.DiagnosticUtils.hasError import org.jetbrains.kotlin.js.analyzer.JsAnalysisResult +import org.jetbrains.kotlin.js.config.JsConfig import org.jetbrains.kotlin.js.facade.K2JSTranslator import org.jetbrains.kotlin.js.facade.MainCallParameters import org.jetbrains.kotlin.psi.KtFile @@ -38,7 +39,7 @@ abstract class AbstractDiagnosticsTestWithJsStdLibAndBackendCompilation : Abstra if (!hasError(diagnostics)) { val translator = K2JSTranslator(config) - translator.translate(files, MainCallParameters.noCall(), analysisResult) + translator.translate(object : JsConfig.Reporter() {}, files, MainCallParameters.noCall(), analysisResult) } return analysisResult diff --git a/js/js.inliner/src/org/jetbrains/kotlin/js/inline/FunctionReader.kt b/js/js.inliner/src/org/jetbrains/kotlin/js/inline/FunctionReader.kt index eb45c2ee154..26b37d8fedc 100644 --- a/js/js.inliner/src/org/jetbrains/kotlin/js/inline/FunctionReader.kt +++ b/js/js.inliner/src/org/jetbrains/kotlin/js/inline/FunctionReader.kt @@ -49,7 +49,12 @@ private val JS_IDENTIFIER="[$JS_IDENTIFIER_START][$JS_IDENTIFIER_PART]*" private val DEFINE_MODULE_PATTERN = ("($JS_IDENTIFIER)\\.defineModule\\(\\s*(['\"])([^'\"]+)\\2\\s*,\\s*(\\w+)\\s*\\)").toRegex().toPattern() private val DEFINE_MODULE_FIND_PATTERN = ".defineModule(" -class FunctionReader(private val config: JsConfig, private val currentModuleName: JsName, fragments: List) { +class FunctionReader( + private val reporter: JsConfig.Reporter, + private val config: JsConfig, + private val currentModuleName: JsName, + fragments: List +) { /** * fileContent: .js file content, that contains this module definition. * One file can contain more than one module definition. @@ -98,7 +103,10 @@ class FunctionReader(private val config: JsConfig, private val currentModuleName val result = SourceMapParser.parse(StringReader(it)) when (result) { is SourceMapSuccess -> result.value - is SourceMapError -> throw RuntimeException("Error parsing source map file: ${result.message}\n$it") + is SourceMapError -> { + reporter.warning("Error parsing source map file for $path: ${result.message}") + null + } } } diff --git a/js/js.inliner/src/org/jetbrains/kotlin/js/inline/JsInliner.java b/js/js.inliner/src/org/jetbrains/kotlin/js/inline/JsInliner.java index dfd4f81df73..a19a425eaf8 100644 --- a/js/js.inliner/src/org/jetbrains/kotlin/js/inline/JsInliner.java +++ b/js/js.inliner/src/org/jetbrains/kotlin/js/inline/JsInliner.java @@ -63,6 +63,7 @@ public class JsInliner extends JsVisitorWithContextImpl { node -> node instanceof JsInvocation && hasToBeInlined((JsInvocation) node); public static void process( + @NotNull JsConfig.Reporter reporter, @NotNull JsConfig config, @NotNull DiagnosticSink trace, @NotNull JsName currentModuleName, @@ -76,7 +77,7 @@ public class JsInliner extends JsVisitorWithContextImpl { accessorInvocationTransformer.accept(fragment.getDeclarationBlock()); accessorInvocationTransformer.accept(fragment.getInitializerBlock()); } - FunctionReader functionReader = new FunctionReader(config, currentModuleName, fragments); + FunctionReader functionReader = new FunctionReader(reporter, config, currentModuleName, fragments); JsInliner inliner = new JsInliner(config, functions, accessors, functionReader, trace); for (JsProgramFragment fragment : fragmentsToProcess) { inliner.inliningContexts.push(inliner.new JsInliningContext()); diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/AbstractJsLineNumberTest.kt b/js/js.tests/test/org/jetbrains/kotlin/js/test/AbstractJsLineNumberTest.kt index 7b26d075469..6f1e9afe348 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/AbstractJsLineNumberTest.kt +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/AbstractJsLineNumberTest.kt @@ -35,6 +35,7 @@ import org.jetbrains.kotlin.js.facade.K2JSTranslator import org.jetbrains.kotlin.js.facade.MainCallParameters import org.jetbrains.kotlin.js.facade.TranslationResult import org.jetbrains.kotlin.js.facade.TranslationUnit +import org.jetbrains.kotlin.js.test.utils.ExceptionThrowingReporter import org.jetbrains.kotlin.js.test.utils.LineCollector import org.jetbrains.kotlin.js.test.utils.LineOutputToStringVisitor import org.jetbrains.kotlin.js.util.TextOutputImpl @@ -69,7 +70,7 @@ abstract class AbstractJsLineNumberTest : KotlinTestWithEnvironment() { val translator = K2JSTranslator(createConfig(module, file, modules)) val units = module.files.map { TranslationUnit.SourceFile(createPsiFile(it.fileName)) } - val translationResult = translator.translateUnits(units, MainCallParameters.noCall()) + val translationResult = translator.translateUnits(ExceptionThrowingReporter, units, MainCallParameters.noCall()) if (translationResult !is TranslationResult.Success) { val outputStream = ByteArrayOutputStream() diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/BasicBoxTest.kt b/js/js.tests/test/org/jetbrains/kotlin/js/test/BasicBoxTest.kt index f3523cd1a3b..f01d048cddf 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/BasicBoxTest.kt +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/BasicBoxTest.kt @@ -331,7 +331,7 @@ abstract class BasicBoxTest( outputPostfixFile: File?, mainCallParameters: MainCallParameters) { val translator = K2JSTranslator(config) - val translationResult = translator.translateUnits(units, mainCallParameters) + val translationResult = translator.translateUnits(ExceptionThrowingReporter, units, mainCallParameters) if (translationResult !is TranslationResult.Success) { val outputStream = ByteArrayOutputStream() diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/utils/ExceptionThrowingReporter.kt b/js/js.tests/test/org/jetbrains/kotlin/js/test/utils/ExceptionThrowingReporter.kt new file mode 100644 index 00000000000..fa17af95de2 --- /dev/null +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/utils/ExceptionThrowingReporter.kt @@ -0,0 +1,25 @@ +/* + * Copyright 2010-2017 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.kotlin.js.test.utils + +import org.jetbrains.kotlin.js.config.JsConfig + +object ExceptionThrowingReporter : JsConfig.Reporter() { + override fun error(message: String) { + throw AssertionError("Error message reported: $message") + } +} \ No newline at end of file diff --git a/js/js.translator/src/org/jetbrains/kotlin/js/facade/K2JSTranslator.java b/js/js.translator/src/org/jetbrains/kotlin/js/facade/K2JSTranslator.java index b169388db8d..53d5fc599ba 100644 --- a/js/js.translator/src/org/jetbrains/kotlin/js/facade/K2JSTranslator.java +++ b/js/js.translator/src/org/jetbrains/kotlin/js/facade/K2JSTranslator.java @@ -66,40 +66,44 @@ public final class K2JSTranslator { @NotNull public TranslationResult translate( + @NotNull JsConfig.Reporter reporter, @NotNull List files, @NotNull MainCallParameters mainCallParameters ) throws TranslationException { - return translate(files, mainCallParameters, null); + return translate(reporter, files, mainCallParameters, null); } @NotNull public TranslationResult translate( + @NotNull JsConfig.Reporter reporter, @NotNull List files, @NotNull MainCallParameters mainCallParameters, @Nullable JsAnalysisResult analysisResult ) throws TranslationException { - List units = new ArrayList(); + List units = new ArrayList<>(); for (KtFile file : files) { units.add(new TranslationUnit.SourceFile(file)); } - return translateUnits(units, mainCallParameters, analysisResult); + return translateUnits(reporter, units, mainCallParameters, analysisResult); } @NotNull public TranslationResult translateUnits( + @NotNull JsConfig.Reporter reporter, @NotNull List units, @NotNull MainCallParameters mainCallParameters ) throws TranslationException { - return translateUnits(units, mainCallParameters, null); + return translateUnits(reporter, units, mainCallParameters, null); } @NotNull public TranslationResult translateUnits( + @NotNull JsConfig.Reporter reporter, @NotNull List units, @NotNull MainCallParameters mainCallParameters, @Nullable JsAnalysisResult analysisResult ) throws TranslationException { - List files = new ArrayList(); + List files = new ArrayList<>(); for (TranslationUnit unit : units) { if (unit instanceof TranslationUnit.SourceFile) { files.add(((TranslationUnit.SourceFile) unit).getFile()); @@ -120,10 +124,11 @@ public final class K2JSTranslator { ProgressIndicatorAndCompilationCanceledStatus.checkCanceled(); if (hasError(diagnostics)) return new TranslationResult.Fail(diagnostics); - List newFragments = new ArrayList(translationResult.getNewFragments()); - List allFragments = new ArrayList(translationResult.getFragments()); + List newFragments = new ArrayList<>(translationResult.getNewFragments()); + List allFragments = new ArrayList<>(translationResult.getFragments()); - JsInliner.process(config, analysisResult.getBindingTrace(), translationResult.getInnerModuleName(), allFragments, newFragments); + JsInliner.process(reporter, config, analysisResult.getBindingTrace(), translationResult.getInnerModuleName(), + allFragments, newFragments); LabeledBlockToDoWhileTransformation.INSTANCE.apply(newFragments); @@ -139,7 +144,7 @@ public final class K2JSTranslator { ExpandIsCallsKt.expandIsCalls(newFragments); ProgressIndicatorAndCompilationCanceledStatus.checkCanceled(); - Map fileMap = new HashMap(); + Map fileMap = new HashMap<>(); JsAstSerializer serializer = new JsAstSerializer(); byte[] metadataHeader = null; boolean serializeFragments = config.getConfiguration().get(JSConfigurationKeys.SERIALIZE_FRAGMENTS, false);