Skip to content

Commit 17a2f16

Browse files
scheglovCommit Queue
authored andcommitted
Elements. Add PropertyAccessorElement2 and PropertyAccessorFragment.
I found a number of places where we need to access `variable3`, so it might be convenient to have a common interface that provides it. Still, there are many places where we want to know that this is specifically `GetterElement`, to separate interfaces for them are also useful. Change-Id: Ib671dc3bdb9a04a022fd74921f8ccedcdba79643 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/395701 Commit-Queue: Konstantin Shcheglov <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent 1293500 commit 17a2f16

File tree

17 files changed

+111
-140
lines changed

17 files changed

+111
-140
lines changed

pkg/analysis_server/lib/src/computer/computer_documentation.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ class DartDocumentationComputer {
5252
element,
5353
...overridden.superElements,
5454
...overridden.interfaceElements,
55-
if (element case GetterElement(variable3: var variable?)) variable,
56-
if (element case SetterElement(variable3: var variable?)) variable,
55+
if (element case PropertyAccessorElement2(variable3: var variable?))
56+
variable,
5757
];
5858
for (var candidate in candidates) {
5959
if (candidate.documentationCommentOrNull != null) {

pkg/analysis_server/lib/src/lsp/handlers/handler_document_color_presentation.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,7 @@ class DocumentColorPresentationHandler
253253
parent is PrefixedIdentifier ? parent.element : node.element;
254254

255255
return switch (element) {
256-
GetterElement(:var variable3) || SetterElement(:var variable3) =>
257-
variable3?.isConst ?? false,
256+
PropertyAccessorElement2(:var variable3) => variable3?.isConst ?? false,
258257
VariableElement2() => element.isConst,
259258
_ => false,
260259
};

pkg/analysis_server/lib/src/services/completion/dart/feature_computer.dart

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -257,13 +257,7 @@ class FeatureComputer {
257257
return protocol.ElementKind.CLASS;
258258
} else if (element is FieldElement2 && element.isEnumConstant) {
259259
return protocol.ElementKind.ENUM_CONSTANT;
260-
} else if (element is GetterElement) {
261-
var variable = element.variable3;
262-
if (variable == null) {
263-
return protocol.ElementKind.UNKNOWN;
264-
}
265-
element = variable;
266-
} else if (element is SetterElement) {
260+
} else if (element is PropertyAccessorElement2) {
267261
var variable = element.variable3;
268262
if (variable == null) {
269263
return protocol.ElementKind.UNKNOWN;
@@ -467,12 +461,7 @@ class FeatureComputer {
467461
return 1.0;
468462
} else if (element is TopLevelVariableElement2 && element.isConst) {
469463
return 1.0;
470-
} else if (element is GetterElement && element.isSynthetic) {
471-
var variable = element.variable3;
472-
if (variable != null && variable.isStatic && variable.isConst) {
473-
return 1.0;
474-
}
475-
} else if (element is SetterElement && element.isSynthetic) {
464+
} else if (element is PropertyAccessorElement2 && element.isSynthetic) {
476465
var variable = element.variable3;
477466
if (variable != null && variable.isStatic && variable.isConst) {
478467
return 1.0;

pkg/analysis_server/lib/src/services/completion/dart/suggestion_builder.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1492,9 +1492,7 @@ class SuggestionBuilder {
14921492
int? requiredParameterCount;
14931493
bool? hasNamedParameters;
14941494
CompletionDefaultArgumentList? defaultArgumentList;
1495-
if (element is ExecutableElement2 &&
1496-
element is! GetterElement &&
1497-
element is! SetterElement) {
1495+
if (element is ExecutableElement2 && element is! PropertyAccessorElement2) {
14981496
parameterNames =
14991497
element.formalParameters.map((parameter) {
15001498
return parameter.displayName;

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,8 +303,7 @@ class _FieldFinder extends RecursiveAstVisitor<void> {
303303
if (node.inSetterContext()) {
304304
var element = node.writeOrReadElement2;
305305
var field = switch (element) {
306-
GetterElement(:var variable3) => variable3,
307-
SetterElement(:var variable3) => variable3,
306+
PropertyAccessorElement2(:var variable3) => variable3,
308307
_ => null,
309308
};
310309
if (field is FieldElement2) {

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -273,11 +273,7 @@ class _ElementReferenceCollector extends RecursiveAstVisitor<void> {
273273
var staticElement = node.writeOrReadElement2;
274274
if (staticElement == element) {
275275
references.add(node);
276-
} else if (staticElement is GetterElement) {
277-
if (staticElement.variable3 == element) {
278-
references.add(node);
279-
}
280-
} else if (staticElement is SetterElement) {
276+
} else if (staticElement is PropertyAccessorElement2) {
281277
if (staticElement.variable3 == element) {
282278
references.add(node);
283279
}

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@ import 'package:analysis_server/src/services/correction/fix/data_driven/element_
77
import 'package:analyzer/dart/ast/token.dart';
88
import 'package:analyzer/dart/element/element2.dart'
99
show
10-
LibraryElement2,
11-
LibraryFragment,
1210
ExtensionElement2,
13-
GetterElement,
1411
InterfaceElement2,
12+
LibraryElement2,
13+
LibraryFragment,
1514
PrefixElement2,
16-
SetterElement;
15+
PropertyAccessorElement2;
1716
import 'package:analyzer/dart/element/type.dart';
1817
import 'package:analyzer/src/dart/ast/ast.dart';
1918

@@ -546,8 +545,8 @@ class _MatcherBuilder {
546545
var element = node.element;
547546
// Add enclosing element to the matcher for non top level property
548547
// accessors when possible.
549-
if (element is GetterElement || element is SetterElement) {
550-
var enclosingElement = element?.enclosingElement2;
548+
if (element is PropertyAccessorElement2) {
549+
var enclosingElement = element.enclosingElement2;
551550
if (enclosingElement is! LibraryElement2) {
552551
_addMatcher(
553552
components: [node.name, (enclosingElement?.displayName)!],

pkg/analysis_server/lib/src/utilities/import_analyzer.dart

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -121,23 +121,14 @@ class _ElementRecorder {
121121
int referenceOffset,
122122
LibraryImport? import,
123123
) {
124-
switch (referencedElement) {
125-
case GetterElement():
126-
if (referencedElement.isSynthetic) {
127-
var variable = referencedElement.variable3;
128-
if (variable == null) {
129-
return;
130-
}
131-
referencedElement = variable;
132-
}
133-
case SetterElement():
134-
if (referencedElement.isSynthetic) {
135-
var variable = referencedElement.variable3;
136-
if (variable == null) {
137-
return;
138-
}
139-
referencedElement = variable;
124+
if (referencedElement is PropertyAccessorElement2) {
125+
if (referencedElement.isSynthetic) {
126+
var variable = referencedElement.variable3;
127+
if (variable == null) {
128+
return;
140129
}
130+
referencedElement = variable;
131+
}
141132
}
142133

143134
if (_isBeingMoved(referenceOffset)) {

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

Lines changed: 45 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,7 +1071,7 @@ abstract class GenericFunctionTypeFragment implements FunctionTypedFragment {
10711071
/// top-level variable or a field. Induced getters are synthetic.
10721072
///
10731073
/// Clients may not extend, implement or mix-in this class.
1074-
abstract class GetterElement implements ExecutableElement2 {
1074+
abstract class GetterElement implements PropertyAccessorElement2 {
10751075
@override
10761076
GetterElement get baseElement;
10771077

@@ -1081,18 +1081,12 @@ abstract class GetterElement implements ExecutableElement2 {
10811081

10821082
@override
10831083
GetterFragment get firstFragment;
1084-
1085-
/// The field or top-level variable associated with this getter.
1086-
///
1087-
/// If this getter was explicitly defined (is not synthetic) then the variable
1088-
/// associated with it will be synthetic.
1089-
PropertyInducingElement2? get variable3;
10901084
}
10911085

10921086
/// The portion of a [GetterElement] contributed by a single declaration.
10931087
///
10941088
/// Clients may not extend, implement or mix-in this class.
1095-
abstract class GetterFragment implements ExecutableFragment {
1089+
abstract class GetterFragment implements PropertyAccessorFragment {
10961090
/// The setter that corresponds to (has the same name as) this getter, or
10971091
/// `null` if there is no corresponding setter.
10981092
SetterFragment? get correspondingSetter2;
@@ -1113,12 +1107,6 @@ abstract class GetterFragment implements ExecutableFragment {
11131107
// but can't because the Impl class supports both getters and setters.
11141108
// @override
11151109
// GetterElement get element;
1116-
1117-
/// The field or top-level variable associated with this getter.
1118-
///
1119-
/// If this getter was explicitly defined (is not synthetic) then the variable
1120-
/// associated with it will be synthetic.
1121-
PropertyInducingFragment? get variable3;
11221110
}
11231111

11241112
/// An element whose instance members can refer to `this`.
@@ -2077,6 +2065,47 @@ abstract class PromotableFragment implements VariableFragment {
20772065
PromotableFragment? get previousFragment;
20782066
}
20792067

2068+
/// A getter or a setter.
2069+
///
2070+
/// Property accessors can either be defined explicitly or they can be induced
2071+
/// by either a top-level variable or a field. Induced property accessors are
2072+
/// synthetic.
2073+
///
2074+
/// Clients may not extend, implement or mix-in this class.
2075+
abstract class PropertyAccessorElement2 implements ExecutableElement2 {
2076+
@override
2077+
PropertyAccessorElement2 get baseElement;
2078+
2079+
@override
2080+
PropertyAccessorFragment get firstFragment;
2081+
2082+
/// The field or top-level variable associated with this getter.
2083+
///
2084+
/// If this getter was explicitly defined (is not synthetic) then the variable
2085+
/// associated with it will be synthetic.
2086+
PropertyInducingElement2? get variable3;
2087+
}
2088+
2089+
/// The portion of a [GetterElement] contributed by a single declaration.
2090+
///
2091+
/// Clients may not extend, implement or mix-in this class.
2092+
abstract class PropertyAccessorFragment implements ExecutableFragment {
2093+
@override
2094+
PropertyAccessorElement2 get element;
2095+
2096+
@override
2097+
PropertyAccessorFragment? get nextFragment;
2098+
2099+
@override
2100+
PropertyAccessorFragment? get previousFragment;
2101+
2102+
/// The field or top-level variable associated with this property accessors.
2103+
///
2104+
/// If this property accessor was explicitly defined (is not synthetic) then
2105+
/// the variable associated with it will be synthetic.
2106+
PropertyInducingFragment? get variable3;
2107+
}
2108+
20802109
/// A variable that has an associated getter and possibly a setter. Note that
20812110
/// explicitly defined variables implicitly define a synthetic getter and that
20822111
/// non-`final` explicitly defined variables implicitly define a synthetic
@@ -2180,7 +2209,7 @@ abstract class PropertyInducingFragment
21802209
/// top-level variable or a field. Induced setters are synthetic.
21812210
///
21822211
/// Clients may not extend, implement or mix-in this class.
2183-
abstract class SetterElement implements ExecutableElement2 {
2212+
abstract class SetterElement implements PropertyAccessorElement2 {
21842213
@override
21852214
SetterElement get baseElement;
21862215

@@ -2190,18 +2219,12 @@ abstract class SetterElement implements ExecutableElement2 {
21902219

21912220
@override
21922221
SetterFragment get firstFragment;
2193-
2194-
/// The field or top-level variable associated with this setter.
2195-
///
2196-
/// If this setter was explicitly defined (is not synthetic) then the variable
2197-
/// associated with it will be synthetic.
2198-
PropertyInducingElement2? get variable3;
21992222
}
22002223

22012224
/// The portion of a [SetterElement] contributed by a single declaration.
22022225
///
22032226
/// Clients may not extend, implement or mix-in this class.
2204-
abstract class SetterFragment implements ExecutableFragment {
2227+
abstract class SetterFragment implements PropertyAccessorFragment {
22052228
/// The getter that corresponds to (has the same name as) this setter, or
22062229
/// `null` if there is no corresponding getter.
22072230
GetterFragment? get correspondingGetter2;
@@ -2222,12 +2245,6 @@ abstract class SetterFragment implements ExecutableFragment {
22222245
// setters.
22232246
// @override
22242247
// SetterFragment? get previousFragment;
2225-
2226-
/// The field or top-level variable associated with this setter.
2227-
///
2228-
/// If this setter was explicitly defined (is not synthetic) then the variable
2229-
/// associated with it will be synthetic.
2230-
PropertyInducingFragment? get variable3;
22312248
}
22322249

22332250
/// A super formal parameter.

0 commit comments

Comments
 (0)