From 1f5e0bc4868ebaf8035fd52e677e8c19a4be0f57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20M=C3=BCller?= Date: Wed, 27 Jul 2022 14:32:13 +0200 Subject: [PATCH] Formatter: Fix assign followed by comment (#12319) --- spec/compiler/formatter/formatter_spec.cr | 3 +++ src/compiler/crystal/tools/formatter.cr | 6 +++--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/spec/compiler/formatter/formatter_spec.cr b/spec/compiler/formatter/formatter_spec.cr index 1231298f0b0f..f0ee8410bd57 100644 --- a/spec/compiler/formatter/formatter_spec.cr +++ b/spec/compiler/formatter/formatter_spec.cr @@ -445,6 +445,9 @@ describe Crystal::Formatter do assert_format "a = \nif 1\n2\nend", "a =\n if 1\n 2\n end" assert_format "a, b = \nif 1\n2\nend", "a, b =\n if 1\n 2\n end" assert_format "a = b = 1\na, b =\n b, a" + assert_format "a = # foo\n bar(1)" + assert_format "a = \\\n # foo\n bar(1)" + assert_format "a = \\\n # foo\n nil" assert_format %(require "foo"), %(require "foo") diff --git a/src/compiler/crystal/tools/formatter.cr b/src/compiler/crystal/tools/formatter.cr index ce631c108fbe..74a5577485bf 100644 --- a/src/compiler/crystal/tools/formatter.cr +++ b/src/compiler/crystal/tools/formatter.cr @@ -3294,7 +3294,7 @@ module Crystal check_align = check_assign_length node.target slash_is_regex! write_token " ", :OP_EQ - skip_space + skip_space(consume_newline: false) accept_assign_value_after_equals node.value, check_align: check_align false @@ -4491,7 +4491,7 @@ module Crystal write_indent next_token @passed_backslash_newline = true - if @token.type.space? + if @token.type.space? || @token.type.comment? return skip_space(write_comma, consume_newline) else return false @@ -4620,7 +4620,7 @@ module Crystal def write_comment(needs_indent = true, consume_newline = true, next_comes_end = false) while @token.type.comment? - empty_line = @line_output.to_s.strip.empty? + empty_line = @line_output.empty? if empty_line write_indent if needs_indent end