Skip to content

Commit

Permalink
Macro. Issue 55702. Support for SuperFormalParameter as diagnostics t…
Browse files Browse the repository at this point in the history
…arget.

Change-Id: Iaafe02ee1e0f604082fa2605a416296705fb93a6
Bug: #55702
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/366225
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
  • Loading branch information
scheglov authored and Commit Queue committed May 13, 2024
1 parent 4f5bd1c commit 31eb496
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 0 deletions.
10 changes: 10 additions & 0 deletions pkg/analyzer/lib/src/generated/error_verifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6782,6 +6782,10 @@ class _MacroTypeAnnotationLocationConverter {
}
var node = nodeLocation.entity;
switch (node) {
case ConstructorDeclaration():
var parameterList = node.parameters;
var next = parameterList.parameters[location.index];
return nodeLocation.next(next);
case FunctionDeclaration():
var parameterList = node.functionExpression.parameters;
var next = parameterList!.parameters[location.index];
Expand Down Expand Up @@ -6893,12 +6897,18 @@ class _MacroTypeAnnotationLocationConverter {
}
var parent = node.ifTypeOrNull<AstNode>()?.parent;
switch (node) {
case FieldFormalParameter():
var next = node.type ?? node.name;
return nodeLocation.next(next);
case SimpleFormalParameter():
var next = node.type ?? node.name;
if (next == null) {
return null;
}
return nodeLocation.next(next);
case SuperFormalParameter():
var next = node.type ?? node.name;
return nodeLocation.next(next);
case VariableDeclaration():
if (parent is VariableDeclarationList) {
var next = parent.type ?? node.name;
Expand Down
2 changes: 2 additions & 0 deletions pkg/analyzer/lib/src/summary2/macro_declarations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1723,6 +1723,8 @@ class DeclarationBuilderFromNode {
typeAnnotation = _typeAnnotationVariable(node.type, element, location);
case ast.SimpleFormalParameter():
typeAnnotation = _typeAnnotationVariable(node.type, element, location);
case ast.SuperFormalParameter():
typeAnnotation = _typeAnnotationVariable(node.type, element, location);
default:
throw UnimplementedError('(${node.runtimeType}) $node');
}
Expand Down
72 changes: 72 additions & 0 deletions pkg/analyzer/test/src/dart/resolution/macro_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,78 @@ class A {
]);
}

test_diagnostic_report_atTypeAnnotation_class_constructor_formalParameter_positional_super_typed() async {
await assertErrorsInCode('''
import 'diagnostic.dart';
class A {
final int foo;
A(int this.foo);
}
class B extends A {
@ReportAtTypeAnnotation([
'positionalFormalParameterType 0',
])
B(int super.foo);
}
''', [
error(WarningCode.MACRO_WARNING, 172, 3),
]);
}

test_diagnostic_report_atTypeAnnotation_class_constructor_formalParameter_positional_super_untyped() async {
await assertErrorsInCode('''
import 'diagnostic.dart';
class A {
final int foo;
A(int this.foo);
}
class B extends A {
@ReportAtTypeAnnotation([
'positionalFormalParameterType 0',
])
B(super.foo);
}
''', [
error(WarningCode.MACRO_WARNING, 178, 3),
]);
}

test_diagnostic_report_atTypeAnnotation_class_constructor_formalParameter_positional_this_typed() async {
await assertErrorsInCode('''
import 'diagnostic.dart';
class A {
final int foo;
@ReportAtTypeAnnotation([
'positionalFormalParameterType 0',
])
A(int this.foo);
}
''', [
error(WarningCode.MACRO_WARNING, 130, 3),
]);
}

test_diagnostic_report_atTypeAnnotation_class_constructor_formalParameter_positional_this_untyped() async {
await assertErrorsInCode('''
import 'diagnostic.dart';
class A {
final int foo;
@ReportAtTypeAnnotation([
'positionalFormalParameterType 0',
])
A(this.foo);
}
''', [
error(WarningCode.MACRO_WARNING, 135, 3),
]);
}

test_diagnostic_report_atTypeAnnotation_class_extends() async {
await assertErrorsInCode('''
import 'diagnostic.dart';
Expand Down
15 changes: 15 additions & 0 deletions pkg/analyzer/test/src/summary/macro/diagnostic.dart
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ import 'package:macros/macros.dart';
/*macro*/ class ReportAtTypeAnnotation
implements
ClassDeclarationsMacro,
ConstructorDeclarationsMacro,
FunctionDeclarationsMacro,
FieldDeclarationsMacro,
MethodDeclarationsMacro,
Expand All @@ -157,6 +158,11 @@ import 'package:macros/macros.dart';
await _report(declaration, builder);
}

@override
buildDeclarationsForConstructor(declaration, builder) async {
await _report(declaration, builder);
}

@override
buildDeclarationsForField(declaration, builder) async {
await _report(declaration, builder);
Expand Down Expand Up @@ -204,6 +210,15 @@ import 'package:macros/macros.dart';
}
}

if (current is ConstructorDeclaration) {
if (_verbIndex(step, 'namedFormalParameterType') case var index?) {
return current.namedParameters.elementAt(index).type;
}
if (_verbIndex(step, 'positionalFormalParameterType') case var index?) {
return current.positionalParameters.elementAt(index).type;
}
}

if (current is MemberDeclaration) {
if (_verbSuffix(step, 'field') case var fieldName?) {
var definingType = await builder.typeDeclarationOf(
Expand Down
67 changes: 67 additions & 0 deletions pkg/analyzer/test/src/summary/macro_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11215,6 +11215,73 @@ class A {
''');
}

test_class_constructor_positionalParameters_super() async {
await _assertIntrospectText(r'''
import 'package:json/json.dart';
class A {
final int f1;
Point(this.foo);
}
class B extends A {
@Introspect()
B(super.f1);
}
''', r'''
<unnamed>
flags: hasStatic
positionalParameters
f1
flags: isRequired
type: OmittedType
returnType: B
''');
}

test_class_constructor_positionalParameters_super_typed() async {
await _assertIntrospectText(r'''
import 'package:json/json.dart';
class A {
final int f1;
Point(this.foo);
}
class B extends A {
@Introspect()
B(int super.f1);
}
''', r'''
<unnamed>
flags: hasStatic
positionalParameters
f1
flags: isRequired
type: int
returnType: B
''');
}

test_class_constructor_positionalParameters_this() async {
await _assertIntrospectText(r'''
import 'package:json/json.dart';
class A {
final int f1;
@Introspect()
Point(this.foo);
}
''', r'''
Point
positionalParameters
foo
flags: isRequired
type: OmittedType
returnType: OmittedType
''');
}

test_class_constructor_unnamed() async {
await _assertIntrospectText(r'''
class A {
Expand Down

0 comments on commit 31eb496

Please sign in to comment.