diff --git a/pkg/analysis_server/lib/src/services/correction/fix_internal.dart b/pkg/analysis_server/lib/src/services/correction/fix_internal.dart index c5d127137f0..e0ef0b5e168 100644 --- a/pkg/analysis_server/lib/src/services/correction/fix_internal.dart +++ b/pkg/analysis_server/lib/src/services/correction/fix_internal.dart @@ -317,7 +317,7 @@ class FixProcessor extends BaseProcessor { /// used to create correction producers. The generators are then used to build /// fixes for those diagnostics. The generators used for non-lint diagnostics /// are in the [nonLintProducerMap]. - static const Map> lintProducerMap = { + static final Map> lintProducerMap = { LintNames.always_declare_return_types: [ // TODO(brianwilkerson) Consider applying in bulk. AddReturnType.newInstance, @@ -1331,6 +1331,12 @@ class FixProcessor extends BaseProcessor { } } } + + /// Associate the given correction producer [generator] with the lint with the + /// given [lintName]. + static void registerFixForLint(String lintName, ProducerGenerator generator) { + lintProducerMap.putIfAbsent(lintName, () => []).add(generator); + } } /// [_FixState] that is still empty. diff --git a/pkg/analysis_server/test/src/services/correction/fix/fix_processor_map_test.dart b/pkg/analysis_server/test/src/services/correction/fix/fix_processor_map_test.dart index cde90377946..6e27913c31a 100644 --- a/pkg/analysis_server/test/src/services/correction/fix/fix_processor_map_test.dart +++ b/pkg/analysis_server/test/src/services/correction/fix/fix_processor_map_test.dart @@ -2,6 +2,7 @@ // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. +import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart'; import 'package:analysis_server/src/services/correction/fix_internal.dart'; import 'package:test/test.dart'; import 'package:test_reflective_loader/test_reflective_loader.dart'; @@ -22,6 +23,17 @@ class FixProcessorMapTest { _testMap(FixProcessor.nonLintProducerMap.values); } + void test_registerFixForLint() { + CorrectionProducer producer() => MockCorrectionProducer(); + + var lintName = 'not_a_lint'; + expect(FixProcessor.lintProducerMap[lintName], null); + FixProcessor.registerFixForLint(lintName, producer); + expect(FixProcessor.lintProducerMap[lintName], contains(producer)); + // Restore the map to it's original state so as to not impact other tests. + FixProcessor.lintProducerMap.remove(lintName); + } + void _testGenerator(ProducerGenerator generator) { var producer = generator(); var className = producer.runtimeType.toString(); @@ -40,3 +52,10 @@ class FixProcessorMapTest { } } } + +class MockCorrectionProducer implements CorrectionProducer { + @override + dynamic noSuchMethod(Invocation invocation) { + return super.noSuchMethod(invocation); + } +}