Skip to content

Commit 89603e3

Browse files
rahul-kamatcopybara-github
authored andcommitted
Stop serializing inline source maps (base64-encoded "data urls") in TypedAST
There are 2 kinds of source maps. #### 1) Input source map A source map for a file passed as an input to JSCompiler. This can be passed in one of two ways: 1) the `--source_map_input_files` flag 2) a special comment embedded into the input JS file: `//# sourceMappingURL=<url>` Where the "<url>" points to the source map content. #### 2) Inline source map An input source map passed by embedding a sourceMappingURL as a base64-encoded "data url". We want TypedAST to support input source maps and not inline source maps. This change stops serializing inline source map URLs in TypedAST. PiperOrigin-RevId: 500744410
1 parent 273f1d6 commit 89603e3

File tree

5 files changed

+47
-11
lines changed

5 files changed

+47
-11
lines changed

src/com/google/javascript/jscomp/AbstractCompiler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ public boolean hasColorAndSimplifiedJSDoc() {
519519
*/
520520
public abstract void addInputSourceMap(String name, SourceMapInput sourceMap);
521521

522-
public abstract SourceMapInput getInputSourceMap(String sourceFileName);
522+
public abstract String getInputSourceMappingURL(String sourceFileName);
523523

524524
abstract void addComments(String filename, List<Comment> comments);
525525

src/com/google/javascript/jscomp/Compiler.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3269,9 +3269,23 @@ public void addInputSourceMap(String sourceFileName, SourceMapInput inputSourceM
32693269
}
32703270
}
32713271

3272+
/**
3273+
* Returns the <url> from a `//# sourceMappingURL=<url>` comment. This sourceMappingURL is a JS
3274+
* file's input source map, embedded into the input JS file with a `//# sourceMappingURL=<url>`
3275+
* comment.
3276+
*
3277+
* <p>We do not want TypedAST to support inline source maps (which are input source maps passed by
3278+
* embedding a `//# sourceMappingURL=<url>` where <url> is a base64-encoded "data url"). If the
3279+
* sourceMappingURL ends with a ".inline.map", return null.
3280+
*/
32723281
@Override
3273-
public @Nullable SourceMapInput getInputSourceMap(String sourceFileName) {
3274-
return inputSourceMaps.getOrDefault(sourceFileName, null);
3282+
public @Nullable String getInputSourceMappingURL(String sourceFileName) {
3283+
SourceMapInput sourceMapInput = inputSourceMaps.getOrDefault(sourceFileName, null);
3284+
String sourceMappingURL = sourceMapInput != null ? sourceMapInput.getOriginalPath() : null;
3285+
if (sourceMappingURL != null && !sourceMappingURL.endsWith(".inline.map")) {
3286+
return sourceMappingURL;
3287+
}
3288+
return null;
32753289
}
32763290

32773291
/**

src/com/google/javascript/jscomp/serialization/TypedAstSerializer.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import com.google.javascript.jscomp.NodeTraversal;
2929
import com.google.javascript.jscomp.NodeUtil;
3030
import com.google.javascript.jscomp.SourceFile;
31-
import com.google.javascript.jscomp.SourceMapInput;
3231
import com.google.javascript.jscomp.colors.Color;
3332
import com.google.javascript.jscomp.colors.ColorId;
3433
import com.google.javascript.jscomp.colors.ColorRegistry;
@@ -119,8 +118,7 @@ private LazyAst serializeScriptNode(Node script) {
119118
AstNode scriptProto = visit(script);
120119
this.subtreeSourceFiles.clear();
121120

122-
SourceMapInput sourceMapInput = compiler.getInputSourceMap(script.getSourceFileName());
123-
String sourceMappingURL = sourceMapInput != null ? sourceMapInput.getOriginalPath() : null;
121+
String sourceMappingURL = compiler.getInputSourceMappingURL(script.getSourceFileName());
124122

125123
LazyAst.Builder lazyAstBuilder =
126124
LazyAst.newBuilder().setScript(scriptProto.toByteString()).setSourceFile(sourceFile);

test/com/google/javascript/jscomp/serialization/SerializeAndDeserializeAstTest.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -419,8 +419,7 @@ public void testSourceMaps() {
419419
String code = sourceMapTestCode + "\n" + sourceMappingURLComment;
420420

421421
Result result = testAndReturnResult(srcs(code), expected(code));
422-
assertThat(result.compiler.getInputSourceMap("testcode").getOriginalPath())
423-
.isEqualTo(sourceMapPath);
422+
assertThat(result.compiler.getInputSourceMappingURL("testcode")).isEqualTo(sourceMapPath);
424423
}
425424

426425
@Test
@@ -440,7 +439,7 @@ public void testSourceMapsWithoutResolvingSourceMapAnnotations() {
440439

441440
Result result = testAndReturnResult(srcs(code), expected(code));
442441
// Input source map not registered because `resolveSourceMapAnnotations = false`
443-
assertThat(result.compiler.getInputSourceMap("testcode")).isNull();
442+
assertThat(result.compiler.getInputSourceMappingURL("testcode")).isNull();
444443
}
445444

446445
@Test
@@ -461,8 +460,7 @@ public void testSourceMapsWithoutParsingInlineSourceMaps() {
461460
Result result = testAndReturnResult(srcs(code), expected(code));
462461
// Input source map is registered when `parseInlineSourceMaps = false`, but we won't try to
463462
// parse it as a Base64 encoded source map.
464-
assertThat(result.compiler.getInputSourceMap("testcode").getOriginalPath())
465-
.isEqualTo(sourceMapPath);
463+
assertThat(result.compiler.getInputSourceMappingURL("testcode")).isEqualTo(sourceMapPath);
466464
}
467465

468466
@Test

test/com/google/javascript/jscomp/serialization/SerializeTypedAstPassTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,32 @@ public void serializesSourceMappingURL() throws InvalidProtocolBufferException {
445445
assertThat(sourceMappingURL).isEqualTo("relativedir/foo.js.map");
446446
}
447447

448+
@Test
449+
public void doesNotSerializeInlineSourceMappingURL() throws InvalidProtocolBufferException {
450+
// We do not want TypedAST to support inline source maps (which are input source maps passed
451+
// by embedding a `//# sourceMappingURL=<url>` where <url> is a base64-encoded "data url").
452+
String sourceMapTestCode =
453+
lines(
454+
"var X = (function () {",
455+
" function X(input) {",
456+
" this.y = input;",
457+
" }",
458+
" return X;",
459+
"}());");
460+
461+
String base64EncodedSourceMappingURL =
462+
"data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZm9vLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdGVzdC9mb28udHMiXSwic291cmNlc0NvbnRlbnQiOlsidmFyIEEgPSAoZnVuY3Rpb24gKCkge1xuICAgIGZ1bmN0aW9uIEEoaW5wdXQpIHtcbiAgICAgICAgdGhpcy5hID0gaW5wdXQ7XG4gICAgfVxuICAgIHJldHVybiBBO1xufSgpKTtcbmNvbnNvbGUubG9nKG5ldyBBKDEpKTsiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUE7SUFHRSxXQUFZLEtBQWE7UUFDdkIsSUFBSSxDQUFDLENBQUMsR0FBRyxLQUFLLENBQUM7SUFDakIsQ0FBQztJQUNILFFBQUM7QUFBRCxDQUFDLEFBTkQsSUFNQztBQUVELE9BQU8sQ0FBQyxHQUFHLENBQUMsSUFBSSxDQUFDLENBQUMsQ0FBQyxDQUFDLENBQUMsQ0FBQyJ9";
463+
464+
String code = sourceMapTestCode + "\n//# sourceMappingURL=" + base64EncodedSourceMappingURL;
465+
466+
SerializationResult result = compile(code);
467+
468+
LazyAst lazyAst = result.ast.getCodeAstList().get(0);
469+
String sourceMappingURL = lazyAst.getSourceMappingUrl();
470+
471+
assertThat(sourceMappingURL).isEmpty(); // Do not serizile this base-64 sourceMappingURL.
472+
}
473+
448474
private AstNode compileToAstNode(String source) {
449475
return compile(source).sourceNodes.get(0);
450476
}

0 commit comments

Comments
 (0)