Skip to content

Commit 576dbd7

Browse files
scheglovCommit Queue
authored andcommitted
Elements. Getter/Setter names can be null.
Change-Id: I64b49f594c982b68c33891bb710ec9e66a70e260 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/390940 Commit-Queue: Konstantin Shcheglov <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]> Reviewed-by: Phil Quitslund <[email protected]>
1 parent 4cc356e commit 576dbd7

File tree

6 files changed

+31
-13
lines changed

6 files changed

+31
-13
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import 'package:analysis_server_plugin/edit/dart/correction_producer.dart';
77
import 'package:analyzer/dart/ast/ast.dart';
88
import 'package:analyzer/dart/element/element2.dart';
99
import 'package:analyzer/dart/element/type.dart';
10+
import 'package:analyzer/src/utilities/extensions/collection.dart';
1011
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
1112
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
1213

@@ -87,7 +88,7 @@ class AddMissingEnumLikeCaseClauses extends ResolvedCorrectionProducer {
8788
if (expression is Identifier) {
8889
var element = expression.element;
8990
if (element is GetterElement) {
90-
caseNames.add(element.name);
91+
caseNames.addIfNotNull(element.name);
9192
}
9293
} else if (expression is PropertyAccess) {
9394
caseNames.add(expression.propertyName.name);

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,16 @@ class ExtractLocalVariable extends ResolvedCorrectionProducer {
9090
required Expression target,
9191
required Element2? targetProperty,
9292
}) async {
93-
if (targetProperty is GetterElement &&
94-
typeSystem.isPotentiallyNullable(targetProperty.returnType)) {
93+
if (targetProperty is! GetterElement) {
94+
return;
95+
}
96+
97+
var propertyName = targetProperty.name;
98+
if (propertyName == null) {
99+
return;
100+
}
101+
102+
if (typeSystem.isPotentiallyNullable(targetProperty.returnType)) {
95103
AstNode? enclosingNode = target;
96104
while (true) {
97105
if (enclosingNode == null || enclosingNode is FunctionBody) {
@@ -121,7 +129,6 @@ class ExtractLocalVariable extends ResolvedCorrectionProducer {
121129
}
122130

123131
await builder.addDartFileEdit(file, (builder) {
124-
var propertyName = targetProperty.name;
125132
builder.addInsertion(ifLineOffset, (builder) {
126133
builder.write(prefix);
127134
builder.writeln('final $propertyName = $initializerCode;');

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ class ShadowField extends ResolvedCorrectionProducer {
3535
return;
3636
}
3737

38+
var fieldName = accessor.name;
39+
if (fieldName == null) {
40+
return;
41+
}
42+
3843
if (accessor.enclosingElement2 is! InterfaceElement2) {
3944
// TODO(brianwilkerson): Should we also require that the getter be synthetic?
4045
return;
@@ -64,7 +69,6 @@ class ShadowField extends ResolvedCorrectionProducer {
6469
return;
6570
}
6671

67-
var fieldName = accessor.name;
6872
var offset = statement.offset;
6973
var prefix = utils.getLinePrefix(offset);
7074
//

pkg/analysis_server/lib/src/services/search/hierarchy.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ String? _getBaseName(Element element) {
327327
String? _getBaseName2(Element2 element) {
328328
if (element is SetterElement) {
329329
var name = element.name;
330-
return name.substring(0, name.length - 1);
330+
return name?.substring(0, name.length - 1);
331331
}
332332
return element.name;
333333
}

pkg/analyzer/lib/dart/element/element2.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -990,7 +990,7 @@ abstract class GetterElement implements ExecutableElement2, FragmentedElement {
990990
GetterFragment? get firstFragment;
991991

992992
@override
993-
String get name;
993+
String? get name;
994994

995995
/// The field or top-level variable associated with this getter.
996996
///
@@ -1013,7 +1013,7 @@ abstract class GetterFragment implements ExecutableFragment {
10131013
// GetterElement get element;
10141014

10151015
@override
1016-
String get name;
1016+
String? get name;
10171017

10181018
/// The field or top-level variable associated with this getter.
10191019
///
@@ -1951,7 +1951,7 @@ abstract class SetterElement implements ExecutableElement2, FragmentedElement {
19511951
SetterFragment? get firstFragment;
19521952

19531953
@override
1954-
String get name;
1954+
String? get name;
19551955

19561956
/// The field or top-level variable associated with this setter.
19571957
///
@@ -1974,7 +1974,7 @@ abstract class SetterFragment implements ExecutableFragment {
19741974
// SetterElement get element;
19751975

19761976
@override
1977-
String get name;
1977+
String? get name;
19781978

19791979
/// The field or top-level variable associated with this setter.
19801980
///

pkg/analyzer/lib/src/dart/element/element.dart

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5028,7 +5028,7 @@ class GetterElementImpl extends ExecutableElementImpl2
50285028
ElementKind get kind => ElementKind.GETTER;
50295029

50305030
@override
5031-
String get name => firstFragment.name;
5031+
String? get name => firstFragment.name;
50325032

50335033
@override
50345034
PropertyInducingElement2? get variable3 => firstFragment.variable2?.element;
@@ -9730,7 +9730,13 @@ class SetterElementImpl extends ExecutableElementImpl2
97309730
firstFragment.correspondingGetter2?.element as GetterElement?;
97319731

97329732
@override
9733-
String get displayName => name.substring(0, name.length - 1);
9733+
String get displayName {
9734+
if (name case var name?) {
9735+
return name.substring(0, name.length - 1);
9736+
} else {
9737+
return '<null-name>';
9738+
}
9739+
}
97349740

97359741
@override
97369742
Element2? get enclosingElement2 => firstFragment.enclosingFragment?.element;
@@ -9742,7 +9748,7 @@ class SetterElementImpl extends ExecutableElementImpl2
97429748
ElementKind get kind => ElementKind.SETTER;
97439749

97449750
@override
9745-
String get name => firstFragment.name;
9751+
String? get name => firstFragment.name;
97469752

97479753
@override
97489754
PropertyInducingElement2? get variable3 => firstFragment.variable2?.element;

0 commit comments

Comments
 (0)