Skip to content

Commit

Permalink
Fix negation in glob patterns
Browse files Browse the repository at this point in the history
Fixes #1407.
  • Loading branch information
Dominator008 committed Jan 30, 2016
1 parent f98608a commit 16d143d
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 8 deletions.
10 changes: 10 additions & 0 deletions src/com/google/javascript/jscomp/AbstractCommandLineRunner.java
Expand Up @@ -2542,6 +2542,16 @@ protected FlagEntry(T flag, String value) {
this.flag = flag;
this.value = value;
}

@Override
public boolean equals(Object o) {
if (o instanceof FlagEntry) {
FlagEntry<?> that = (FlagEntry<?>) o;
return that.flag.equals(this.flag)
&& that.value.equals(this.value);
}
return false;
}
}

/**
Expand Down
30 changes: 22 additions & 8 deletions src/com/google/javascript/jscomp/CommandLineRunner.java
Expand Up @@ -61,6 +61,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -745,12 +746,21 @@ protected List<String> getJsFiles() throws CmdLineException, IOException {
protected List<FlagEntry<JsSourceType>> getMixedJsSources()
throws CmdLineException, IOException {
List<FlagEntry<JsSourceType>> mixedSources = new ArrayList<>();
Set<String> excludes = new HashSet<>();
for (FlagEntry<JsSourceType> source : Flags.mixedJsSources) {
if (source.value.endsWith(".zip")) {
mixedSources.add(source);
} else if (source.value.startsWith("!")) {
for (String filename : findJsFiles(
Collections.singletonList(source.value.substring(1)))) {
excludes.add(filename);
mixedSources.remove(new FlagEntry<>(JsSourceType.JS, filename));
}
} else {
for (String filename : findJsFiles(Collections.singletonList(source.value))) {
mixedSources.add(new FlagEntry<>(JsSourceType.JS, filename));
if (!excludes.contains(filename)) {
mixedSources.add(new FlagEntry<>(JsSourceType.JS, filename));
}
}
}
}
Expand Down Expand Up @@ -1475,23 +1485,25 @@ public static List<SourceFile> getDefaultExterns() throws IOException {
*/
public static List<String> findJsFiles(Collection<String> patterns) throws IOException {
Set<String> allJsInputs = new TreeSet<>();
Set<String> excludes = new HashSet<>();
for (String pattern : patterns) {
if (!pattern.contains("*") && !pattern.startsWith("!")) {
File matchedFile = new File(pattern);
if (matchedFile.isDirectory()) {
matchPaths(new File(matchedFile, "**.js").toString(), allJsInputs);
} else {
matchPaths(new File(matchedFile, "**.js").toString(), allJsInputs, excludes);
} else if (!excludes.contains(pattern)) {
allJsInputs.add(pattern);
}
} else {
matchPaths(pattern, allJsInputs);
matchPaths(pattern, allJsInputs, excludes);
}
}

return new ArrayList<>(allJsInputs);
}

private static void matchPaths(String pattern, final Set<String> allJsInputs)
private static void matchPaths(String pattern, final Set<String> allJsInputs,
final Set<String> excludes)
throws IOException {
FileSystem fs = FileSystems.getDefault();
final boolean remove = pattern.indexOf('!') == 0;
Expand Down Expand Up @@ -1526,10 +1538,12 @@ private static void matchPaths(String pattern, final Set<String> allJsInputs)
@Override public FileVisitResult visitFile(
Path p, BasicFileAttributes attrs) {
if (matcher.matches(p.normalize())) {
String pathString = p.toString();
if (remove) {
allJsInputs.remove(p.toString());
} else {
allJsInputs.add(p.toString());
excludes.add(pathString);
allJsInputs.remove(pathString);
} else if (!excludes.contains(pathString)) {
allJsInputs.add(pathString);
}
}
return FileVisitResult.CONTINUE;
Expand Down
28 changes: 28 additions & 0 deletions test/com/google/javascript/jscomp/CommandLineRunnerTest.java
Expand Up @@ -1139,6 +1139,34 @@ public void testGlobJs2() throws IOException {
}
}

public void testGlobJs3() throws IOException, FlagUsageException {
FlagEntry<JsSourceType> jsFile1 = createJsFile("test1", "var a;");
FlagEntry<JsSourceType> jsFile2 = createJsFile("test2", "var b;");
new File(jsFile2.value).renameTo(new File(
new File(jsFile1.value).getParentFile() + File.separator + "test2.js"));
// Make sure test2.js is excluded from the inputs when the exclusion
// comes after the inclusion
String glob1 = new File(jsFile1.value).getParent() + File.separator + "**.js";
String glob2 = "!" + new File(jsFile1.value).getParent() + File.separator + "**test2.js";
compileFiles(
"var a;", new FlagEntry<>(JsSourceType.JS, glob1),
new FlagEntry<>(JsSourceType.JS, glob2));
}

public void testGlobJs4() throws IOException, FlagUsageException {
FlagEntry<JsSourceType> jsFile1 = createJsFile("test1", "var a;");
FlagEntry<JsSourceType> jsFile2 = createJsFile("test2", "var b;");
new File(jsFile2.value).renameTo(new File(
new File(jsFile1.value).getParentFile() + File.separator + "test2.js"));
// Make sure test2.js is excluded from the inputs when the exclusion
// comes before the inclusion
String glob1 = "!" + new File(jsFile1.value).getParent() + File.separator + "**test2.js";
String glob2 = new File(jsFile1.value).getParent() + File.separator + "**.js";
compileFiles(
"var a;", new FlagEntry<>(JsSourceType.JS, glob1),
new FlagEntry<>(JsSourceType.JS, glob2));
}

public void testSourceMapInputs() throws Exception {
args.add("--js_output_file");
args.add("/path/to/out.js");
Expand Down

0 comments on commit 16d143d

Please sign in to comment.