From d6f4640c0d0f62ce650c704a0ee97911212cf0f4 Mon Sep 17 00:00:00 2001 From: Brian Wilkerson Date: Mon, 23 Aug 2021 18:19:27 +0000 Subject: [PATCH] Make the set of fixes for lints extensible Change-Id: I70ba8737f91aa2d3728ae67d656de26d3619579e Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/210840 Commit-Queue: Brian Wilkerson Reviewed-by: Phil Quitslund Reviewed-by: Konstantin Shcheglov --- .../src/services/correction/fix_internal.dart | 8 +++++++- .../fix/fix_processor_map_test.dart | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) 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); + } +}