Skip to content

Instantly share code, notes, and snippets.

@zdennis
Last active August 2, 2019 16:40
Show Gist options
  • Save zdennis/c1d64ac6a1f98a5d35812f45b7069caa to your computer and use it in GitHub Desktop.
Save zdennis/c1d64ac6a1f98a5d35812f45b7069caa to your computer and use it in GitHub Desktop.
Rewriting spec files to add `rails3: true`
# requires pry, parser, and ast gems
#
# It also expects that you have build a file of spec files that should be modified. This file
# should contain one path/to/file per line.
#
# Running:
# cat list_of_passing_spec_files.txt | xargs ruby-rewrite --load 'mark_specs_as_rails_3.rb' -m
require 'pry'
class MarkSpecsAsRails_3 < ::Parser::TreeRewriter
# Method calls are tagged as :send
# We're only interested in method calls so hook into on_send.
def on_send(node)
return if @already_rewrote_top_level_describe
method = node.children[1]
return unless method == :describe
@already_rewrote_top_level_describe = true
hash_node = node.children.find { |child_node| child_node.is_a?(Parser::AST::Node) && child_node.type == :hash }
if hash_node
rails3_node = hash_node.children.map(&:children).find do |(key_sexp, _)|
key_sexp == Parser::CurrentRuby.parse(":rails3")
end
return if rails3_node # already present, no need to do anything
# always use the range of the last child otherwise we will
# insert after the closing right bracket if the file explicitly uses { key: value, .. }
# to define the hash
insertion_point = hash_node.children.last.loc.expression.end
else
insertion_point = node.loc.expression.end
end
insert_after insertion_point, ", rails3: true"
super
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment