rbr 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: '0586dc665c13aa156f7997779e6b2eccda40556896ef39b445c6b6d1160cd33a'
4
+ data.tar.gz: 10413f9d6eab1a7cfa5376b79ec37cbc91984a3915e59a850aced3c6a3515642
5
+ SHA512:
6
+ metadata.gz: eb5c7da4463d013eda03ca6243189cbea36ecb5e6b70d5a30ea7e3c02030456bb3dcbe715651d1c18216d6bc120292b2d5e4256abe5349403078d3d5b420d341
7
+ data.tar.gz: 27730f7b04550f8e11897b685db8f2bea44f3f65ff3402a3adee045b113c185dad1e90399d6d008fda0b60cffe7f6859533f4d5b2edeaa7191c95a3004f12c58
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /coverage/
3
+ /doc/
4
+ *.gem
5
+ .git/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /.yardoc
10
+ /_yardoc/
@@ -0,0 +1,7 @@
1
+ ---
2
+ Style/Documentation:
3
+ Exclude:
4
+ - "test/**"
5
+
6
+ Style/StringLiterals:
7
+ EnforcedStyle: double_quotes
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 rbr.gemspec
6
+ gemspec
@@ -0,0 +1,32 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rbr (0.1.0)
5
+ parser
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ ast (2.4.0)
11
+ coderay (1.1.3)
12
+ method_source (1.0.0)
13
+ minitest (5.14.1)
14
+ parser (2.7.1.3)
15
+ ast (~> 2.4.0)
16
+ pry (0.13.1)
17
+ coderay (~> 1.1)
18
+ method_source (~> 1.0)
19
+ rake (10.5.0)
20
+
21
+ PLATFORMS
22
+ ruby
23
+
24
+ DEPENDENCIES
25
+ bundler (~> 1.17)
26
+ minitest (~> 5.0)
27
+ pry (~> 0.0)
28
+ rake (~> 10.0)
29
+ rbr!
30
+
31
+ BUNDLED WITH
32
+ 1.17.2
@@ -0,0 +1,35 @@
1
+ # Rbr
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/rbr`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'rbr'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install rbr
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ 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).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/rbr.
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
data/exe/rbr ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # frozen_string_literal: true
4
+
5
+ require "parser/current"
6
+ require "rbr/cli"
7
+
8
+ Rbr::CLI.start
@@ -0,0 +1,44 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # frozen_string_literal: true
4
+
5
+ require "parser/current"
6
+ require "rbr/query"
7
+
8
+ # A semantically-aware code search tool for Ruby
9
+
10
+ module Rbr
11
+ class CLI
12
+ def self.start
13
+ return unless check_arg_count
14
+
15
+ matcher = ARGV[0].to_sym
16
+ name = ARGV[1].to_sym
17
+ root, comments = Parser::CurrentRuby.parse_file_with_comments(ARGV[2])
18
+
19
+ nodes = Query.new(matcher => { name: name }).run(root)
20
+
21
+ nodes.each do |node|
22
+ puts node.pretty_print
23
+ end
24
+ end
25
+
26
+ def self.check_arg_count
27
+ return true if ARGV.count >= 3
28
+
29
+ warn <<~USAGE
30
+ Usage:
31
+ rbr MATCHER PATTERN FILE
32
+
33
+ Examples:
34
+ # find assignments to lvalue :@author in the file test/book.rb
35
+ rbr assignment @author test/book.rb
36
+
37
+ # find strings matching /Author.*name/ in the file test/book.rb
38
+ rbr str "Author.*name" test/book.rb
39
+ USAGE
40
+
41
+ false
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rbr
4
+ # methods for determining if a node matches given conditions
5
+ class Matchers
6
+ def self.match(node, query)
7
+ query.map do |matcher, conds|
8
+ send(matcher, node, conds)
9
+ end.all? #TODO any? or all?
10
+ end
11
+
12
+ # Updating an ActiveRecord model attribute
13
+ def self.ar_update(node, name)
14
+ attribute_name = conds[:name]
15
+ matches = if attribute_name
16
+ node.children.first == attribute_name
17
+ else
18
+ true # everything matches if no condition
19
+ end
20
+
21
+ node.method_call? &&
22
+ node.children[1] == :update &&
23
+ node.children[1].is_a_hash_containing_node(children: [:name])
24
+ end
25
+
26
+ # Assignment to a specified lvalue
27
+ def self.assignment(node, conds)
28
+ return false unless node.assignment?
29
+
30
+ !conds || node.children.first == conds[:name]
31
+ end
32
+
33
+ # Node is a literal int, float, or string
34
+ # TODO rename. Literal?
35
+ def self.is(node, conds)
36
+ return false unless node.literal?
37
+
38
+ !conds ||
39
+ # Ruby symbols can't start with a number, so #to_s first
40
+ node.children.first.to_s.to_sym == conds[:name]
41
+ end
42
+
43
+ # Node is a Ruby constant
44
+ def self.const(node, conds)
45
+ return false unless node.const?
46
+
47
+ !conds || node.children.last == conds[:name]
48
+ end
49
+
50
+ # Node is a string
51
+ def self.str(node, conds)
52
+ return false unless node.str?
53
+
54
+ !conds || node.any_child_matches?(
55
+ ->(n) { n.is_a?(String) && n.match?(conds[:pattern]) }
56
+ )
57
+ end
58
+
59
+ # Node is a comment
60
+ def self.comment(node, conds)
61
+ return false unless node.comment?
62
+
63
+ #TODO
64
+ end
65
+
66
+ # Anything other than a string
67
+ def self.not_str(node, conds)
68
+ return false if node.str?
69
+
70
+ !conds || node.children.last == conds[:name]
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rbr
4
+ # Wraps a Parser::AST::Node object and provides convenience methods
5
+ class Node
6
+ def initialize(ast_node)
7
+ @ast_node = ast_node
8
+ end
9
+
10
+ def assignment?
11
+ %i[lvasgn ivasgn cvasgn gvasgn casgn masgn].include? @ast_node.type
12
+ end
13
+
14
+ def const?
15
+ :const == @ast_node.type
16
+ end
17
+
18
+ def literal?
19
+ %i[int float str].include? @ast_node.type
20
+ end
21
+
22
+ def method_call?
23
+ %i[send csend].include? @ast_node.type
24
+ end
25
+
26
+ def nil?
27
+ @ast_node.nil? || !(@ast_node.is_a? Parser::AST::Node)
28
+ end
29
+
30
+ def str?
31
+ %i[str dstr xstr].include? @ast_node.type
32
+ end
33
+
34
+ def children
35
+ @ast_node.children
36
+ end
37
+
38
+ def pretty_print
39
+ "#{@ast_node.loc.expression.line}: #{@ast_node.loc.expression.source}"
40
+ end
41
+
42
+ # Call the the proc, passing in this node and all children recursively. Return
43
+ # true if any call evaluates to true.
44
+ def any_child_matches?(match_proc, root = self)
45
+ if root.respond_to?(:children)
46
+ root.children.any? { |c| any_child_matches?(match_proc, c) }
47
+ else
48
+ match_proc.call(root)
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rbr/matchers"
4
+ require "rbr/node"
5
+
6
+ module Rbr
7
+ class Query
8
+ attr_reader :conditions
9
+
10
+ def initialize(conditions)
11
+ @conditions = conditions
12
+ end
13
+
14
+ def run(node)
15
+ node = Node.new(node) unless node.is_a?(Node)
16
+ return [] if node.nil?
17
+
18
+ node_matches = Matchers.match(node, conditions)
19
+ found_children = node.children.map { |child| run(child) }
20
+
21
+ (node_matches ? [node] : []) + found_children.flatten
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,3 @@
1
+ module Rbr
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path("lib", __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require "rbr/version"
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "rbr"
9
+ spec.version = Rbr::VERSION
10
+ spec.authors = ["Eddie Lebow"]
11
+ spec.email = ["elebow@users.noreply.github.com"]
12
+
13
+ spec.summary = "Semantically-aware code search tool for Ruby"
14
+ #spec.description = "TODO: Write a longer description or delete this line."
15
+ spec.homepage = "https://github.com/elebow/rbr"
16
+
17
+ if spec.respond_to?(:metadata)
18
+ spec.metadata["homepage_uri"] = spec.homepage
19
+ spec.metadata["source_code_uri"] = "https://github.com/elebow/rbr"
20
+ spec.metadata["changelog_uri"] = "https://github.com/elebow/rbr/blob/master/CHANGELOG.md"
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against " \
23
+ "public gem pushes."
24
+ end
25
+
26
+ # Specify which files should be added to the gem when it is released.
27
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
28
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
29
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
30
+ end
31
+ spec.bindir = "exe"
32
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
33
+ spec.require_paths = ["lib"]
34
+
35
+ spec.add_dependency "parser"
36
+
37
+ spec.add_development_dependency "bundler", "~> 1.17"
38
+ spec.add_development_dependency "minitest", "~> 5.0"
39
+ spec.add_development_dependency "pry", "~> 0.0"
40
+ spec.add_development_dependency "rake", "~> 10.0"
41
+ end
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rbr
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Eddie Lebow
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-06-05 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: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
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.17'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.17'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.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.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
+ description:
84
+ email:
85
+ - elebow@users.noreply.github.com
86
+ executables:
87
+ - rbr
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - ".rubocop.yml"
93
+ - Gemfile
94
+ - Gemfile.lock
95
+ - README.md
96
+ - Rakefile
97
+ - exe/rbr
98
+ - lib/rbr/cli.rb
99
+ - lib/rbr/matchers.rb
100
+ - lib/rbr/node.rb
101
+ - lib/rbr/query.rb
102
+ - lib/rbr/version.rb
103
+ - rbr.gemspec
104
+ homepage: https://github.com/elebow/rbr
105
+ licenses: []
106
+ metadata:
107
+ homepage_uri: https://github.com/elebow/rbr
108
+ source_code_uri: https://github.com/elebow/rbr
109
+ changelog_uri: https://github.com/elebow/rbr/blob/master/CHANGELOG.md
110
+ post_install_message:
111
+ rdoc_options: []
112
+ require_paths:
113
+ - lib
114
+ required_ruby_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ requirements: []
125
+ rubygems_version: 3.0.3
126
+ signing_key:
127
+ specification_version: 4
128
+ summary: Semantically-aware code search tool for Ruby
129
+ test_files: []