@@ -16,10 +16,12 @@ import 'package:analysis_server_plugin/src/registry.dart';
16
16
import 'package:analyzer/analysis_rule/rule_context.dart' ;
17
17
import 'package:analyzer/dart/analysis/analysis_context.dart' ;
18
18
import 'package:analyzer/dart/analysis/analysis_context_collection.dart' ;
19
+ import 'package:analyzer/dart/analysis/analysis_options.dart' ;
19
20
import 'package:analyzer/dart/analysis/results.dart' ;
20
21
import 'package:analyzer/dart/analysis/session.dart' ;
21
22
import 'package:analyzer/dart/ast/ast.dart' ;
22
23
import 'package:analyzer/diagnostic/diagnostic.dart' ;
24
+ import 'package:analyzer/error/error.dart' ;
23
25
import 'package:analyzer/error/listener.dart' ;
24
26
import 'package:analyzer/file_system/file_system.dart' ;
25
27
import 'package:analyzer/file_system/overlay_file_system.dart' ;
@@ -30,6 +32,7 @@ import 'package:analyzer/src/dart/analysis/byte_store.dart';
30
32
import 'package:analyzer/src/dart/analysis/file_content_cache.dart' ;
31
33
import 'package:analyzer/src/dart/element/type_system.dart' ;
32
34
import 'package:analyzer/src/ignore_comments/ignore_info.dart' ;
35
+ import 'package:analyzer/src/lint/config.dart' ;
33
36
import 'package:analyzer/src/lint/linter.dart' ;
34
37
import 'package:analyzer/src/lint/linter_visitor.dart' ;
35
38
import 'package:analyzer/src/lint/registry.dart' ;
@@ -351,8 +354,11 @@ class PluginServer {
351
354
null ,
352
355
);
353
356
354
- // A mapping from each lint and warning code to its corresponding plugin.
355
- var pluginCodeMapping = < String , String > {};
357
+ // A mapping from each diagnostic code to its corresponding plugin.
358
+ var pluginCodeMapping = < DiagnosticCode , String > {};
359
+
360
+ // A mapping from each diagnostic code to its configured severity.
361
+ var severityMapping = < DiagnosticCode , protocol.AnalysisErrorSeverity ? > {};
356
362
357
363
for (var configuration in analysisOptions.pluginConfigurations) {
358
364
if (! configuration.isEnabled) continue ;
@@ -362,14 +368,13 @@ class PluginServer {
362
368
for (var rule in rules) {
363
369
rule.reporter = diagnosticReporter;
364
370
// TODO(srawlins): Enable timing similar to what the linter package's
365
- // `benchhmark .dart` script does.
371
+ // `benchmark .dart` script does.
366
372
rule.registerNodeProcessors (nodeRegistry, context);
367
373
}
368
374
for (var code in rules.expand ((r) => r.diagnosticCodes)) {
369
- var existingPlugin = pluginCodeMapping[code.name];
370
- if (existingPlugin == null ) {
371
- pluginCodeMapping[code.name] = configuration.name;
372
- }
375
+ pluginCodeMapping.putIfAbsent (code, () => configuration.name);
376
+ severityMapping.putIfAbsent (
377
+ code, () => _configuredSeverity (configuration, code));
373
378
}
374
379
}
375
380
@@ -379,7 +384,7 @@ class PluginServer {
379
384
380
385
var ignoreInfo = IgnoreInfo .forDart (unitResult.unit, unitResult.content);
381
386
var diagnostics = listener.diagnostics.where ((e) {
382
- var pluginName = pluginCodeMapping[e.diagnosticCode.name ];
387
+ var pluginName = pluginCodeMapping[e.diagnosticCode];
383
388
if (pluginName == null ) {
384
389
// If [e] is somehow not mapped, something is wrong; but don't mark it
385
390
// as ignored.
@@ -395,7 +400,8 @@ class PluginServer {
395
400
(
396
401
diagnostic: diagnostic,
397
402
protocolError: protocol.AnalysisError (
398
- _severityOf (diagnostic),
403
+ severityMapping[diagnostic.diagnosticCode] ??
404
+ protocol.AnalysisErrorSeverity .INFO ,
399
405
protocol.AnalysisErrorType .STATIC_WARNING ,
400
406
_locationFor (currentUnit.unit, path, diagnostic),
401
407
diagnostic.message,
@@ -413,17 +419,27 @@ class PluginServer {
413
419
return diagnosticsAndProtocolErrors.map ((e) => e.protocolError).toList ();
414
420
}
415
421
416
- /// Converts the severity of [diagnostic] into a
417
- /// [protocol.AnalysisErrorSeverity] .
418
- protocol.AnalysisErrorSeverity _severityOf (Diagnostic diagnostic) {
419
- try {
420
- return protocol.AnalysisErrorSeverity .values
421
- .byName (diagnostic.severity.name.toUpperCase ());
422
- } catch (_) {
423
- assert (false , 'Invalid severity: ${diagnostic .severity }' );
424
- // Return the default severity of `LintCode`.
425
- return protocol.AnalysisErrorSeverity .INFO ;
422
+ /// Converts the severity of [code] into a [protocol.AnalysisErrorSeverity] .
423
+ protocol.AnalysisErrorSeverity ? _configuredSeverity (
424
+ PluginConfiguration configuration, DiagnosticCode code) {
425
+ var configuredSeverity =
426
+ configuration.diagnosticConfigs[code.name]? .severity;
427
+ if (configuredSeverity != null &&
428
+ configuredSeverity != ConfiguredSeverity .enable) {
429
+ var severityName = configuredSeverity.name.toUpperCase ();
430
+ var severity =
431
+ protocol.AnalysisErrorSeverity .values.asNameMap ()[severityName];
432
+ assert (severity != null ,
433
+ 'Invalid configured severity: ${configuredSeverity .name }' );
434
+ return severity;
426
435
}
436
+
437
+ // Fall back to the declared severity of [code].
438
+ var severityName = code.severity.name.toUpperCase ();
439
+ var severity =
440
+ protocol.AnalysisErrorSeverity .values.asNameMap ()[code.severity.name];
441
+ assert (severity != null , 'Invalid severity: $severityName ' );
442
+ return severity;
427
443
}
428
444
429
445
/// Invokes [fn] first for priority analysis contexts, then for the rest.
0 commit comments