Skip to content

Instantly share code, notes, and snippets.

@ykpythemind
Last active May 29, 2023 07:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ykpythemind/be92f8e88b3b38afd5ef4a07d54567ad to your computer and use it in GitHub Desktop.
Save ykpythemind/be92f8e88b3b38afd5ef4a07d54567ad to your computer and use it in GitHub Desktop.
find_by(email: 'xxx')みたいなのを探すやつ
# https://nacl-ltd.github.io/2021/07/02/ruby-ast.html
require 'bundler/inline'
gemfile do
ruby '3.2.2' # 適宜変える
source 'https://rubygems.org'
gem 'parser'
gem 'debug'
end
require 'parser/current'
require 'debug'
class Processor
include AST::Processor::Mixin
def initialize(path)
@path = path
@result = []
end
attr_reader :result, :path
def on_send(node)
rsv = node.to_a[0]
m = node.to_a[1]
#if m == :email
# binding.b
#end
if m == :find_by && rsv&.type == :send
if check_hash(node.to_a[2], node.location.line)
puts "#{path}: #{node.location.line}"
end
end
node
end
def check_hash(node, line)
return if node.type != :hash
# s(:hash,
# s(:pair,
# s(:sym, :email),
# s(:lvar, :foo)))
# binding.b
node.to_a.tap {
# 複数pairがあるとき ... find_by(email:, fuga:)
if _1.size != 1
puts "あやしいなあ #{path}: #{line}"
# raise(_1.inspect)
end
}[0].to_a[0].to_a[0] == :email
end
def handler_missing(node)
node.children.each do |child|
process(child) if child.is_a?(AST::Node)
end
end
end
if ENV['DEBUG'] == 'true'
str = "ffffff.find_by(email: user.email, name: user.display_name)"
expr = Parser::CurrentRuby.parse(str)
processor = Processor.new('')
processor.process(expr)
exit
end
Dir.glob("**/*.rb").each do |path|
# puts path
expr = Parser::CurrentRuby.parse(File.read(path))
processor = Processor.new(path)
processor.process(expr)
end
@ykpythemind
Copy link
Author

emailでユニークなのを前提にしているものを探している

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment