From f8aa350b182f7f5bc96109a4b4edaee3bfe43a35 Mon Sep 17 00:00:00 2001 From: Chad Killingsworth Date: Tue, 11 Dec 2018 13:09:53 -0800 Subject: [PATCH] Properly handle absolute paths when resolving between two paths Closes https://github.com/google/closure-compiler/pull/3170. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=225057217 --- .../google/javascript/jscomp/Compiler.java | 44 ++++++++++--------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/src/com/google/javascript/jscomp/Compiler.java b/src/com/google/javascript/jscomp/Compiler.java index 1661ad384ca..aaef26e795a 100644 --- a/src/com/google/javascript/jscomp/Compiler.java +++ b/src/com/google/javascript/jscomp/Compiler.java @@ -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 path1Parts = new ArrayList<>(Arrays.asList(path1.split("/"))); - List 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 fromPathParts = new ArrayList<>(Arrays.asList(fromPath.split("/"))); + List 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); } }