Update 'mightBeTypeIdentifier' to return the type name, or null.

This way, instead of implicit result that the given node is a
SimpleIdentifier and requiring a cast in the client, or a
PrefixedIdentifier - so we have to check whether it is one or another
again; we now prove the result by returning the actual value, not
just a flag.

Change-Id: I65f0a21e5d3c1e7be06e621a0f7fb6a5e6b2db2d
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/232780
Reviewed-by: Phil Quitslund <pquitslund@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
This commit is contained in:
Konstantin Shcheglov 2022-02-17 17:04:30 +00:00 committed by Commit Bot
parent 61415f99e5
commit aec9bccdd9
5 changed files with 20 additions and 24 deletions

View file

@ -541,16 +541,15 @@ abstract class _AbstractCorrectionProducer {
return false;
}
/// Return `true` if the [node] might be a type name.
bool mightBeTypeIdentifier(AstNode node) {
/// If the [node] might be a type name, return its name.
String? nameOfType(AstNode node) {
if (node is SimpleIdentifier) {
var parent = node.parent;
if (parent is NamedType) {
return true;
var name = node.name;
if (node.parent is NamedType || _isNameOfType(name)) {
return name;
}
return _isNameOfType(node.name);
}
return false;
return null;
}
/// Replace all occurrences of the [oldIndent] with the [newIndent] within the

View file

@ -74,9 +74,9 @@ class ChangeTo extends CorrectionProducer {
node = node.identifier;
}
// Process if looks like a type.
if (mightBeTypeIdentifier(node)) {
var name = nameOfType(node);
if (name != null) {
// Prepare for selecting the closest element.
var name = (node as SimpleIdentifier).name;
var finder = _ClosestElementFinder(
name, (Element element) => element is ClassElement);
// Check elements of this library.

View file

@ -48,10 +48,13 @@ class CreateClass extends CorrectionProducer {
} else {
return;
}
if (!mightBeTypeIdentifier(nameNode)) {
final className = nameOfType(nameNode);
if (className == null) {
return;
}
className = nameNode.name;
this.className = className;
// prepare environment
Element targetUnit;
var prefix = '';

View file

@ -49,7 +49,7 @@ class CreateMixin extends CorrectionProducer {
} else {
return;
}
if (!mightBeTypeIdentifier(nameNode)) {
if (nameOfType(nameNode) == null) {
return;
}
// prepare environment

View file

@ -104,10 +104,8 @@ class ImportLibrary extends MultiCorrectionProducer {
targetNode = name;
}
}
if (mightBeTypeIdentifier(targetNode)) {
var typeName = (targetNode is SimpleIdentifier)
? targetNode.name
: (targetNode as PrefixedIdentifier).prefix.name;
var typeName = nameOfType(targetNode);
if (typeName != null) {
yield* _importLibraryForElement(typeName, const [
ElementKind.CLASS,
ElementKind.ENUM,
@ -124,17 +122,13 @@ class ImportLibrary extends MultiCorrectionProducer {
}
@override
bool mightBeTypeIdentifier(AstNode node) {
if (super.mightBeTypeIdentifier(node)) {
return true;
}
String? nameOfType(AstNode node) {
if (node is PrefixedIdentifier) {
var parent = node.parent;
if (parent is NamedType) {
return true;
if (node.parent is NamedType) {
return node.prefix.name;
}
}
return false;
return super.nameOfType(node);
}
Stream<CorrectionProducer> _importExtensionInLibrary(