Skip to content

Commit

Permalink
Handle more special characters in file paths which cause createUri to…
Browse files Browse the repository at this point in the history
… throw exceptions

Merge pull request #1818 from ChadKillingsworth/closure-compiler
Closes #1818
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=123368193
  • Loading branch information
ChadKillingsworth authored and blickly committed May 26, 2016
1 parent c100b64 commit 747aa4d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 7 deletions.
14 changes: 10 additions & 4 deletions src/com/google/javascript/jscomp/ES6ModuleLoader.java
Expand Up @@ -159,10 +159,16 @@ private URI normalizeAddress(URI uri) {
}

static URI createUri(String input) {
// Colons might cause URI.create() to fail
String forwardSlashes =
input.replace(':', '-').replace("\\", MODULE_SLASH).replace(" ", "%20");
return URI.create(forwardSlashes).normalize();
// Handle special characters
String encodedInput = input.replace(':', '-')
.replace('\\', '/')
.replace(" ", "%20")
.replace("[", "%5B")
.replace("]", "%5D")
.replace("<", "%3C")
.replace(">", "%3E");

return URI.create(encodedInput).normalize();
}

private static String stripJsExtension(String fileName) {
Expand Down
13 changes: 10 additions & 3 deletions src/com/google/javascript/jscomp/deps/Es6SortedDependencies.java
Expand Up @@ -160,9 +160,16 @@ private static String toModuleName(URI filename) {
* TODO(tbreisacher): Switch to using the ES6ModuleLoader once the BUILD graph allows it.
*/
private static URI createUri(String input) {
// Colons might cause URI.create() to fail
String forwardSlashes = input.replace(':', '-').replace('\\', '/').replace(" ", "%20");
return URI.create(forwardSlashes).normalize();
// Handle special characters
String encodedInput = input.replace(':', '-')
.replace('\\', '/')
.replace(" ", "%20")
.replace("[", "%5B")
.replace("]", "%5D")
.replace("<", "%3C")
.replace(">", "%3E");

return URI.create(encodedInput).normalize();
}

private void orderInput(INPUT input) {
Expand Down
27 changes: 27 additions & 0 deletions test/com/google/javascript/jscomp/CompilerTest.java
Expand Up @@ -770,6 +770,33 @@ public void testEs6ModuleEntryPoint() throws Exception {
assertThat(result.errors).isEmpty();
}

public void testEs6ModulePathWithOddCharacters() throws Exception {
List<SourceFile> inputs = ImmutableList.of(
SourceFile.fromCode(
"/index[0].js", "import foo from './foo'; foo('hello');"),
SourceFile.fromCode("/foo.js",
"export default (foo) => { alert(foo); }"));

List<ModuleIdentifier> entryPoints = ImmutableList.of(
ModuleIdentifier.forFile("/index[0]"));

CompilerOptions options = createNewFlagBasedOptions();
options.setLanguageIn(CompilerOptions.LanguageMode.ECMASCRIPT6);
options.setLanguageOut(CompilerOptions.LanguageMode.ECMASCRIPT5);
options.dependencyOptions.setDependencyPruning(true);
options.dependencyOptions.setDependencySorting(true);
options.dependencyOptions.setEntryPoints(entryPoints);

List<SourceFile> externs =
AbstractCommandLineRunner.getBuiltinExterns(options.getEnvironment());

Compiler compiler = new Compiler();
compiler.compile(externs, inputs, options);

Result result = compiler.getResult();
assertThat(result.errors).isEmpty();
}

public void testGetEmptyResult() {
Result result = new Compiler().getResult();
assertThat(result.errors).isEmpty();
Expand Down

0 comments on commit 747aa4d

Please sign in to comment.