From ef85244ad8541763f59208b1e2327e4351fed36c Mon Sep 17 00:00:00 2001 From: TSUYUSATO Kitsune Date: Thu, 5 Apr 2018 20:59:33 +0900 Subject: [PATCH] Format: fix formatter bug on nesting begin/end --- spec/compiler/formatter/formatter_spec.cr | 2 ++ spec/compiler/parser/to_s_spec.cr | 1 + src/compiler/crystal/syntax/parser.cr | 2 +- 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/spec/compiler/formatter/formatter_spec.cr b/spec/compiler/formatter/formatter_spec.cr index d306f1d291eb..5ba0822341aa 100644 --- a/spec/compiler/formatter/formatter_spec.cr +++ b/spec/compiler/formatter/formatter_spec.cr @@ -150,6 +150,8 @@ describe Crystal::Formatter do assert_format "begin; 1; end", "begin\n 1\nend" assert_format "begin\n1\n2\n3\nend", "begin\n 1\n 2\n 3\nend" assert_format "begin\n1 ? 2 : 3\nend", "begin\n 1 ? 2 : 3\nend" + assert_format "begin\n begin\n\n end\nend" + assert_format "begin\n ()\nend" assert_format "def foo \n end", "def foo\nend" assert_format "def foo\n1\nend", "def foo\n 1\nend" diff --git a/spec/compiler/parser/to_s_spec.cr b/spec/compiler/parser/to_s_spec.cr index a53a8fbc7bc9..80dbe091cc74 100644 --- a/spec/compiler/parser/to_s_spec.cr +++ b/spec/compiler/parser/to_s_spec.cr @@ -124,4 +124,5 @@ describe "ASTNode#to_s" do expect_to_s %(foo(1, (2 + 3), bar: (4 + 5))) expect_to_s %(if (1 + 2\n3)\n 4\nend) expect_to_s "%x(whoami)", "`whoami`" + expect_to_s %(begin\n ()\nend) end diff --git a/src/compiler/crystal/syntax/parser.cr b/src/compiler/crystal/syntax/parser.cr index ef88ea06bfb9..c0fc31ee4c57 100644 --- a/src/compiler/crystal/syntax/parser.cr +++ b/src/compiler/crystal/syntax/parser.cr @@ -1222,7 +1222,7 @@ module Crystal exps = parse_expressions node, end_location = parse_exception_handler exps node.end_location = end_location - if !node.is_a?(ExceptionHandler) && !node.is_a?(Expressions) + if !node.is_a?(ExceptionHandler) && (!node.is_a?(Expressions) || node.keyword) node = Expressions.new([node]).at(node).at_end(node) end node.keyword = :begin if node.is_a?(Expressions)