Skip to content

Commit

Permalink
Automated g4 rollback of changelist 195478973.
Browse files Browse the repository at this point in the history
*** Reason for rollback ***

When the API changed from Path to URI calling code used path.toURI to convert. However this was a behavioral change that ends up including the file:// scheme. In the roll forward the equivalent code is actually new URI(path.toString()).

*** Original change description ***

Allow the transpiler to take URIs so that URLs may be used. Useful for servers that output sourcemaps to ensure the sourcemap points to a server file and not one on disc.

***

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=195713404
  • Loading branch information
johnplaisted authored and tjgq committed May 8, 2018
1 parent 1a9c860 commit 5d60d64
Show file tree
Hide file tree
Showing 15 changed files with 126 additions and 217 deletions.
2 changes: 1 addition & 1 deletion src/com/google/javascript/jscomp/CommandLineRunner.java
Expand Up @@ -1071,7 +1071,7 @@ List<SourceMap.LocationMapping> getSourceMapLocationMappings() throws CmdLineExc
ImmutableMap<String, String> split = splitPipeParts( ImmutableMap<String, String> split = splitPipeParts(
sourceMapLocationMapping, "--source_map_location_mapping"); sourceMapLocationMapping, "--source_map_location_mapping");
for (Map.Entry<String, String> mapping : split.entrySet()) { for (Map.Entry<String, String> mapping : split.entrySet()) {
locationMappings.add(new SourceMap.PrefixLocationMapping(mapping.getKey(), locationMappings.add(new SourceMap.LocationMapping(mapping.getKey(),
mapping.getValue())); mapping.getValue()));
} }


Expand Down
4 changes: 2 additions & 2 deletions src/com/google/javascript/jscomp/CompilerOptions.java
Expand Up @@ -1089,7 +1089,7 @@ public void setTracerOutput(PrintStream out) {
*/ */
boolean resolveSourceMapAnnotations = true; boolean resolveSourceMapAnnotations = true;


public List<? extends SourceMap.LocationMapping> sourceMapLocationMappings = public List<SourceMap.LocationMapping> sourceMapLocationMappings =
Collections.emptyList(); Collections.emptyList();


/** /**
Expand Down Expand Up @@ -2665,7 +2665,7 @@ public void setSourceMapFormat(SourceMap.Format sourceMapFormat) {
} }


public void setSourceMapLocationMappings( public void setSourceMapLocationMappings(
List<? extends SourceMap.LocationMapping> sourceMapLocationMappings) { List<SourceMap.LocationMapping> sourceMapLocationMappings) {
this.sourceMapLocationMappings = sourceMapLocationMappings; this.sourceMapLocationMappings = sourceMapLocationMappings;
} }


Expand Down
43 changes: 12 additions & 31 deletions src/com/google/javascript/jscomp/SourceMap.java
Expand Up @@ -95,47 +95,27 @@ public static enum DetailLevel implements Predicate<Node> {
} }


/** /**
* Function that mape a "destination" location to use within the source map. Should return null * A simple pair of path prefixes to the desired "destination" location to use within the source
* if the value is not mapped. * map.
*/ */
@FunctionalInterface public static final class LocationMapping {
public interface LocationMapping {
/**
* @param location the location to transform
* @return the transformed location or null if not transformed
*/
@Nullable
String map(String location);
}

/**
* Simple {@link LocationMapping} that strips a prefix from a location.
*/
public static final class PrefixLocationMapping implements LocationMapping {
final String prefix; final String prefix;
final String replacement; final String replacement;
public PrefixLocationMapping(String prefix, String replacement) { public LocationMapping(String prefix, String replacement) {
this.prefix = prefix; this.prefix = prefix;
this.replacement = replacement; this.replacement = replacement;
} }


@Override @Override
public String map(String location) {
if (location.startsWith(prefix)) {
return replacement + location.substring(prefix.length());
}
return null;
}

public String toString() { public String toString() {
return "(" + prefix + "|" + replacement + ")"; return "(" + prefix + "|" + replacement + ")";
} }


@Override @Override
public boolean equals(Object other) { public boolean equals(Object other) {
if (other instanceof PrefixLocationMapping) { if (other instanceof LocationMapping) {
return ((PrefixLocationMapping) other).prefix.equals(prefix) return ((LocationMapping) other).prefix.equals(prefix)
&& ((PrefixLocationMapping) other).replacement.equals(replacement); && ((LocationMapping) other).replacement.equals(replacement);
} else { } else {
return false; return false;
} }
Expand All @@ -148,7 +128,7 @@ public int hashCode() {
} }


private final SourceMapGenerator generator; private final SourceMapGenerator generator;
private List<? extends LocationMapping> prefixMappings = Collections.emptyList(); private List<LocationMapping> prefixMappings = Collections.emptyList();
private final Map<String, String> sourceLocationFixupCache = private final Map<String, String> sourceLocationFixupCache =
new HashMap<>(); new HashMap<>();
/** /**
Expand Down Expand Up @@ -226,8 +206,9 @@ private String fixupSourceLocation(String sourceFile) {


// Replace the first prefix found with its replacement // Replace the first prefix found with its replacement
for (LocationMapping mapping : prefixMappings) { for (LocationMapping mapping : prefixMappings) {
fixed = mapping.map(sourceFile); if (sourceFile.startsWith(mapping.prefix)) {
if (fixed != null) { fixed = mapping.replacement + sourceFile.substring(
mapping.prefix.length());
break; break;
} }
} }
Expand Down Expand Up @@ -265,7 +246,7 @@ public void validate(boolean validate) {
/** /**
* @param sourceMapLocationMappings * @param sourceMapLocationMappings
*/ */
public void setPrefixMappings(List<? extends LocationMapping> sourceMapLocationMappings) { public void setPrefixMappings(List<LocationMapping> sourceMapLocationMappings) {
this.prefixMappings = sourceMapLocationMappings; this.prefixMappings = sourceMapLocationMappings;
} }


Expand Down
4 changes: 2 additions & 2 deletions src/com/google/javascript/jscomp/ant/CompileTask.java
Expand Up @@ -32,7 +32,7 @@
import com.google.javascript.jscomp.SourceFile; import com.google.javascript.jscomp.SourceFile;
import com.google.javascript.jscomp.SourceMap; import com.google.javascript.jscomp.SourceMap;
import com.google.javascript.jscomp.SourceMap.Format; import com.google.javascript.jscomp.SourceMap.Format;
import com.google.javascript.jscomp.SourceMap.PrefixLocationMapping; import com.google.javascript.jscomp.SourceMap.LocationMapping;
import com.google.javascript.jscomp.WarningLevel; import com.google.javascript.jscomp.WarningLevel;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
Expand Down Expand Up @@ -476,7 +476,7 @@ private CompilerOptions createCompilerOptions() {


if (!Strings.isNullOrEmpty(sourceMapLocationMapping)) { if (!Strings.isNullOrEmpty(sourceMapLocationMapping)) {
String[] tokens = sourceMapLocationMapping.split("\\|", -1); String[] tokens = sourceMapLocationMapping.split("\\|", -1);
PrefixLocationMapping lm = new PrefixLocationMapping(tokens[0], tokens[1]); LocationMapping lm = new LocationMapping(tokens[0], tokens[1]);
options.setSourceMapLocationMappings(Arrays.asList(lm)); options.setSourceMapLocationMappings(Arrays.asList(lm));
} }


Expand Down
10 changes: 2 additions & 8 deletions src/com/google/javascript/jscomp/deps/ClosureBundler.java
Expand Up @@ -23,9 +23,8 @@
import com.google.javascript.jscomp.transpile.Transpiler; import com.google.javascript.jscomp.transpile.Transpiler;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.file.Paths;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;


Expand Down Expand Up @@ -136,12 +135,7 @@ public String getSourceMap(String path) {
} }


private String transpile(String s, Transpiler t) { private String transpile(String s, Transpiler t) {
TranspileResult result; TranspileResult result = t.transpile(Paths.get(path), s);
try {
result = t.transpile(new URI(path), s);
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
sourceMapCache.put(path, result.sourceMap()); sourceMapCache.put(path, result.sourceMap());
return result.transpiled(); return result.transpiled();
} }
Expand Down
23 changes: 6 additions & 17 deletions src/com/google/javascript/jscomp/transpile/BaseTranspiler.java
Expand Up @@ -35,8 +35,7 @@
import com.google.javascript.jscomp.bundle.TranspilationException; import com.google.javascript.jscomp.bundle.TranspilationException;
import com.google.javascript.rhino.Node; import com.google.javascript.rhino.Node;
import java.io.IOException; import java.io.IOException;
import java.net.URI; import java.nio.file.Path;
import java.net.URISyntaxException;


/** /**
* Basic Transpiler implementation for outputting ES5 code. * Basic Transpiler implementation for outputting ES5 code.
Expand All @@ -52,7 +51,7 @@ public final class BaseTranspiler implements Transpiler {
} }


@Override @Override
public TranspileResult transpile(URI path, String code) { public TranspileResult transpile(Path path, String code) {
CompileResult result = compilerSupplier.compile(path, code); CompileResult result = compilerSupplier.compile(path, code);
if (!result.transpiled) { if (!result.transpiled) {
return new TranspileResult(path, code, code, ""); return new TranspileResult(path, code, code, "");
Expand Down Expand Up @@ -85,7 +84,7 @@ public String runtime() {
* time when we're in single-file mode. * time when we're in single-file mode.
*/ */
public static class CompilerSupplier { public static class CompilerSupplier {
public CompileResult compile(URI path, String code) { public CompileResult compile(Path path, String code) {
Compiler compiler = compiler(); Compiler compiler = compiler();
Result result = Result result =
compiler.compile(EXTERNS, SourceFile.fromCode(path.toString(), code), options()); compiler.compile(EXTERNS, SourceFile.fromCode(path.toString(), code), options());
Expand Down Expand Up @@ -150,19 +149,9 @@ protected void setOptions(CompilerOptions options) {
options.setSourceMapOutputPath("/dev/null"); options.setSourceMapOutputPath("/dev/null");
options.setSourceMapIncludeSourcesContent(true); options.setSourceMapIncludeSourcesContent(true);
// Make sourcemaps use absolute paths, so that the path is not duplicated if a build tool adds // Make sourcemaps use absolute paths, so that the path is not duplicated if a build tool adds
// a sourceurl. Exception: if the location has a scheme (like http:) then leave the path // a sourceurl.
// intact. This makes this usable from web servers.
options.setSourceMapLocationMappings( options.setSourceMapLocationMappings(
ImmutableList.of((location) -> { ImmutableList.of(new SourceMap.LocationMapping("", "/")));
try {
if (new URI(location).getScheme() != null) {
return location;
}
} catch (URISyntaxException e) {
// Swallow, return the absolute version below.
}
return new SourceMap.PrefixLocationMapping("", "/").map(location);
}));
} }


protected static final SourceFile EXTERNS = protected static final SourceFile EXTERNS =
Expand All @@ -178,7 +167,7 @@ protected void setOptions(CompilerOptions options) {
*/ */
public static class EsmToCjsCompilerSupplier extends CompilerSupplier { public static class EsmToCjsCompilerSupplier extends CompilerSupplier {
@Override @Override
public CompileResult compile(URI path, String code) { public CompileResult compile(Path path, String code) {
CompilerOptions options = new CompilerOptions(); CompilerOptions options = new CompilerOptions();
options.setLanguageIn(LanguageMode.ECMASCRIPT_NEXT); options.setLanguageIn(LanguageMode.ECMASCRIPT_NEXT);
options.setEmitUseStrict(false); options.setEmitUseStrict(false);
Expand Down
Expand Up @@ -25,7 +25,7 @@
import com.google.common.cache.LoadingCache; import com.google.common.cache.LoadingCache;
import com.google.common.util.concurrent.UncheckedExecutionException; import com.google.common.util.concurrent.UncheckedExecutionException;
import com.google.javascript.jscomp.bundle.TranspilationException; import com.google.javascript.jscomp.bundle.TranspilationException;
import java.net.URI; import java.nio.file.Path;
import java.util.Objects; import java.util.Objects;


/** /**
Expand All @@ -51,7 +51,7 @@ public TranspileResult load(Key key) {
} }


@Override @Override
public TranspileResult transpile(URI path, String code) { public TranspileResult transpile(Path path, String code) {
try { try {
return cache.getUnchecked(new Key(path, code)); return cache.getUnchecked(new Key(path, code));
} catch (UncheckedExecutionException e) { } catch (UncheckedExecutionException e) {
Expand All @@ -72,10 +72,9 @@ public String runtime() {
} }


private static final class Key { private static final class Key {
private final URI path; private final Path path;
private final String code; private final String code;

Key(Path path, String code) {
Key(URI path, String code) {
this.path = checkNotNull(path); this.path = checkNotNull(path);
this.code = checkNotNull(code); this.code = checkNotNull(code);
} }
Expand Down
Expand Up @@ -20,7 +20,7 @@


import com.google.common.escape.Escaper; import com.google.common.escape.Escaper;
import com.google.common.net.PercentEscaper; import com.google.common.net.PercentEscaper;
import java.net.URI; import java.nio.file.Path;
import java.util.Objects; import java.util.Objects;


/** /**
Expand All @@ -29,19 +29,19 @@
*/ */
public final class TranspileResult { public final class TranspileResult {


private final URI path; private final Path path;
private final String original; private final String original;
private final String transpiled; private final String transpiled;
private final String sourceMap; private final String sourceMap;


public TranspileResult(URI path, String original, String transpiled, String sourceMap) { public TranspileResult(Path path, String original, String transpiled, String sourceMap) {
this.path = checkNotNull(path); this.path = checkNotNull(path);
this.original = checkNotNull(original); this.original = checkNotNull(original);
this.transpiled = checkNotNull(transpiled); this.transpiled = checkNotNull(transpiled);
this.sourceMap = checkNotNull(sourceMap); this.sourceMap = checkNotNull(sourceMap);
} }


public URI path() { public Path path() {
return path; return path;
} }


Expand Down
34 changes: 19 additions & 15 deletions src/com/google/javascript/jscomp/transpile/Transpiler.java
Expand Up @@ -16,7 +16,7 @@


package com.google.javascript.jscomp.transpile; package com.google.javascript.jscomp.transpile;


import java.net.URI; import java.nio.file.Path;


/** /**
* Common interface for a transpiler. * Common interface for a transpiler.
Expand All @@ -39,8 +39,11 @@
* </ol> * </ol>
*/ */
public interface Transpiler { public interface Transpiler {
/** Transforms the given chunk of code. The input should be an entire file worth of code. */ /**
TranspileResult transpile(URI path, String code); * Transforms the given chunk of code. The input should be an entire file
* worth of code.
*/
TranspileResult transpile(Path path, String code);


/** /**
* Returns any necessary runtime code as a string. This should include * Returns any necessary runtime code as a string. This should include
Expand All @@ -49,17 +52,18 @@ public interface Transpiler {
*/ */
String runtime(); String runtime();


/** Null implementation that does no transpilation at all. */ /**
Transpiler NULL = * Null implementation that does no transpilation at all.
new Transpiler() { */
@Override Transpiler NULL = new Transpiler() {
public TranspileResult transpile(URI path, String code) { @Override
return new TranspileResult(path, code, code, ""); public TranspileResult transpile(Path path, String code) {
} return new TranspileResult(path, code, code, "");
}


@Override @Override
public String runtime() { public String runtime() {
return ""; return "";
} }
}; };
} }
9 changes: 4 additions & 5 deletions test/com/google/javascript/jscomp/CommandLineRunnerTest.java
Expand Up @@ -1033,9 +1033,9 @@ public void testSourceMapLocationsTranslations1() {
args.add("--source_map_location_mapping=foo/|http://bar"); args.add("--source_map_location_mapping=foo/|http://bar");
testSame("var x = 3;"); testSame("var x = 3;");


List<? extends LocationMapping> mappings = lastCompiler.getOptions().sourceMapLocationMappings; List<LocationMapping> mappings = lastCompiler.getOptions().sourceMapLocationMappings;
assertThat(ImmutableSet.copyOf(mappings)) assertThat(ImmutableSet.copyOf(mappings))
.containsExactly(new SourceMap.PrefixLocationMapping("foo/", "http://bar")); .containsExactly(new LocationMapping("foo/", "http://bar"));
} }


public void testSourceMapLocationsTranslations2() { public void testSourceMapLocationsTranslations2() {
Expand All @@ -1046,12 +1046,11 @@ public void testSourceMapLocationsTranslations2() {
args.add("--source_map_location_mapping=xxx/|http://yyy"); args.add("--source_map_location_mapping=xxx/|http://yyy");
testSame("var x = 3;"); testSame("var x = 3;");


List<? extends LocationMapping> mappings = lastCompiler.getOptions() List<LocationMapping> mappings = lastCompiler.getOptions()
.sourceMapLocationMappings; .sourceMapLocationMappings;
assertThat(ImmutableSet.copyOf(mappings)) assertThat(ImmutableSet.copyOf(mappings))
.containsExactly( .containsExactly(
new SourceMap.PrefixLocationMapping("foo/", "http://bar"), new LocationMapping("foo/", "http://bar"), new LocationMapping("xxx/", "http://yyy"));
new SourceMap.PrefixLocationMapping("xxx/", "http://yyy"));
} }


public void testSourceMapLocationsTranslations3() { public void testSourceMapLocationsTranslations3() {
Expand Down

0 comments on commit 5d60d64

Please sign in to comment.