Skip to content

Commit

Permalink
Formatter: Add feature flag for method_signature_yield (#13215)
Browse files Browse the repository at this point in the history
  • Loading branch information
straight-shoota authored Mar 25, 2023
1 parent 5aac124 commit a69aa14
Show file tree
Hide file tree
Showing 2 changed files with 193 additions and 39 deletions.
212 changes: 181 additions & 31 deletions spec/compiler/formatter/formatter_spec.cr
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
require "spec"
require "../../../src/compiler/crystal/formatter"

private def assert_format(input, output = input, strict = false, file = __FILE__, line = __LINE__)
private def assert_format(input, output = input, strict = false, flags = nil, file = __FILE__, line = __LINE__)
it "formats #{input.inspect}", file, line do
output = "#{output}\n" unless strict
result = Crystal.format(input)
result = Crystal.format(input, flags: flags)
unless result == output
message = <<-ERROR
Expected
Expand Down Expand Up @@ -553,162 +553,149 @@ describe Crystal::Formatter do
assert_format "with foo yield bar"

context "adds `&` to yielding methods that don't have a block parameter (#8764)" do
assert_format <<-CRYSTAL,
assert_format <<-CRYSTAL, <<-CRYSTAL, flags: %w[method_signature_yield]
def foo
yield
end
CRYSTAL
<<-CRYSTAL
def foo(&)
yield
end
CRYSTAL

assert_format <<-CRYSTAL,
assert_format <<-CRYSTAL, <<-CRYSTAL, flags: %w[method_signature_yield]
def foo()
yield
end
CRYSTAL
<<-CRYSTAL
def foo(&)
yield
end
CRYSTAL

assert_format <<-CRYSTAL,
assert_format <<-CRYSTAL, <<-CRYSTAL, flags: %w[method_signature_yield]
def foo(
)
yield
end
CRYSTAL
<<-CRYSTAL
def foo(&)
yield
end
CRYSTAL

# #13091
assert_format <<-CRYSTAL,
assert_format <<-CRYSTAL, <<-CRYSTAL, flags: %w[method_signature_yield]
def foo # bar
yield
end
CRYSTAL
<<-CRYSTAL
def foo(&) # bar
yield
end
CRYSTAL

assert_format <<-CRYSTAL,
assert_format <<-CRYSTAL, <<-CRYSTAL, flags: %w[method_signature_yield]
def foo(x)
yield
end
CRYSTAL
<<-CRYSTAL
def foo(x, &)
yield
end
CRYSTAL

assert_format <<-CRYSTAL,
assert_format <<-CRYSTAL, <<-CRYSTAL, flags: %w[method_signature_yield]
def foo(x ,)
yield
end
CRYSTAL
<<-CRYSTAL
def foo(x, &)
yield
end
CRYSTAL

assert_format <<-CRYSTAL,
assert_format <<-CRYSTAL, <<-CRYSTAL, flags: %w[method_signature_yield]
def foo(x,
y)
yield
end
CRYSTAL
<<-CRYSTAL
def foo(x,
y, &)
yield
end
CRYSTAL

assert_format <<-CRYSTAL,
assert_format <<-CRYSTAL, <<-CRYSTAL, flags: %w[method_signature_yield]
def foo(x,
y,)
yield
end
CRYSTAL
<<-CRYSTAL
def foo(x,
y, &)
yield
end
CRYSTAL

assert_format <<-CRYSTAL,
assert_format <<-CRYSTAL, <<-CRYSTAL, flags: %w[method_signature_yield]
def foo(x
)
yield
end
CRYSTAL
<<-CRYSTAL
def foo(x,
&)
yield
end
CRYSTAL

assert_format <<-CRYSTAL,
assert_format <<-CRYSTAL, <<-CRYSTAL, flags: %w[method_signature_yield]
def foo(x,
)
yield
end
CRYSTAL
<<-CRYSTAL
def foo(x,
&)
yield
end
CRYSTAL

assert_format <<-CRYSTAL,
assert_format <<-CRYSTAL, <<-CRYSTAL, flags: %w[method_signature_yield]
def foo(
x)
yield
end
CRYSTAL
<<-CRYSTAL
def foo(
x, &
)
yield
end
CRYSTAL

assert_format <<-CRYSTAL,
assert_format <<-CRYSTAL, <<-CRYSTAL, flags: %w[method_signature_yield]
def foo(
x, y)
yield
end
CRYSTAL
<<-CRYSTAL
def foo(
x, y, &
)
yield
end
CRYSTAL

assert_format <<-CRYSTAL,
assert_format <<-CRYSTAL, <<-CRYSTAL, flags: %w[method_signature_yield]
def foo(
x,
y)
yield
end
CRYSTAL
<<-CRYSTAL
def foo(
x,
y, &
Expand All @@ -717,14 +704,13 @@ describe Crystal::Formatter do
end
CRYSTAL

assert_format <<-CRYSTAL,
assert_format <<-CRYSTAL, <<-CRYSTAL, flags: %w[method_signature_yield]
def foo(
x,
)
yield
end
CRYSTAL
<<-CRYSTAL
def foo(
x,
&
Expand All @@ -733,7 +719,171 @@ describe Crystal::Formatter do
end
CRYSTAL

assert_format "macro f\n yield\n {{ yield }}\nend"
assert_format <<-CRYSTAL, <<-CRYSTAL, flags: %w[method_signature_yield]
def foo(a, **b)
yield
end
CRYSTAL
def foo(a, **b, &)
yield
end
CRYSTAL

assert_format "macro f\n yield\n {{ yield }}\nend", flags: %w[method_signature_yield]
end

context "does not add `&` without flag `method_signature_yield`" do
assert_format <<-CRYSTAL
def foo
yield
end
CRYSTAL

assert_format <<-CRYSTAL, <<-CRYSTAL
def foo()
yield
end
CRYSTAL
def foo
yield
end
CRYSTAL

assert_format <<-CRYSTAL, <<-CRYSTAL
def foo(
)
yield
end
CRYSTAL
def foo
yield
end
CRYSTAL

# #13091
assert_format <<-CRYSTAL
def foo # bar
yield
end
CRYSTAL

assert_format <<-CRYSTAL
def foo(x)
yield
end
CRYSTAL

assert_format <<-CRYSTAL, <<-CRYSTAL
def foo(x ,)
yield
end
CRYSTAL
def foo(x)
yield
end
CRYSTAL

assert_format <<-CRYSTAL
def foo(x,
y)
yield
end
CRYSTAL

assert_format <<-CRYSTAL, <<-CRYSTAL
def foo(x,
y,)
yield
end
CRYSTAL
def foo(x,
y)
yield
end
CRYSTAL

assert_format <<-CRYSTAL, <<-CRYSTAL
def foo(x
)
yield
end
CRYSTAL
def foo(x)
yield
end
CRYSTAL

assert_format <<-CRYSTAL, <<-CRYSTAL
def foo(x,
)
yield
end
CRYSTAL
def foo(x)
yield
end
CRYSTAL

assert_format <<-CRYSTAL, <<-CRYSTAL
def foo(
x)
yield
end
CRYSTAL
def foo(
x
)
yield
end
CRYSTAL

assert_format <<-CRYSTAL, <<-CRYSTAL
def foo(
x, y)
yield
end
CRYSTAL
def foo(
x, y
)
yield
end
CRYSTAL

assert_format <<-CRYSTAL, <<-CRYSTAL
def foo(
x,
y)
yield
end
CRYSTAL
def foo(
x,
y
)
yield
end
CRYSTAL

assert_format <<-CRYSTAL, <<-CRYSTAL
def foo(
x,
)
yield
end
CRYSTAL
def foo(
x
)
yield
end
CRYSTAL

assert_format <<-CRYSTAL
def foo(a, **b)
yield
end
CRYSTAL
end

assert_format "1 + 2", "1 + 2"
Expand Down
Loading

0 comments on commit a69aa14

Please sign in to comment.