Skip to content

Commit 440c93a

Browse files
srawlinsCommit Queue
authored andcommitted
DAS: Rearrange code to promote variables better
The motivation here was to remove the local duplicate variables, `isEmptyIdentifier_final`, `context_final`, and `returnType_final`. These variables only existed because the variables they duplicate lose their promoted types inside the `builder.addDartFileEdit` closure. They lose their promoted type because they are multiply assigned. This CL simplifies each method by taking advantage of early returns and implicit late assignment. Change-Id: I81ff248d97e875a5b75c7d20b8e95f1b0c08ea24 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/417701 Commit-Queue: Samuel Rawlins <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent 7f5485d commit 440c93a

File tree

3 files changed

+61
-71
lines changed

3 files changed

+61
-71
lines changed

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

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,56 +24,53 @@ class ConvertIntoIsNotEmpty extends ResolvedCorrectionProducer {
2424

2525
@override
2626
Future<void> compute(ChangeBuilder builder) async {
27-
// prepare "expr.isEmpty"
28-
SimpleIdentifier? isEmptyIdentifier;
29-
AstNode? isEmptyAccess;
30-
if (node is SimpleIdentifier) {
31-
var identifier = node as SimpleIdentifier;
32-
var parent = identifier.parent;
33-
// normal case (but rare)
34-
if (parent is PropertyAccess) {
35-
isEmptyIdentifier = parent.propertyName;
36-
isEmptyAccess = parent;
37-
}
38-
// usual case
39-
if (parent is PrefixedIdentifier) {
40-
isEmptyIdentifier = parent.identifier;
41-
isEmptyAccess = parent;
42-
}
27+
if (node is! SimpleIdentifier) {
28+
return;
4329
}
44-
if (isEmptyIdentifier == null || isEmptyAccess == null) {
30+
31+
// Prepare `expr.isEmpty`.
32+
SimpleIdentifier isEmptyIdentifier;
33+
AstNode isEmptyAccess;
34+
var identifier = node as SimpleIdentifier;
35+
var parent = identifier.parent;
36+
if (parent is PropertyAccess) {
37+
// Normal case (but rare).
38+
isEmptyIdentifier = parent.propertyName;
39+
isEmptyAccess = parent;
40+
} else if (parent is PrefixedIdentifier) {
41+
// Usual case.
42+
isEmptyIdentifier = parent.identifier;
43+
isEmptyAccess = parent;
44+
} else {
4545
return;
4646
}
47-
// should be "isEmpty"
47+
48+
// Should be `isEmpty`.
4849
var propertyElement = isEmptyIdentifier.element;
4950
if (propertyElement == null || 'isEmpty' != propertyElement.name3) {
5051
return;
5152
}
52-
// should have "isNotEmpty"
53+
// Should have `isNotEmpty`.
5354
var propertyTarget = propertyElement.enclosingElement2;
5455
if (propertyTarget == null ||
5556
getChildren(propertyTarget, 'isNotEmpty').isEmpty) {
5657
return;
5758
}
58-
// should be in PrefixExpression
59+
// Should be in `PrefixExpression`.
5960
if (isEmptyAccess.parent is! PrefixExpression) {
6061
return;
6162
}
6263
var prefixExpression = isEmptyAccess.parent as PrefixExpression;
63-
// should be !
64+
// Should be `!`.
6465
if (prefixExpression.operator.type != TokenType.BANG) {
6566
return;
6667
}
6768

68-
var isEmptyIdentifier_final = isEmptyIdentifier;
6969
await builder.addDartFileEdit(file, (builder) {
7070
builder.addDeletion(
7171
range.startStart(prefixExpression, prefixExpression.operand),
7272
);
73-
builder.addSimpleReplacement(
74-
range.node(isEmptyIdentifier_final),
75-
'isNotEmpty',
76-
);
73+
builder.addSimpleReplacement(range.node(isEmptyIdentifier), 'isNotEmpty');
7774
});
7875
}
7976
}

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

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import 'package:analyzer/dart/element/element2.dart';
1111
import 'package:analyzer_plugin/utilities/assist/assist.dart';
1212
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
1313
import 'package:analyzer_plugin/utilities/range_factory.dart';
14+
import 'package:collection/collection.dart';
1415

1516
class ConvertToFieldParameter extends ResolvedCorrectionProducer {
1617
ConvertToFieldParameter({required super.context});
@@ -44,48 +45,38 @@ class ConvertToFieldParameter extends ResolvedCorrectionProducer {
4445
if (visitor.count != 1) {
4546
return;
4647
}
47-
// find the field initializer
48-
ConstructorFieldInitializer? parameterInitializer;
49-
for (var initializer in initializers) {
50-
if (initializer is ConstructorFieldInitializer) {
51-
var expression = initializer.expression;
52-
if (expression is SimpleIdentifier &&
53-
expression.name == parameterName) {
54-
parameterInitializer = initializer;
55-
}
56-
}
57-
}
48+
// Find the field initializer.
49+
var parameterInitializer = initializers
50+
.whereType<ConstructorFieldInitializer>()
51+
.firstWhereOrNull((i) {
52+
var expression = i.expression;
53+
return expression is SimpleIdentifier &&
54+
expression.name == parameterName;
55+
});
5856
if (parameterInitializer == null) {
5957
return;
6058
}
6159
var fieldName = parameterInitializer.fieldName.name;
6260

63-
var context_final = context;
64-
var parameterInitializer_final = parameterInitializer;
6561
await builder.addDartFileEdit(file, (builder) {
66-
// replace parameter
62+
// Replace the parameter.
6763
builder.addSimpleReplacement(
68-
range.node(context_final.parameter),
64+
range.node(context.parameter),
6965
'this.$fieldName',
7066
);
71-
// remove initializer
72-
var initializerIndex = initializers.indexOf(parameterInitializer_final);
67+
// Remove the initializer.
68+
var initializerIndex = initializers.indexOf(parameterInitializer);
7369
if (initializers.length == 1) {
7470
builder.addDeletion(
75-
range.endEnd(
76-
context_final.constructor.parameters,
77-
parameterInitializer_final,
78-
),
71+
range.endEnd(context.constructor.parameters, parameterInitializer),
7972
);
8073
} else {
8174
if (initializerIndex == 0) {
8275
var next = initializers[initializerIndex + 1];
83-
builder.addDeletion(
84-
range.startStart(parameterInitializer_final, next),
85-
);
76+
builder.addDeletion(range.startStart(parameterInitializer, next));
8677
} else {
8778
var prev = initializers[initializerIndex - 1];
88-
builder.addDeletion(range.endEnd(prev, parameterInitializer_final));
79+
builder.addDeletion(range.endEnd(prev, parameterInitializer));
8980
}
9081
}
9182
});

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

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,6 @@ class MakeReturnTypeNullable extends ResolvedCorrectionProducer {
4545
return;
4646
}
4747

48-
if (body.isAsynchronous || body.isGenerator) {
49-
if (returnType is! NamedType) {
50-
return;
51-
}
52-
var typeArguments = returnType.typeArguments;
53-
if (typeArguments == null) {
54-
return;
55-
}
56-
var arguments = typeArguments.arguments;
57-
if (arguments.length != 1) {
58-
return;
59-
}
60-
returnType = arguments[0];
61-
}
62-
6348
if (node is! NullLiteral &&
6449
!typeSystem.isAssignableTo(
6550
returnType.typeOrThrow,
@@ -69,9 +54,8 @@ class MakeReturnTypeNullable extends ResolvedCorrectionProducer {
6954
return;
7055
}
7156

72-
var returnType_final = returnType;
7357
await builder.addDartFileEdit(file, (builder) {
74-
builder.addSimpleInsertion(returnType_final.end, '?');
58+
builder.addSimpleInsertion(returnType.end, '?');
7559
});
7660
}
7761

@@ -80,11 +64,29 @@ class MakeReturnTypeNullable extends ResolvedCorrectionProducer {
8064
if (function is FunctionExpression) {
8165
function = function.parent;
8266
}
67+
TypeAnnotation? returnType;
8368
if (function is MethodDeclaration) {
84-
return function.returnType;
69+
returnType = function.returnType;
8570
} else if (function is FunctionDeclaration) {
86-
return function.returnType;
71+
returnType = function.returnType;
72+
} else {
73+
return null;
74+
}
75+
76+
if (body.isAsynchronous || body.isGenerator) {
77+
if (returnType is! NamedType) {
78+
return null;
79+
}
80+
var typeArguments = returnType.typeArguments;
81+
if (typeArguments == null) {
82+
return null;
83+
}
84+
var arguments = typeArguments.arguments;
85+
if (arguments.length != 1) {
86+
return null;
87+
}
88+
returnType = arguments.single;
8789
}
88-
return null;
90+
return returnType;
8991
}
9092
}

0 commit comments

Comments
 (0)