Skip to content

Commit c14fd57

Browse files
stereotype441commit-bot@chromium.org
authored andcommitted
Update AstBuilder to handle generic annotations.
Bug: #44838 Change-Id: If69cbbc13a087f0f6280da0bc7f9dbca1d3ec0ea Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/182740 Reviewed-by: Brian Wilkerson <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]> Commit-Queue: Paul Berry <[email protected]>
1 parent 93cd0cd commit c14fd57

File tree

3 files changed

+186
-0
lines changed

3 files changed

+186
-0
lines changed

pkg/analyzer/lib/src/fasta/ast_builder.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1842,6 +1842,7 @@ class AstBuilder extends StackListener {
18421842
push(ast.annotation(
18431843
atSign: atSign,
18441844
name: name,
1845+
typeArguments: typeArguments,
18451846
period: periodBeforeName,
18461847
constructorName: constructorName,
18471848
arguments: invocation?.argumentList));
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:analyzer/dart/analysis/features.dart';
6+
import 'package:analyzer/dart/ast/ast.dart';
7+
import 'package:analyzer/src/dart/error/syntactic_errors.dart';
8+
import 'package:test/test.dart';
9+
import 'package:test_reflective_loader/test_reflective_loader.dart';
10+
11+
import 'parser_test_base.dart';
12+
import 'test_support.dart';
13+
14+
main() {
15+
defineReflectiveSuite(() {
16+
defineReflectiveTests(GenericMetadataEnabledParserTest);
17+
defineReflectiveTests(GenericMetadataDisabledParserTest);
18+
});
19+
}
20+
21+
@reflectiveTest
22+
class GenericMetadataDisabledParserTest extends FastaParserTestCase
23+
with GenericMetadataParserTest {
24+
@override
25+
CompilationUnit _parseCompilationUnit(String content,
26+
{List<ExpectedError>? errors, required ExpectedError? disabledError}) {
27+
var combinedErrors =
28+
disabledError == null ? errors : [disabledError, ...?errors];
29+
return parseCompilationUnit(content,
30+
errors: combinedErrors,
31+
featureSet: FeatureSet.forTesting(sdkVersion: '2.12'));
32+
}
33+
}
34+
35+
@reflectiveTest
36+
class GenericMetadataEnabledParserTest extends FastaParserTestCase
37+
with GenericMetadataParserTest {
38+
@override
39+
CompilationUnit _parseCompilationUnit(String content,
40+
{List<ExpectedError>? errors,
41+
required ExpectedError? disabledError}) =>
42+
parseCompilationUnit(content,
43+
errors: errors,
44+
featureSet: FeatureSet.forTesting(
45+
sdkVersion: '2.12',
46+
additionalFeatures: [Feature.generic_metadata]));
47+
}
48+
49+
mixin GenericMetadataParserTest on FastaParserTestCase {
50+
void test_className_prefixed_constructorName_absent() {
51+
var compilationUnit = _parseCompilationUnit('@p.A<B>() class C {}',
52+
disabledError: expectedError(
53+
ParserErrorCode.ANNOTATION_WITH_TYPE_ARGUMENTS, 4, 1));
54+
var classDeclaration =
55+
compilationUnit.declarations.single as ClassDeclaration;
56+
var annotation = classDeclaration.metadata.single;
57+
var className = annotation.name as PrefixedIdentifier;
58+
expect(className.prefix.name, 'p');
59+
expect(className.identifier.name, 'A');
60+
var typeArgument = annotation.typeArguments!.arguments.single as TypeName;
61+
var typeArgumentName = typeArgument.name as SimpleIdentifier;
62+
expect(typeArgumentName.name, 'B');
63+
expect(annotation.constructorName, isNull);
64+
}
65+
66+
void test_className_prefixed_constructorName_present() {
67+
var compilationUnit = _parseCompilationUnit('@p.A<B>.ctor() class C {}',
68+
disabledError: expectedError(
69+
ParserErrorCode.ANNOTATION_WITH_TYPE_ARGUMENTS, 4, 1));
70+
var classDeclaration =
71+
compilationUnit.declarations.single as ClassDeclaration;
72+
var annotation = classDeclaration.metadata.single;
73+
var className = annotation.name as PrefixedIdentifier;
74+
expect(className.prefix.name, 'p');
75+
expect(className.identifier.name, 'A');
76+
var typeArgument = annotation.typeArguments!.arguments.single as TypeName;
77+
var typeArgumentName = typeArgument.name as SimpleIdentifier;
78+
expect(typeArgumentName.name, 'B');
79+
expect(annotation.constructorName!.name, 'ctor');
80+
}
81+
82+
void test_className_unprefixed_constructorName_absent() {
83+
var compilationUnit = _parseCompilationUnit('@A<B>() class C {}',
84+
disabledError: expectedError(
85+
ParserErrorCode.ANNOTATION_WITH_TYPE_ARGUMENTS, 2, 1));
86+
var classDeclaration =
87+
compilationUnit.declarations.single as ClassDeclaration;
88+
var annotation = classDeclaration.metadata.single;
89+
var className = annotation.name as SimpleIdentifier;
90+
expect(className.name, 'A');
91+
var typeArgument = annotation.typeArguments!.arguments.single as TypeName;
92+
var typeArgumentName = typeArgument.name as SimpleIdentifier;
93+
expect(typeArgumentName.name, 'B');
94+
expect(annotation.constructorName, isNull);
95+
}
96+
97+
void test_className_unprefixed_constructorName_present() {
98+
var compilationUnit = _parseCompilationUnit('@A<B>.ctor() class C {}',
99+
disabledError: expectedError(
100+
ParserErrorCode.ANNOTATION_WITH_TYPE_ARGUMENTS, 2, 1));
101+
var classDeclaration =
102+
compilationUnit.declarations.single as ClassDeclaration;
103+
var annotation = classDeclaration.metadata.single;
104+
var className = annotation.name as SimpleIdentifier;
105+
expect(className.name, 'A');
106+
var typeArgument = annotation.typeArguments!.arguments.single as TypeName;
107+
var typeArgumentName = typeArgument.name as SimpleIdentifier;
108+
expect(typeArgumentName.name, 'B');
109+
expect(annotation.constructorName!.name, 'ctor');
110+
}
111+
112+
void test_reference_prefixed() {
113+
var compilationUnit = _parseCompilationUnit('@p.x<A> class C {}',
114+
errors: [
115+
expectedError(
116+
ParserErrorCode.ANNOTATION_WITH_TYPE_ARGUMENTS_UNINSTANTIATED,
117+
6,
118+
1),
119+
],
120+
disabledError: expectedError(
121+
ParserErrorCode.ANNOTATION_WITH_TYPE_ARGUMENTS, 4, 1));
122+
var classDeclaration =
123+
compilationUnit.declarations.single as ClassDeclaration;
124+
var annotation = classDeclaration.metadata.single;
125+
var name = annotation.name as PrefixedIdentifier;
126+
expect(name.prefix.name, 'p');
127+
expect(name.identifier.name, 'x');
128+
var typeArgument = annotation.typeArguments!.arguments.single as TypeName;
129+
var typeArgumentName = typeArgument.name as SimpleIdentifier;
130+
expect(typeArgumentName.name, 'A');
131+
expect(annotation.constructorName, isNull);
132+
}
133+
134+
void test_reference_unprefixed() {
135+
var compilationUnit = _parseCompilationUnit('@x<A> class C {}',
136+
errors: [
137+
expectedError(
138+
ParserErrorCode.ANNOTATION_WITH_TYPE_ARGUMENTS_UNINSTANTIATED,
139+
4,
140+
1),
141+
],
142+
disabledError: expectedError(
143+
ParserErrorCode.ANNOTATION_WITH_TYPE_ARGUMENTS, 2, 1));
144+
var classDeclaration =
145+
compilationUnit.declarations.single as ClassDeclaration;
146+
var annotation = classDeclaration.metadata.single;
147+
var name = annotation.name as SimpleIdentifier;
148+
expect(name.name, 'x');
149+
var typeArgument = annotation.typeArguments!.arguments.single as TypeName;
150+
var typeArgumentName = typeArgument.name as SimpleIdentifier;
151+
expect(typeArgumentName.name, 'A');
152+
expect(annotation.constructorName, isNull);
153+
}
154+
155+
test_typeArguments_after_constructorName() {
156+
_parseCompilationUnit('@p.A.ctor<B>() class C {}',
157+
errors: [
158+
expectedError(ParserErrorCode.EXPECTED_EXECUTABLE, 9, 1),
159+
expectedError(ParserErrorCode.MISSING_CONST_FINAL_VAR_OR_TYPE, 10, 1),
160+
expectedError(ParserErrorCode.EXPECTED_TOKEN, 10, 1),
161+
expectedError(ParserErrorCode.TOP_LEVEL_OPERATOR, 11, 1),
162+
expectedError(ParserErrorCode.MISSING_FUNCTION_BODY, 15, 5),
163+
],
164+
disabledError: null);
165+
}
166+
167+
test_typeArguments_after_prefix() {
168+
_parseCompilationUnit('@p<A>.B.ctor() class C {}',
169+
errors: [
170+
expectedError(
171+
ParserErrorCode.ANNOTATION_WITH_TYPE_ARGUMENTS_UNINSTANTIATED,
172+
6,
173+
1),
174+
expectedError(ParserErrorCode.EXPECTED_EXECUTABLE, 7, 1),
175+
expectedError(ParserErrorCode.MISSING_FUNCTION_BODY, 15, 5),
176+
],
177+
disabledError: expectedError(
178+
ParserErrorCode.ANNOTATION_WITH_TYPE_ARGUMENTS, 2, 1));
179+
}
180+
181+
CompilationUnit _parseCompilationUnit(String content,
182+
{List<ExpectedError>? errors, required ExpectedError? disabledError});
183+
}

pkg/analyzer/test/generated/test_all.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import 'error_suppression_test.dart' as error_suppression;
1616
import 'expression_parser_test.dart' as expression_parser;
1717
import 'extension_methods_parser_test.dart' as extension_methods_parser;
1818
import 'formal_parameter_parser_test.dart' as formal_parameter_parser;
19+
import 'generic_metadata_parser_test.dart' as generic_metadata_parser;
1920
import 'invalid_code_test.dart' as invalid_code;
2021
import 'issues_test.dart' as issues;
2122
import 'java_core_test.dart' as java_core_test;
@@ -55,6 +56,7 @@ main() {
5556
expression_parser.main();
5657
extension_methods_parser.main();
5758
formal_parameter_parser.main();
59+
generic_metadata_parser.main();
5860
invalid_code.main();
5961
issues.main();
6062
java_core_test.main();

0 commit comments

Comments
 (0)