Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# Cleans up branches like:
# if Shopify.rails_next?
# # Rails 5 login
# else
# # Rails 4 login
# end
module RuboCop
module Cop
module ShopifyRails
class RailsNextUnless < Cop
MSG = 'Rails_next'.freeze
def on_if(node)
loc = node.loc
ifst = node.child_nodes[0]
if loc.respond_to?(:keyword) && loc.keyword.is?('unless'.freeze) && ifst.method_name == :rails_next? && ifst.receiver.source == "Shopify"
add_offense(node, :expression)
end
end
private
def autocorrect(node)
->(corrector) {
found_indent = node.source_range.source_line =~ /^( +)/
if found_indent
whitespaces = $1.size
# the range has to include indentation
r = node.source_range
line_range = r.class.new(r.source_buffer, r.begin_pos - whitespaces, r.end_pos + 1)
corrector.remove(line_range)
else
corrector.remove(node.source_range)
end
}
end
end
class RailsNextIfElse < Cop
MSG = 'Rails_next'.freeze
def on_if(node)
ifst = node.child_nodes[0]
if ifst.method_name == :rails_next? && ifst.receiver.source == "Shopify"
add_offense(node, :expression)
end
end
private
def autocorrect(node)
if_content = node.child_nodes[1]
->(corrector) {
new_source = String.new
if_content.source.each_line do |line|
if line =~ /^( +)/
line = line[2..-1]
end
new_source << line
end
corrector.insert_before(node.source_range, new_source)
corrector.remove(node.source_range)
}
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.