Search in sources :

Example 1 with SourceMapConsumerV3

use of com.google.debugging.sourcemap.SourceMapConsumerV3 in project closure-compiler by google.

the class SourceMapInput method getSourceMap.

/**
 * Gets the source map, reading from disk and parsing if necessary. Returns null if the sourcemap
 * cannot be resolved or is malformed.
 */
@Nullable
public synchronized SourceMapConsumerV3 getSourceMap(ErrorManager errorManager) {
    if (!cached) {
        // Avoid re-reading or reparsing files.
        cached = true;
        String sourceMapPath = sourceFile.getOriginalPath();
        try {
            String sourceMapContents = sourceFile.getCode();
            SourceMapConsumerV3 consumer = new SourceMapConsumerV3();
            consumer.parse(sourceMapContents);
            parsedSourceMap = consumer;
        } catch (IOException e) {
            JSError error = JSError.make(SourceMapInput.SOURCEMAP_RESOLVE_FAILED, sourceMapPath, e.getMessage());
            errorManager.report(error.getDefaultLevel(), error);
        } catch (SourceMapParseException e) {
            JSError error = JSError.make(SourceMapInput.SOURCEMAP_PARSE_FAILED, sourceMapPath, e.getMessage());
            errorManager.report(error.getDefaultLevel(), error);
        }
    }
    return parsedSourceMap;
}
Also used : SourceMapParseException(com.google.debugging.sourcemap.SourceMapParseException) SourceMapConsumerV3(com.google.debugging.sourcemap.SourceMapConsumerV3) IOException(java.io.IOException) Nullable(javax.annotation.Nullable)

Example 2 with SourceMapConsumerV3

use of com.google.debugging.sourcemap.SourceMapConsumerV3 in project closure-compiler by google.

the class CompilerTest method testResolveRelativeSourceMap.

public void testResolveRelativeSourceMap() throws Exception {
    Compiler compiler = new Compiler();
    compiler.initCompilerOptionsIfTesting();
    File tempDir = Files.createTempDir();
    String code = SOURCE_MAP_TEST_CODE + "\n//# sourceMappingURL=foo.js.map";
    File jsFile = new File(tempDir, "foo.js");
    Files.asCharSink(jsFile, UTF_8).write(code);
    File sourceMapFile = new File(tempDir, "foo.js.map");
    Files.asCharSink(sourceMapFile, UTF_8).write(SOURCE_MAP);
    CompilerInput input = new CompilerInput(SourceFile.fromFile(jsFile.getAbsolutePath()));
    input.getAstRoot(compiler);
    // The source map is still
    assertThat(compiler.inputSourceMaps).hasSize(1);
    for (SourceMapInput inputSourceMap : compiler.inputSourceMaps.values()) {
        SourceMapConsumerV3 sourceMap = inputSourceMap.getSourceMap(null);
        assertThat(sourceMap.getOriginalSources()).containsExactly("foo.ts");
    }
}
Also used : SourceMapConsumerV3(com.google.debugging.sourcemap.SourceMapConsumerV3) File(java.io.File)

Example 3 with SourceMapConsumerV3

use of com.google.debugging.sourcemap.SourceMapConsumerV3 in project closure-compiler by google.

the class CompilerTest method testMissingSourceMapFile.

public void testMissingSourceMapFile() throws Exception {
    Compiler compiler = new Compiler();
    compiler.initCompilerOptionsIfTesting();
    File tempDir = Files.createTempDir();
    String code = SOURCE_MAP_TEST_CODE + "\n//# sourceMappingURL=foo-does-not-exist.js.map";
    File jsFile = new File(tempDir, "foo2.js");
    Files.asCharSink(jsFile, UTF_8).write(code);
    CompilerInput input = new CompilerInput(SourceFile.fromFile(jsFile.getAbsolutePath()));
    input.getAstRoot(compiler);
    assertThat(compiler.inputSourceMaps).hasSize(1);
    TestErrorManager errorManager = new TestErrorManager();
    for (SourceMapInput inputSourceMap : compiler.inputSourceMaps.values()) {
        SourceMapConsumerV3 sourceMap = inputSourceMap.getSourceMap(errorManager);
        assertNull(sourceMap);
    }
    // WARNING: Failed to resolve input sourcemap: foo-does-not-exist.js.map
    assertEquals(1, errorManager.getWarningCount());
}
Also used : SourceMapConsumerV3(com.google.debugging.sourcemap.SourceMapConsumerV3) File(java.io.File)

Example 4 with SourceMapConsumerV3

use of com.google.debugging.sourcemap.SourceMapConsumerV3 in project closure-compiler by google.

the class CompilerTest method testInputSourceMapInline.

public void testInputSourceMapInline() throws Exception {
    Compiler compiler = new Compiler();
    compiler.initCompilerOptionsIfTesting();
    String code = SOURCE_MAP_TEST_CODE + "\n//# sourceMappingURL=" + BASE64_ENCODED_SOURCE_MAP;
    CompilerInput input = new CompilerInput(SourceFile.fromCode("tmp", code));
    input.getAstRoot(compiler);
    SourceMapInput inputSourceMap = compiler.inputSourceMaps.get("tmp");
    SourceMapConsumerV3 sourceMap = inputSourceMap.getSourceMap(null);
    assertThat(sourceMap.getOriginalSources()).containsExactly("foo.ts");
}
Also used : SourceMapConsumerV3(com.google.debugging.sourcemap.SourceMapConsumerV3)

Example 5 with SourceMapConsumerV3

use of com.google.debugging.sourcemap.SourceMapConsumerV3 in project closure-compiler by google.

the class Compiler method getSourceMapping.

@Override
@Nullable
public OriginalMapping getSourceMapping(String sourceName, int lineNumber, int columnNumber) {
    if (sourceName == null) {
        return null;
    }
    SourceMapInput sourceMap = inputSourceMaps.get(sourceName);
    if (sourceMap == null) {
        return null;
    }
    // JSCompiler uses 1-indexing for lineNumber and 0-indexing for columnNumber.
    // Sourcemaps use 1-indexing for both.
    SourceMapConsumerV3 consumer = sourceMap.getSourceMap(errorManager);
    if (consumer == null) {
        return null;
    }
    OriginalMapping result = consumer.getMappingForLine(lineNumber, columnNumber + 1);
    if (result == null) {
        return null;
    }
    // The sourcemap will return a path relative to the sourcemap's file.
    // Translate it to one relative to our base directory.
    SourceFile source = SourceMapResolver.getRelativePath(sourceMap.getOriginalPath(), result.getOriginalFile());
    if (source == null) {
        return null;
    }
    String originalPath = source.getOriginalPath();
    sourceMapOriginalSources.putIfAbsent(originalPath, source);
    return result.toBuilder().setOriginalFile(originalPath).setColumnPosition(result.getColumnPosition() - 1).build();
}
Also used : OriginalMapping(com.google.debugging.sourcemap.proto.Mapping.OriginalMapping) SourceMapConsumerV3(com.google.debugging.sourcemap.SourceMapConsumerV3) Nullable(javax.annotation.Nullable)

Aggregations

SourceMapConsumerV3 (com.google.debugging.sourcemap.SourceMapConsumerV3)8 File (java.io.File)3 OriginalMapping (com.google.debugging.sourcemap.proto.Mapping.OriginalMapping)2 Nullable (javax.annotation.Nullable)2 FilePosition (com.google.debugging.sourcemap.FilePosition)1 SourceMapParseException (com.google.debugging.sourcemap.SourceMapParseException)1 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1