Last active
May 29, 2023 07:22
-
-
Save ykpythemind/be92f8e88b3b38afd5ef4a07d54567ad to your computer and use it in GitHub Desktop.
find_by(email: 'xxx')みたいなのを探すやつ
This file contains 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
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
emailでユニークなのを前提にしているものを探している