Skip to content

Commit 917e519

Browse files
kallentuCommit Queue
authored and
Commit Queue
committed
[analyzer] Dot shorthands: Check for required parameters in invocations.
Added an additional check in the analyzer to make sure required parameters are given. Unit test added. Bug: #59835 Change-Id: I887a46294c961136bd0a1a3999978fe2db99b315 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/428221 Reviewed-by: Chloe Stefantsova <[email protected]> Commit-Queue: Kallen Tu <[email protected]>
1 parent ecc6b93 commit 917e519

File tree

4 files changed

+65
-0
lines changed

4 files changed

+65
-0
lines changed

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

+23
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,29 @@ class RequiredParametersVerifier extends SimpleAstVisitor<void> {
3434
}
3535
}
3636

37+
@override
38+
void visitDotShorthandConstructorInvocation(
39+
DotShorthandConstructorInvocation node,
40+
) {
41+
var constructorElement = node.constructorName.element;
42+
if (constructorElement is ConstructorElement) {
43+
_check(
44+
parameters: constructorElement.formalParameters,
45+
arguments: node.argumentList.arguments,
46+
errorEntity: node.constructorName,
47+
);
48+
}
49+
}
50+
51+
@override
52+
void visitDotShorthandInvocation(DotShorthandInvocation node) {
53+
_check(
54+
parameters: _executableElement(node.memberName.element)?.formalParameters,
55+
arguments: node.argumentList.arguments,
56+
errorEntity: node.memberName,
57+
);
58+
}
59+
3760
@override
3861
void visitEnumConstantDeclaration(EnumConstantDeclaration node) {
3962
_check(

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

+7
Original file line numberDiff line numberDiff line change
@@ -657,9 +657,16 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
657657
node.constKeyword,
658658
);
659659
}
660+
_requiredParametersVerifier.visitDotShorthandConstructorInvocation(node);
660661
super.visitDotShorthandConstructorInvocation(node);
661662
}
662663

664+
@override
665+
void visitDotShorthandInvocation(DotShorthandInvocation node) {
666+
_requiredParametersVerifier.visitDotShorthandInvocation(node);
667+
super.visitDotShorthandInvocation(node);
668+
}
669+
663670
@override
664671
void visitEnumConstantDeclaration(
665672
covariant EnumConstantDeclarationImpl node,

pkg/analyzer/test/src/dart/resolution/dot_shorthand_constructor_invocation_test.dart

+17
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,23 @@ DotShorthandConstructorInvocation
566566
''');
567567
}
568568

569+
test_requiredParameters_missing() async {
570+
await assertErrorsInCode(
571+
r'''
572+
class C {
573+
int x;
574+
C({required this.x});
575+
}
576+
577+
void main() {
578+
C c = .new();
579+
print(c);
580+
}
581+
''',
582+
[error(CompileTimeErrorCode.MISSING_REQUIRED_ARGUMENT, 69, 3)],
583+
);
584+
}
585+
569586
test_typeParameters() async {
570587
await assertErrorsInCode(
571588
r'''

pkg/analyzer/test/src/dart/resolution/dot_shorthand_invocation_test.dart

+18
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,24 @@ DotShorthandInvocation
619619
''');
620620
}
621621

622+
test_requiredParameters_missing() async {
623+
await assertErrorsInCode(
624+
r'''
625+
class C {
626+
static C member({required int x}) => C(x);
627+
int x;
628+
C(this.x);
629+
}
630+
631+
void main() {
632+
C c = .member();
633+
print(c);
634+
}
635+
''',
636+
[error(CompileTimeErrorCode.MISSING_REQUIRED_ARGUMENT, 103, 6)],
637+
);
638+
}
639+
622640
test_typeParameters_inference() async {
623641
await assertNoErrorsInCode(r'''
624642
class C<T> {

0 commit comments

Comments
 (0)