com.google.javascript.jscomp.ByPathWarningsGuard Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of closure-compiler-linter Show documentation
Show all versions of closure-compiler-linter Show documentation
Closure Compiler is a JavaScript optimizing compiler. It parses your
JavaScript, analyzes it, removes dead code and rewrites and minimizes
what's left. It also checks syntax, variable references, and types, and
warns about common JavaScript pitfalls. It is used in many of Google's
JavaScript apps, including Gmail, Google Web Search, Google Maps, and
Google Docs.
This binary checks for style issues such as incorrect or missing JSDoc
usage, and missing goog.require() statements. It does not do more advanced
checks such as typechecking.
/*
* Copyright 2008 The Closure Compiler Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.javascript.jscomp;
import static com.google.common.base.Preconditions.checkArgument;
import java.util.List;
/**
* An implementation of a {@link WarningsGuard} that can modify the
* {@link CheckLevel} based on the file that caused the warning, and whether
* this file matches a set of paths (specified either as include or exclude
* of path name parts).
*
* For example:
*
* List<String> paths = new ArrayList<String>();
* paths.add("foo");
* WarningsGuard guard =
* ByPathWarningsGuard.forPath(paths, CheckLevel.ERROR, 1);
*
*
* This guard will convert any warning that came from a file that contains "foo"
* in its path to an error.
*
*/
public final class ByPathWarningsGuard extends WarningsGuard {
private static final long serialVersionUID = 1L;
private final List paths;
private final boolean include;
private final int priority;
private final CheckLevel level;
/**
* Constructs a new instance. The priority is determined by the
* {@link CheckLevel}: ERROR have Priority.STRICT, and OFF have priority
* FILTER_BY_PATH.
*
* Use {@link #forPath} or {@link #exceptPath} to actually create a new
* instance.
*/
private ByPathWarningsGuard(
List paths, boolean include, CheckLevel level) {
checkArgument(paths != null);
checkArgument(level == CheckLevel.OFF || level == CheckLevel.ERROR);
this.paths = paths;
this.include = include;
this.level = level;
this.priority = level == CheckLevel.ERROR ?
WarningsGuard.Priority.STRICT.value :
WarningsGuard.Priority.FILTER_BY_PATH.value;
}
/**
* @param paths Paths for matching.
* @param level The {@link CheckLevel} to apply on affected files.
* @return a new {@link ByPathWarningsGuard} that would affect any file in the
* given set of paths.
*/
public static ByPathWarningsGuard forPath(
List paths, CheckLevel level) {
return new ByPathWarningsGuard(paths, true, level);
}
/**
* @param paths Paths for matching.
* @param level The {@link CheckLevel} to apply on affected files.
* @return a new {@link ByPathWarningsGuard} that would affect any file not
* in the given set of paths.
*/
public static ByPathWarningsGuard exceptPath(
List paths, CheckLevel level) {
return new ByPathWarningsGuard(paths, false, level);
}
@Override
public CheckLevel level(JSError error) {
final String errorPath = error.sourceName;
CheckLevel defaultLevel = error.getDefaultLevel();
if (defaultLevel != CheckLevel.ERROR && errorPath != null) {
boolean inPath = false;
for (String path : paths) {
inPath |= errorPath.contains(path);
}
if (inPath == include) {
return level;
}
}
return null;
}
@Override
protected int getPriority() {
return priority;
}
}