@@ -490,21 +490,18 @@ mixin PieceFactory {
490490
491491 // Hoist any leading comments so they don't force the for-in clauses
492492 // to split.
493- var leadingComments = const < Piece > [];
494- if (metadata.isEmpty) {
495- leadingComments = pieces.takeCommentsBefore (keyword);
496- }
497-
498- // Use a nested piece so that the metadata precedes the keyword and
499- // not the `(`.
500- var forInPiece =
501- pieces.build (metadata: metadata, inlineMetadata: true , () {
502- pieces.token (keyword);
503- pieces.space ();
504- _writeForIn (pattern, forEachParts.inKeyword, forEachParts.iterable);
493+ pieces.hoistLeadingComments (
494+ metadata.firstOrNull? .beginToken ?? keyword, () {
495+ // Use a nested piece so that the metadata precedes the keyword and
496+ // not the `(`.
497+ return pieces.build (metadata: metadata, inlineMetadata: true , () {
498+ pieces.token (keyword);
499+ pieces.space ();
500+ _writeForIn (
501+ pattern, forEachParts.inKeyword, forEachParts.iterable);
502+ });
505503 });
506504
507- pieces.add (prependLeadingComments (leadingComments, forInPiece));
508505 pieces.token (rightParenthesis);
509506 });
510507 }
@@ -1400,17 +1397,14 @@ mixin PieceFactory {
14001397 void _writeForIn (AstNode leftHandSide, Token inKeyword, Expression sequence) {
14011398 // Hoist any leading comments so they don't force the for-in clauses to
14021399 // split.
1403- var leadingComments =
1404- pieces.takeCommentsBefore (leftHandSide.firstNonCommentToken);
1405-
1406- var leftPiece =
1407- nodePiece (leftHandSide, context: NodeContext .forLoopVariable);
1408- var sequencePiece = _createForInSequence (inKeyword, sequence);
1400+ pieces.hoistLeadingComments (leftHandSide.firstNonCommentToken, () {
1401+ var leftPiece =
1402+ nodePiece (leftHandSide, context: NodeContext .forLoopVariable);
1403+ var sequencePiece = _createForInSequence (inKeyword, sequence);
14091404
1410- pieces.add (prependLeadingComments (
1411- leadingComments,
1412- ForInPiece (leftPiece, sequencePiece,
1413- canBlockSplitSequence: sequence.canBlockSplit)));
1405+ return ForInPiece (leftPiece, sequencePiece,
1406+ canBlockSplitSequence: sequence.canBlockSplit);
1407+ });
14141408 }
14151409
14161410 /// Writes the `<variable> in <expression>` part of a for-in loop when the
@@ -1423,28 +1417,24 @@ mixin PieceFactory {
14231417 DeclaredIdentifier identifier, Token inKeyword, Expression sequence) {
14241418 // Hoist any leading comments so they don't force the for-in clauses
14251419 // to split.
1426- var leadingComments = const < Piece > [];
1427- if (identifier.metadata.isEmpty) {
1428- leadingComments = pieces
1429- .takeCommentsBefore (identifier.firstTokenAfterCommentAndMetadata);
1430- }
1431-
1432- // Use a nested piece so that the metadata precedes the keyword and
1433- // not the `(`.
1434- var forInPiece =
1435- pieces.build (metadata: identifier.metadata, inlineMetadata: true , () {
1436- var leftPiece = pieces.build (() {
1437- writeParameter (
1438- modifiers: [identifier.keyword], identifier.type, identifier.name);
1439- });
1420+ pieces.hoistLeadingComments (identifier.beginToken, () {
1421+ // Use a nested piece so that the metadata precedes the keyword and
1422+ // not the `(`.
1423+ return pieces.build (metadata: identifier.metadata, inlineMetadata: true ,
1424+ () {
1425+ var leftPiece = pieces.build (() {
1426+ writeParameter (
1427+ modifiers: [identifier.keyword],
1428+ identifier.type,
1429+ identifier.name);
1430+ });
14401431
1441- var sequencePiece = _createForInSequence (inKeyword, sequence);
1432+ var sequencePiece = _createForInSequence (inKeyword, sequence);
14421433
1443- pieces.add (ForInPiece (leftPiece, sequencePiece,
1444- canBlockSplitSequence: sequence.canBlockSplit));
1434+ pieces.add (ForInPiece (leftPiece, sequencePiece,
1435+ canBlockSplitSequence: sequence.canBlockSplit));
1436+ });
14451437 });
1446-
1447- pieces.add (prependLeadingComments (leadingComments, forInPiece));
14481438 }
14491439
14501440 /// Creates a piece for the `in <sequence>` part of a for-in loop.
0 commit comments