Skip to content

Commit

Permalink
Fix source map generation in BaseTranspiler.
Browse files Browse the repository at this point in the history
There's some spooky action going on in Compiler#toSource that populates the source map that had already been returned from compile().  So we need to call toSource() before SourceMap#appendTo.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=141129216
  • Loading branch information
shicks authored and blickly committed Dec 6, 2016
1 parent 0c4173c commit 72cd039
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
Expand Up @@ -88,6 +88,7 @@ public static class CompilerSupplier {
public CompileResult compile(String path, String code) {
Compiler compiler = compiler();
Result result = compiler.compile(EXTERNS, SourceFile.fromCode(path, code), options());
String source = compiler.toSource();
StringBuilder sourceMap = new StringBuilder();
if (result.sourceMap != null) {
try {
Expand All @@ -96,11 +97,12 @@ public CompileResult compile(String path, String code) {
// impossible, and not a big deal even if it did happen.
}
}
boolean transpiled = !result.transpiledFiles.isEmpty();
return new CompileResult(
compiler.toSource(),
source,
result.errors,
!result.transpiledFiles.isEmpty(),
sourceMap.toString());
transpiled,
transpiled ? sourceMap.toString() : "");
}

public String runtime(String library) {
Expand Down
Expand Up @@ -70,15 +70,16 @@ public void testCompilerSupplier_compileChanged() {
assertThat(result.source).isEqualTo("var x = function() {\n return 42;\n};\n");
assertThat(result.errors).isEmpty();
assertThat(result.transpiled).isTrue();
assertThat(result.sourceMap).contains("\"mappings\":\";\"");
assertThat(result.sourceMap)
.contains("\"mappings\":\"AAAA,IAAMA,IAAIA,QAAA,EAAM;AAAA,SAAA,EAAA;AAAA,CAAhB;;\"");
}

public void testCompilerSupplier_compileNoChange() {
BaseTranspiler.CompileResult result = compiler.compile("source.js", "var x = 42;");
assertThat(result.source).isEqualTo("var x = 42;\n");
assertThat(result.errors).isEmpty();
assertThat(result.transpiled).isFalse();
assertThat(result.sourceMap).contains("\"mappings\":\";\"");
assertThat(result.sourceMap).isEmpty();
}

public void testCompilerSupplier_runtime() {
Expand Down

0 comments on commit 72cd039

Please sign in to comment.