Skip to content

Commit 54c51d6

Browse files
scheglovCommit Queue
authored andcommitted
Elements. Migrate FlutterConvertToStatelessWidget.
Change-Id: I774702b07092a48ed1584b3b1e8774d5f5bdf9d8 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/389561 Commit-Queue: Konstantin Shcheglov <[email protected]> Reviewed-by: Samuel Rawlins <[email protected]> Reviewed-by: Phil Quitslund <[email protected]>
1 parent 4391ca6 commit 54c51d6

File tree

2 files changed

+51
-48
lines changed

2 files changed

+51
-48
lines changed

pkg/analysis_server/analyzer_use_new_elements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@ lib/src/services/correction/dart/extend_class_for_mixin.dart
318318
lib/src/services/correction/dart/extract_local_variable.dart
319319
lib/src/services/correction/dart/flutter_convert_to_children.dart
320320
lib/src/services/correction/dart/flutter_convert_to_stateful_widget.dart
321+
lib/src/services/correction/dart/flutter_convert_to_stateless_widget.dart
321322
lib/src/services/correction/dart/flutter_move_down.dart
322323
lib/src/services/correction/dart/flutter_move_up.dart
323324
lib/src/services/correction/dart/flutter_remove_widget.dart
@@ -1506,4 +1507,4 @@ tool/spec/codegen_protocol_constants.dart
15061507
tool/spec/from_html.dart
15071508
tool/spec/generate_all.dart
15081509
tool/spec/implied_types.dart
1509-
tool/spec/to_html.dart
1510+
tool/spec/to_html.dart

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

Lines changed: 49 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import 'package:analysis_server/src/utilities/extensions/flutter.dart';
88
import 'package:analysis_server_plugin/edit/dart/correction_producer.dart';
99
import 'package:analyzer/dart/ast/ast.dart';
1010
import 'package:analyzer/dart/ast/visitor.dart';
11-
import 'package:analyzer/dart/element/element.dart';
11+
import 'package:analyzer/dart/element/element2.dart';
1212
import 'package:analyzer/dart/element/type.dart';
1313
import 'package:analyzer/source/source_range.dart';
1414
import 'package:analyzer/src/dart/ast/extensions.dart';
@@ -42,7 +42,8 @@ class FlutterConvertToStatelessWidget extends ResolvedCorrectionProducer {
4242
}
4343

4444
// Must be a StatefulWidget subclass.
45-
var widgetClassElement = widgetClass.declaredElement!;
45+
var widgetClassFragment = widgetClass.declaredFragment!;
46+
var widgetClassElement = widgetClassFragment.element;
4647
var superType = widgetClassElement.supertype;
4748
if (superType == null || !superType.isExactlyStatefulWidgetType) {
4849
return;
@@ -52,7 +53,7 @@ class FlutterConvertToStatelessWidget extends ResolvedCorrectionProducer {
5253
if (createStateMethod == null) return;
5354

5455
var stateClass = _findStateClass(widgetClassElement);
55-
var stateClassElement = stateClass?.declaredElement;
56+
var stateClassElement = stateClass?.declaredFragment!.element;
5657
if (stateClass == null ||
5758
stateClassElement == null ||
5859
!Identifier.isPrivateName(stateClass.name.lexeme) ||
@@ -83,24 +84,25 @@ class FlutterConvertToStatelessWidget extends ResolvedCorrectionProducer {
8384

8485
// Prepare nodes to move.
8586
var nodesToMove = <ClassMember>[];
86-
var elementsToMove = <Element>{};
87+
var elementsToMove = <Element2>{};
8788
for (var member in stateClass.members) {
8889
if (member is FieldDeclaration) {
8990
if (member.isStatic) {
9091
return;
9192
}
9293
for (var fieldNode in member.fields.variables) {
93-
var fieldElement = fieldNode.declaredElement as FieldElement;
94+
var fieldElement =
95+
fieldNode.declaredFragment!.element as FieldElement2;
9496
if (!fieldsAssignedInConstructors.contains(fieldElement)) {
9597
nodesToMove.add(member);
9698
elementsToMove.add(fieldElement);
9799

98-
var getter = fieldElement.getter;
100+
var getter = fieldElement.getter2;
99101
if (getter != null) {
100102
elementsToMove.add(getter);
101103
}
102104

103-
var setter = fieldElement.setter;
105+
var setter = fieldElement.setter2;
104106
if (setter != null) {
105107
elementsToMove.add(setter);
106108
}
@@ -112,7 +114,7 @@ class FlutterConvertToStatelessWidget extends ResolvedCorrectionProducer {
112114
}
113115
if (!_isDefaultOverride(member)) {
114116
nodesToMove.add(member);
115-
elementsToMove.add(member.declaredElement!);
117+
elementsToMove.add(member.declaredFragment!.element);
116118
}
117119
}
118120
}
@@ -132,14 +134,14 @@ class FlutterConvertToStatelessWidget extends ResolvedCorrectionProducer {
132134
}
133135

134136
var statelessWidgetClass =
135-
await sessionHelper.getFlutterClass('StatelessWidget');
137+
await sessionHelper.getFlutterClass2('StatelessWidget');
136138
if (statelessWidgetClass == null) {
137139
return;
138140
}
139141

140142
await builder.addDartFileEdit(file, (builder) {
141143
builder.addReplacement(range.node(superclass), (builder) {
142-
builder.writeReference(statelessWidgetClass);
144+
builder.writeReference2(statelessWidgetClass);
143145
});
144146

145147
builder.addDeletion(range.deletionRange(stateClass));
@@ -186,7 +188,7 @@ class FlutterConvertToStatelessWidget extends ResolvedCorrectionProducer {
186188
return null;
187189
}
188190

189-
ClassDeclaration? _findStateClass(ClassElement widgetClassElement) {
191+
ClassDeclaration? _findStateClass(ClassElement2 widgetClassElement) {
190192
for (var declaration in unit.declarations) {
191193
if (declaration is ClassDeclaration) {
192194
var type = declaration.extendsClause?.superclass.type;
@@ -254,56 +256,58 @@ class FlutterConvertToStatelessWidget extends ResolvedCorrectionProducer {
254256
return false;
255257
}
256258

257-
static bool _isState(ClassElement widgetClassElement, DartType? type) {
259+
static bool _isState(ClassElement2 widgetClassElement, DartType? type) {
258260
if (type is! InterfaceType) return false;
259261

260262
var firstArgument = type.typeArguments.singleOrNull;
261263
if (firstArgument is! InterfaceType ||
262-
firstArgument.element != widgetClassElement) {
264+
firstArgument.element3 != widgetClassElement) {
263265
return false;
264266
}
265267

266-
var classElement = type.element;
267-
return classElement is ClassElement && classElement.isExactState;
268+
var classElement = type.element3;
269+
return classElement is ClassElement2 && classElement.isExactState;
268270
}
269271
}
270272

271273
class _FieldFinder extends RecursiveAstVisitor<void> {
272-
Set<FieldElement> fieldsAssignedInConstructors = {};
274+
Set<FieldElement2> fieldsAssignedInConstructors = {};
273275

274276
@override
275277
void visitSimpleIdentifier(SimpleIdentifier node) {
276278
if (node.parent is FieldFormalParameter) {
277-
var element = node.staticElement;
278-
if (element is FieldFormalParameterElement) {
279-
var field = element.field;
279+
var element = node.element;
280+
if (element is FieldFormalParameterElement2) {
281+
var field = element.field2;
280282
if (field != null) {
281283
fieldsAssignedInConstructors.add(field);
282284
}
283285
}
284286
}
285287
if (node.parent is ConstructorFieldInitializer) {
286-
var element = node.staticElement;
287-
if (element is FieldElement) {
288+
var element = node.element;
289+
if (element is FieldElement2) {
288290
fieldsAssignedInConstructors.add(element);
289291
}
290292
}
291293
if (node.inSetterContext()) {
292-
var element = node.writeOrReadElement;
293-
if (element is PropertyAccessorElement) {
294-
var field = element.variable2;
295-
if (field is FieldElement) {
296-
fieldsAssignedInConstructors.add(field);
297-
}
294+
var element = node.writeOrReadElement2;
295+
var field = switch (element) {
296+
GetterElement(:var variable3) => variable3,
297+
SetterElement(:var variable3) => variable3,
298+
_ => null,
299+
};
300+
if (field is FieldElement2) {
301+
fieldsAssignedInConstructors.add(field);
298302
}
299303
}
300304
}
301305
}
302306

303307
class _ReplacementEditBuilder extends RecursiveAstVisitor<void> {
304-
final ClassElement widgetClassElement;
308+
final ClassElement2 widgetClassElement;
305309

306-
final Set<Element> elementsToMove;
310+
final Set<Element2> elementsToMove;
307311

308312
final SourceRange linesRange;
309313

@@ -317,9 +321,9 @@ class _ReplacementEditBuilder extends RecursiveAstVisitor<void> {
317321
if (node.inDeclarationContext()) {
318322
return;
319323
}
320-
var element = node.staticElement;
321-
if (element is ExecutableElement &&
322-
element.enclosingElement3 == widgetClassElement &&
324+
var element = node.element;
325+
if (element is ExecutableElement2 &&
326+
element.enclosingElement2 == widgetClassElement &&
323327
!elementsToMove.contains(element)) {
324328
var parent = node.parent;
325329
if (parent is PrefixedIdentifier) {
@@ -368,41 +372,39 @@ class _StatelessVerifier extends RecursiveAstVisitor<void> {
368372

369373
@override
370374
void visitMethodInvocation(MethodInvocation node) {
371-
var methodElement = node.methodName.staticElement?.declaration;
372-
if (methodElement is ClassMemberElement) {
373-
var classElement = methodElement.enclosingElement3;
374-
if (classElement is ClassElement &&
375-
classElement.isExactState &&
376-
!FlutterConvertToStatelessWidget._isDefaultOverride(
377-
node.thisOrAncestorOfType<MethodDeclaration>())) {
378-
canBeStateless = false;
379-
return;
380-
}
375+
var methodElement = node.methodName.element?.baseElement;
376+
var classElement = methodElement?.enclosingElement2;
377+
if (classElement is ClassElement2 &&
378+
classElement.isExactState &&
379+
!FlutterConvertToStatelessWidget._isDefaultOverride(
380+
node.thisOrAncestorOfType<MethodDeclaration>())) {
381+
canBeStateless = false;
382+
return;
381383
}
382384
super.visitMethodInvocation(node);
383385
}
384386
}
385387

386388
class _StateUsageVisitor extends RecursiveAstVisitor<void> {
387389
bool used = false;
388-
ClassElement widgetClassElement;
389-
ClassElement stateClassElement;
390+
ClassElement2 widgetClassElement;
391+
ClassElement2 stateClassElement;
390392

391393
_StateUsageVisitor(this.widgetClassElement, this.stateClassElement);
392394

393395
@override
394396
void visitInstanceCreationExpression(InstanceCreationExpression node) {
395397
super.visitInstanceCreationExpression(node);
396398
var type = node.staticType;
397-
if (type is! InterfaceType || type.element != stateClassElement) {
399+
if (type is! InterfaceType || type.element3 != stateClassElement) {
398400
return;
399401
}
400402
var methodDeclaration = node.thisOrAncestorOfType<MethodDeclaration>();
401403
var classDeclaration =
402404
methodDeclaration?.thisOrAncestorOfType<ClassDeclaration>();
403405

404406
if (methodDeclaration?.name.lexeme != 'createState' ||
405-
classDeclaration?.declaredElement != widgetClassElement) {
407+
classDeclaration?.declaredFragment!.element != widgetClassElement) {
406408
used = true;
407409
}
408410
}
@@ -413,7 +415,7 @@ class _StateUsageVisitor extends RecursiveAstVisitor<void> {
413415
if (type is InterfaceType &&
414416
node.methodName.name == 'createState' &&
415417
(FlutterConvertToStatelessWidget._isState(widgetClassElement, type) ||
416-
type.element == stateClassElement)) {
418+
type.element3 == stateClassElement)) {
417419
used = true;
418420
}
419421
}

0 commit comments

Comments
 (0)