-
-
Save sahidursuman/050dc0ce35746a9ee7a8ab154b5a717b to your computer and use it in GitHub Desktop.
Using `parser` to look for `Resque.enqueue` calls inside a `ActiveRecord::Base.transaction` block.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'parser/current' | |
class Processor < AST::Processor | |
attr_accessor :verbose | |
def initialize(*args) | |
super | |
self.verbose = false | |
end | |
def process(node) | |
log "process: #{node.type}, #{node.children}" if node | |
super | |
return unless node.respond_to?(:children) | |
node.children.each do |child| | |
process(child) unless [Symbol, Fixnum].include?(child.class) | |
end | |
end | |
def on_begin(node) | |
node.children.each { |c| process(c) } | |
end | |
def handler_missing(node) | |
log "missing #{node.type}" | |
end | |
def on_def(node) | |
line_num = node.loc.line | |
method_name = node.children[0] | |
log "method: #{method_name}" | |
end | |
def on_defs(node) | |
line_num = node.loc.line | |
method_name = "self.#{node.children[1]}" | |
log "method: #{method_name}" | |
end | |
def on_class(node) | |
log node.type | |
end | |
def on_begin(node) | |
log 'found `begin`' | |
end | |
def on_block(node) | |
log 'found `block`' | |
end | |
def on_send(node) | |
log node.children.join(" : ") | |
if node.children.last == :transaction | |
log ?< * 40 | |
log "Found `transaction`: #{node}" | |
log ?* * 40 | |
log node.children | |
log node.loc.begin | |
log node.loc.end | |
log ?> * 40 | |
end | |
if node.children[0].children[1].to_s == 'Resque' && node.children[1].to_s == 'enqueue' | |
log ?< * 40 | |
log "Found `Resque.enqueue`" | |
log ?> * 40 | |
end | |
end | |
def on_if(node) | |
end | |
def on_const(node) | |
end | |
def log(*args) | |
# return unless verbose | |
puts args | |
end | |
end | |
code = <<-CODE | |
class ClientTeamMemberCreateForm < BaseForm | |
def save | |
return false unless self.valid? | |
User.transaction do | |
if @user.save | |
Resque.enqueue :myjob, 1 | |
end | |
end | |
end | |
end | |
CODE | |
parsed_code = Parser::CurrentRuby.parse(code) | |
processor = Processor.new | |
processor.process(parsed_code) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment