@@ -40,119 +40,135 @@ class ElementDescriptor {
4040 /// Return `true` if the described element is a constructor.
4141 bool get isConstructor => kind == ElementKind .constructorKind;
4242
43- /// Return `true` if the given [node] appears to be consistent with this kind
44- /// of element.
43+ /// Return `true` if the given [node] appears to be consistent with the
44+ /// element being described .
4545 bool matches (AstNode node) {
46- // TODO(brianwilkerson) Check the resolved element if one exists for more
46+ // TODO(brianwilkerson) Check the resolved element, if one exists, for more
4747 // accurate results.
4848 switch (kind) {
4949 case ElementKind .classKind:
50- // TODO: Handle this case.
50+ // TODO(brianwilkerson) Handle this case.
5151 return false ;
5252 case ElementKind .constantKind:
53- // TODO: Handle this case.
53+ // TODO(brianwilkerson) Handle this case.
5454 return false ;
5555 case ElementKind .constructorKind:
56- if (node is Annotation ) {
57- var className = _nameFromIdentifier (node.name);
58- var constructorName = node.constructorName ?? '' ;
59- if (components[0 ] == constructorName && components[1 ] == className) {
60- return true ;
61- }
62- } else if (node is InstanceCreationExpression ) {
63- var name = node.constructorName;
64- var className = _nameFromIdentifier (name.type2.name);
65- var constructorName = name.name? .name ?? '' ;
66- if (components[0 ] == constructorName && components[1 ] == className) {
67- return true ;
68- }
69- } else if (node is MethodInvocation ) {
70- var target = node.target;
71- if (target == null ) {
72- if (components[0 ] == '' && components[1 ] == node.methodName.name) {
73- return true ;
74- }
75- } else if (target is Identifier ) {
76- var className = _nameFromIdentifier (target);
77- var constructorName = node.methodName.name;
78- if (components[0 ] == constructorName &&
79- components[1 ] == className) {
80- return true ;
81- }
82- }
83- }
84- return false ;
56+ return _matchesConstructor (node);
8557 case ElementKind .enumKind:
86- // TODO: Handle this case.
58+ // TODO(brianwilkerson) Handle this case.
8759 return false ;
8860 case ElementKind .extensionKind:
89- // TODO: Handle this case.
61+ // TODO(brianwilkerson) Handle this case.
9062 return false ;
9163 case ElementKind .fieldKind:
92- // TODO: Handle this case.
64+ // TODO(brianwilkerson) Handle this case.
9365 return false ;
9466 case ElementKind .functionKind:
95- if (node is MethodInvocation ) {
96- if (node.realTarget == null &&
97- components[0 ] == node.methodName.name) {
98- return true ;
99- }
100- }
101- return false ;
67+ return _matchesFunction (node);
10268 case ElementKind .getterKind:
103- // TODO: Handle this case.
69+ // TODO(brianwilkerson) Handle this case.
10470 return false ;
10571 case ElementKind .methodKind:
106- if (node is MethodInvocation ) {
107- if (components[0 ] == node.methodName.name) {
108- var target = node.realTarget;
109- if (target == null ) {
110- // TODO(brianwilkerson) If `node.target == null` then the invocation
111- // should be in a subclass of the element's class.
112- return true ;
113- } else {
114- var type = target.staticType;
115- if (type == null && target is SimpleIdentifier ) {
116- var element = target.staticElement;
117- // TODO(brianwilkerson) Handle more than `ClassElement`.
118- if (element is ClassElement ) {
119- type = element.thisType;
120- }
121- }
122- if (type == null ) {
123- // We can't get more specific type information, so we assume
124- // that the method might have been in the element's class.
125- return true ;
126- }
127- if (components[1 ] == type.element? .name) {
128- return true ;
129- }
130- if (type is InterfaceType ) {
131- for (var supertype in type.allSupertypes) {
132- if (components[1 ] == supertype.element.name) {
133- return true ;
134- }
135- }
136- }
137- }
138- }
139- }
140- return false ;
72+ return _matchesMethod (node);
14173 case ElementKind .mixinKind:
142- // TODO: Handle this case.
74+ // TODO(brianwilkerson) Handle this case.
14375 return false ;
14476 case ElementKind .setterKind:
145- // TODO: Handle this case.
77+ // TODO(brianwilkerson) Handle this case.
14678 return false ;
14779 case ElementKind .typedefKind:
148- // TODO: Handle this case.
80+ // TODO(brianwilkerson) Handle this case.
14981 return false ;
15082 case ElementKind .variableKind:
151- // TODO: Handle this case.
83+ // TODO(brianwilkerson) Handle this case.
15284 return false ;
15385 }
15486 }
15587
88+ /// Return `true` if the given [node] appears to be consistent with the
89+ /// constructor being described.
90+ bool _matchesConstructor (AstNode node) {
91+ if (node is Annotation ) {
92+ var className = _nameFromIdentifier (node.name);
93+ var constructorName = node.constructorName ?? '' ;
94+ if (components[0 ] == constructorName && components[1 ] == className) {
95+ return true ;
96+ }
97+ } else if (node is InstanceCreationExpression ) {
98+ var name = node.constructorName;
99+ var className = _nameFromIdentifier (name.type2.name);
100+ var constructorName = name.name? .name ?? '' ;
101+ if (components[0 ] == constructorName && components[1 ] == className) {
102+ return true ;
103+ }
104+ } else if (node is MethodInvocation ) {
105+ var target = node.target;
106+ if (target == null ) {
107+ if (components[0 ] == '' && components[1 ] == node.methodName.name) {
108+ return true ;
109+ }
110+ } else if (target is Identifier ) {
111+ var className = _nameFromIdentifier (target);
112+ var constructorName = node.methodName.name;
113+ if (components[0 ] == constructorName && components[1 ] == className) {
114+ return true ;
115+ }
116+ }
117+ }
118+ return false ;
119+ }
120+
121+ /// Return `true` if the given [node] appears to be consistent with the
122+ /// function being described.
123+ bool _matchesFunction (AstNode node) {
124+ if (node is MethodInvocation ) {
125+ if (node.realTarget == null && components[0 ] == node.methodName.name) {
126+ return true ;
127+ }
128+ }
129+ return false ;
130+ }
131+
132+ /// Return `true` if the given [node] appears to be consistent with the
133+ /// method being described.
134+ bool _matchesMethod (AstNode node) {
135+ if (node is MethodInvocation ) {
136+ if (components[0 ] == node.methodName.name) {
137+ var target = node.realTarget;
138+ if (target == null ) {
139+ // TODO(brianwilkerson) If `node.target == null` then the invocation
140+ // should be in a subclass of the element's class.
141+ return true ;
142+ } else {
143+ var type = target.staticType;
144+ if (type == null && target is SimpleIdentifier ) {
145+ var element = target.staticElement;
146+ // TODO(brianwilkerson) Handle more than `ClassElement`.
147+ if (element is ClassElement ) {
148+ type = element.thisType;
149+ }
150+ }
151+ if (type == null ) {
152+ // We can't get more specific type information, so we assume
153+ // that the method might have been in the element's class.
154+ return true ;
155+ }
156+ if (components[1 ] == type.element? .name) {
157+ return true ;
158+ }
159+ if (type is InterfaceType ) {
160+ for (var supertype in type.allSupertypes) {
161+ if (components[1 ] == supertype.element.name) {
162+ return true ;
163+ }
164+ }
165+ }
166+ }
167+ }
168+ }
169+ return false ;
170+ }
171+
156172 String _nameFromIdentifier (Identifier identifier) {
157173 if (identifier is SimpleIdentifier ) {
158174 return identifier.name;
0 commit comments