forked from bazelbuild/rules_closure
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiagnostics.java
More file actions
245 lines (223 loc) · 9.2 KB
/
Diagnostics.java
File metadata and controls
245 lines (223 loc) · 9.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*
* Copyright 2016 The Closure Rules Authors. All rights reserved.
*
* 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 com.google.common.collect.HashMultimap;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Multimap;
import com.google.javascript.jscomp.lint.CheckJSDocStyle;
import com.google.javascript.jscomp.lint.CheckMissingSemicolon;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
final class Diagnostics {
static final DiagnosticType SUPERFLUOUS_SUPPRESS =
DiagnosticType.error(
"CR_SUPERFLUOUS_SUPPRESS", "Build rule ({0}) contains superfluous suppress codes: {1}");
static {
DiagnosticGroups.registerGroup("superfluousSuppress", SUPERFLUOUS_SUPPRESS);
DiagnosticGroups.registerGroup("setTestOnly", CheckSetTestOnly.INVALID_SETTESTONLY);
DiagnosticGroups.registerGroup("strictDependencies",
CheckStrictDeps.DUPLICATE_PROVIDES,
CheckStrictDeps.REDECLARED_PROVIDES,
CheckStrictDeps.NOT_PROVIDED);
}
static final DiagnosticGroups GROUPS = new DiagnosticGroups();
/**
* Diagnostic groups {@link JsChecker} will check and {@link JsCompiler} will ignore.
*
* <p>These are the simpler checks, e.g. linting, that can be examined on a per-file basis.
*/
static final ImmutableSet<String> JSCHECKER_ONLY_ERRORS =
ImmutableSet.of(
"deprecatedAnnotations",
"lintChecks",
"nonStandardJsDocs",
"setTestOnly",
"strictDependencies",
"strictModuleChecks",
"superfluousSuppress",
"underscore");
/** Diagnostic groups both {@link JsChecker} and {@link JsCompiler} will check. */
static final ImmutableSet<String> JSCHECKER_EXTRA_ERRORS =
ImmutableSet.of(
// Even though we're not running the typechecker, enable the checkTypes DiagnosticGroup,
// since it contains some warnings we do want to report, such as JSDoc parse warnings.
"checkTypes");
/** Legal values for a {@code @suppress {foo}} JSDoc tag. */
// Keep in sync with com/google/javascript/jscomp/parsing/ParserConfig.properties
private static final ImmutableSet<String> LEGAL_JSDOC_SUPPRESSIONS =
ImmutableSet.of(
"accessControls",
"checkDebuggerStatement",
"checkPrototypalTypes",
"checkRegExp",
"checkTypes",
"checkVars",
"closureDepMethodUsageChecks",
"const",
"constantProperty",
"deprecated",
"duplicate",
"es5Strict",
"externsValidation",
"extraProvide",
"extraRequire",
"globalThis",
"invalidCasts",
"lateProvide",
"lintChecks",
"messageConventions",
"misplacedTypeAnnotation",
"missingOverride",
"missingPolyfill",
"missingProperties",
"missingProvide",
"missingRequire",
"missingReturn",
"missingSourcesWarnings",
"moduleLoad",
"nonStandardJsDocs",
"partialAlias",
"polymer",
"reportUnknownTypes",
"strictCheckTypes",
"strictMissingProperties",
"strictModuleDepCheck",
"strictPrimitiveOperators",
"suspiciousCode",
"transitionalSuspiciousCodeWarnings",
"undefinedNames",
"undefinedVars",
"underscore",
"unknownDefines",
"untranspilableFeatures",
"unusedLocalVariables",
"useOfGoogProvide",
"uselessCode",
"visibility",
"with");
/** Checks to suppress if closure_js_library convention is not GOOGLE. */
static final ImmutableSet<DiagnosticType> GOOGLE_LINTER_CHECKS =
ImmutableSet.of(
CheckJSDocStyle.MISSING_JSDOC,
CheckJSDocStyle.MISSING_PARAMETER_JSDOC,
CheckJSDocStyle.MISSING_RETURN_JSDOC,
CheckJSDocStyle.OPTIONAL_PARAM_NOT_MARKED_OPTIONAL,
CheckMissingSemicolon.MISSING_SEMICOLON);
/** Checks to suppress if closure_js_library convention is not CLOSURE. */
static final ImmutableSet<DiagnosticType> CLOSURE_LINTER_CHECKS =
new ImmutableSet.Builder<DiagnosticType>()
.addAll(GOOGLE_LINTER_CHECKS)
.add(ClosureCheckModule.LET_GOOG_REQUIRE)
.add(ClosureRewriteModule.USELESS_USE_STRICT_DIRECTIVE)
.add(ImplicitNullabilityCheck.IMPLICITLY_NULLABLE_JSDOC)
.add(RhinoErrorReporter.BAD_JSDOC_ANNOTATION)
.build();
/**
* Errors that should be ignored entirely if encountered in synthetic code.
*
* <p>Code generated by compiler passes currently does a poor job conforming to these checks.
* They've been listed here because: a) the user can't do anything about it; and b) they would be
* very noisy otherwise.
*/
static final ImmutableSet<DiagnosticType> IGNORE_FOR_SYNTHETIC =
ImmutableSet.of(
ImplicitNullabilityCheck.IMPLICITLY_NULLABLE_JSDOC,
TypeCheck.UNKNOWN_EXPR_TYPE);
static final ImmutableSet<DiagnosticType> IGNORE_FOR_LEGACY =
ImmutableSet.of(
ImplicitNullabilityCheck.IMPLICITLY_NULLABLE_JSDOC,
TypeCheck.UNKNOWN_EXPR_TYPE);
/** Compiler checks that Closure Rules will always ignore. */
static final ImmutableSet<DiagnosticType> IGNORE_ALWAYS =
ImmutableSet.of(
// TODO(hochhaus): Make unknownDefines an error for user supplied defines.
// https://github.com/bazelbuild/rules_closure/issues/79
ProcessDefines.UNKNOWN_DEFINE_WARNING,
// TODO(jart): Remove these when regression is fixed relating to jscomp being able to
// identify externs files that were passed via srcs.
CheckJSDoc.INVALID_MODIFIES_ANNOTATION,
CheckJSDoc.INVALID_NO_SIDE_EFFECT_ANNOTATION);
static final ImmutableMap<String, DiagnosticType> DIAGNOSTIC_TYPES = initDiagnosticTypes();
static final ImmutableMultimap<DiagnosticType, String> DIAGNOSTIC_GROUPS = initDiagnosticGroups();
static final ImmutableSet<String> JSCHECKER_ONLY_SUPPRESS_CODES = initJscheckerSuppressCodes();
static final ImmutableSet<DiagnosticGroup> JSCHECKER_ONLY_GROUPS = initJscheckerGroups();
static final ImmutableMap<DiagnosticType, String> JSDOC_SUPPRESS_CODES = initJsdocSuppressCodes();
static ImmutableSet<DiagnosticType> getDiagnosticTypesForSuppressCode(String code) {
DiagnosticGroup group = GROUPS.forName(code);
if (group != null) {
return ImmutableSet.copyOf(group.getTypes());
}
DiagnosticType type = DIAGNOSTIC_TYPES.get(code);
if (type == null) {
return ImmutableSet.of();
}
return ImmutableSet.of(type);
}
private static ImmutableMap<String, DiagnosticType> initDiagnosticTypes() {
Map<String, DiagnosticType> builder = new HashMap<>();
for (DiagnosticGroup group : DiagnosticGroups.getRegisteredGroups().values()) {
for (DiagnosticType type : group.getTypes()) {
builder.put(type.key, type);
}
}
return ImmutableMap.copyOf(builder);
}
private static ImmutableMultimap<DiagnosticType, String> initDiagnosticGroups() {
Multimap<DiagnosticType, String> builder = HashMultimap.create();
for (Map.Entry<String, DiagnosticGroup> group :
DiagnosticGroups.getRegisteredGroups().entrySet()) {
for (DiagnosticType type : group.getValue().getTypes()) {
builder.put(type, group.getKey());
}
}
return ImmutableMultimap.copyOf(builder);
}
private static ImmutableSet<String> initJscheckerSuppressCodes() {
Set<String> builder = new HashSet<>();
for (String groupName : JSCHECKER_ONLY_ERRORS) {
builder.add(groupName);
for (DiagnosticType type : GROUPS.forName(groupName).getTypes()) {
builder.add(type.key);
}
}
return ImmutableSet.copyOf(builder);
}
private static ImmutableSet<DiagnosticGroup> initJscheckerGroups() {
ImmutableSet.Builder<DiagnosticGroup> builder = new ImmutableSet.Builder<>();
for (String groupName : JSCHECKER_ONLY_ERRORS) {
builder.add(GROUPS.forName(groupName));
}
return builder.build();
}
private static ImmutableMap<DiagnosticType, String> initJsdocSuppressCodes() {
Map<DiagnosticType, String> builder = new HashMap<>();
builder.put(StrictModeCheck.USE_OF_WITH, "with");
for (String suppress : LEGAL_JSDOC_SUPPRESSIONS) {
if (GROUPS.forName(suppress) == null) {
continue;
}
for (DiagnosticType type : GROUPS.forName(suppress).getTypes()) {
builder.put(type, suppress);
}
}
return ImmutableMap.copyOf(builder);
}
private Diagnostics() {}
}