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;
}
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");
}
}
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());
}
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");
}
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();
}
Aggregations