Avoid doing the same regex match over and over.
Once we've gotten a match on the SCRIPT node we can continue to traverse the contents of the script without checking the filename again.
@@ -24,6 +24,7 @@ import com.google.javascript.rhino.Node;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
+import javax.annotation.Nullable;
/**
* A compiler node traversal callback that invokes matchers against every node and
@@ -34,11 +35,12 @@ import java.util.regex.Pattern;
final class JsFlumeCallback implements Callback {
private final Scanner scanner;
+ @Nullable
private final Pattern includeFilePattern;
private final List<Match> matches = new ArrayList<>();
private final List<SuggestedFix> fixes = new ArrayList<>();
- JsFlumeCallback(Scanner scanner, Pattern includeFilePattern) {
+ JsFlumeCallback(Scanner scanner, @Nullable Pattern includeFilePattern) {
this.scanner = scanner;
this.includeFilePattern = includeFilePattern;
}
@@ -56,12 +58,14 @@ final class JsFlumeCallback implements Callback {
if (n.isFromExterns()) {
return false;
}
+ if (n.isScript()) {
String filename = n.getSourceFileName();
if (includeFilePattern != null
&& !Strings.isNullOrEmpty(includeFilePattern.pattern())
&& !Strings.isNullOrEmpty(filename)) {
return includeFilePattern.matcher(filename).find();
}
+ }
return true;
}
