report error for invalid super/this initializer

This reports an error for two more invalid initializer situations
to address comment in https://dart-review.googlesource.com/c/sdk/+/97404

The comments about error message text will be addressed in a subsequent CL.

Change-Id: I45785ae7de4adfdb63a238383e6accf465bd8a74
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/97601
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 c0c6e03..ddf4af8 100644
--- a/pkg/analyzer/lib/src/fasta/ast_builder.dart
+++ b/pkg/analyzer/lib/src/fasta/ast_builder.dart
@@ -1142,7 +1142,13 @@
               // This error is also reported in the body builder
               handleRecoverableError(messageThisAccessInInitializer,
                   targetFunct.thisKeyword, targetFunct.thisKeyword);
+            } else {
+              throw new UnsupportedError(
+                  'unsupported initializer $initializerObject');
             }
+          } else {
+            throw new UnsupportedError(
+                'unsupported initializer $initializerObject');
           }
         }
       } else if (initializerObject is AssignmentExpression) {
@@ -1177,6 +1183,36 @@
             initializerObject.rightHandSide));
       } else if (initializerObject is AssertInitializer) {
         initializers.add(initializerObject);
+      } else if (initializerObject is PropertyAccess) {
+        // Recovery: Invalid initializer
+        Expression target = initializerObject.target;
+        if (target is FunctionExpressionInvocation) {
+          var targetFunct = target.function;
+          if (targetFunct is SuperExpression) {
+            initializers.add(ast.superConstructorInvocation(
+                targetFunct.superKeyword, null, null, target.argumentList));
+            // TODO(danrubel): Consider generating this error in the parser
+            // This error is also reported in the body builder
+            handleRecoverableError(messageSuperAsExpression,
+                targetFunct.superKeyword, targetFunct.superKeyword);
+          } else if (targetFunct is ThisExpression) {
+            initializers.add(ast.redirectingConstructorInvocation(
+                targetFunct.thisKeyword, null, null, target.argumentList));
+            // TODO(danrubel): Consider generating this error in the parser
+            // This error is also reported in the body builder
+            handleRecoverableError(messageThisAccessInInitializer,
+                targetFunct.thisKeyword, targetFunct.thisKeyword);
+          } else {
+            throw new UnsupportedError(
+                'unsupported initializer $initializerObject');
+          }
+        } else {
+          throw new UnsupportedError(
+              'unsupported initializer $initializerObject');
+        }
+      } else {
+        throw new UnsupportedError('unsupported initializer:'
+            ' ${initializerObject.runtimeType} :: $initializerObject');
       }
     }
 
diff --git a/pkg/analyzer/test/generated/parser_fasta_test.dart b/pkg/analyzer/test/generated/parser_fasta_test.dart
index d416886..f90130b 100644
--- a/pkg/analyzer/test/generated/parser_fasta_test.dart
+++ b/pkg/analyzer/test/generated/parser_fasta_test.dart
@@ -843,7 +843,15 @@
     ]);
   }
 
-  void test_constructor_super_asExpression() {
+  void test_constructor_super_field() {
+    // https://github.com/dart-lang/sdk/issues/36262
+    // https://github.com/dart-lang/sdk/issues/31198
+    parseCompilationUnit('class B extends A { B(): super().foo {} }', errors: [
+      expectedError(ParserErrorCode.SUPER_AS_EXPRESSION, 25, 5),
+    ]);
+  }
+
+  void test_constructor_super_method() {
     // https://github.com/dart-lang/sdk/issues/36262
     // https://github.com/dart-lang/sdk/issues/31198
     parseCompilationUnit('class B extends A { B(): super().foo() {} }',
@@ -852,7 +860,15 @@
         ]);
   }
 
-  void test_constructor_this_invalid() {
+  void test_constructor_this_field() {
+    // https://github.com/dart-lang/sdk/issues/36262
+    // https://github.com/dart-lang/sdk/issues/31198
+    parseCompilationUnit('class B extends A { B(): this().foo; }', errors: [
+      expectedError(ParserErrorCode.THIS_ACCESS_FROM_INITIALIZER, 25, 4),
+    ]);
+  }
+
+  void test_constructor_this_method() {
     // https://github.com/dart-lang/sdk/issues/36262
     // https://github.com/dart-lang/sdk/issues/31198
     parseCompilationUnit('class B extends A { B(): this().foo(); }', errors: [