dart-lang / dart_style

An opinionated formatter/linter for Dart code
https://pub.dev/packages/dart_style
BSD 3-Clause "New" or "Revised" License
650 stars 121 forks source link

Crash if doc comment before pattern variable declaration #1586

Closed munificent closed 1 month ago

munificent commented 1 month ago

If you try to format:

main() {
  /// Doc comment.
  final (a, b) = c;
}

The formatter crashes with:

Null check operator used on a null value
#0      CommentWriter._commentsBefore (package:dart_style/src/front_end/comment_writer.dart:81:47)
#1      CommentWriter.takeCommentsBefore (package:dart_style/src/front_end/comment_writer.dart:62:12)
#2      SequenceBuilder.addCommentsBefore (package:dart_style/src/front_end/sequence_builder.dart:125:38)
#3      SequenceBuilder.visit (package:dart_style/src/front_end/sequence_builder.dart:103:5)
#4      PieceFactory.writeBody (package:dart_style/src/front_end/piece_factory.dart:152:16)
#5      PieceFactory.writeBlock (package:dart_style/src/front_end/piece_factory.dart:172:5)
#6      AstNodeVisitor.visitBlock (package:dart_style/src/front_end/ast_node_visitor.dart:225:5)
#7      BlockImpl.accept (package:analyzer/src/dart/ast/ast.dart:2047:50)
#8      AstNodeVisitor.visitNode (package:dart_style/src/front_end/ast_node_visitor.dart:1969:10)
#9      PieceWriter.visit (package:dart_style/src/front_end/piece_writer.dart:145:14)
#10     AstNodeVisitor.visitBlockFunctionBody (package:dart_style/src/front_end/ast_node_visitor.dart:232:12)
#11     BlockFunctionBodyImpl.accept (package:analyzer/src/dart/ast/ast.dart:1999:50)
#12     AstNodeVisitor.visitNode (package:dart_style/src/front_end/ast_node_visitor.dart:1969:10)
#13     PieceWriter.visit (package:dart_style/src/front_end/piece_writer.dart:145:14)
#14     PieceFactory.writeFunction.<anonymous closure>.<anonymous closure> (package:dart_style/src/front_end/piece_factory.dart:597:16)
#15     PieceFactory.writeFunctionAndReturnType (package:dart_style/src/front_end/piece_factory.dart:615:20)
#16     PieceFactory.writeFunction.<anonymous closure> (package:dart_style/src/front_end/piece_factory.dart:584:7)
#17     PieceWriter.withMetadata (package:dart_style/src/front_end/piece_writer.dart:265:20)
#18     PieceFactory.writeFunction (package:dart_style/src/front_end/piece_factory.dart:583:12)
#19     AstNodeVisitor.visitFunctionDeclaration (package:dart_style/src/front_end/ast_node_visitor.dart:798:5)
#20     FunctionDeclarationImpl.accept (package:analyzer/src/dart/ast/ast.dart:8113:50)
#21     AstNodeVisitor.visitNode (package:dart_style/src/front_end/ast_node_visitor.dart:1969:10)
#22     PieceFactory.nodePiece.<anonymous closure> (package:dart_style/src/front_end/piece_factory.dart:1483:7)
#23     PieceWriter.build (package:dart_style/src/front_end/piece_writer.dart:189:18)
#24     PieceFactory.nodePiece (package:dart_style/src/front_end/piece_factory.dart:1482:25)
#25     SequenceBuilder.visit (package:dart_style/src/front_end/sequence_builder.dart:104:18)
#26     AstNodeVisitor.run (package:dart_style/src/front_end/ast_node_visitor.dart:112:18)
#27     DartFormatter.formatSource (package:dart_style/src/dart_formatter.dart:208:24)
#28     _processFile (package:dart_style/src/io.dart:175:28)
<asynchronous suspension>
#29     formatPaths (package:dart_style/src/io.dart:103:12)
<asynchronous suspension>
#30     FormatCommand.run (package:dart_style/src/cli/format_command.dart:267:7)
<asynchronous suspension>
#31     CommandRunner.runCommand (package:args/command_runner.dart:212:13)
<asynchronous suspension>
#32     main (file:///Users/rnystrom/dev/dart_style/bin/format.dart:18:5)
<asynchronous suspension>

If the comment is not a doc comment, it formats fine. Likewise, if the variable is not a pattern variable declaration, there's no issue.