call_searcher 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ec09541488a9824d627522654b9c860fc6eb253d1156aa6e689da48013426e08
4
+ data.tar.gz: 2614c0df4752eb151ac47c9cc0b313c2609a725a78d73e4ef2815461dbeb0f85
5
+ SHA512:
6
+ metadata.gz: 2bf7befd237982b4a6b2cbc9d985b63dbdd8ab9f0b1ca66cc2a0c5f5c381efe7882740e81dd51b8c84646aea673cfde5cb8c7deb58562b154a389e2fc9a4012c
7
+ data.tar.gz: 0f6211615e827d2ab58da58c791c2b0acf0875359c159cecea59406d368190f49cfcfc6a30bc0c88963497a4bd4b6e9586715a1b2ad0fb93605f85c5d4da1a79
@@ -0,0 +1,40 @@
1
+ version: 2 # use CircleCI 2.0
2
+ jobs:
3
+ build:
4
+ parallelism: 1
5
+ docker:
6
+ - image: mtsmfm/ruby-trunk
7
+ environment:
8
+ BUNDLE_JOBS: 2
9
+ BUNDLE_PATH: vendor/bundle
10
+ steps:
11
+ - checkout
12
+ - run:
13
+ name: Install bundler
14
+ command: |
15
+ gem install bundler
16
+ bundler update --bundler
17
+ - run:
18
+ name: Which bundler?
19
+ command: bundle -v
20
+ - restore_cache:
21
+ keys:
22
+ - bundle-v2-{{ checksum "Gemfile.lock" }}
23
+ - bundle-v2-
24
+ - run:
25
+ name: Bundle Install
26
+ command: bundle check || bundle install
27
+ - save_cache:
28
+ key: demo-bundle-v2-{{ checksum "Gemfile.lock" }}
29
+ paths:
30
+ - vendor/bundle
31
+ - run:
32
+ name: Run rspec in parallel
33
+ command: |
34
+ bundle exec rspec --profile 10 \
35
+ --format RspecJunitFormatter \
36
+ --out test_results/rspec.xml \
37
+ --format progress \
38
+ $(circleci tests glob "spec/**/*_spec.rb" | circleci tests split --split-by=timings)
39
+ - store_test_results:
40
+ path: test_results
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in call_searcher.gemspec
6
+ gemspec
7
+
8
+ gem 'pry-byebug'
9
+ gem 'rspec_junit_formatter'
@@ -0,0 +1,48 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ call_searcher (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ byebug (10.0.2)
10
+ coderay (1.1.2)
11
+ diff-lcs (1.3)
12
+ method_source (0.9.0)
13
+ pry (0.11.3)
14
+ coderay (~> 1.1.0)
15
+ method_source (~> 0.9.0)
16
+ pry-byebug (3.6.0)
17
+ byebug (~> 10.0)
18
+ pry (~> 0.10)
19
+ rake (10.5.0)
20
+ rspec (3.8.0)
21
+ rspec-core (~> 3.8.0)
22
+ rspec-expectations (~> 3.8.0)
23
+ rspec-mocks (~> 3.8.0)
24
+ rspec-core (3.8.0)
25
+ rspec-support (~> 3.8.0)
26
+ rspec-expectations (3.8.1)
27
+ diff-lcs (>= 1.2.0, < 2.0)
28
+ rspec-support (~> 3.8.0)
29
+ rspec-mocks (3.8.0)
30
+ diff-lcs (>= 1.2.0, < 2.0)
31
+ rspec-support (~> 3.8.0)
32
+ rspec-support (3.8.0)
33
+ rspec_junit_formatter (0.4.1)
34
+ rspec-core (>= 2, < 4, != 2.12.0)
35
+
36
+ PLATFORMS
37
+ ruby
38
+
39
+ DEPENDENCIES
40
+ bundler (~> 2.0)
41
+ call_searcher!
42
+ pry-byebug
43
+ rake (~> 10.0)
44
+ rspec (~> 3.0)
45
+ rspec_junit_formatter
46
+
47
+ BUNDLED WITH
48
+ 2.0.0
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 hkdnet
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,47 @@
1
+ # CallSearcher
2
+
3
+ A static analyzer which helps you know where a method is called.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'call_searcher'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install call_searcher
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ require 'call_searcher'
25
+
26
+ s = CallSearcher::Searcher.new { |e| e.mid == :method_name }
27
+
28
+ result = s.search_dir(root_dir: 'path/to/your/repo', path: '{app,lib,spec}', github: 'org/repo')
29
+
30
+ result.each do |e|
31
+ puts e.github_url
32
+ end
33
+ ```
34
+
35
+ ## Development
36
+
37
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
38
+
39
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
40
+
41
+ ## Contributing
42
+
43
+ Bug reports and pull requests are welcome on GitHub at https://github.com/hkdnet/call_searcher.
44
+
45
+ ## License
46
+
47
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "call_searcher"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,30 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "call_searcher/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "call_searcher"
7
+ spec.version = CallSearcher::VERSION
8
+ spec.authors = ["hkdnet"]
9
+ spec.email = ["satoko.itse@gmail.com"]
10
+
11
+ spec.summary = "ast searcher"
12
+ spec.homepage = "https://github.com/hdknet/call_searcher"
13
+ spec.license = "MIT"
14
+
15
+ # Specify which files should be added to the gem when it is released.
16
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
17
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
18
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ # We need `RubyVM::AbstractSyntaxTree`
25
+ spec.required_ruby_version = '>= 2.6'
26
+
27
+ spec.add_development_dependency "bundler", "~> 2.0"
28
+ spec.add_development_dependency "rake", "~> 10.0"
29
+ spec.add_development_dependency "rspec", "~> 3.0"
30
+ end
@@ -0,0 +1,9 @@
1
+ ---
2
+ version: "3"
3
+ services:
4
+ ruby:
5
+ image: mtsmfm/ruby-trunk
6
+ volumes:
7
+ - ".:/src"
8
+ working_dir: /src
9
+ command: ./test.sh
@@ -0,0 +1,9 @@
1
+ require "call_searcher/version"
2
+
3
+ module CallSearcher
4
+ # Your code goes here...
5
+ end
6
+
7
+ require 'call_searcher/context'
8
+ require 'call_searcher/method_call'
9
+ require 'call_searcher/searcher'
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CallSearcher
4
+ Context = Struct.new(:root_dir, :github, :path, keyword_init: true)
5
+ end
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'pathname'
4
+
5
+ module CallSearcher
6
+ class MethodCall
7
+ attr_reader :node
8
+
9
+ def initialize(node:, context:)
10
+ @node = node
11
+ @context = context
12
+ end
13
+
14
+ def type
15
+ @type ||= node.type
16
+ end
17
+
18
+ def receiver
19
+ case type
20
+ when :FCALL, :VCALL
21
+ nil
22
+ else
23
+ node.children[0]
24
+ end
25
+ end
26
+
27
+ def mid
28
+ case type
29
+ when :FCALL, :VCALL
30
+ node.children[0]
31
+ else
32
+ node.children[1]
33
+ end
34
+ end
35
+
36
+ def location
37
+ "#{@context.path}:#{@node.first_lineno}"
38
+ end
39
+
40
+ def inspect
41
+ "<#{self.class} #{type}@#{location}>"
42
+ end
43
+
44
+ def github_url
45
+ if @context.github
46
+ "https://github.com/#{@context.github}/blob/master/#{relative_path}#L#{@node.first_lineno}"
47
+ end
48
+ end
49
+
50
+ def relative_path
51
+ Pathname.new(@context.path).relative_path_from(Pathname.new(@context.root_dir))
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CallSearcher
4
+ class Searcher
5
+ def initialize(&blk)
6
+ @condition = blk || ->(*) { true }
7
+ end
8
+
9
+ def search_dir(root_dir: '.', path:, github: nil)
10
+ path = File.expand_path(path, root_dir)
11
+ context = CallSearcher::Context.new(root_dir: root_dir, github: github)
12
+ Dir.glob(File.join(path, "**", "*.rb")).reduce([]) do |arr, f|
13
+ if File.file?(f)
14
+ arr + search(file: f, context: context)
15
+ end
16
+ end
17
+ end
18
+
19
+ def search(file:, context: nil)
20
+ context = context&.dup || CallSearcher::Context.new
21
+ context.path = file
22
+
23
+ ast = RubyVM::AbstractSyntaxTree.parse_file(file)
24
+
25
+ filter(ast, context)
26
+ end
27
+
28
+ private
29
+
30
+ def filter(node, context)
31
+ ret = node.children.grep(RubyVM::AbstractSyntaxTree::Node).flat_map do |child|
32
+ filter(child, context)
33
+ end
34
+
35
+ case node.type
36
+ when :CALL, :QCALL, :OPCALL, :FCALL, :VCALL
37
+ method_call = CallSearcher::MethodCall.new(node: node, context: context)
38
+ if @condition.call(method_call)
39
+ ret << method_call
40
+ end
41
+ else
42
+ nil
43
+ end
44
+
45
+ ret
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,3 @@
1
+ module CallSearcher
2
+ VERSION = "0.1.0"
3
+ end
data/test.sh ADDED
@@ -0,0 +1,5 @@
1
+ #!/bin/bash
2
+
3
+ bundle update --bundler
4
+
5
+ bundle exec rake spec
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: call_searcher
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - hkdnet
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-05-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description:
56
+ email:
57
+ - satoko.itse@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".circleci/config.yml"
63
+ - ".gitignore"
64
+ - ".rspec"
65
+ - Gemfile
66
+ - Gemfile.lock
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - call_searcher.gemspec
73
+ - docker-compose.yml
74
+ - lib/call_searcher.rb
75
+ - lib/call_searcher/context.rb
76
+ - lib/call_searcher/method_call.rb
77
+ - lib/call_searcher/searcher.rb
78
+ - lib/call_searcher/version.rb
79
+ - test.sh
80
+ homepage: https://github.com/hdknet/call_searcher
81
+ licenses:
82
+ - MIT
83
+ metadata: {}
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '2.6'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubygems_version: 3.0.3
100
+ signing_key:
101
+ specification_version: 4
102
+ summary: ast searcher
103
+ test_files: []