Skip to content

Commit f2fafcd

Browse files
scheglovcommit-bot@chromium.org
authored andcommitted
Extract and reuse EnclosingExecutableContext.displayName
Change-Id: If411305b7484e59b234366b656d07d2235e6513d Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/151305 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent 48f20c6 commit f2fafcd

File tree

2 files changed

+48
-38
lines changed

2 files changed

+48
-38
lines changed

pkg/analyzer/lib/src/error/return_type_verifier.dart

Lines changed: 24 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -140,36 +140,29 @@ class ReturnTypeVerifier {
140140
var S = getStaticType(expression);
141141

142142
void reportTypeError() {
143-
String displayName = enclosingExecutable.element.displayName;
144-
if (enclosingExecutable.isConstructor) {
145-
var constructor = enclosingExecutable.element as ConstructorElement;
146-
var className = constructor.enclosingElement.displayName;
147-
var constructorBaseName = constructor.displayName;
148-
var constructorName = constructorBaseName.isEmpty
149-
? className
150-
: '$className.$constructorBaseName';
151-
_errorReporter.reportErrorForNode(
152-
StaticTypeWarningCode.RETURN_OF_INVALID_TYPE_FROM_CONSTRUCTOR,
153-
expression,
154-
[S, T, constructorName],
155-
);
156-
} else if (displayName.isEmpty) {
143+
if (enclosingExecutable.isClosure) {
157144
_errorReporter.reportErrorForNode(
158145
StaticTypeWarningCode.RETURN_OF_INVALID_TYPE_FROM_CLOSURE,
159146
expression,
160147
[S, T],
161148
);
162-
} else if (enclosingExecutable.isMethod) {
149+
} else if (enclosingExecutable.isConstructor) {
163150
_errorReporter.reportErrorForNode(
164-
StaticTypeWarningCode.RETURN_OF_INVALID_TYPE_FROM_METHOD,
151+
StaticTypeWarningCode.RETURN_OF_INVALID_TYPE_FROM_CONSTRUCTOR,
165152
expression,
166-
[S, T, displayName],
153+
[S, T, enclosingExecutable.displayName],
167154
);
168-
} else {
155+
} else if (enclosingExecutable.isFunction) {
169156
_errorReporter.reportErrorForNode(
170157
StaticTypeWarningCode.RETURN_OF_INVALID_TYPE_FROM_FUNCTION,
171158
expression,
172-
[S, T, displayName],
159+
[S, T, enclosingExecutable.displayName],
160+
);
161+
} else if (enclosingExecutable.isMethod) {
162+
_errorReporter.reportErrorForNode(
163+
StaticTypeWarningCode.RETURN_OF_INVALID_TYPE_FROM_METHOD,
164+
expression,
165+
[S, T, enclosingExecutable.displayName],
173166
);
174167
}
175168
}
@@ -248,36 +241,29 @@ class ReturnTypeVerifier {
248241
var S = getStaticType(expression);
249242

250243
void reportTypeError() {
251-
String displayName = enclosingExecutable.element.displayName;
252-
if (enclosingExecutable.isConstructor) {
253-
var constructor = enclosingExecutable.element as ConstructorElement;
254-
var className = constructor.enclosingElement.displayName;
255-
var constructorBaseName = constructor.displayName;
256-
var constructorName = constructorBaseName.isEmpty
257-
? className
258-
: '$className.$constructorBaseName';
259-
_errorReporter.reportErrorForNode(
260-
StaticTypeWarningCode.RETURN_OF_INVALID_TYPE_FROM_CONSTRUCTOR,
261-
expression,
262-
[S, T, constructorName],
263-
);
264-
} else if (displayName.isEmpty) {
244+
if (enclosingExecutable.isClosure) {
265245
_errorReporter.reportErrorForNode(
266246
StaticTypeWarningCode.RETURN_OF_INVALID_TYPE_FROM_CLOSURE,
267247
expression,
268248
[S, T],
269249
);
270-
} else if (enclosingExecutable.isMethod) {
250+
} else if (enclosingExecutable.isConstructor) {
271251
_errorReporter.reportErrorForNode(
272-
StaticTypeWarningCode.RETURN_OF_INVALID_TYPE_FROM_METHOD,
252+
StaticTypeWarningCode.RETURN_OF_INVALID_TYPE_FROM_CONSTRUCTOR,
273253
expression,
274-
[S, T, displayName],
254+
[S, T, enclosingExecutable.displayName],
275255
);
276-
} else {
256+
} else if (enclosingExecutable.isFunction) {
277257
_errorReporter.reportErrorForNode(
278258
StaticTypeWarningCode.RETURN_OF_INVALID_TYPE_FROM_FUNCTION,
279259
expression,
280-
[S, T, displayName],
260+
[S, T, enclosingExecutable.displayName],
261+
);
262+
} else if (enclosingExecutable.isMethod) {
263+
_errorReporter.reportErrorForNode(
264+
StaticTypeWarningCode.RETURN_OF_INVALID_TYPE_FROM_METHOD,
265+
expression,
266+
[S, T, enclosingExecutable.displayName],
281267
);
282268
}
283269
}

pkg/analyzer/lib/src/generated/error_verifier.dart

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,32 @@ class EnclosingExecutableContext {
7171

7272
EnclosingExecutableContext.empty() : this(null);
7373

74+
String get displayName {
75+
var element = this.element;
76+
if (element is ConstructorElement) {
77+
var className = element.enclosingElement.displayName;
78+
var constructorName = element.displayName;
79+
return constructorName.isEmpty
80+
? className
81+
: '$className.$constructorName';
82+
} else {
83+
return element.displayName;
84+
}
85+
}
86+
87+
bool get isClosure {
88+
return element is FunctionElement && element.displayName.isEmpty;
89+
}
90+
7491
bool get isConstructor => element is ConstructorElement;
7592

93+
bool get isFunction {
94+
if (element is FunctionElement) {
95+
return element.displayName.isNotEmpty;
96+
}
97+
return element is PropertyAccessorElement;
98+
}
99+
76100
bool get isMethod => element is MethodElement;
77101

78102
bool get isSynchronous => !isAsynchronous;

0 commit comments

Comments
 (0)