Update ImportedElementsComputer for primary constructors

This CL is to resolve a TODO. I couldn't find a way to trigger any bugs
in the code, but I did add a test. I believe that the reason I couldn't
trigger a bug is because any constructor that has a null `typeName`
will not have a simple identifier in a place that would allow
`_isConstructorDeclarationReturnType` to be invoked.

I realized, while looking at the code, that the type check is
unnecessary. If the type name is null, then the test will somply return
`false`, which is what we'd expect it to do. Given that it isn't needed,
I decided to remove it.

Change-Id: Ib36aaf16475db0db5817e65a8b8bfbb8aa834dbd
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/505501
Reviewed-by: Samuel Rawlins <srawlins@google.com>
Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
This commit is contained in:
Brian Wilkerson
2026-05-22 12:53:51 -07:00
committed by dart-scoped@luci-project-accounts.iam.gserviceaccount.com
parent c8a74e71ef
commit 739826d041
2 changed files with 14 additions and 2 deletions
@@ -148,7 +148,6 @@ class _Visitor extends UnifyingAstVisitor<void> {
static bool _isConstructorDeclarationReturnType(SimpleIdentifier node) {
var parent = node.parent;
// TODO(scheglov): support primary constructors
return parent is ConstructorDeclaration && parent.typeName! == node;
return parent is ConstructorDeclaration && parent.typeName == node;
}
}
@@ -176,6 +176,19 @@ blankLine() {
expect(importedElements, hasLength(0));
}
Future<void> test_none_constructorDeclaration_withNew() async {
var content = '''
[!
class A {
new ();
new named();
}
!]
''';
await _computeElements(content);
expect(importedElements, hasLength(0));
}
Future<void> test_none_constructorDeclarationReturnType() async {
var content = '''
[!