Skip to content

Commit f1e3bfc

Browse files
DanTupCommit Queue
authored and
Commit Queue
committed
[analysis_server] Check for bulk-fixable errors to skip resolution during bulk fix operations
Change-Id: I21a9fa4ed71839e7e196240c0bda31403cfea96c Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/308080 Reviewed-by: Konstantin Shcheglov <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]>
1 parent f52bdb9 commit f1e3bfc

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

pkg/analysis_server/lib/src/services/correction/bulk_fix_processor.dart

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,19 @@ class BulkFixProcessor {
533533
}
534534

535535
/// Returns whether [error] is something that might be fixable.
536-
bool _isFixableError(AnalysisError error) => hasFix(error.errorCode);
536+
bool _isFixableError(AnalysisError error) {
537+
final errorCode = error.errorCode;
538+
539+
// Special cases that can be bulk fixed by this class but not by
540+
// FixProcessor.
541+
if (errorCode == WarningCode.DUPLICATE_IMPORT ||
542+
errorCode == HintCode.UNNECESSARY_IMPORT ||
543+
errorCode == WarningCode.UNUSED_IMPORT) {
544+
return true;
545+
}
546+
547+
return FixProcessor.canBulkFix(errorCode);
548+
}
537549

538550
/// Return the override set corresponding to the given [result], or `null` if
539551
/// there is no corresponding configuration file or the file content isn't a

pkg/analysis_server/lib/src/services/correction/fix.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
1919
bool hasFix(ErrorCode errorCode) {
2020
if (errorCode is LintCode) {
2121
var lintName = errorCode.name;
22-
return FixProcessor.lintProducerMap.containsKey(lintName);
22+
return FixProcessor.lintProducerMap.containsKey(lintName) ||
23+
FixProcessor.lintMultiProducerMap.containsKey(lintName);
2324
}
2425
// TODO(brianwilkerson) Either deprecate the part of the protocol supported by
2526
// this function, or handle error codes associated with non-dart files.

pkg/analysis_server/lib/src/services/correction/fix_internal.dart

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import 'package:analysis_server/plugin/edit/fix/fix_core.dart';
66
import 'package:analysis_server/plugin/edit/fix/fix_dart.dart';
77
import 'package:analysis_server/src/services/correction/base_processor.dart';
8+
import 'package:analysis_server/src/services/correction/bulk_fix_processor.dart';
89
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
910
import 'package:analysis_server/src/services/correction/dart/add_async.dart';
1011
import 'package:analysis_server/src/services/correction/dart/add_await.dart';
@@ -376,6 +377,9 @@ class FixInFileProcessor {
376377

377378
/// The computer for Dart fixes.
378379
class FixProcessor extends BaseProcessor {
380+
/// Cached results of [canBulkFix].
381+
static final Map<ErrorCode, bool> _bulkFixableErrorCodes = {};
382+
379383
static final Map<String, List<MultiProducerGenerator>> lintMultiProducerMap =
380384
{
381385
LintNames.deprecated_member_use_from_same_package: [
@@ -1821,6 +1825,36 @@ class FixProcessor extends BaseProcessor {
18211825
}
18221826
}
18231827

1828+
/// Returns whether [errorCode] is an error that can be fixed in bulk.
1829+
static bool canBulkFix(ErrorCode errorCode) {
1830+
bool hasBulkFixProducers(List<ProducerGenerator>? producers) {
1831+
return producers != null &&
1832+
producers.any((producer) => producer().canBeAppliedInBulk);
1833+
}
1834+
1835+
return _bulkFixableErrorCodes.putIfAbsent(errorCode, () {
1836+
if (errorCode is LintCode) {
1837+
final producers = FixProcessor.lintProducerMap[errorCode.name];
1838+
if (hasBulkFixProducers(producers)) {
1839+
return true;
1840+
}
1841+
1842+
return FixProcessor.lintMultiProducerMap.containsKey(errorCode.name);
1843+
}
1844+
1845+
final producers = FixProcessor.nonLintProducerMap[errorCode];
1846+
if (hasBulkFixProducers(producers)) {
1847+
return true;
1848+
}
1849+
1850+
// We can't do detailed checks on multi-producers because the set of
1851+
// producers may vary depending on the resolved unit (we must configure
1852+
// them before we can determine the producers).
1853+
return FixProcessor.nonLintMultiProducerMap.containsKey(errorCode) ||
1854+
BulkFixProcessor.nonLintMultiProducerMap.containsKey(errorCode);
1855+
});
1856+
}
1857+
18241858
/// Associate the given correction producer [generator] with the lint with the
18251859
/// given [lintName].
18261860
static void registerFixForLint(String lintName, ProducerGenerator generator) {

0 commit comments

Comments
 (0)