Skip to content

Commit da9697f

Browse files
bwilkersoncommit-bot@chromium.org
authored andcommitted
Fix the update-sdk-constraints fix
The bug was reported on Dart-Code/Dart-Code#2629. The fix is not ideal, but the best I know how to do quickly. I think that a better fix would be to not have a `DartChangeBuilder` and to have `ChangeBuilder` be able to build file builders for both Dart and non-Dart files. The reason I haven't done that yet is that I want to spend some time thinking about the best API for that change. Change-Id: Idbc55e347f724cd1102b6032c2f7239cd9ee7fc6 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/154240 Reviewed-by: Konstantin Shcheglov <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]>
1 parent 388c983 commit da9697f

File tree

3 files changed

+97
-62
lines changed

3 files changed

+97
-62
lines changed

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

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -24,47 +24,18 @@ import 'package:analyzer/src/dart/analysis/session_helper.dart';
2424
import 'package:analyzer/src/dart/ast/utilities.dart';
2525
import 'package:analyzer/src/dart/element/type.dart';
2626
import 'package:analyzer_plugin/utilities/assist/assist.dart';
27+
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
2728
import 'package:analyzer_plugin/utilities/change_builder/change_builder_dart.dart';
2829
import 'package:analyzer_plugin/utilities/change_builder/change_workspace.dart';
2930
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
3031
import 'package:analyzer_plugin/utilities/range_factory.dart';
3132
import 'package:meta/meta.dart';
3233

33-
/// An object that can compute a correction (fix or assist).
34-
abstract class CorrectionProducer extends _AbstractCorrectionProducer {
35-
/// Return the arguments that should be used when composing the message for an
36-
/// assist, or `null` if the assist message has no parameters or if this
37-
/// producer doesn't support assists.
38-
List<Object> get assistArguments => null;
39-
40-
/// Return the assist kind that should be used to build an assist, or `null`
41-
/// if this producer doesn't support assists.
42-
AssistKind get assistKind => null;
43-
34+
/// An object that can compute a correction (fix or assist) in a Dart file.
35+
abstract class CorrectionProducer extends SingleCorrectionProducer {
4436
/// Return the type for the class `bool` from `dart:core`.
4537
DartType get coreTypeBool => resolvedResult.typeProvider.boolType;
4638

47-
/// Return the length of the error message being fixed, or `null` if there is
48-
/// no diagnostic.
49-
int get errorLength => diagnostic?.problemMessage?.length;
50-
51-
/// Return the text of the error message being fixed, or `null` if there is
52-
/// no diagnostic.
53-
String get errorMessage => diagnostic?.problemMessage?.message;
54-
55-
/// Return the offset of the error message being fixed, or `null` if there is
56-
/// no diagnostic.
57-
int get errorOffset => diagnostic?.problemMessage?.offset;
58-
59-
/// Return the arguments that should be used when composing the message for a
60-
/// fix, or `null` if the fix message has no parameters or if this producer
61-
/// doesn't support fixes.
62-
List<Object> get fixArguments => null;
63-
64-
/// Return the fix kind that should be used to build a fix, or `null` if this
65-
/// producer doesn't support fixes.
66-
FixKind get fixKind => null;
67-
6839
/// Returns `true` if [node] is in a static context.
6940
bool get inStaticContext {
7041
// constructor initializer cannot reference "this"
@@ -300,6 +271,45 @@ abstract class MultiCorrectionProducer extends _AbstractCorrectionProducer {
300271
Iterable<CorrectionProducer> get producers;
301272
}
302273

274+
/// An object that can compute a correction (fix or assist) in a non-Dart file.
275+
abstract class NonDartCorrectionProducer extends SingleCorrectionProducer {
276+
/// Use the [builder] to create the changes necessary to implement this fix.
277+
Future<void> compute(ChangeBuilder builder);
278+
}
279+
280+
/// An object that can compute a correction (fix or assist) in a Dart file.
281+
abstract class SingleCorrectionProducer extends _AbstractCorrectionProducer {
282+
/// Return the arguments that should be used when composing the message for an
283+
/// assist, or `null` if the assist message has no parameters or if this
284+
/// producer doesn't support assists.
285+
List<Object> get assistArguments => null;
286+
287+
/// Return the assist kind that should be used to build an assist, or `null`
288+
/// if this producer doesn't support assists.
289+
AssistKind get assistKind => null;
290+
291+
/// Return the length of the error message being fixed, or `null` if there is
292+
/// no diagnostic.
293+
int get errorLength => diagnostic?.problemMessage?.length;
294+
295+
/// Return the text of the error message being fixed, or `null` if there is
296+
/// no diagnostic.
297+
String get errorMessage => diagnostic?.problemMessage?.message;
298+
299+
/// Return the offset of the error message being fixed, or `null` if there is
300+
/// no diagnostic.
301+
int get errorOffset => diagnostic?.problemMessage?.offset;
302+
303+
/// Return the arguments that should be used when composing the message for a
304+
/// fix, or `null` if the fix message has no parameters or if this producer
305+
/// doesn't support fixes.
306+
List<Object> get fixArguments => null;
307+
308+
/// Return the fix kind that should be used to build a fix, or `null` if this
309+
/// producer doesn't support fixes.
310+
FixKind get fixKind => null;
311+
}
312+
303313
/// The behavior shared by [CorrectionProducer] and [MultiCorrectionProducer].
304314
abstract class _AbstractCorrectionProducer {
305315
/// The context used to produce corrections.

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ import 'package:analysis_server/src/services/correction/fix.dart';
77
import 'package:analyzer/file_system/file_system.dart';
88
import 'package:analyzer/source/source_range.dart';
99
import 'package:analyzer/src/hint/sdk_constraint_extractor.dart';
10-
import 'package:analyzer_plugin/utilities/change_builder/change_builder_dart.dart';
10+
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
1111
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
1212

13-
class UpdateSdkConstraints extends CorrectionProducer {
13+
class UpdateSdkConstraints extends NonDartCorrectionProducer {
1414
/// The minimum version to which the SDK constraints should be updated.
1515
final String _minimumVersion;
1616

@@ -22,7 +22,7 @@ class UpdateSdkConstraints extends CorrectionProducer {
2222
FixKind get fixKind => DartFixKind.UPDATE_SDK_CONSTRAINTS;
2323

2424
@override
25-
Future<void> compute(DartChangeBuilder builder) async {
25+
Future<void> compute(ChangeBuilder builder) async {
2626
var context = resourceProvider.pathContext;
2727
File pubspecFile;
2828
var folder = resourceProvider.getFolder(context.dirname(file));

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

Lines changed: 52 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -152,13 +152,17 @@ import 'package:analyzer/src/generated/java_core.dart';
152152
import 'package:analyzer/src/generated/parser.dart';
153153
import 'package:analyzer_plugin/protocol/protocol_common.dart'
154154
hide AnalysisError, Element, ElementKind;
155+
import 'package:analyzer_plugin/src/utilities/change_builder/change_builder_core.dart';
155156
import 'package:analyzer_plugin/src/utilities/change_builder/change_builder_dart.dart';
156157
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
157158
import 'package:analyzer_plugin/utilities/fixes/fixes.dart' hide FixContributor;
158159

159160
/// A function that can be executed to create a multi-correction producer.
160161
typedef MultiProducerGenerator = MultiCorrectionProducer Function();
161162

163+
/// A function that can be executed to create a non-Dart correction producer.
164+
typedef NonDartProducerGenerator = NonDartCorrectionProducer Function();
165+
162166
/// A function that can be executed to create a correction producer.
163167
typedef ProducerGenerator = CorrectionProducer Function();
164168

@@ -568,6 +572,40 @@ class FixProcessor extends BaseProcessor {
568572
],
569573
};
570574

575+
/// A map from error codes to a list of generators used to create the
576+
/// correction producers used to build fixes for those diagnostics. The
577+
/// generators used for lint rules are in the [lintProducerMap].
578+
static const Map<ErrorCode, List<NonDartProducerGenerator>>
579+
nonDartProducerMap = {
580+
HintCode.SDK_VERSION_AS_EXPRESSION_IN_CONST_CONTEXT: [
581+
UpdateSdkConstraints.version_2_2_2,
582+
],
583+
HintCode.SDK_VERSION_ASYNC_EXPORTED_FROM_CORE: [
584+
UpdateSdkConstraints.version_2_1_0,
585+
],
586+
HintCode.SDK_VERSION_BOOL_OPERATOR_IN_CONST_CONTEXT: [
587+
UpdateSdkConstraints.version_2_2_2,
588+
],
589+
HintCode.SDK_VERSION_EQ_EQ_OPERATOR_IN_CONST_CONTEXT: [
590+
UpdateSdkConstraints.version_2_2_2,
591+
],
592+
HintCode.SDK_VERSION_EXTENSION_METHODS: [
593+
UpdateSdkConstraints.version_2_6_0,
594+
],
595+
HintCode.SDK_VERSION_GT_GT_GT_OPERATOR: [
596+
UpdateSdkConstraints.version_2_2_2,
597+
],
598+
HintCode.SDK_VERSION_IS_EXPRESSION_IN_CONST_CONTEXT: [
599+
UpdateSdkConstraints.version_2_2_2,
600+
],
601+
HintCode.SDK_VERSION_SET_LITERAL: [
602+
UpdateSdkConstraints.version_2_2_0,
603+
],
604+
HintCode.SDK_VERSION_UI_AS_CODE: [
605+
UpdateSdkConstraints.version_2_2_2,
606+
],
607+
};
608+
571609
/// A map from error codes to a list of generators used to create the
572610
/// correction producers used to build fixes for those diagnostics. The
573611
/// generators used for lint rules are in the [lintProducerMap].
@@ -757,33 +795,6 @@ class FixProcessor extends BaseProcessor {
757795
],
758796
// TODO(brianwilkerson) Add a fix to normalize the path.
759797
// HintCode.PACKAGE_IMPORT_CONTAINS_DOT_DOT: [],
760-
HintCode.SDK_VERSION_AS_EXPRESSION_IN_CONST_CONTEXT: [
761-
UpdateSdkConstraints.version_2_2_2,
762-
],
763-
HintCode.SDK_VERSION_ASYNC_EXPORTED_FROM_CORE: [
764-
UpdateSdkConstraints.version_2_1_0,
765-
],
766-
HintCode.SDK_VERSION_BOOL_OPERATOR_IN_CONST_CONTEXT: [
767-
UpdateSdkConstraints.version_2_2_2,
768-
],
769-
HintCode.SDK_VERSION_EQ_EQ_OPERATOR_IN_CONST_CONTEXT: [
770-
UpdateSdkConstraints.version_2_2_2,
771-
],
772-
HintCode.SDK_VERSION_EXTENSION_METHODS: [
773-
UpdateSdkConstraints.version_2_6_0,
774-
],
775-
HintCode.SDK_VERSION_GT_GT_GT_OPERATOR: [
776-
UpdateSdkConstraints.version_2_2_2,
777-
],
778-
HintCode.SDK_VERSION_IS_EXPRESSION_IN_CONST_CONTEXT: [
779-
UpdateSdkConstraints.version_2_2_2,
780-
],
781-
HintCode.SDK_VERSION_SET_LITERAL: [
782-
UpdateSdkConstraints.version_2_2_0,
783-
],
784-
HintCode.SDK_VERSION_UI_AS_CODE: [
785-
UpdateSdkConstraints.version_2_2_2,
786-
],
787798
HintCode.TYPE_CHECK_IS_NOT_NULL: [
788799
UseNotEqNull.newInstance,
789800
],
@@ -1050,6 +1061,14 @@ class FixProcessor extends BaseProcessor {
10501061
args: producer.fixArguments);
10511062
}
10521063

1064+
Future<void> computeNonDart(NonDartCorrectionProducer producer) async {
1065+
producer.configure(context);
1066+
var builder = ChangeBuilderImpl();
1067+
await producer.compute(builder);
1068+
_addFixFromBuilder(builder, producer.fixKind,
1069+
args: producer.fixArguments);
1070+
}
1071+
10531072
var errorCode = error.errorCode;
10541073
if (errorCode is LintCode) {
10551074
var generators = lintProducerMap[errorCode.name];
@@ -1065,6 +1084,12 @@ class FixProcessor extends BaseProcessor {
10651084
await compute(generator());
10661085
}
10671086
}
1087+
var nonDartGenerators = nonDartProducerMap[errorCode];
1088+
if (nonDartGenerators != null) {
1089+
for (var generator in nonDartGenerators) {
1090+
await computeNonDart(generator());
1091+
}
1092+
}
10681093
var multiGenerators = nonLintMultiProducerMap[errorCode];
10691094
if (multiGenerators != null) {
10701095
for (var multiGenerator in multiGenerators) {

0 commit comments

Comments
 (0)