Skip to content

Commit

Permalink
Properly handle absolute paths when resolving between two paths
Browse files Browse the repository at this point in the history
Closes #3170.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=225057217
  • Loading branch information
ChadKillingsworth authored and blickly committed Dec 13, 2018
1 parent b7aa9d6 commit f8aa350
Showing 1 changed file with 24 additions and 20 deletions.
44 changes: 24 additions & 20 deletions src/com/google/javascript/jscomp/Compiler.java
Expand Up @@ -3692,31 +3692,35 @@ public void setModuleMetadataMap(ModuleMetadataMap moduleMetadataMap) {
}

/**
* Simplistic implementation of the java.nio.file.Path resolveSibling method that works
* with GWT.
* Simplistic implementation of the java.nio.file.Path resolveSibling method that works with GWT.
*
* @param path1 from path - must be a file (not directory)
* @param path2 to path - must be a file (not directory)
*/
private static String resolveSibling(String path1, String path2) {
List<String> path1Parts = new ArrayList<>(Arrays.asList(path1.split("/")));
List<String> path2Parts = new ArrayList<>(Arrays.asList(path2.split("/")));
if (!path1Parts.isEmpty()) {
path1Parts.remove(path1Parts.size() - 1);
}

while (!path1Parts.isEmpty() && !path2Parts.isEmpty()) {
if (path2Parts.get(0).equals(".")) {
path2Parts.remove(0);
} else if (path2Parts.get(0).equals("..")) {
path2Parts.remove(0);
path1Parts.remove(path1Parts.size() - 1);
* @param fromPath - must be a file (not directory)
* @param toPath - must be a file (not directory)
*/
private static String resolveSibling(String fromPath, String toPath) {
// If the destination is an absolute path, nothing to do.
if (toPath.startsWith("/")) {
return toPath;
}

List<String> fromPathParts = new ArrayList<>(Arrays.asList(fromPath.split("/")));
List<String> toPathParts = new ArrayList<>(Arrays.asList(toPath.split("/")));
if (!fromPathParts.isEmpty()) {
fromPathParts.remove(fromPathParts.size() - 1);
}

while (!fromPathParts.isEmpty() && !toPathParts.isEmpty()) {
if (toPathParts.get(0).equals(".")) {
toPathParts.remove(0);
} else if (toPathParts.get(0).equals("..")) {
toPathParts.remove(0);
fromPathParts.remove(fromPathParts.size() - 1);
} else {
break;
}
}

path1Parts.addAll(path2Parts);
return String.join("/", path1Parts);
fromPathParts.addAll(toPathParts);
return String.join("/", fromPathParts);
}
}

0 comments on commit f8aa350

Please sign in to comment.