From bca02812e47cdc7f9320dc60df7505930673cd09 Mon Sep 17 00:00:00 2001 From: Robert Nystrom Date: Fri, 27 Oct 2023 12:25:06 -0700 Subject: [PATCH] Apply review feedback. --- lib/src/front_end/ast_node_visitor.dart | 14 +++--- lib/src/front_end/piece_factory.dart | 2 +- lib/src/piece/for.dart | 65 ------------------------- 3 files changed, 9 insertions(+), 72 deletions(-) diff --git a/lib/src/front_end/ast_node_visitor.dart b/lib/src/front_end/ast_node_visitor.dart index 6aa94e0e..f8a4e418 100644 --- a/lib/src/front_end/ast_node_visitor.dart +++ b/lib/src/front_end/ast_node_visitor.dart @@ -461,18 +461,20 @@ class AstNodeVisitor extends ThrowingAstVisitor when node.rightParenthesis.precedingComments == null: token(forLoopParts.leftSeparator); token(forLoopParts.rightSeparator); + case ForPartsWithDeclarations(): partsList.addCommentsBefore(forLoopParts.variables.beginToken); visit(forLoopParts.variables); finishForParts(forLoopParts, partsList); + case ForPartsWithExpression(:var initialization?): + partsList.addCommentsBefore(initialization.beginToken); + visit(initialization); + finishForParts(forLoopParts, partsList); + case ForPartsWithExpression(): - if (forLoopParts.initialization case var initializer?) { - partsList.addCommentsBefore(initializer.beginToken); - visit(initializer); - } else { - partsList.addCommentsBefore(forLoopParts.leftSeparator); - } + // No initializer part. + partsList.addCommentsBefore(forLoopParts.leftSeparator); finishForParts(forLoopParts, partsList); case ForPartsWithPattern(): diff --git a/lib/src/front_end/piece_factory.dart b/lib/src/front_end/piece_factory.dart index 0f517d98..a6e91afc 100644 --- a/lib/src/front_end/piece_factory.dart +++ b/lib/src/front_end/piece_factory.dart @@ -431,7 +431,7 @@ mixin PieceFactory implements CommentWriter { isValueDelimited: rightHandSide.isDelimited)); } - /// Writes the condition and updaters part of for a [ForParts] after the + /// Writes the condition and updaters parts of a [ForParts] after the /// subclass's initializer clause has been written. void finishForParts(ForParts forLoopParts, DelimitedListBuilder partsList) { token(forLoopParts.leftSeparator); diff --git a/lib/src/piece/for.dart b/lib/src/piece/for.dart index 1ddea5a7..2e87fdff 100644 --- a/lib/src/piece/for.dart +++ b/lib/src/piece/for.dart @@ -63,68 +63,3 @@ class ForPiece extends Piece { @override String toString() => 'For'; } - -class ForPartsPiece extends Piece { - final Piece? _initializer; - final Piece _leftSemicolon; - final Piece? _condition; - final Piece _rightSemicolon; - final Piece? _increment; - - ForPartsPiece(this._initializer, this._leftSemicolon, this._condition, - this._rightSemicolon, this._increment); - - /// If there is at least one else or else-if clause, then it always splits. - @override - List get additionalStates => [if (_hasAnyClause) State.split]; - - /// Whether any of the clauses have been provided or its an empty loop like: - /// - /// ``` - /// for (;;) body; - /// ``` - bool get _hasAnyClause => - _initializer != null || _condition != null || _increment != null; - - @override - void format(CodeWriter writer, State state) { - if (state == State.unsplit) writer.setAllowNewlines(false); - - writer.splitIf(state == State.split, space: false, indent: Indent.block); - - if (_initializer case var initializer?) { - writer.format(initializer); - } - - writer.format(_leftSemicolon); - writer.splitIf(state == State.split, - space: _condition != null, indent: Indent.block); - - if (_condition case var condition?) { - writer.format(condition); - } - - writer.format(_rightSemicolon); - - if (_increment case var increment?) { - writer.splitIf(state == State.split, - space: _increment != null, indent: Indent.block); - - writer.format(increment); - } - - writer.splitIf(state == State.split, space: false, indent: Indent.none); - } - - @override - void forEachChild(void Function(Piece piece) callback) { - if (_initializer case var initializer?) callback(initializer); - callback(_leftSemicolon); - if (_condition case var condition?) callback(condition); - callback(_rightSemicolon); - if (_increment case var increment?) callback(increment); - } - - @override - String toString() => 'ForParts'; -}