| 1 | /* |
| 2 | * Copyright 2008 The Closure Compiler Authors. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.google.javascript.jscomp; |
| 18 | |
| 19 | import com.google.common.base.Preconditions; |
| 20 | |
| 21 | import java.util.List; |
| 22 | |
| 23 | /** |
| 24 | * An implementation of a {@link WarningsGuard} that can modify the |
| 25 | * {@link CheckLevel} based on the file that caused the warning, and whether |
| 26 | * this file matches a set of paths (specified either as include or exclude |
| 27 | * of path name parts). |
| 28 | * |
| 29 | * <p>For example: |
| 30 | * <pre> |
| 31 | * List<String> paths = new ArrayList<String>(); |
| 32 | * paths.add("foo"); |
| 33 | * WarningsGuard guard = |
| 34 | * ByPathWarningsGuard.forPath(paths, CheckLevel.ERROR, 1); |
| 35 | * </pre> |
| 36 | * |
| 37 | * This guard will convert any warning that came from a file that contains "foo" |
| 38 | * in its path to an error. |
| 39 | * |
| 40 | */ |
| 41 | public class ByPathWarningsGuard extends WarningsGuard { |
| 42 | private static final long serialVersionUID = 1L; |
| 43 | |
| 44 | private final List<String> paths; |
| 45 | private final boolean include; |
| 46 | private final int priority; |
| 47 | private CheckLevel level; |
| 48 | |
| 49 | /** |
| 50 | * Constructs a new instance. The priority is determined by the |
| 51 | * {@link CheckLevel}: ERROR have Priority.STRICT, and OFF have priority |
| 52 | * FILTER_BY_PATH. |
| 53 | * |
| 54 | * Use {@link #forPath} or {@link #exceptPath} to actually create a new |
| 55 | * instance. |
| 56 | */ |
| 57 | private ByPathWarningsGuard( |
| 58 | List<String> paths, boolean include, CheckLevel level) { |
| 59 | Preconditions.checkArgument(paths != null); |
| 60 | Preconditions.checkArgument( |
| 61 | level == CheckLevel.OFF || level == CheckLevel.ERROR); |
| 62 | this.paths = paths; |
| 63 | this.include = include; |
| 64 | this.level = level; |
| 65 | this.priority = level == CheckLevel.ERROR ? |
| 66 | WarningsGuard.Priority.STRICT.value : |
| 67 | WarningsGuard.Priority.FILTER_BY_PATH.value; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * @param paths Paths for matching. |
| 72 | * @param level The {@link CheckLevel} to apply on affected files. |
| 73 | * @return a new {@link ByPathWarningsGuard} that would affect any file in the |
| 74 | * given set of paths. |
| 75 | */ |
| 76 | public static ByPathWarningsGuard forPath( |
| 77 | List<String> paths, CheckLevel level) { |
| 78 | return new ByPathWarningsGuard(paths, true, level); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * @param paths Paths for matching. |
| 83 | * @param level The {@link CheckLevel} to apply on affected files. |
| 84 | * @return a new {@link ByPathWarningsGuard} that would affect any file not |
| 85 | * in the given set of paths. |
| 86 | */ |
| 87 | public static ByPathWarningsGuard exceptPath( |
| 88 | List<String> paths, CheckLevel level) { |
| 89 | return new ByPathWarningsGuard(paths, false, level); |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public CheckLevel level(JSError error) { |
| 94 | final String errorPath = error.sourceName; |
| 95 | CheckLevel defaultLevel = error.getDefaultLevel(); |
| 96 | if (defaultLevel != CheckLevel.ERROR && errorPath != null) { |
| 97 | boolean inPath = false; |
| 98 | for (String path : paths) { |
| 99 | inPath |= errorPath.contains(path); |
| 100 | } |
| 101 | if (inPath == include) { |
| 102 | return level; |
| 103 | } |
| 104 | } |
| 105 | return null; |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | protected int getPriority() { |
| 110 | return priority; |
| 111 | } |
| 112 | } |