Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Formatter: add & to yielding methods without a block parameter #12951

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions samples/2048.cr
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ module Screen
65536 => {Colorize::ColorANSI::White, Colorize::ColorANSI::Black},
}

def self.colorize_for(tile)
def self.colorize_for(tile, &)
fg_color, bg_color = TILES[tile]
color = Colorize.with.fore(fg_color)
color = color.back(bg_color) if bg_color
Expand Down Expand Up @@ -241,7 +241,7 @@ class Game
end
end

def each_cell_with_index
def each_cell_with_index(&)
0.upto(@grid.size - 1) do |row|
0.upto(@grid.size - 1) do |col|
yield @grid[row][col], row, col
Expand Down Expand Up @@ -295,7 +295,7 @@ class Game
end
end

def movable_tiles(direction, drow, dcol)
def movable_tiles(direction, drow, dcol, &)
max = @grid.size - 1
from_row, to_row, from_column, to_column =
case direction
Expand Down
4 changes: 2 additions & 2 deletions samples/meteor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class MyIterator(T)
def initialize(@data : T, &@block : T -> T)
end

def each
def each(&)
while true
yield @data
@data = @block.call(@data)
Expand Down Expand Up @@ -145,7 +145,7 @@ class SolutionNode
getter :x
getter :prev

def each
def each(&)
yield @x
p = prev
while y = p
Expand Down
10 changes: 5 additions & 5 deletions samples/red_black_tree.cr
Original file line number Diff line number Diff line change
Expand Up @@ -167,27 +167,27 @@ class RedBlackTree
y
end

def inorder_walk(x = root)
def inorder_walk(x = root, &)
x = self.minimum
while !x.nil_node?
yield x.key
x = successor(x)
end
end

def each(x = root)
def each(x = root, &)
inorder_walk(x) { |k| yield k }
end

def reverse_inorder_walk(x = root)
def reverse_inorder_walk(x = root, &)
x = self.maximum
while !x.nil_node?
yield x.key
x = predecessor(x)
end
end

def reverse_each(x = root)
def reverse_each(x = root, &)
reverse_inorder_walk(x) { |k| yield k }
end

Expand Down Expand Up @@ -382,7 +382,7 @@ class RedBlackTreeRunner
end
end

def bench(name, n = 1)
def bench(name, n = 1, &)
start = Time.monotonic
print "#{name}: "
res = nil
Expand Down
2 changes: 1 addition & 1 deletion samples/sdl/fire.cr
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ class Points
end
end

def each
def each(&)
@points.each do |point|
yield point unless point.dead?
end
Expand Down
2 changes: 1 addition & 1 deletion samples/sdl/sdl/sdl.cr
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ module SDL
LibSDL.quit
end

def self.poll_events
def self.poll_events(&)
while LibSDL.poll_event(out event) == 1
yield event
end
Expand Down
2 changes: 1 addition & 1 deletion spec/compiler/crystal/tools/context_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ private def processed_context_visitor(code, cursor_location)
{visitor, process_result}
end

private def run_context_tool(code)
private def run_context_tool(code, &)
cursor_location = nil

code.lines.each_with_index do |line, line_number_0|
Expand Down
6 changes: 3 additions & 3 deletions spec/compiler/crystal/tools/expand_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ private def processed_expand_visitor(code, cursor_location)
{visitor, process_result}
end

private def run_expand_tool(code)
private def run_expand_tool(code, &)
cursor_location = nil

code.lines.each_with_index do |line, line_number_0|
Expand Down Expand Up @@ -41,7 +41,7 @@ private def assert_expand(code, expected_result)
assert_expand(code, expected_result) { }
end

private def assert_expand(code, expected_result)
private def assert_expand(code, expected_result, &)
run_expand_tool code do |result|
result.status.should eq("ok")
result.message.should eq("#{expected_result.size} expansion#{expected_result.size >= 2 ? "s" : ""} found")
Expand All @@ -59,7 +59,7 @@ private def assert_expand_simple(code, expanded, original = code.delete('‸'))
assert_expand_simple(code, expanded, original) { }
end

private def assert_expand_simple(code, expanded, original = code.delete('‸'))
private def assert_expand_simple(code, expanded, original = code.delete('‸'), &)
assert_expand(code, [[original, expanded]]) { |result| yield result.expansions.not_nil![0] }
end

Expand Down
4 changes: 2 additions & 2 deletions spec/compiler/crystal/tools/init_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ end

# Creates a temporary directory, cd to it and run the block inside it.
# The directory and its content is deleted when the block return.
private def within_temporary_directory
private def within_temporary_directory(&)
with_tempfile "init_spec_tmp" do |tmp_path|
Dir.mkdir_p(tmp_path)
Dir.cd(tmp_path) do
Expand All @@ -31,7 +31,7 @@ private def within_temporary_directory
end
end

private def with_file(name)
private def with_file(name, &)
yield File.read(name)
end

Expand Down
2 changes: 1 addition & 1 deletion spec/compiler/crystal/tools/table_print_spec.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require "spec"
require "compiler/crystal/tools/table_print"

private def assert_table(expected)
private def assert_table(expected, &)
actual = String::Builder.build do |builder|
Crystal::TablePrint.new(builder).build do |tp|
with tp yield
Expand Down
2 changes: 1 addition & 1 deletion spec/compiler/crystal/types_spec.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "../../spec_helper"

private def assert_type_to_s(expected)
private def assert_type_to_s(expected, &)
p = Program.new
t = with p yield p
t.to_s.should eq(expected)
Expand Down
172 changes: 172 additions & 0 deletions spec/compiler/formatter/formatter_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,178 @@ 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,
def foo
yield
end
CRYSTAL
<<-CRYSTAL
def foo(&)
yield
end
CRYSTAL

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

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

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

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

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

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

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

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

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

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

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

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

assert_format "macro f\n yield\n {{ yield }}\nend"
end

assert_format "1 + 2", "1 + 2"
assert_format "1 &+ 2", "1 &+ 2"
assert_format "1 > 2", "1 > 2"
Expand Down
2 changes: 1 addition & 1 deletion spec/compiler/semantic/concrete_types_spec.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "../../spec_helper"

private def assert_concrete_types(str)
private def assert_concrete_types(str, &)
result = semantic("struct Witness;end\n\n#{str}")
program = result.program

Expand Down
2 changes: 1 addition & 1 deletion spec/compiler/semantic/def_overload_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1674,7 +1674,7 @@ describe "Semantic: def overload" do
end
end

private def each_union_variant(t1, t2)
private def each_union_variant(t1, t2, &)
yield "#{t1} | #{t2}"
yield "#{t2} | #{t1}"
# yield "Union(#{t1}, #{t2})"
Expand Down
6 changes: 3 additions & 3 deletions spec/spec_helper.cr
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ record SemanticResult,
program : Program,
node : ASTNode

def assert_type(str, *, inject_primitives = false, flags = nil, file = __FILE__, line = __LINE__)
def assert_type(str, *, inject_primitives = false, flags = nil, file = __FILE__, line = __LINE__, &)
result = semantic(str, flags: flags, inject_primitives: inject_primitives)
program = result.program
expected_type = with program yield program
Expand Down Expand Up @@ -166,7 +166,7 @@ def assert_macro_error(macro_body, message = nil, *, flags = nil, file = __FILE_
end
end

def prepare_macro_call(macro_body, flags = nil)
def prepare_macro_call(macro_body, flags = nil, &)
program = new_program
program.flags.concat(flags.split) if flags
args = yield program
Expand Down Expand Up @@ -280,7 +280,7 @@ def run(code, filename = nil, inject_primitives = true, debug = Crystal::Debug::
end
end

def test_c(c_code, crystal_code, *, file = __FILE__)
def test_c(c_code, crystal_code, *, file = __FILE__, &)
with_temp_c_object_file(c_code, file: file) do |o_filename|
yield run(%(
require "prelude"
Expand Down
Loading