Skip to content

Instantly share code, notes, and snippets.

@baweaver
Created April 14, 2024 05:35
Show Gist options
  • Save baweaver/1746ec0fdb5054442e22bc4c3920ad87 to your computer and use it in GitHub Desktop.
Save baweaver/1746ec0fdb5054442e22bc4c3920ad87 to your computer and use it in GitHub Desktop.
class ShorthandProcCallMacro < Refactor::Rule
matches do |node|
node in [:block, receiver,
[[:arg, arg_name]], [:send, [:lvar, ^arg_name], method_name]
]
end
replace do |_node, match_data|
"#{match_data[:receiver].source}(&:#{match_data[:method_name]})"
end
end
# Should translate into something like:
class ShorthandProcCallMacro < Refactor::Rule
def on_block(node)
return false unless node in [:block, receiver,
[[:arg, arg_name]], [:send, [:lvar, ^arg_name], method_name]
]
rewrite("#{receiver.source}(&:#{method_name})")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment