Report errors in JS source maps as compiler warnings

This commit is contained in:
Alexey Andreev
2017-06-05 12:37:51 +03:00
parent bf21cfd6e0
commit 59240a4bd9
8 changed files with 60 additions and 18 deletions
@@ -135,7 +135,7 @@ public class K2JSCompiler extends CLICompiler<K2JSCompilerArguments> {
configuration.put(CommonConfigurationKeys.MODULE_NAME, FileUtil.getNameWithoutExtension(outputFile)); configuration.put(CommonConfigurationKeys.MODULE_NAME, FileUtil.getNameWithoutExtension(outputFile));
JsConfig config = new JsConfig(project, configuration); JsConfig config = new JsConfig(project, configuration);
if (config.checkLibFilesAndReportErrors(new JsConfig.Reporter() { JsConfig.Reporter reporter = new JsConfig.Reporter() {
@Override @Override
public void error(@NotNull String message) { public void error(@NotNull String message) {
messageCollector.report(ERROR, message, null); messageCollector.report(ERROR, message, null);
@@ -145,7 +145,8 @@ public class K2JSCompiler extends CLICompiler<K2JSCompilerArguments> {
public void warning(@NotNull String message) { public void warning(@NotNull String message) {
messageCollector.report(STRONG_WARNING, message, null); messageCollector.report(STRONG_WARNING, message, null);
} }
})) { };
if (config.checkLibFilesAndReportErrors(reporter)) {
return COMPILATION_ERROR; return COMPILATION_ERROR;
} }
@@ -184,7 +185,7 @@ public class K2JSCompiler extends CLICompiler<K2JSCompilerArguments> {
K2JSTranslator translator = new K2JSTranslator(config); K2JSTranslator translator = new K2JSTranslator(config);
try { try {
//noinspection unchecked //noinspection unchecked
translationResult = translator.translate(sourcesFiles, mainCallParameters, jsAnalysisResult); translationResult = translator.translate(reporter, sourcesFiles, mainCallParameters, jsAnalysisResult);
} }
catch (Exception e) { catch (Exception e) {
throw ExceptionUtilsKt.rethrow(e); throw ExceptionUtilsKt.rethrow(e);
@@ -20,6 +20,7 @@ import org.jetbrains.kotlin.config.LanguageVersionSettings
import org.jetbrains.kotlin.context.ModuleContext import org.jetbrains.kotlin.context.ModuleContext
import org.jetbrains.kotlin.diagnostics.DiagnosticUtils.hasError import org.jetbrains.kotlin.diagnostics.DiagnosticUtils.hasError
import org.jetbrains.kotlin.js.analyzer.JsAnalysisResult 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.K2JSTranslator
import org.jetbrains.kotlin.js.facade.MainCallParameters import org.jetbrains.kotlin.js.facade.MainCallParameters
import org.jetbrains.kotlin.psi.KtFile import org.jetbrains.kotlin.psi.KtFile
@@ -38,7 +39,7 @@ abstract class AbstractDiagnosticsTestWithJsStdLibAndBackendCompilation : Abstra
if (!hasError(diagnostics)) { if (!hasError(diagnostics)) {
val translator = K2JSTranslator(config) val translator = K2JSTranslator(config)
translator.translate(files, MainCallParameters.noCall(), analysisResult) translator.translate(object : JsConfig.Reporter() {}, files, MainCallParameters.noCall(), analysisResult)
} }
return analysisResult return analysisResult
@@ -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_PATTERN = ("($JS_IDENTIFIER)\\.defineModule\\(\\s*(['\"])([^'\"]+)\\2\\s*,\\s*(\\w+)\\s*\\)").toRegex().toPattern()
private val DEFINE_MODULE_FIND_PATTERN = ".defineModule(" private val DEFINE_MODULE_FIND_PATTERN = ".defineModule("
class FunctionReader(private val config: JsConfig, private val currentModuleName: JsName, fragments: List<JsProgramFragment>) { class FunctionReader(
private val reporter: JsConfig.Reporter,
private val config: JsConfig,
private val currentModuleName: JsName,
fragments: List<JsProgramFragment>
) {
/** /**
* fileContent: .js file content, that contains this module definition. * fileContent: .js file content, that contains this module definition.
* One file can contain more than one 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)) val result = SourceMapParser.parse(StringReader(it))
when (result) { when (result) {
is SourceMapSuccess -> result.value 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
}
} }
} }
@@ -63,6 +63,7 @@ public class JsInliner extends JsVisitorWithContextImpl {
node -> node instanceof JsInvocation && hasToBeInlined((JsInvocation) node); node -> node instanceof JsInvocation && hasToBeInlined((JsInvocation) node);
public static void process( public static void process(
@NotNull JsConfig.Reporter reporter,
@NotNull JsConfig config, @NotNull JsConfig config,
@NotNull DiagnosticSink trace, @NotNull DiagnosticSink trace,
@NotNull JsName currentModuleName, @NotNull JsName currentModuleName,
@@ -76,7 +77,7 @@ public class JsInliner extends JsVisitorWithContextImpl {
accessorInvocationTransformer.accept(fragment.getDeclarationBlock()); accessorInvocationTransformer.accept(fragment.getDeclarationBlock());
accessorInvocationTransformer.accept(fragment.getInitializerBlock()); 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); JsInliner inliner = new JsInliner(config, functions, accessors, functionReader, trace);
for (JsProgramFragment fragment : fragmentsToProcess) { for (JsProgramFragment fragment : fragmentsToProcess) {
inliner.inliningContexts.push(inliner.new JsInliningContext()); inliner.inliningContexts.push(inliner.new JsInliningContext());
@@ -35,6 +35,7 @@ import org.jetbrains.kotlin.js.facade.K2JSTranslator
import org.jetbrains.kotlin.js.facade.MainCallParameters import org.jetbrains.kotlin.js.facade.MainCallParameters
import org.jetbrains.kotlin.js.facade.TranslationResult import org.jetbrains.kotlin.js.facade.TranslationResult
import org.jetbrains.kotlin.js.facade.TranslationUnit 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.LineCollector
import org.jetbrains.kotlin.js.test.utils.LineOutputToStringVisitor import org.jetbrains.kotlin.js.test.utils.LineOutputToStringVisitor
import org.jetbrains.kotlin.js.util.TextOutputImpl import org.jetbrains.kotlin.js.util.TextOutputImpl
@@ -69,7 +70,7 @@ abstract class AbstractJsLineNumberTest : KotlinTestWithEnvironment() {
val translator = K2JSTranslator(createConfig(module, file, modules)) val translator = K2JSTranslator(createConfig(module, file, modules))
val units = module.files.map { TranslationUnit.SourceFile(createPsiFile(it.fileName)) } 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) { if (translationResult !is TranslationResult.Success) {
val outputStream = ByteArrayOutputStream() val outputStream = ByteArrayOutputStream()
@@ -331,7 +331,7 @@ abstract class BasicBoxTest(
outputPostfixFile: File?, outputPostfixFile: File?,
mainCallParameters: MainCallParameters) { mainCallParameters: MainCallParameters) {
val translator = K2JSTranslator(config) val translator = K2JSTranslator(config)
val translationResult = translator.translateUnits(units, mainCallParameters) val translationResult = translator.translateUnits(ExceptionThrowingReporter, units, mainCallParameters)
if (translationResult !is TranslationResult.Success) { if (translationResult !is TranslationResult.Success) {
val outputStream = ByteArrayOutputStream() val outputStream = ByteArrayOutputStream()
@@ -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")
}
}
@@ -66,40 +66,44 @@ public final class K2JSTranslator {
@NotNull @NotNull
public TranslationResult translate( public TranslationResult translate(
@NotNull JsConfig.Reporter reporter,
@NotNull List<KtFile> files, @NotNull List<KtFile> files,
@NotNull MainCallParameters mainCallParameters @NotNull MainCallParameters mainCallParameters
) throws TranslationException { ) throws TranslationException {
return translate(files, mainCallParameters, null); return translate(reporter, files, mainCallParameters, null);
} }
@NotNull @NotNull
public TranslationResult translate( public TranslationResult translate(
@NotNull JsConfig.Reporter reporter,
@NotNull List<KtFile> files, @NotNull List<KtFile> files,
@NotNull MainCallParameters mainCallParameters, @NotNull MainCallParameters mainCallParameters,
@Nullable JsAnalysisResult analysisResult @Nullable JsAnalysisResult analysisResult
) throws TranslationException { ) throws TranslationException {
List<TranslationUnit> units = new ArrayList<TranslationUnit>(); List<TranslationUnit> units = new ArrayList<>();
for (KtFile file : files) { for (KtFile file : files) {
units.add(new TranslationUnit.SourceFile(file)); units.add(new TranslationUnit.SourceFile(file));
} }
return translateUnits(units, mainCallParameters, analysisResult); return translateUnits(reporter, units, mainCallParameters, analysisResult);
} }
@NotNull @NotNull
public TranslationResult translateUnits( public TranslationResult translateUnits(
@NotNull JsConfig.Reporter reporter,
@NotNull List<TranslationUnit> units, @NotNull List<TranslationUnit> units,
@NotNull MainCallParameters mainCallParameters @NotNull MainCallParameters mainCallParameters
) throws TranslationException { ) throws TranslationException {
return translateUnits(units, mainCallParameters, null); return translateUnits(reporter, units, mainCallParameters, null);
} }
@NotNull @NotNull
public TranslationResult translateUnits( public TranslationResult translateUnits(
@NotNull JsConfig.Reporter reporter,
@NotNull List<TranslationUnit> units, @NotNull List<TranslationUnit> units,
@NotNull MainCallParameters mainCallParameters, @NotNull MainCallParameters mainCallParameters,
@Nullable JsAnalysisResult analysisResult @Nullable JsAnalysisResult analysisResult
) throws TranslationException { ) throws TranslationException {
List<KtFile> files = new ArrayList<KtFile>(); List<KtFile> files = new ArrayList<>();
for (TranslationUnit unit : units) { for (TranslationUnit unit : units) {
if (unit instanceof TranslationUnit.SourceFile) { if (unit instanceof TranslationUnit.SourceFile) {
files.add(((TranslationUnit.SourceFile) unit).getFile()); files.add(((TranslationUnit.SourceFile) unit).getFile());
@@ -120,10 +124,11 @@ public final class K2JSTranslator {
ProgressIndicatorAndCompilationCanceledStatus.checkCanceled(); ProgressIndicatorAndCompilationCanceledStatus.checkCanceled();
if (hasError(diagnostics)) return new TranslationResult.Fail(diagnostics); if (hasError(diagnostics)) return new TranslationResult.Fail(diagnostics);
List<JsProgramFragment> newFragments = new ArrayList<JsProgramFragment>(translationResult.getNewFragments()); List<JsProgramFragment> newFragments = new ArrayList<>(translationResult.getNewFragments());
List<JsProgramFragment> allFragments = new ArrayList<JsProgramFragment>(translationResult.getFragments()); List<JsProgramFragment> 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); LabeledBlockToDoWhileTransformation.INSTANCE.apply(newFragments);
@@ -139,7 +144,7 @@ public final class K2JSTranslator {
ExpandIsCallsKt.expandIsCalls(newFragments); ExpandIsCallsKt.expandIsCalls(newFragments);
ProgressIndicatorAndCompilationCanceledStatus.checkCanceled(); ProgressIndicatorAndCompilationCanceledStatus.checkCanceled();
Map<KtFile, FileTranslationResult> fileMap = new HashMap<KtFile, FileTranslationResult>(); Map<KtFile, FileTranslationResult> fileMap = new HashMap<>();
JsAstSerializer serializer = new JsAstSerializer(); JsAstSerializer serializer = new JsAstSerializer();
byte[] metadataHeader = null; byte[] metadataHeader = null;
boolean serializeFragments = config.getConfiguration().get(JSConfigurationKeys.SERIALIZE_FRAGMENTS, false); boolean serializeFragments = config.getConfiguration().get(JSConfigurationKeys.SERIALIZE_FRAGMENTS, false);