ast_search 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
+ SHA1:
3
+ metadata.gz: e3657c57448d0aa9c6d91510a7b3e160497fd1fc
4
+ data.tar.gz: 70259aabe4ba27ec24885b2fa51591323429f68c
5
+ SHA512:
6
+ metadata.gz: 0c5b19f69f4df44f5b22ffedf8eef8246c010fc0ccaed2f9f2298f8ddfbed08550f1c8ae8e10c908e8d2b7aaa74171e6b0b730e84832ef919bef73eb84a4c27f
7
+ data.tar.gz: c7550afc554fbb9a6cfc0e7e4715511033174b7b9d592057821bff299633ad9f43bbafd996ecd177db9d17ad66bb6c8f9f3ba63374f86ce5782df8888e013600
@@ -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
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.3.1
7
+ before_install: gem install bundler -v 1.16.3
data/Gemfile ADDED
@@ -0,0 +1,6 @@
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 ast_search.gemspec
6
+ gemspec
@@ -0,0 +1,50 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ast_search (0.1.0)
5
+ parser (>= 2.3)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ ast (2.4.0)
11
+ byebug (10.0.2)
12
+ coderay (1.1.2)
13
+ diff-lcs (1.3)
14
+ method_source (0.9.0)
15
+ parser (2.5.1.2)
16
+ ast (~> 2.4.0)
17
+ pry (0.11.3)
18
+ coderay (~> 1.1.0)
19
+ method_source (~> 0.9.0)
20
+ pry-byebug (3.6.0)
21
+ byebug (~> 10.0)
22
+ pry (~> 0.10)
23
+ rake (10.5.0)
24
+ rspec (3.7.0)
25
+ rspec-core (~> 3.7.0)
26
+ rspec-expectations (~> 3.7.0)
27
+ rspec-mocks (~> 3.7.0)
28
+ rspec-core (3.7.1)
29
+ rspec-support (~> 3.7.0)
30
+ rspec-expectations (3.7.0)
31
+ diff-lcs (>= 1.2.0, < 2.0)
32
+ rspec-support (~> 3.7.0)
33
+ rspec-mocks (3.7.0)
34
+ diff-lcs (>= 1.2.0, < 2.0)
35
+ rspec-support (~> 3.7.0)
36
+ rspec-support (3.7.1)
37
+
38
+ PLATFORMS
39
+ ruby
40
+
41
+ DEPENDENCIES
42
+ ast_search!
43
+ bundler (~> 1.16)
44
+ pry
45
+ pry-byebug
46
+ rake (~> 10.0)
47
+ rspec (~> 3.0)
48
+
49
+ BUNDLED WITH
50
+ 1.16.3
@@ -0,0 +1,17 @@
1
+ Permission is hereby granted, free of charge, to any person obtaining a copy
2
+ of this software and associated documentation files (the "Software"), to deal
3
+ in the Software without restriction, including without limitation the rights
4
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
5
+ copies of the Software, and to permit persons to whom the Software is
6
+ furnished to do so, subject to the following conditions:
7
+
8
+ The above copyright notice and this permission notice shall be included in
9
+ all copies or substantial portions of the Software.
10
+
11
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
17
+ THE SOFTWARE.
@@ -0,0 +1,78 @@
1
+ # AstSearch
2
+
3
+ Ruby code analyzer powered by [parser](https://github.com/whitequark/parser) and [ast](https://github.com/whitequark/ast).
4
+
5
+ So far the public API is limited to a single function:
6
+ ```ruby
7
+ AstSearch.find_external_classes(ruby_source)
8
+ ```
9
+
10
+ It finds references to classes defined outside of the parsed source code, e.g.:
11
+ ```
12
+ irb(main):001:0> class Foo
13
+ irb(main):002:1> end
14
+ => nil
15
+ irb(main):003:0> AstSearch.find_external_classes <<-SRC
16
+ irb(main):004:0" class Bar
17
+ irb(main):005:0" Foo.hello
18
+ irb(main):006:0" end
19
+ irb(main):007:0" SRC
20
+ => [Foo]
21
+ ```
22
+
23
+ ### Hm, so how can it be useful?
24
+
25
+ You can build a rake task that scans database migrations for occurrences of model classes.
26
+ In case of a Rails app it would be something like this:
27
+ ```ruby
28
+ task find_models_in_migrations: :environment do
29
+ Dir[Rails.root.join("db/migrate/*.rb")].each do |path|
30
+ models = AstSearch.find_external_classes(IO.read(path)).select do |klass|
31
+ kalss != ActiveRecord::Base && klass.ancestors.include?(ActiveRecord::Base)
32
+ end
33
+
34
+ if models.any?
35
+ puts path
36
+ models.each { |model| puts model }
37
+ end
38
+ end
39
+ end
40
+ ```
41
+
42
+ Though, the idea of looking something up in code is not limited to migrations.
43
+
44
+ ## Installation
45
+
46
+ Add this line to your application's Gemfile:
47
+
48
+ ```ruby
49
+ gem 'ast_search'
50
+ ```
51
+
52
+ And then execute:
53
+
54
+ $ bundle
55
+
56
+ Or install it yourself as:
57
+
58
+ $ gem install ast_search
59
+
60
+ ## Usage
61
+
62
+ ```ruby
63
+ require "ast_search"
64
+
65
+ source = IO.read("path/to/source_code.rb")
66
+
67
+ AstSearch.find_external_classes(source)
68
+ ```
69
+
70
+ ## Development
71
+
72
+ 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.
73
+
74
+ 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).
75
+
76
+ ## Contributing
77
+
78
+ Bug reports and pull requests are welcome on GitHub at https://github.com/letmein/ast_search.
@@ -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,41 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "ast_search/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ast_search"
8
+ spec.version = AstSearch::VERSION
9
+ spec.authors = ["Yuriy Kharchenko"]
10
+ spec.email = ["rm1945@yandex.ru"]
11
+
12
+ spec.summary = %q{Parse Ruby code & search for AST nodes}
13
+ spec.description = %q{Parse Ruby code & search for AST nodes}
14
+ spec.homepage = "https://github.com/letmein/ast_search"
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ if spec.respond_to?(:metadata)
19
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
20
+ else
21
+ raise "RubyGems 2.0 or newer is required to protect against " \
22
+ "public gem pushes."
23
+ end
24
+
25
+ # Specify which files should be added to the gem when it is released.
26
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
27
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
28
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
29
+ end
30
+ spec.bindir = "exe"
31
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
32
+ spec.require_paths = ["lib"]
33
+
34
+ spec.add_dependency "parser", ">= 2.3"
35
+
36
+ spec.add_development_dependency "bundler", "~> 1.16"
37
+ spec.add_development_dependency "pry"
38
+ spec.add_development_dependency "pry-byebug"
39
+ spec.add_development_dependency "rake", "~> 10.0"
40
+ spec.add_development_dependency "rspec", "~> 3.0"
41
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ast_search"
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,24 @@
1
+ require "pry"
2
+ require "parser/current"
3
+
4
+ require "ast_search/version"
5
+ require "ast_search/name_path"
6
+ require "ast_search/node"
7
+ require "ast_search/queries/class_defs"
8
+ require "ast_search/queries/const_occurrences"
9
+ require "ast_search/queries/external_classes"
10
+
11
+ module AstSearch
12
+ def self.parse(src)
13
+ Parser::CurrentRuby.parse(src)
14
+ end
15
+
16
+ #
17
+ # Public API
18
+ #
19
+
20
+ def self.find_external_classes(src)
21
+ ast = parse(src)
22
+ AstSearch::Queries::ExternalClasses.new.call(ast)
23
+ end
24
+ end
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AstSearch
4
+ class NamePath
5
+ SEPARATOR = "::"
6
+
7
+ def self.from_string(str)
8
+ new(str.split(SEPARATOR))
9
+ end
10
+
11
+ def initialize(path = [])
12
+ @path = path
13
+ end
14
+
15
+ def +(path_element)
16
+ new_path = path.dup.push(path_element)
17
+ NamePath.new(new_path)
18
+ end
19
+
20
+ def eql?(other)
21
+ to_a == other.to_a
22
+ end
23
+
24
+ def hash
25
+ path.hash
26
+ end
27
+
28
+ def shift
29
+ NamePath.new(path[1..-1])
30
+ end
31
+
32
+ def blank?
33
+ path.size.zero?
34
+ end
35
+
36
+ def to_a
37
+ path
38
+ end
39
+
40
+ def to_s
41
+ @_to_s ||= path.join(SEPARATOR)
42
+ end
43
+
44
+ def constantize
45
+ if to_s.respond_to?(:constantize)
46
+ # NOTE: ActiveSupport's `constantize` behaves slightly different from `Object.const_get`
47
+ to_s.constantize
48
+ else
49
+ Object.const_get(to_s)
50
+ end
51
+ rescue NameError
52
+ nil
53
+ end
54
+
55
+ private
56
+
57
+ attr_reader :path
58
+ end
59
+ end
@@ -0,0 +1,42 @@
1
+ module AstSearch
2
+ class Node
3
+ def initialize(ast)
4
+ @ast = ast
5
+ end
6
+
7
+ attr_reader :ast
8
+
9
+ def const?
10
+ type == :const
11
+ end
12
+
13
+ def class?
14
+ type == :class
15
+ end
16
+
17
+ def module?
18
+ type == :module
19
+ end
20
+
21
+ def name
22
+ return unless ast.loc.respond_to?(:name)
23
+ ast.loc.name.source
24
+ end
25
+
26
+ def source
27
+ ast.loc.expression.source
28
+ end
29
+
30
+ def children
31
+ return [] unless ast.respond_to?(:children)
32
+ ast.children
33
+ end
34
+
35
+ private
36
+
37
+ def type
38
+ return nil unless ast.respond_to?(:type)
39
+ ast.type
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,30 @@
1
+ module AstSearch
2
+ module Queries
3
+ class ClassDefs
4
+ def call(ast)
5
+ traverse(ast, [], NamePath.new).flatten.map(&:to_s)
6
+ end
7
+
8
+ private
9
+
10
+ def traverse(ast, result, path)
11
+ result + process_node(Node.new(ast), result, path)
12
+ end
13
+
14
+ def process_node(node, result, path)
15
+ if node.module?
16
+ traverse_children(node, result, path + node.name)
17
+ elsif node.class?
18
+ new_path = path + node.name
19
+ [new_path] + traverse_children(node, result, new_path)
20
+ else
21
+ traverse_children(node, result, path)
22
+ end
23
+ end
24
+
25
+ def traverse_children(node, result, path)
26
+ node.children.map { |child| traverse(child, result, path) }.flatten
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,27 @@
1
+ module AstSearch
2
+ module Queries
3
+ class ConstOccurrences
4
+ def call(ast)
5
+ traverse(ast, []).compact.uniq
6
+ end
7
+
8
+ private
9
+
10
+ def traverse(ast, result)
11
+ result + process_node(Node.new(ast), result)
12
+ end
13
+
14
+ def process_node(node, result)
15
+ if node.const?
16
+ [node.source]
17
+ else
18
+ traverse_children(node, result)
19
+ end
20
+ end
21
+
22
+ def traverse_children(node, result)
23
+ node.children.map { |child| traverse(child, result) }.flatten
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,32 @@
1
+ module AstSearch
2
+ module Queries
3
+ # A class is considered external when it is defined outside of the parsed class.
4
+ class ExternalClasses
5
+ def initialize
6
+ @find_constants = ConstOccurrences.new
7
+ @find_classes = ClassDefs.new
8
+ end
9
+
10
+ def call(ast)
11
+ (referred_constant_names(ast) - internal_class_names(ast))
12
+ .map(&:constantize).compact.uniq.select { |const| const.is_a?(Class) }
13
+ end
14
+
15
+ private
16
+
17
+ attr_reader :find_constants, :find_classes
18
+
19
+ def referred_constant_names(ast)
20
+ find_constants.call(ast).map { |name| to_path(name) }
21
+ end
22
+
23
+ def internal_class_names(ast)
24
+ find_classes.call(ast).flatten.slice(1..-1).map { |name| to_path(name).shift }
25
+ end
26
+
27
+ def to_path(str)
28
+ NamePath.from_string(str)
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,3 @@
1
+ module AstSearch
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,146 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ast_search
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Yuriy Kharchenko
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-08-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: parser
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '2.3'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '2.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.16'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.16'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry-byebug
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.0'
97
+ description: Parse Ruby code & search for AST nodes
98
+ email:
99
+ - rm1945@yandex.ru
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - ".travis.yml"
107
+ - Gemfile
108
+ - Gemfile.lock
109
+ - LICENSE.txt
110
+ - README.md
111
+ - Rakefile
112
+ - ast_search.gemspec
113
+ - bin/console
114
+ - bin/setup
115
+ - lib/ast_search.rb
116
+ - lib/ast_search/name_path.rb
117
+ - lib/ast_search/node.rb
118
+ - lib/ast_search/queries/class_defs.rb
119
+ - lib/ast_search/queries/const_occurrences.rb
120
+ - lib/ast_search/queries/external_classes.rb
121
+ - lib/ast_search/version.rb
122
+ homepage: https://github.com/letmein/ast_search
123
+ licenses: []
124
+ metadata:
125
+ allowed_push_host: https://rubygems.org
126
+ post_install_message:
127
+ rdoc_options: []
128
+ require_paths:
129
+ - lib
130
+ required_ruby_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ required_rubygems_version: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ requirements: []
141
+ rubyforge_project:
142
+ rubygems_version: 2.5.1
143
+ signing_key:
144
+ specification_version: 4
145
+ summary: Parse Ruby code & search for AST nodes
146
+ test_files: []