@@ -16,10 +16,12 @@ import 'package:analysis_server_plugin/src/registry.dart';
1616import 'package:analyzer/analysis_rule/rule_context.dart' ;
1717import 'package:analyzer/dart/analysis/analysis_context.dart' ;
1818import 'package:analyzer/dart/analysis/analysis_context_collection.dart' ;
19+ import 'package:analyzer/dart/analysis/analysis_options.dart' ;
1920import 'package:analyzer/dart/analysis/results.dart' ;
2021import 'package:analyzer/dart/analysis/session.dart' ;
2122import 'package:analyzer/dart/ast/ast.dart' ;
2223import 'package:analyzer/diagnostic/diagnostic.dart' ;
24+ import 'package:analyzer/error/error.dart' ;
2325import 'package:analyzer/error/listener.dart' ;
2426import 'package:analyzer/file_system/file_system.dart' ;
2527import 'package:analyzer/file_system/overlay_file_system.dart' ;
@@ -30,6 +32,7 @@ import 'package:analyzer/src/dart/analysis/byte_store.dart';
3032import 'package:analyzer/src/dart/analysis/file_content_cache.dart' ;
3133import 'package:analyzer/src/dart/element/type_system.dart' ;
3234import 'package:analyzer/src/ignore_comments/ignore_info.dart' ;
35+ import 'package:analyzer/src/lint/config.dart' ;
3336import 'package:analyzer/src/lint/linter.dart' ;
3437import 'package:analyzer/src/lint/linter_visitor.dart' ;
3538import 'package:analyzer/src/lint/registry.dart' ;
@@ -351,8 +354,11 @@ class PluginServer {
351354 null ,
352355 );
353356
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 ? > {};
356362
357363 for (var configuration in analysisOptions.pluginConfigurations) {
358364 if (! configuration.isEnabled) continue ;
@@ -362,14 +368,13 @@ class PluginServer {
362368 for (var rule in rules) {
363369 rule.reporter = diagnosticReporter;
364370 // TODO(srawlins): Enable timing similar to what the linter package's
365- // `benchhmark .dart` script does.
371+ // `benchmark .dart` script does.
366372 rule.registerNodeProcessors (nodeRegistry, context);
367373 }
368374 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));
373378 }
374379 }
375380
@@ -379,7 +384,7 @@ class PluginServer {
379384
380385 var ignoreInfo = IgnoreInfo .forDart (unitResult.unit, unitResult.content);
381386 var diagnostics = listener.diagnostics.where ((e) {
382- var pluginName = pluginCodeMapping[e.diagnosticCode.name ];
387+ var pluginName = pluginCodeMapping[e.diagnosticCode];
383388 if (pluginName == null ) {
384389 // If [e] is somehow not mapped, something is wrong; but don't mark it
385390 // as ignored.
@@ -395,7 +400,8 @@ class PluginServer {
395400 (
396401 diagnostic: diagnostic,
397402 protocolError: protocol.AnalysisError (
398- _severityOf (diagnostic),
403+ severityMapping[diagnostic.diagnosticCode] ??
404+ protocol.AnalysisErrorSeverity .INFO ,
399405 protocol.AnalysisErrorType .STATIC_WARNING ,
400406 _locationFor (currentUnit.unit, path, diagnostic),
401407 diagnostic.message,
@@ -413,17 +419,27 @@ class PluginServer {
413419 return diagnosticsAndProtocolErrors.map ((e) => e.protocolError).toList ();
414420 }
415421
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;
426435 }
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;
427443 }
428444
429445 /// Invokes [fn] first for priority analysis contexts, then for the rest.
0 commit comments