| 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 | package com.google.javascript.jscomp; |
| 17 | |
| 18 | import com.google.javascript.jscomp.CheckLevel; |
| 19 | |
| 20 | import java.io.Serializable; |
| 21 | |
| 22 | /** |
| 23 | * Class that allows to flexibly manage what to do with a reported |
| 24 | * warning/error. |
| 25 | * |
| 26 | * Guard has several choices: |
| 27 | * - return OFF - suppress the warning/error |
| 28 | * - return WARNING |
| 29 | * - return ERROR report it with high severity |
| 30 | * - return null. Does not know what to do with it. Lets the other guard |
| 31 | * decide what to do with it. |
| 32 | * |
| 33 | * Although the interface is very simple, it allows you easily customize what |
| 34 | * warnings you are interested in. |
| 35 | * |
| 36 | * For example there are could be several implementations: |
| 37 | * StrictGuard - {return ERROR}. All warnings should be treat as errors. |
| 38 | * SilentGuard - {if (WARNING) return OFF}. Suppress all warnings but still |
| 39 | * fail if JS has errors. |
| 40 | * WhitelistGuard (if !whitelistErrors.contains(error) return ERROR) return |
| 41 | * error if it does not present in the whitelist. |
| 42 | * |
| 43 | * @author [email protected] (Anatol Pomazau) |
| 44 | */ |
| 45 | public abstract class WarningsGuard implements Serializable { |
| 46 | |
| 47 | public static enum Priority { |
| 48 | MAX(1), |
| 49 | MIN(100), |
| 50 | STRICT(100), |
| 51 | DEFAULT(50), |
| 52 | SUPPRESS_BY_WHITELIST(40), |
| 53 | SUPPRESS_DOC(20), |
| 54 | FILTER_BY_PATH(1); |
| 55 | |
| 56 | final int value; |
| 57 | |
| 58 | Priority(int value) { |
| 59 | this.value = value; |
| 60 | } |
| 61 | |
| 62 | public int getValue() { |
| 63 | return value; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Returns a new check level for a given error. OFF - suppress it, ERROR - |
| 69 | * report as error. null means that this guard does not know what to do |
| 70 | * with the error. Null is extremely helpful when you have a chain of |
| 71 | * guards. If current guard returns null, then the next in the chain should |
| 72 | * process it. |
| 73 | * |
| 74 | * @param error a reported error. |
| 75 | * @return what level given error should have. |
| 76 | */ |
| 77 | public abstract CheckLevel level(JSError error); |
| 78 | |
| 79 | /** |
| 80 | * The priority in which warnings guards are applied. Lower means the |
| 81 | * guard will be applied sooner. Expressed on a scale of 1 to 100. |
| 82 | */ |
| 83 | protected int getPriority() { |
| 84 | return Priority.DEFAULT.value; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Returns whether all warnings in the given diagnostic group will be |
| 89 | * filtered out. Used to determine which passes to skip. |
| 90 | * |
| 91 | * @param group A group of DiagnosticTypes. |
| 92 | * @return Whether all warnings of these types are disabled by this guard. |
| 93 | */ |
| 94 | protected boolean disables(DiagnosticGroup group) { |
| 95 | return false; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Returns whether any of the warnings in the given diagnostic group will be |
| 100 | * upgraded to a warning or error. |
| 101 | * |
| 102 | * @param group A group of DiagnosticTypes. |
| 103 | * @return Whether any warnings of these types are enabled by this guard. |
| 104 | */ |
| 105 | protected boolean enables(DiagnosticGroup group) { |
| 106 | return false; |
| 107 | } |
| 108 | } |