diff --git a/spec/compiler/parser/to_s_spec.cr b/spec/compiler/parser/to_s_spec.cr index d69cc3720040..faf73fd64e00 100644 --- a/spec/compiler/parser/to_s_spec.cr +++ b/spec/compiler/parser/to_s_spec.cr @@ -110,7 +110,7 @@ describe "ASTNode#to_s" do expect_to_s "def foo(@[Foo] x, @[Bar] **args, @[Baz] &block)\nend" expect_to_s "{% [1, 2, 3].each { |v| pp(v) } %}", "{% [1, 2, 3].each do |v| pp(v) end %}" expect_to_s "{%\n [1, 2, 3].each { |v| pp(v) }\n%}", "{%\n [1, 2, 3].each do |v| pp(v) end\n%}" - expect_to_s "{% [1, 2, 3].find(&.!.even?) %}", "{% [1, 2, 3].find() do |__arg0| !__arg0.even? end %}" + expect_to_s "{% [1, 2, 3].find(&.even?.!) %}", "{% [1, 2, 3].find() do |__arg0| !__arg0.even? end %}" expect_to_s <<-'CR' {% [1, 2, 3].find do |e| @@ -161,6 +161,10 @@ describe "ASTNode#to_s" do expect_to_s "!a" expect_to_s "!(1 < 2)" expect_to_s "!a.b && true" + expect_to_s "x.!.foo", "(!x).foo" + expect_to_s "x.!.!.foo", "(!(!x)).foo" + expect_to_s "x.foo.!", "!x.foo" + expect_to_s "x.foo.!.!", "!!x.foo" expect_to_s "(1 + 2)..3" expect_to_s "macro foo\n{{ @type }}\nend" expect_to_s "macro foo\n\\{{ @type }}\nend" diff --git a/src/compiler/crystal/syntax/to_s.cr b/src/compiler/crystal/syntax/to_s.cr index 774ffe7973c4..9710742a2040 100644 --- a/src/compiler/crystal/syntax/to_s.cr +++ b/src/compiler/crystal/syntax/to_s.cr @@ -578,9 +578,16 @@ module Crystal true end end + when Not + case exp = obj.exp + when Call + exp.obj.nil? + else + !obj.exp.is_a? Call + end when Var, NilLiteral, BoolLiteral, CharLiteral, NumberLiteral, StringLiteral, StringInterpolation, Path, Generic, InstanceVar, ClassVar, Global, - ImplicitObj, TupleLiteral, NamedTupleLiteral, IsA, Not + ImplicitObj, TupleLiteral, NamedTupleLiteral, IsA false when ArrayLiteral !!obj.of