Skip to content

Commit 36e605d

Browse files
bwilkersonCommit Queue
authored andcommitted
[Migrate] flutter.dart (extensions)
Change-Id: I9546d82fd8bd307a06ca9b56ca38a7c4d8cbbce6 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/403140 Commit-Queue: Brian Wilkerson <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]>
1 parent fb5fbf1 commit 36e605d

File tree

3 files changed

+30
-99
lines changed

3 files changed

+30
-99
lines changed

pkg/analysis_server/lib/src/cider/rename.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ class CanRenameResponse {
9898
}
9999

100100
FlutterWidgetState? _findFlutterStateClass(Element element, String newName) {
101-
if (element is ClassElement && element.isStatefulWidgetDeclaration) {
101+
if (element is ClassElement &&
102+
element.asElement2.isStatefulWidgetDeclaration) {
102103
var oldStateName = '${element.displayName}State';
103104
var library = element.library;
104105
var state =

pkg/analyzer/analyzer_use_new_elements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@ lib/src/test_utilities/find_element.dart
142142
lib/src/test_utilities/find_node.dart
143143
lib/src/test_utilities/mock_sdk_elements.dart
144144
lib/src/utilities/extensions/element.dart
145-
lib/src/utilities/extensions/flutter.dart
146145
lib/src/utilities/extensions/library_element.dart
147146
test/error/error_reporter_test.dart
148147
test/generated/elements_types_mixin.dart

pkg/analyzer/lib/src/utilities/extensions/flutter.dart

Lines changed: 28 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'package:analyzer/dart/ast/ast.dart';
6-
import 'package:analyzer/dart/element/element.dart';
76
import 'package:analyzer/dart/element/element2.dart';
87
import 'package:analyzer/dart/element/type.dart';
98
import 'package:analyzer/src/utilities/extensions/string.dart';
@@ -169,18 +168,6 @@ extension AstNodeExtension on AstNode? {
169168
}
170169
}
171170

172-
extension ClassElementExtension on ClassElement {
173-
/// Whether this is the Flutter class `State`.
174-
bool get isExactState => _isExactly(_nameState, _uriFramework);
175-
176-
/// Whether this has the Flutter class `State` as a superclass.
177-
bool get isState => _hasSupertype(_uriFramework, _nameState);
178-
179-
/// Whether this is a [ClassElement] that extends the Flutter class
180-
/// `StatefulWidget`.
181-
bool get isStatefulWidgetDeclaration => supertype.isExactlyStatefulWidgetType;
182-
}
183-
184171
extension ClassElementExtension2 on ClassElement2 {
185172
/// Whether this is the Flutter class `State`.
186173
bool get isExactState => _isExactly(_nameState, _uriFramework);
@@ -201,8 +188,8 @@ extension DartTypeExtension on DartType? {
201188
return false;
202189
}
203190

204-
return [self, ...self.element.allSupertypes].any((t) =>
205-
t.element.name == 'Color' && t.element.library.name == 'dart.ui');
191+
return [self, ...self.element3.allSupertypes].any((t) =>
192+
t.element3.name3 == 'Color' && t.element3.library2.name3 == 'dart.ui');
206193
}
207194

208195
/// Whether this is the Flutter mixin `Diagnosticable` or a subtype.
@@ -212,93 +199,93 @@ extension DartTypeExtension on DartType? {
212199
return false;
213200
}
214201

215-
return [self, ...self.element.allSupertypes].any((t) =>
216-
t.element.name == 'Diagnosticable' &&
217-
t.element.source.uri == _uriDiagnostics);
202+
return [self, ...self.element3.allSupertypes].any((t) =>
203+
t.element3.name3 == 'Diagnosticable' &&
204+
t.element3.library2.firstFragment.source.uri == _uriDiagnostics);
218205
}
219206

220207
/// Whether this is the Flutter type `EdgeInsetsGeometry`.
221208
bool get isExactEdgeInsetsGeometryType {
222209
var self = this;
223210
return self is InterfaceType &&
224-
self.element._isExactly('EdgeInsetsGeometry', _uriEdgeInsets);
211+
self.element3._isExactly('EdgeInsetsGeometry', _uriEdgeInsets);
225212
}
226213

227214
/// Whether this is the Flutter class `StatefulWidget`.
228215
bool get isExactlyStatefulWidgetType {
229216
var self = this;
230217
return self is InterfaceType &&
231-
self.element._isExactly(_nameStatefulWidget, _uriFramework);
218+
self.element3._isExactly(_nameStatefulWidget, _uriFramework);
232219
}
233220

234221
/// Whether this is the Flutter class `StatelessWidget`.
235222
bool get isExactlyStatelessWidgetType {
236223
var self = this;
237224
return self is InterfaceType &&
238-
self.element._isExactly(_nameStatelessWidget, _uriFramework);
225+
self.element3._isExactly(_nameStatelessWidget, _uriFramework);
239226
}
240227

241228
/// Whether this is the Flutter class `Align`.
242229
bool get isExactWidgetTypeAlign {
243230
var self = this;
244231
return self is InterfaceType &&
245-
self.element._isExactly(_nameAlign, _uriBasic);
232+
self.element3._isExactly(_nameAlign, _uriBasic);
246233
}
247234

248235
/// Whether this is the Flutter class `StreamBuilder`.
249236
bool get isExactWidgetTypeBuilder {
250237
var self = this;
251238
return self is InterfaceType &&
252-
self.element._isExactly(_nameBuilder, _uriBasic);
239+
self.element3._isExactly(_nameBuilder, _uriBasic);
253240
}
254241

255242
/// Whether this is the Flutter class `Center`.
256243
bool get isExactWidgetTypeCenter {
257244
var self = this;
258245
return self is InterfaceType &&
259-
self.element._isExactly(_nameCenter, _uriBasic);
246+
self.element3._isExactly(_nameCenter, _uriBasic);
260247
}
261248

262249
/// Whether this is the Flutter class `Container`.
263250
bool get isExactWidgetTypeContainer {
264251
var self = this;
265252
return self is InterfaceType &&
266-
self.element._isExactly(_nameContainer, _uriContainer);
253+
self.element3._isExactly(_nameContainer, _uriContainer);
267254
}
268255

269256
/// Whether this is the Flutter class `Expanded`.
270257
bool get isExactWidgetTypeExpanded {
271258
var self = this;
272259
return self is InterfaceType &&
273-
self.element._isExactly(_nameExpanded, _uriBasic);
260+
self.element3._isExactly(_nameExpanded, _uriBasic);
274261
}
275262

276263
/// Whether this is the Flutter class `Flexible`.
277264
bool get isExactWidgetTypeFlexible {
278265
var self = this;
279266
return self is InterfaceType &&
280-
self.element._isExactly(_nameFlexible, _uriBasic);
267+
self.element3._isExactly(_nameFlexible, _uriBasic);
281268
}
282269

283270
/// Whether this is the Flutter class `Padding`.
284271
bool get isExactWidgetTypePadding {
285272
var self = this;
286273
return self is InterfaceType &&
287-
self.element._isExactly(_namePadding, _uriBasic);
274+
self.element3._isExactly(_namePadding, _uriBasic);
288275
}
289276

290277
/// Whether this is the Flutter class `SizedBox`.
291278
bool get isExactWidgetTypeSizedBox {
292279
var self = this;
293280
return self is InterfaceType &&
294-
self.element._isExactly(_nameSizedBox, _uriBasic);
281+
self.element3._isExactly(_nameSizedBox, _uriBasic);
295282
}
296283

297284
/// Whether this is the Flutter class `StreamBuilder`.
298285
bool get isExactWidgetTypeStreamBuilder {
299286
var self = this;
300287
return self is InterfaceType &&
301-
self.element._isExactly(_nameStreamBuilder, _uriAsync);
288+
self.element3._isExactly(_nameStreamBuilder, _uriAsync);
302289
}
303290

304291
/// Whether this is the Flutter class `Widget`, or its subtype.
@@ -317,15 +304,15 @@ extension DartTypeExtension on DartType? {
317304
return false;
318305
}
319306

320-
return [self, ...self.element.allSupertypes].any((t) =>
321-
t.element.name == 'Matrix4' &&
322-
t.element.library.name == 'vector_math_64');
307+
return [self, ...self.element3.allSupertypes].any((t) =>
308+
t.element3.name3 == 'Matrix4' &&
309+
t.element3.library2.name3 == 'vector_math_64');
323310
}
324311

325312
/// Whether this is the Flutter class `Widget`, or its subtype.
326313
bool get isWidgetType {
327314
var self = this;
328-
return self is InterfaceType && self.element.isWidget;
315+
return self is InterfaceType && self.element3.isWidget;
329316
}
330317
}
331318

@@ -389,8 +376,7 @@ extension InstanceCreationExpressionExtension on InstanceCreationExpression {
389376
/// Whether this is a constructor invocation for a class that has the Flutter
390377
/// class `Widget` as a superclass.
391378
bool get isWidgetCreation {
392-
var element = constructorName
393-
.staticElement?.enclosingElement3.augmented.firstFragment;
379+
var element = constructorName.element?.enclosingElement2;
394380
return element.isWidget;
395381
}
396382

@@ -408,8 +394,7 @@ extension InstanceCreationExpressionExtension on InstanceCreationExpression {
408394

409395
/// The presentation for this node.
410396
String? get widgetPresentationText {
411-
var element = constructorName
412-
.staticElement?.enclosingElement3.augmented.firstFragment;
397+
var element = constructorName.element?.enclosingElement2;
413398
if (!element.isWidget) {
414399
return null;
415400
}
@@ -432,7 +417,7 @@ extension InstanceCreationExpressionExtension on InstanceCreationExpression {
432417
return 'Text';
433418
}
434419
}
435-
return element?.name;
420+
return element?.name3;
436421
}
437422
}
438423

@@ -450,61 +435,7 @@ extension InterfaceElement2Extension on InterfaceElement2? {
450435
return true;
451436
}
452437
return self.allSupertypes
453-
.any((type) => type.element._isExactly(_nameFlex, _uriBasic));
454-
}
455-
}
456-
457-
extension InterfaceElementExtension on InterfaceElement? {
458-
/// Whether this is the Flutter class `Alignment`.
459-
bool get isExactAlignment {
460-
return _isExactly('Alignment', _uriAlignment);
461-
}
462-
463-
/// Whether this is the Flutter class `AlignmentDirectional`.
464-
bool get isExactAlignmentDirectional {
465-
return _isExactly('AlignmentDirectional', _uriAlignment);
466-
}
467-
468-
/// Whether this is the Flutter class `AlignmentGeometry`.
469-
bool get isExactAlignmentGeometry {
470-
return _isExactly('AlignmentGeometry', _uriAlignment);
471-
}
472-
473-
/// Whether this is the Flutter class `Widget`, or a subtype.
474-
bool get isWidget {
475-
var self = this;
476-
if (self is! ClassElement) {
477-
return false;
478-
}
479-
if (_isExactly(_nameWidget, _uriFramework)) {
480-
return true;
481-
}
482-
return self.allSupertypes
483-
.any((type) => type.element._isExactly(_nameWidget, _uriFramework));
484-
}
485-
486-
/// Whether this has a supertype with the [requiredName] defined in the file
487-
/// with the [requiredUri].
488-
bool _hasSupertype(Uri requiredUri, String requiredName) {
489-
var self = this;
490-
if (self == null) {
491-
return false;
492-
}
493-
for (var type in self.allSupertypes) {
494-
if (type.element.name == requiredName) {
495-
var uri = type.element.source.uri;
496-
if (uri == requiredUri) {
497-
return true;
498-
}
499-
}
500-
}
501-
return false;
502-
}
503-
504-
/// Whether this is the exact [type] defined in the file with the given [uri].
505-
bool _isExactly(String type, Uri uri) {
506-
var self = this;
507-
return self is ClassElement && self.name == type && self.source.uri == uri;
438+
.any((type) => type.element3._isExactly(_nameFlex, _uriBasic));
508439
}
509440
}
510441

@@ -534,7 +465,7 @@ extension InterfaceElementExtension2 on InterfaceElement2? {
534465
return true;
535466
}
536467
return self.allSupertypes
537-
.any((type) => type.element._isExactly(_nameWidget, _uriFramework));
468+
.any((type) => type.element3._isExactly(_nameWidget, _uriFramework));
538469
}
539470

540471
/// Whether this has a supertype with the [requiredName] defined in the file
@@ -545,8 +476,8 @@ extension InterfaceElementExtension2 on InterfaceElement2? {
545476
return false;
546477
}
547478
for (var type in self.allSupertypes) {
548-
if (type.element.name == requiredName) {
549-
var uri = type.element.source.uri;
479+
if (type.element3.name3 == requiredName) {
480+
var uri = type.element3.library2.firstFragment.source.uri;
550481
if (uri == requiredUri) {
551482
return true;
552483
}

0 commit comments

Comments
 (0)