Use named parameters in 'for' related nodes.

Change-Id: Id3ae04f383da65780157065278a692b2a654bddd
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/258500
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
diff --git a/pkg/analyzer/lib/src/dart/ast/ast.dart b/pkg/analyzer/lib/src/dart/ast/ast.dart
index 0623a6c..8e00e49 100644
--- a/pkg/analyzer/lib/src/dart/ast/ast.dart
+++ b/pkg/analyzer/lib/src/dart/ast/ast.dart
@@ -4858,7 +4858,10 @@
   /// Initialize a newly created for-each statement whose loop control variable
   /// is declared internally (in the for-loop part). The [awaitKeyword] can be
   /// `null` if this is not an asynchronous for loop.
-  ForEachPartsImpl(this.inKeyword, this._iterable) {
+  ForEachPartsImpl({
+    required this.inKeyword,
+    required ExpressionImpl iterable,
+  }) : _iterable = iterable {
     _becomeParentOf(_iterable);
   }
 
@@ -4893,9 +4896,11 @@
 
   /// Initialize a newly created for-each statement whose loop control variable
   /// is declared internally (inside the for-loop part).
-  ForEachPartsWithDeclarationImpl(
-      this._loopVariable, Token inKeyword, ExpressionImpl iterator)
-      : super(inKeyword, iterator) {
+  ForEachPartsWithDeclarationImpl({
+    required DeclaredIdentifierImpl loopVariable,
+    required super.inKeyword,
+    required super.iterable,
+  }) : _loopVariable = loopVariable {
     _becomeParentOf(_loopVariable);
   }
 
@@ -4932,9 +4937,11 @@
 
   /// Initialize a newly created for-each statement whose loop control variable
   /// is declared externally (outside the for-loop part).
-  ForEachPartsWithIdentifierImpl(
-      this._identifier, Token inKeyword, ExpressionImpl iterator)
-      : super(inKeyword, iterator) {
+  ForEachPartsWithIdentifierImpl({
+    required SimpleIdentifierImpl identifier,
+    required super.inKeyword,
+    required super.iterable,
+  }) : _identifier = identifier {
     _becomeParentOf(_identifier);
   }
 
@@ -4977,12 +4984,12 @@
   @override
   final DartPatternImpl pattern;
 
-  ForEachPartsWithPatternImpl(
-      {required this.keyword,
-      required this.pattern,
-      required Token inKeyword,
-      required ExpressionImpl iterable})
-      : super(inKeyword, iterable) {
+  ForEachPartsWithPatternImpl({
+    required this.keyword,
+    required this.pattern,
+    required super.inKeyword,
+    required super.iterable,
+  }) {
     _becomeParentOf(pattern);
   }
 
@@ -5026,8 +5033,15 @@
   CollectionElementImpl _body;
 
   /// Initialize a newly created for element.
-  ForElementImpl(this.awaitKeyword, this.forKeyword, this.leftParenthesis,
-      this._forLoopParts, this.rightParenthesis, this._body) {
+  ForElementImpl({
+    required this.awaitKeyword,
+    required this.forKeyword,
+    required this.leftParenthesis,
+    required ForLoopPartsImpl forLoopParts,
+    required this.rightParenthesis,
+    required CollectionElementImpl body,
+  })  : _forLoopParts = forLoopParts,
+        _body = body {
     _becomeParentOf(_forLoopParts);
     _becomeParentOf(_body);
   }
@@ -5255,8 +5269,12 @@
   /// [initialization] must be `null`. Either the [condition] and the list of
   /// [updaters] can be `null` if the loop does not have the corresponding
   /// attribute.
-  ForPartsImpl(this.leftSeparator, this._condition, this.rightSeparator,
-      List<Expression>? updaters) {
+  ForPartsImpl({
+    required this.leftSeparator,
+    required ExpressionImpl? condition,
+    required this.rightSeparator,
+    required List<Expression>? updaters,
+  }) : _condition = condition {
     _becomeParentOf(_condition);
     _updaters._initialize(this, updaters);
   }
@@ -5301,13 +5319,13 @@
   /// Initialize a newly created for statement. Both the [condition] and the
   /// list of [updaters] can be `null` if the loop does not have the
   /// corresponding attribute.
-  ForPartsWithDeclarationsImpl(
-      this._variableList,
-      Token leftSeparator,
-      ExpressionImpl? condition,
-      Token rightSeparator,
-      List<Expression>? updaters)
-      : super(leftSeparator, condition, rightSeparator, updaters) {
+  ForPartsWithDeclarationsImpl({
+    required VariableDeclarationListImpl variableList,
+    required super.leftSeparator,
+    required super.condition,
+    required super.rightSeparator,
+    required super.updaters,
+  }) : _variableList = variableList {
     _becomeParentOf(_variableList);
   }
 
@@ -5348,13 +5366,13 @@
   /// Initialize a newly created for statement. Both the [condition] and the
   /// list of [updaters] can be `null` if the loop does not have the
   /// corresponding attribute.
-  ForPartsWithExpressionImpl(
-      this._initialization,
-      Token leftSeparator,
-      ExpressionImpl? condition,
-      Token rightSeparator,
-      List<Expression>? updaters)
-      : super(leftSeparator, condition, rightSeparator, updaters) {
+  ForPartsWithExpressionImpl({
+    required ExpressionImpl? initialization,
+    required super.leftSeparator,
+    required super.condition,
+    required super.rightSeparator,
+    required super.updaters,
+  }) : _initialization = initialization {
     _becomeParentOf(_initialization);
   }
 
@@ -5394,13 +5412,13 @@
   @override
   final PatternVariableDeclarationImpl variables;
 
-  ForPartsWithPatternImpl(
-      {required this.variables,
-      required Token leftSeparator,
-      required ExpressionImpl? condition,
-      required Token rightSeparator,
-      required List<Expression>? updaters})
-      : super(leftSeparator, condition, rightSeparator, updaters) {
+  ForPartsWithPatternImpl({
+    required this.variables,
+    required super.leftSeparator,
+    required super.condition,
+    required super.rightSeparator,
+    required super.updaters,
+  }) {
     _becomeParentOf(variables);
   }
 
@@ -5443,8 +5461,15 @@
   StatementImpl _body;
 
   /// Initialize a newly created for statement.
-  ForStatementImpl(this.awaitKeyword, this.forKeyword, this.leftParenthesis,
-      this._forLoopParts, this.rightParenthesis, this._body) {
+  ForStatementImpl({
+    required this.awaitKeyword,
+    required this.forKeyword,
+    required this.leftParenthesis,
+    required ForLoopPartsImpl forLoopParts,
+    required this.rightParenthesis,
+    required StatementImpl body,
+  })  : _forLoopParts = forLoopParts,
+        _body = body {
     _becomeParentOf(_forLoopParts);
     _becomeParentOf(_body);
   }
diff --git a/pkg/analyzer/lib/src/dart/ast/ast_factory.dart b/pkg/analyzer/lib/src/dart/ast/ast_factory.dart
index acd41d7..41886e0 100644
--- a/pkg/analyzer/lib/src/dart/ast/ast_factory.dart
+++ b/pkg/analyzer/lib/src/dart/ast/ast_factory.dart
@@ -83,35 +83,6 @@
           parameters as FormalParameterListImpl?,
           question);
 
-  ForEachPartsWithDeclarationImpl forEachPartsWithDeclaration(
-          {required DeclaredIdentifier loopVariable,
-          required Token inKeyword,
-          required Expression iterable}) =>
-      ForEachPartsWithDeclarationImpl(loopVariable as DeclaredIdentifierImpl,
-          inKeyword, iterable as ExpressionImpl);
-
-  ForEachPartsWithIdentifierImpl forEachPartsWithIdentifier(
-          {required SimpleIdentifier identifier,
-          required Token inKeyword,
-          required Expression iterable}) =>
-      ForEachPartsWithIdentifierImpl(identifier as SimpleIdentifierImpl,
-          inKeyword, iterable as ExpressionImpl);
-
-  ForElementImpl forElement(
-          {Token? awaitKeyword,
-          required Token forKeyword,
-          required Token leftParenthesis,
-          required ForLoopParts forLoopParts,
-          required Token rightParenthesis,
-          required CollectionElement body}) =>
-      ForElementImpl(
-          awaitKeyword,
-          forKeyword,
-          leftParenthesis,
-          forLoopParts as ForLoopPartsImpl,
-          rightParenthesis,
-          body as CollectionElementImpl);
-
   FormalParameterListImpl formalParameterList(
           Token leftParenthesis,
           List<FormalParameter> parameters,
@@ -121,48 +92,6 @@
       FormalParameterListImpl(leftParenthesis, parameters, leftDelimiter,
           rightDelimiter, rightParenthesis);
 
-  ForPartsWithDeclarationsImpl forPartsWithDeclarations(
-          {required VariableDeclarationList variables,
-          required Token leftSeparator,
-          Expression? condition,
-          required Token rightSeparator,
-          List<Expression>? updaters}) =>
-      ForPartsWithDeclarationsImpl(
-          variables as VariableDeclarationListImpl,
-          leftSeparator,
-          condition as ExpressionImpl?,
-          rightSeparator,
-          updaters);
-
-  ForPartsWithExpressionImpl forPartsWithExpression(
-          {Expression? initialization,
-          required Token leftSeparator,
-          Expression? condition,
-          required Token rightSeparator,
-          List<Expression>? updaters}) =>
-      ForPartsWithExpressionImpl(
-          initialization as ExpressionImpl?,
-          leftSeparator,
-          condition as ExpressionImpl?,
-          rightSeparator,
-          updaters);
-
-  ForStatementImpl forStatement(
-      {Token? awaitKeyword,
-      required Token forKeyword,
-      required Token leftParenthesis,
-      required ForLoopParts forLoopParts,
-      required Token rightParenthesis,
-      required Statement body}) {
-    return ForStatementImpl(
-        awaitKeyword,
-        forKeyword,
-        leftParenthesis,
-        forLoopParts as ForLoopPartsImpl,
-        rightParenthesis,
-        body as StatementImpl);
-  }
-
   FunctionDeclarationStatementImpl functionDeclarationStatement(
           FunctionDeclaration functionDeclaration) =>
       FunctionDeclarationStatementImpl(
diff --git a/pkg/analyzer/lib/src/fasta/ast_builder.dart b/pkg/analyzer/lib/src/fasta/ast_builder.dart
index 952f83a..4c4d6ec3 100644
--- a/pkg/analyzer/lib/src/fasta/ast_builder.dart
+++ b/pkg/analyzer/lib/src/fasta/ast_builder.dart
@@ -1549,7 +1549,7 @@
   void endForControlFlow(Token token) {
     debugEvent("endForControlFlow");
     var entry = pop() as Object;
-    var forLoopParts = pop() as ForParts;
+    var forLoopParts = pop() as ForPartsImpl;
     var leftParen = pop() as Token;
     var forToken = pop() as Token;
 
@@ -1560,20 +1560,22 @@
   void endForIn(Token endToken) {
     debugEvent("ForInExpression");
 
-    var body = pop() as Statement;
-    var forLoopParts = pop() as ForEachParts;
+    var body = pop() as StatementImpl;
+    var forLoopParts = pop() as ForEachPartsImpl;
     var leftParenthesis = pop() as Token;
     var forToken = pop() as Token;
     var awaitToken = pop(NullValue.AwaitToken) as Token?;
 
-    push(ast.forStatement(
-      awaitKeyword: awaitToken,
-      forKeyword: forToken,
-      leftParenthesis: leftParenthesis,
-      forLoopParts: forLoopParts,
-      rightParenthesis: leftParenthesis.endGroup!,
-      body: body,
-    ));
+    push(
+      ForStatementImpl(
+        awaitKeyword: awaitToken,
+        forKeyword: forToken,
+        leftParenthesis: leftParenthesis,
+        forLoopParts: forLoopParts,
+        rightParenthesis: leftParenthesis.endGroup!,
+        body: body,
+      ),
+    );
   }
 
   @override
@@ -1586,7 +1588,7 @@
     debugEvent("endForInControlFlow");
 
     var entry = pop() as Object;
-    var forLoopParts = pop() as ForEachParts;
+    var forLoopParts = pop() as ForEachPartsImpl;
     var leftParenthesis = pop() as Token;
     var forToken = pop() as Token;
     var awaitToken = pop(NullValue.AwaitToken) as Token?;
@@ -1789,18 +1791,21 @@
   @override
   void endForStatement(Token endToken) {
     debugEvent("ForStatement");
-    var body = pop() as Statement;
-    var forLoopParts = pop() as ForParts;
+    var body = pop() as StatementImpl;
+    var forLoopParts = pop() as ForPartsImpl;
     var leftParen = pop() as Token;
     var forToken = pop() as Token;
 
-    push(ast.forStatement(
-      forKeyword: forToken,
-      leftParenthesis: leftParen,
-      forLoopParts: forLoopParts,
-      rightParenthesis: leftParen.endGroup!,
-      body: body,
-    ));
+    push(
+      ForStatementImpl(
+        awaitKeyword: null,
+        forKeyword: forToken,
+        leftParenthesis: leftParen,
+        forLoopParts: forLoopParts,
+        rightParenthesis: leftParen.endGroup!,
+        body: body,
+      ),
+    );
   }
 
   @override
@@ -3501,13 +3506,13 @@
     assert(optional('(', leftParenthesis));
     assert(optional('in', inKeyword) || optional(':', inKeyword));
 
-    var iterator = pop() as Expression;
+    var iterable = pop() as ExpressionImpl;
     var variableOrDeclaration = pop()!;
 
     ForEachParts forLoopParts;
     if (variableOrDeclaration is VariableDeclarationStatement) {
       VariableDeclarationList variableList = variableOrDeclaration.variables;
-      forLoopParts = ast.forEachPartsWithDeclaration(
+      forLoopParts = ForEachPartsWithDeclarationImpl(
         loopVariable: DeclaredIdentifierImpl(
           comment: variableList.documentationComment as CommentImpl?,
           metadata: variableList.metadata,
@@ -3516,20 +3521,20 @@
           name: variableList.variables.first.name2,
         ),
         inKeyword: inKeyword,
-        iterable: iterator,
+        iterable: iterable,
       );
     } else {
-      if (variableOrDeclaration is! SimpleIdentifier) {
+      if (variableOrDeclaration is! SimpleIdentifierImpl) {
         // Parser has already reported the error.
         if (!leftParenthesis.next!.isIdentifier) {
           parser.rewriter.insertSyntheticIdentifier(leftParenthesis);
         }
         variableOrDeclaration = ast.simpleIdentifier(leftParenthesis.next!);
       }
-      forLoopParts = ast.forEachPartsWithIdentifier(
+      forLoopParts = ForEachPartsWithIdentifierImpl(
         identifier: variableOrDeclaration,
         inKeyword: inKeyword,
-        iterable: iterator,
+        iterable: iterable,
       );
     }
 
@@ -3551,9 +3556,9 @@
     var conditionStatement = pop() as Statement;
     var initializerPart = pop();
 
-    Expression? condition;
+    ExpressionImpl? condition;
     Token rightSeparator;
-    if (conditionStatement is ExpressionStatement) {
+    if (conditionStatement is ExpressionStatementImpl) {
       condition = conditionStatement.expression;
       rightSeparator = conditionStatement.semicolon!;
     } else {
@@ -3561,17 +3566,17 @@
     }
 
     ForParts forLoopParts;
-    if (initializerPart is VariableDeclarationStatement) {
-      forLoopParts = ast.forPartsWithDeclarations(
-        variables: initializerPart.variables,
+    if (initializerPart is VariableDeclarationStatementImpl) {
+      forLoopParts = ForPartsWithDeclarationsImpl(
+        variableList: initializerPart.variables,
         leftSeparator: leftSeparator,
         condition: condition,
         rightSeparator: rightSeparator,
         updaters: updates,
       );
     } else {
-      forLoopParts = ast.forPartsWithExpression(
-        initialization: initializerPart as Expression?,
+      forLoopParts = ForPartsWithExpressionImpl(
+        initialization: initializerPart as ExpressionImpl?,
         leftSeparator: leftSeparator,
         condition: condition,
         rightSeparator: rightSeparator,
@@ -4575,18 +4580,20 @@
   }
 
   void pushForControlFlowInfo(Token? awaitToken, Token forToken,
-      Token leftParenthesis, ForLoopParts forLoopParts, Object entry) {
+      Token leftParenthesis, ForLoopPartsImpl forLoopParts, Object entry) {
     if (entry == _invalidCollectionElement) {
       push(_invalidCollectionElement);
     } else if (enableControlFlowCollections) {
-      push(ast.forElement(
-        awaitKeyword: awaitToken,
-        forKeyword: forToken,
-        leftParenthesis: leftParenthesis,
-        forLoopParts: forLoopParts,
-        rightParenthesis: leftParenthesis.endGroup!,
-        body: entry as CollectionElement,
-      ));
+      push(
+        ForElementImpl(
+          awaitKeyword: awaitToken,
+          forKeyword: forToken,
+          leftParenthesis: leftParenthesis,
+          forLoopParts: forLoopParts,
+          rightParenthesis: leftParenthesis.endGroup!,
+          body: entry as CollectionElementImpl,
+        ),
+      );
     } else {
       _reportFeatureNotEnabled(
         feature: ExperimentalFeatures.control_flow_collections,
diff --git a/pkg/analyzer/lib/src/generated/testing/ast_test_factory.dart b/pkg/analyzer/lib/src/generated/testing/ast_test_factory.dart
index d01db68..42fa341 100644
--- a/pkg/analyzer/lib/src/generated/testing/ast_test_factory.dart
+++ b/pkg/analyzer/lib/src/generated/testing/ast_test_factory.dart
@@ -339,32 +339,6 @@
   static FieldFormalParameterImpl fieldFormalParameter2(String identifier) =>
       fieldFormalParameter(null, null, identifier);
 
-  static ForEachPartsWithDeclarationImpl forEachPartsWithDeclaration(
-          DeclaredIdentifier loopVariable, Expression iterable) =>
-      astFactory.forEachPartsWithDeclaration(
-          loopVariable: loopVariable,
-          inKeyword: TokenFactory.tokenFromKeyword(Keyword.IN),
-          iterable: iterable);
-
-  static ForEachPartsWithIdentifierImpl forEachPartsWithIdentifier(
-          SimpleIdentifier identifier, Expression iterable) =>
-      astFactory.forEachPartsWithIdentifier(
-          identifier: identifier,
-          inKeyword: TokenFactory.tokenFromKeyword(Keyword.IN),
-          iterable: iterable);
-
-  static ForElementImpl forElement(
-          ForLoopParts forLoopParts, CollectionElement body,
-          {bool hasAwait = false}) =>
-      astFactory.forElement(
-          awaitKeyword:
-              hasAwait ? TokenFactory.tokenFromKeyword(Keyword.AWAIT) : null,
-          forKeyword: TokenFactory.tokenFromKeyword(Keyword.FOR),
-          leftParenthesis: TokenFactory.tokenFromType(TokenType.OPEN_PAREN),
-          forLoopParts: forLoopParts,
-          rightParenthesis: TokenFactory.tokenFromType(TokenType.CLOSE_PAREN),
-          body: body);
-
   static FormalParameterListImpl formalParameterList(
           [List<FormalParameter> parameters = const []]) =>
       astFactory.formalParameterList(
@@ -374,37 +348,6 @@
           null,
           TokenFactory.tokenFromType(TokenType.CLOSE_PAREN));
 
-  static ForPartsWithDeclarationsImpl forPartsWithDeclarations(
-          VariableDeclarationList variables,
-          Expression? condition,
-          List<Expression>? updaters) =>
-      astFactory.forPartsWithDeclarations(
-          variables: variables,
-          leftSeparator: TokenFactory.tokenFromType(TokenType.SEMICOLON),
-          condition: condition,
-          rightSeparator: TokenFactory.tokenFromType(TokenType.SEMICOLON),
-          updaters: updaters);
-
-  static ForPartsWithExpressionImpl forPartsWithExpression(
-          Expression? initialization,
-          Expression? condition,
-          List<Expression>? updaters) =>
-      astFactory.forPartsWithExpression(
-          initialization: initialization,
-          leftSeparator: TokenFactory.tokenFromType(TokenType.SEMICOLON),
-          condition: condition,
-          rightSeparator: TokenFactory.tokenFromType(TokenType.SEMICOLON),
-          updaters: updaters);
-
-  static ForStatementImpl forStatement(
-          ForLoopParts forLoopParts, Statement body) =>
-      astFactory.forStatement(
-          forKeyword: TokenFactory.tokenFromKeyword(Keyword.FOR),
-          leftParenthesis: TokenFactory.tokenFromType(TokenType.OPEN_PAREN),
-          forLoopParts: forLoopParts,
-          rightParenthesis: TokenFactory.tokenFromType(TokenType.CLOSE_PAREN),
-          body: body);
-
   static FunctionDeclarationImpl functionDeclaration(
           TypeAnnotation? type,
           Keyword? keyword,
diff --git a/pkg/analyzer/lib/src/summary2/ast_binary_reader.dart b/pkg/analyzer/lib/src/summary2/ast_binary_reader.dart
index 2144568..0840c45 100644
--- a/pkg/analyzer/lib/src/summary2/ast_binary_reader.dart
+++ b/pkg/analyzer/lib/src/summary2/ast_binary_reader.dart
@@ -507,9 +507,9 @@
   }
 
   ForEachPartsWithDeclaration _readForEachPartsWithDeclaration() {
-    var loopVariable = readNode() as DeclaredIdentifier;
-    var iterable = readNode() as Expression;
-    return astFactory.forEachPartsWithDeclaration(
+    var loopVariable = readNode() as DeclaredIdentifierImpl;
+    var iterable = readNode() as ExpressionImpl;
+    return ForEachPartsWithDeclarationImpl(
       inKeyword: Tokens.in_(),
       iterable: iterable,
       loopVariable: loopVariable,
@@ -518,9 +518,9 @@
 
   ForElement _readForElement() {
     var flags = _readByte();
-    var forLoopParts = readNode() as ForLoopParts;
-    var body = readNode() as CollectionElement;
-    return astFactory.forElement(
+    var forLoopParts = readNode() as ForLoopPartsImpl;
+    var body = readNode() as CollectionElementImpl;
+    return ForElementImpl(
       awaitKeyword: AstBinaryFlags.hasAwait(flags) ? Tokens.await_() : null,
       body: body,
       forKeyword: Tokens.for_(),
@@ -554,23 +554,23 @@
   }
 
   ForPartsWithDeclarations _readForPartsWithDeclarations() {
-    var variables = readNode() as VariableDeclarationList;
-    var condition = _readOptionalNode() as Expression?;
+    var variables = readNode() as VariableDeclarationListImpl;
+    var condition = _readOptionalNode() as ExpressionImpl?;
     var updaters = _readNodeList<Expression>();
-    return astFactory.forPartsWithDeclarations(
+    return ForPartsWithDeclarationsImpl(
       condition: condition,
       leftSeparator: Tokens.semicolon(),
       rightSeparator: Tokens.semicolon(),
       updaters: updaters,
-      variables: variables,
+      variableList: variables,
     );
   }
 
   ForPartsWithExpression _readForPartsWithExpression() {
-    var initialization = _readOptionalNode() as Expression?;
-    var condition = _readOptionalNode() as Expression?;
+    var initialization = _readOptionalNode() as ExpressionImpl?;
+    var condition = _readOptionalNode() as ExpressionImpl?;
     var updaters = _readNodeList<Expression>();
-    return astFactory.forPartsWithExpression(
+    return ForPartsWithExpressionImpl(
       condition: condition,
       initialization: initialization,
       leftSeparator: Tokens.semicolon(),
diff --git a/pkg/analyzer/lib/src/test_utilities/find_node.dart b/pkg/analyzer/lib/src/test_utilities/find_node.dart
index 6df4a53..5aae962 100644
--- a/pkg/analyzer/lib/src/test_utilities/find_node.dart
+++ b/pkg/analyzer/lib/src/test_utilities/find_node.dart
@@ -223,6 +223,10 @@
     return _node(search, (n) => n is ForEachPartsWithIdentifier);
   }
 
+  ForElement forElement(String search) {
+    return _node(search, (n) => n is ForElement);
+  }
+
   FormalParameterList formalParameterList(String search) {
     return _node(search, (n) => n is FormalParameterList);
   }
diff --git a/pkg/analyzer/test/dart/ast/ast_test.dart b/pkg/analyzer/test/dart/ast/ast_test.dart
index c60343c..93be469 100644
--- a/pkg/analyzer/test/dart/ast/ast_test.dart
+++ b/pkg/analyzer/test/dart/ast/ast_test.dart
@@ -1439,15 +1439,12 @@
   }
 
   void test_inGetterContext_forEachLoop() {
-    SimpleIdentifier identifier = AstTestFactory.identifier3("a");
-    Expression iterator = AstTestFactory.listLiteral();
-    Statement body = BlockImpl(
-      leftBracket: Tokens.openCurlyBracket(),
-      statements: [],
-      rightBracket: Tokens.closeCurlyBracket(),
-    );
-    AstTestFactory.forStatement(
-        AstTestFactory.forEachPartsWithIdentifier(identifier, iterator), body);
+    final parseResult = parseStringWithErrors('''
+void f() {
+  for (v in [0]) {}
+}
+''');
+    final identifier = parseResult.findNode.simple('v in');
     expect(identifier.inGetterContext(), isFalse);
   }
 
@@ -1483,15 +1480,12 @@
   }
 
   void test_inSetterContext_forEachLoop() {
-    SimpleIdentifier identifier = AstTestFactory.identifier3("a");
-    Expression iterator = AstTestFactory.listLiteral();
-    Statement body = BlockImpl(
-      leftBracket: Tokens.openCurlyBracket(),
-      statements: [],
-      rightBracket: Tokens.closeCurlyBracket(),
-    );
-    AstTestFactory.forStatement(
-        AstTestFactory.forEachPartsWithIdentifier(identifier, iterator), body);
+    final parseResult = parseStringWithErrors('''
+void f() {
+  for (v in [0]) {}
+}
+''');
+    final identifier = parseResult.findNode.simple('v in');
     expect(identifier.inSetterContext(), isTrue);
   }
 
diff --git a/pkg/analyzer/test/src/dart/ast/to_source_visitor_test.dart b/pkg/analyzer/test/src/dart/ast/to_source_visitor_test.dart
index 04a716e..a5e1806 100644
--- a/pkg/analyzer/test/src/dart/ast/to_source_visitor_test.dart
+++ b/pkg/analyzer/test/src/dart/ast/to_source_visitor_test.dart
@@ -1564,12 +1564,13 @@
   }
 
   void test_visitForEachPartsWithIdentifier() {
-    _assertSource(
-        'e in l',
-        astFactory.forEachPartsWithIdentifier(
-            identifier: AstTestFactory.identifier3('e'),
-            inKeyword: Tokens.in_(),
-            iterable: AstTestFactory.identifier3('l')));
+    final code = 'e in []';
+    final findNode = _parseStringToFindNode('''
+void f() {
+  for ($code) {}
+}
+''');
+    _assertSource(code, findNode.forEachPartsWithIdentifier(code));
   }
 
   void test_visitForEachStatement_declared() {
@@ -1603,18 +1604,11 @@
   }
 
   void test_visitForElement() {
-    _assertSource(
-      'for (e in l) 0',
-      astFactory.forElement(
-          forKeyword: Tokens.for_(),
-          leftParenthesis: Tokens.openParenthesis(),
-          forLoopParts: astFactory.forEachPartsWithIdentifier(
-              identifier: AstTestFactory.identifier3('e'),
-              inKeyword: Tokens.in_(),
-              iterable: AstTestFactory.identifier3('l')),
-          rightParenthesis: Tokens.closeParenthesis(),
-          body: AstTestFactory.integer(0)),
-    );
+    final code = 'for (e in []) 0';
+    final findNode = _parseStringToFindNode('''
+final v = [ $code ];
+''');
+    _assertSource(code, findNode.forElement(code));
   }
 
   void test_visitFormalParameterList_empty() {
@@ -1742,42 +1736,33 @@
   }
 
   void test_visitForPartsWithDeclarations() {
-    _assertSource(
-        'var v; b; u',
-        astFactory.forPartsWithDeclarations(
-            variables: AstTestFactory.variableDeclarationList2(
-                Keyword.VAR, [AstTestFactory.variableDeclaration('v')]),
-            leftSeparator: Tokens.semicolon(),
-            condition: AstTestFactory.identifier3('b'),
-            rightSeparator: Tokens.semicolon(),
-            updaters: [AstTestFactory.identifier3('u')]));
+    final code = 'var v = 0; v < 10; v++';
+    final findNode = _parseStringToFindNode('''
+void f() {
+  for ($code) {}
+}
+''');
+    _assertSource(code, findNode.forPartsWithDeclarations(code));
   }
 
   void test_visitForPartsWithExpression() {
-    _assertSource(
-        'v; b; u',
-        astFactory.forPartsWithExpression(
-            initialization: AstTestFactory.identifier3('v'),
-            leftSeparator: Tokens.semicolon(),
-            condition: AstTestFactory.identifier3('b'),
-            rightSeparator: Tokens.semicolon(),
-            updaters: [AstTestFactory.identifier3('u')]));
+    final code = 'v = 0; v < 10; v++';
+    final findNode = _parseStringToFindNode('''
+void f() {
+  for ($code) {}
+}
+''');
+    _assertSource(code, findNode.forPartsWithExpression(code));
   }
 
   void test_visitForStatement() {
-    _assertSource(
-      'for (e in l) s;',
-      astFactory.forStatement(
-          forKeyword: Tokens.for_(),
-          leftParenthesis: Tokens.openParenthesis(),
-          forLoopParts: astFactory.forEachPartsWithIdentifier(
-              identifier: AstTestFactory.identifier3('e'),
-              inKeyword: Tokens.in_(),
-              iterable: AstTestFactory.identifier3('l')),
-          rightParenthesis: Tokens.closeParenthesis(),
-          body: AstTestFactory.expressionStatement(
-              AstTestFactory.identifier3('s'))),
-    );
+    final code = 'for (var v in [0]) {}';
+    final findNode = _parseStringToFindNode('''
+void f() {
+  $code
+}
+''');
+    _assertSource(code, findNode.forStatement(code));
   }
 
   void test_visitForStatement_c() {
@@ -2374,40 +2359,11 @@
   }
 
   void test_visitListLiteral_complex() {
-    _assertSource(
-        '<int>[0, for (e in l) 0, if (b) 1, ...[0]]',
-        astFactory.listLiteral(
-            null,
-            AstTestFactory.typeArgumentList([AstTestFactory.namedType4('int')]),
-            Tokens.openSquareBracket(),
-            [
-              AstTestFactory.integer(0),
-              astFactory.forElement(
-                  forKeyword: Tokens.for_(),
-                  leftParenthesis: Tokens.openParenthesis(),
-                  forLoopParts: astFactory.forEachPartsWithIdentifier(
-                      identifier: AstTestFactory.identifier3('e'),
-                      inKeyword: Tokens.in_(),
-                      iterable: AstTestFactory.identifier3('l')),
-                  rightParenthesis: Tokens.closeParenthesis(),
-                  body: AstTestFactory.integer(0)),
-              astFactory.ifElement(
-                  ifKeyword: Tokens.if_(),
-                  leftParenthesis: Tokens.openParenthesis(),
-                  condition: AstTestFactory.identifier3('b'),
-                  rightParenthesis: Tokens.closeParenthesis(),
-                  thenElement: AstTestFactory.integer(1)),
-              astFactory.spreadElement(
-                  spreadOperator: TokenFactory.tokenFromType(
-                      TokenType.PERIOD_PERIOD_PERIOD),
-                  expression: astFactory.listLiteral(
-                      null,
-                      null,
-                      Tokens.openSquareBracket(),
-                      [AstTestFactory.integer(0)],
-                      Tokens.closeSquareBracket()))
-            ],
-            Tokens.closeSquareBracket()));
+    final code = '<int>[0, for (e in []) 0, if (b) 1, ...[0]]';
+    final findNode = _parseStringToFindNode('''
+final v = $code;
+''');
+    _assertSource(code, findNode.listLiteral(code));
   }
 
   void test_visitListLiteral_const() {
@@ -2882,46 +2838,12 @@
   }
 
   void test_visitSetOrMapLiteral_map_complex() {
-    _assertSource(
-      "<String, String>{'a' : 'b', for (c in d) 'e' : 'f', if (g) 'h' : 'i', ...{'j' : 'k'}}",
-      astFactory.setOrMapLiteral(
-        leftBracket: Tokens.openCurlyBracket(),
-        typeArguments: AstTestFactory.typeArgumentList([
-          AstTestFactory.namedType4('String'),
-          AstTestFactory.namedType4('String')
-        ]),
-        elements: [
-          AstTestFactory.mapLiteralEntry3('a', 'b'),
-          astFactory.forElement(
-              forKeyword: Tokens.for_(),
-              leftParenthesis: Tokens.openParenthesis(),
-              forLoopParts: astFactory.forEachPartsWithIdentifier(
-                identifier: AstTestFactory.identifier3('c'),
-                inKeyword: Tokens.in_(),
-                iterable: AstTestFactory.identifier3('d'),
-              ),
-              rightParenthesis: Tokens.closeParenthesis(),
-              body: AstTestFactory.mapLiteralEntry3('e', 'f')),
-          astFactory.ifElement(
-            ifKeyword: Tokens.if_(),
-            leftParenthesis: Tokens.openParenthesis(),
-            condition: AstTestFactory.identifier3('g'),
-            rightParenthesis: Tokens.closeParenthesis(),
-            thenElement: AstTestFactory.mapLiteralEntry3('h', 'i'),
-          ),
-          astFactory.spreadElement(
-            spreadOperator:
-                TokenFactory.tokenFromType(TokenType.PERIOD_PERIOD_PERIOD),
-            expression: astFactory.setOrMapLiteral(
-              leftBracket: Tokens.openCurlyBracket(),
-              elements: [AstTestFactory.mapLiteralEntry3('j', 'k')],
-              rightBracket: Tokens.closeCurlyBracket(),
-            ),
-          )
-        ],
-        rightBracket: Tokens.closeCurlyBracket(),
-      ),
-    );
+    final code =
+        "<String, String>{'a' : 'b', for (c in d) 'e' : 'f', if (g) 'h' : 'i', ...{'j' : 'k'}}";
+    final findNode = _parseStringToFindNode('''
+final v = $code;
+''');
+    _assertSource(code, findNode.setOrMapLiteral(code));
   }
 
   void test_visitSetOrMapLiteral_map_withConst_withoutTypeArgs() {
@@ -2979,46 +2901,11 @@
   }
 
   void test_visitSetOrMapLiteral_set_complex() {
-    _assertSource(
-      '<int>{0, for (e in l) 0, if (b) 1, ...[0]}',
-      astFactory.setOrMapLiteral(
-        typeArguments:
-            AstTestFactory.typeArgumentList([AstTestFactory.namedType4('int')]),
-        leftBracket: Tokens.openCurlyBracket(),
-        elements: [
-          AstTestFactory.integer(0),
-          astFactory.forElement(
-              forKeyword: Tokens.for_(),
-              leftParenthesis: Tokens.openParenthesis(),
-              forLoopParts: astFactory.forEachPartsWithIdentifier(
-                identifier: AstTestFactory.identifier3('e'),
-                inKeyword: Tokens.in_(),
-                iterable: AstTestFactory.identifier3('l'),
-              ),
-              rightParenthesis: Tokens.closeParenthesis(),
-              body: AstTestFactory.integer(0)),
-          astFactory.ifElement(
-            ifKeyword: Tokens.if_(),
-            leftParenthesis: Tokens.openParenthesis(),
-            condition: AstTestFactory.identifier3('b'),
-            rightParenthesis: Tokens.closeParenthesis(),
-            thenElement: AstTestFactory.integer(1),
-          ),
-          astFactory.spreadElement(
-            spreadOperator:
-                TokenFactory.tokenFromType(TokenType.PERIOD_PERIOD_PERIOD),
-            expression: astFactory.listLiteral(
-              null,
-              null,
-              Tokens.openSquareBracket(),
-              [AstTestFactory.integer(0)],
-              Tokens.closeSquareBracket(),
-            ),
-          )
-        ],
-        rightBracket: Tokens.closeCurlyBracket(),
-      ),
-    );
+    final code = '<int>{0, for (e in l) 0, if (b) 1, ...[0]}';
+    final findNode = _parseStringToFindNode('''
+final v = $code;
+''');
+    _assertSource(code, findNode.setOrMapLiteral(code));
   }
 
   void test_visitSetOrMapLiteral_set_withConst_withoutTypeArgs() {