Skip to content

Commit 519e288

Browse files
scheglovCommit Queue
authored andcommitted
Elements. Migrate ElementNameUnion.
Change-Id: I163fb60cea95f8e7a53b4be75239864b65bdfbd1 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/403925 Reviewed-by: Paul Berry <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent 01f5956 commit 519e288

File tree

3 files changed

+139
-77
lines changed

3 files changed

+139
-77
lines changed

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

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5562,18 +5562,23 @@ abstract class InstanceElementImpl2 extends ElementImpl2
55625562
LibraryElement2 get enclosingElement2 => firstFragment.library;
55635563

55645564
@override
5565-
List<FieldElement2> get fields2 =>
5566-
fields.map((e) => e.asElement2 as FieldElement2?).nonNulls.toList();
5565+
List<FieldElement2> get fields2 {
5566+
_readMembers();
5567+
return fields.map((e) => e.asElement2 as FieldElement2?).nonNulls.toList();
5568+
}
55675569

55685570
@override
55695571
InstanceElementImpl get firstFragment;
55705572

55715573
@override
5572-
List<GetterElement> get getters2 => accessors
5573-
.where((e) => e.isGetter)
5574-
.map((e) => e.asElement2 as GetterElement?)
5575-
.nonNulls
5576-
.toList();
5574+
List<GetterElement> get getters2 {
5575+
_readMembers();
5576+
return accessors
5577+
.where((e) => e.isGetter)
5578+
.map((e) => e.asElement2 as GetterElement?)
5579+
.nonNulls
5580+
.toList();
5581+
}
55775582

55785583
@override
55795584
String get identifier => name3 ?? firstFragment.identifier;
@@ -5613,11 +5618,14 @@ abstract class InstanceElementImpl2 extends ElementImpl2
56135618
AnalysisSession? get session => firstFragment.session;
56145619

56155620
@override
5616-
List<SetterElement> get setters2 => accessors
5617-
.where((e) => e.isSetter)
5618-
.map((e) => e.asElement2 as SetterElement?)
5619-
.nonNulls
5620-
.toList();
5621+
List<SetterElement> get setters2 {
5622+
_readMembers();
5623+
return accessors
5624+
.where((e) => e.isSetter)
5625+
.map((e) => e.asElement2 as SetterElement?)
5626+
.nonNulls
5627+
.toList();
5628+
}
56215629

56225630
@override
56235631
List<TypeParameterElementImpl2> get typeParameters2 =>
@@ -5892,6 +5900,11 @@ abstract class InstanceElementImpl2 extends ElementImpl2
58925900
Iterable<PropertyAccessorElement2> _implementationsOfSetter2(String name) {
58935901
return _implementationsOfSetter(name).map((e) => e.asElement2);
58945902
}
5903+
5904+
void _readMembers() {
5905+
// TODO(scheglov): use better implementation
5906+
firstFragment.element;
5907+
}
58955908
}
58965909

58975910
abstract class InterfaceElementImpl extends InstanceElementImpl

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

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
// ignore_for_file: analyzer_use_new_elements
6-
75
import 'dart:typed_data';
86

9-
import 'package:analyzer/dart/element/element.dart';
10-
import 'package:analyzer/dart/element/visitor.dart';
7+
import 'package:analyzer/dart/element/element2.dart';
8+
import 'package:analyzer/dart/element/visitor2.dart';
119

1210
/// Union of a set of names.
1311
class ElementNameUnion {
@@ -92,33 +90,45 @@ class ElementNameUnion {
9290
return true;
9391
}
9492

95-
static ElementNameUnion forLibrary(LibraryElement libraryElement) {
93+
static ElementNameUnion forLibrary(LibraryElement2 libraryElement) {
9694
var result = ElementNameUnion.empty();
97-
libraryElement.accept(
98-
_ElementVisitor(result),
95+
libraryElement.accept2(
96+
_ElementVisitor2(result),
9997
);
10098
return result;
10199
}
100+
101+
static bool _hasInterestingElements(Element2 element) {
102+
if (element is ExecutableElement2) {
103+
return false;
104+
}
105+
return true;
106+
}
107+
108+
static bool _isInterestingElement(Element2 element) {
109+
return element.enclosingElement2 is LibraryElement2 ||
110+
element is FieldElement2 ||
111+
element is MethodElement2 ||
112+
element is PropertyAccessorElement2;
113+
}
102114
}
103115

104-
class _ElementVisitor extends GeneralizingElementVisitor<void> {
116+
class _ElementVisitor2 extends GeneralizingElementVisitor2<void> {
105117
final ElementNameUnion union;
106118

107-
_ElementVisitor(this.union);
119+
_ElementVisitor2(this.union);
108120

109121
@override
110-
void visitElement(Element element) {
111-
var enclosing = element.enclosingElement3;
112-
if (enclosing is CompilationUnitElement ||
113-
element is FieldElement ||
114-
element is MethodElement ||
115-
element is PropertyAccessorElement) {
116-
var name = element.name;
122+
void visitElement(Element2 element) {
123+
if (ElementNameUnion._isInterestingElement(element)) {
124+
var name = element.name3;
117125
if (name != null) {
118126
union.add(name);
119127
}
120128
}
121129

122-
super.visitElement(element);
130+
if (ElementNameUnion._hasInterestingElements(element)) {
131+
super.visitElement(element);
132+
}
123133
}
124134
}

pkg/analyzer/test/src/dart/element/name_union_test.dart

Lines changed: 87 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,10 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
// ignore_for_file: analyzer_use_new_elements
6-
7-
import 'package:analyzer/dart/analysis/results.dart';
8-
import 'package:analyzer/dart/element/element.dart';
9-
import 'package:analyzer/dart/element/visitor.dart';
10-
import 'package:analyzer/src/dart/element/name_union.dart';
115
import 'package:test/test.dart';
126
import 'package:test_reflective_loader/test_reflective_loader.dart';
137

14-
import '../resolution/context_collection_resolution.dart';
8+
import '../../summary/elements_base.dart';
159

1610
main() {
1711
defineReflectiveSuite(() {
@@ -20,54 +14,99 @@ main() {
2014
}
2115

2216
@reflectiveTest
23-
class ElementNameUnionTest extends PubPackageResolutionTest {
24-
test_it() async {
25-
await _checkLibrary('dart:async');
26-
await _checkLibrary('dart:core');
27-
await _checkLibrary('dart:math');
28-
}
17+
class ElementNameUnionTest extends ElementsBaseTest {
18+
@override
19+
bool get keepLinkingLibraries => false;
20+
21+
test_class() async {
22+
var library = await buildLibrary(r'''
23+
class MyClass {
24+
final myField = 0;
25+
int get myGetter => 0;
26+
set mySetter(int _) {}
27+
void myMethod() {}
28+
}
29+
''');
30+
31+
var nameUnion = library.nameUnion;
32+
expect(nameUnion.contains('MyClass'), isTrue);
33+
expect(nameUnion.contains('NotMyClass'), isFalse);
2934

30-
Future<void> _checkLibrary(String uriStr) async {
31-
var analysisContext = contextFor(testFile);
32-
var analysisSession = analysisContext.currentSession;
35+
expect(nameUnion.contains('myField'), isTrue);
36+
expect(nameUnion.contains('NotMyField'), isFalse);
3337

34-
var result = await analysisSession.getLibraryByUri(uriStr);
35-
result as LibraryElementResult;
36-
var element = result.element;
38+
expect(nameUnion.contains('myGetter'), isTrue);
39+
expect(nameUnion.contains('NotMyGetter'), isFalse);
3740

38-
var union = ElementNameUnion.forLibrary(element);
39-
element.accept(
40-
_ElementVisitor(union),
41-
);
41+
expect(nameUnion.contains('mySetter'), isTrue);
42+
expect(nameUnion.contains('NotMySetter'), isFalse);
43+
44+
expect(nameUnion.contains('myMethod'), isTrue);
45+
expect(nameUnion.contains('NotMyMethod'), isFalse);
4246
}
47+
48+
test_enum() async {
49+
var library = await buildLibrary(r'''
50+
enum MyEnum {
51+
myValue
4352
}
53+
''');
54+
55+
var nameUnion = library.nameUnion;
56+
expect(nameUnion.contains('MyEnum'), isTrue);
57+
expect(nameUnion.contains('NotMyEnum'), isFalse);
4458

45-
/// Checks that the name of every interesting element is in [union].
46-
class _ElementVisitor extends GeneralizingElementVisitor<void> {
47-
final ElementNameUnion union;
59+
expect(nameUnion.contains('MyValue'), isTrue);
60+
expect(nameUnion.contains('NotMyValue'), isFalse);
61+
}
4862

49-
_ElementVisitor(this.union);
63+
test_extension() async {
64+
var library = await buildLibrary(r'''
65+
extension MyExtension on int {}
66+
''');
5067

51-
@override
52-
void visitElement(Element element) {
53-
var enclosing = element.enclosingElement3;
54-
if (enclosing is CompilationUnitElement ||
55-
element is FieldElement ||
56-
element is MethodElement ||
57-
element is PropertyAccessorElement) {
58-
var name = element.name;
59-
if (name != null) {
60-
expect(union.contains(name), isTrue, reason: 'Expected to find $name');
61-
// This might fail, but the probability is low. If this does fail, try
62-
// adding another `z` to the prefix.
63-
expect(
64-
union.contains('zz$name'),
65-
isFalse,
66-
reason: 'Expected to not find $name',
67-
);
68-
}
69-
}
70-
71-
super.visitElement(element);
68+
var nameUnion = library.nameUnion;
69+
expect(nameUnion.contains('MyExtension'), isTrue);
70+
expect(nameUnion.contains('NotMyExtension'), isFalse);
71+
}
72+
73+
test_extensionType() async {
74+
var library = await buildLibrary(r'''
75+
extension type MyExtensionType(int it) {}
76+
''');
77+
78+
var nameUnion = library.nameUnion;
79+
expect(nameUnion.contains('MyExtensionType'), isTrue);
80+
expect(nameUnion.contains('NotMyExtensionType'), isFalse);
81+
}
82+
83+
test_mixin() async {
84+
var library = await buildLibrary(r'''
85+
mixin MyMixin {}
86+
''');
87+
88+
var nameUnion = library.nameUnion;
89+
expect(nameUnion.contains('MyMixin'), isTrue);
90+
expect(nameUnion.contains('NotMyMixin'), isFalse);
91+
}
92+
93+
test_topLevelVariable() async {
94+
var library = await buildLibrary(r'''
95+
final myVariable = 0;
96+
''');
97+
98+
var nameUnion = library.nameUnion;
99+
expect(nameUnion.contains('myVariable'), isTrue);
100+
expect(nameUnion.contains('NotMyVariable'), isFalse);
101+
}
102+
103+
test_typedef() async {
104+
var library = await buildLibrary(r'''
105+
typedef MyTypedef = int;
106+
''');
107+
108+
var nameUnion = library.nameUnion;
109+
expect(nameUnion.contains('MyTypedef'), isTrue);
110+
expect(nameUnion.contains('NotMyTypedef'), isFalse);
72111
}
73112
}

0 commit comments

Comments
 (0)