fix AstBuilder invalid getter recovery
This fixes the Astbuilder recovery when encountering "get <identifier> :"
in a class body to build a method node rather than a constructor node.
Fix https://github.com/dart-lang/sdk/issues/36961
Change-Id: I48242838d72c3a6c18e78a469b6e07fd1481d0c4
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/103260
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Dan Rubel <danrubel@google.com>
diff --git a/pkg/analyzer/lib/src/fasta/ast_builder.dart b/pkg/analyzer/lib/src/fasta/ast_builder.dart
index df9bd69..b4f6a9b 100644
--- a/pkg/analyzer/lib/src/fasta/ast_builder.dart
+++ b/pkg/analyzer/lib/src/fasta/ast_builder.dart
@@ -1483,7 +1483,7 @@
if (name is SimpleIdentifier) {
if (name.name == declaration.name.name && getOrSet == null) {
constructor(name, null, null);
- } else if (initializers.isNotEmpty) {
+ } else if (initializers.isNotEmpty && getOrSet == null) {
constructor(name, null, null);
} else {
method(null, name);
diff --git a/pkg/analyzer/test/generated/parser_test.dart b/pkg/analyzer/test/generated/parser_test.dart
index 1f29027..fb476f6 100644
--- a/pkg/analyzer/test/generated/parser_test.dart
+++ b/pkg/analyzer/test/generated/parser_test.dart
@@ -1628,6 +1628,25 @@
expect(initializer.period, isNull);
}
+ void test_parseGetter_identifier_colon_issue_36961() {
+ createParser('get a:');
+ MethodDeclaration method = parser.parseClassMember('C');
+ expect(method, isNotNull);
+ listener.assertErrors([
+ expectedError(ParserErrorCode.MISSING_INITIALIZER, 5, 1),
+ expectedError(ParserErrorCode.MISSING_FUNCTION_BODY, 6, 0),
+ ]);
+ expect(method.body, isNotNull);
+ expect(method.documentationComment, isNull);
+ expect(method.externalKeyword, isNull);
+ expect(method.modifierKeyword, isNull);
+ expect(method.name, isNotNull);
+ expect(method.operatorKeyword, isNull);
+ expect(method.parameters, isNull);
+ expect(method.propertyKeyword, isNotNull);
+ expect(method.returnType, isNull);
+ }
+
void test_parseGetter_nonStatic() {
createParser('/// Doc\nT get a;');
MethodDeclaration method = parser.parseClassMember('C');