cql_ruby 0.0.10 → 0.0.11

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e75a456d8dff7bb1b1affde452bfc7df1e01508a2514ca1729cf996e28c3bff6
4
- data.tar.gz: 57d5bdbfe2991ba0ed0c529373405885cc4382c8cf2f6cec3c86117b65fc31bf
3
+ metadata.gz: d117740a94a14a7c3f8a6a62f7bb22ae3dbbc450a909de2c9d5c3a9d5eb6fb91
4
+ data.tar.gz: 1850b47222e7d5be2caf3b50221f2facececedff355873f4b752d08a9aabf096
5
5
  SHA512:
6
- metadata.gz: b56ead4780a2823d1e327c7ab24e5d73923d21096a5b95832cdfbe7bc499a335bdc2ea5d252efaff96e2ba4fd6471b4ca6e12c9c18f90249116bff509217b66d
7
- data.tar.gz: 1b30aedc86c325e1cc6fdd8813abe2d46f5d76d34f542e0d9f0446b0a7fa42721c4f76dfec376c477287eff46d71311f2b86b20c1dea6c9658cfe8b04207b775
6
+ metadata.gz: 37a8dc083b6a7ee31d93ad01e01a1028bf58a0e5cf4ddee3fdb676ac3af606aeab679d6f9e8498865b42797cb159da93fa65479bce432811ead57f1c8827e0d9
7
+ data.tar.gz: a78b4f787e661d21e02f914058b6877ef863de7ed21beef8502f1ab278fa61799da32dd0a3956f54f4c0a7a9d3af95f34e7203819090f4f1189879333f0d4b70
@@ -7,7 +7,8 @@ def show_help
7
7
  puts <<~HELP
8
8
 
9
9
  \tSYNOPSIS
10
- \t\tcql_ruby options pattern path filters ...
10
+ \t\tcql_ruby [--token] pattern path options filters ...
11
+ \t\tcql_ruby --node type path options filters ...
11
12
 
12
13
  \tDESCRIPTION
13
14
  \t\tCQL (Code Query Language) is a semantic search tool for your Ruby source code.
@@ -49,6 +50,7 @@ def extract_options
49
50
  surrounding_lines: 0,
50
51
  include_pattern: nil,
51
52
  exclude_pattern: nil,
53
+ search_type: :token,
52
54
  }
53
55
 
54
56
  ARGV.delete_if do |arg|
@@ -72,6 +74,10 @@ def extract_options
72
74
  options[:include_pattern] = arg.split('=')[1]
73
75
  elsif arg.start_with?('--exclude=')
74
76
  options[:exclude_pattern] = arg.split('=')[1]
77
+ elsif arg == '--node'
78
+ options[:search_type] = :node
79
+ elsif arg == '--token'
80
+ options[:search_type] = :token
75
81
  else
76
82
  raise "Unknown arg #{arg}"
77
83
  end
@@ -124,6 +130,7 @@ begin
124
130
  recursive: options[:recursive_search],
125
131
  include: options[:include_pattern],
126
132
  exclude: options[:exclude_pattern],
133
+ search_type: options[:search_type],
127
134
  ).search_all
128
135
  rescue
129
136
  puts "Error: #{$!}"
@@ -2,7 +2,7 @@
2
2
 
3
3
  module CqlRuby
4
4
  #
5
- # Printing Cqlruby::Crumb-s.
5
+ # Printing CqlRuby::Crumb-s.
6
6
  #
7
7
  class AbstractPrinter
8
8
  def print(_crumb)
@@ -21,7 +21,7 @@ module CqlRuby
21
21
  end
22
22
 
23
23
  #
24
- # @param crumb [Cqlruby::Crumb]
24
+ # @param crumb [CqlRuby::Crumb]
25
25
  #
26
26
  def print(crumb)
27
27
  parts = "##{color(97)}#{@counter}#{decor_reset}"
@@ -71,7 +71,7 @@ module CqlRuby
71
71
  end
72
72
  end
73
73
 
74
- # @param [Cqlruby::Crumb] crumb
74
+ # @param [CqlRuby::Crumb] crumb
75
75
  # @return [String]
76
76
  def decorate_source_line(crumb)
77
77
  source = crumb.source
@@ -3,7 +3,7 @@
3
3
  module CqlRuby
4
4
  class CrumbCollector
5
5
  #
6
- # @param printer [Cqlruby::AbstractPrinter]
6
+ # @param printer [CqlRuby::AbstractPrinter]
7
7
  #
8
8
  def initialize(printer)
9
9
  super()
@@ -20,7 +20,7 @@ end
20
20
  #
21
21
  # Executes search and dumps results into the collector.
22
22
  #
23
- # @param collector [Cqlruby::CrumbCollector]
23
+ # @param collector [CqlRuby::CrumbCollector]
24
24
  # @param pattern [String]
25
25
  # @param path [String]
26
26
  # @param filters [Array<String>]
@@ -35,7 +35,8 @@ module CqlRuby
35
35
  filters: [],
36
36
  recursive: true,
37
37
  include: nil,
38
- exclude: nil
38
+ exclude: nil,
39
+ search_type: :token
39
40
  )
40
41
  @collector = collector
41
42
  @filter_reader = filter_reader
@@ -45,6 +46,7 @@ module CqlRuby
45
46
  @recursive = recursive
46
47
  @include = include
47
48
  @exclude = exclude
49
+ @search_type = search_type
48
50
  end
49
51
 
50
52
  def search_all
@@ -72,11 +74,17 @@ module CqlRuby
72
74
 
73
75
  def walk(node, ancestors, source_reader)
74
76
  if node.is_a?(Parser::AST::Node)
77
+ if search_for_node?
78
+ if match?(node.type) && CqlRuby::FilterEvaluator.pass?(filter_reader, ancestors, node)
79
+ collector.add(CqlRuby::Crumb.new(node, ancestors, source_reader))
80
+ end
81
+ end
82
+
75
83
  node.children.flat_map do |child|
76
84
  walk(child, ancestors.dup + [node], source_reader)
77
85
  end
78
86
  else
79
- if match?(node) && CqlRuby::FilterEvaluator.pass?(filter_reader, node, ancestors)
87
+ if search_for_token? && match?(node) && CqlRuby::FilterEvaluator.pass?(filter_reader, ancestors, node)
80
88
  collector.add(CqlRuby::Crumb.new(node, ancestors, source_reader))
81
89
  end
82
90
  end
@@ -98,6 +106,14 @@ module CqlRuby
98
106
  Dir.glob(clean_path)
99
107
  end
100
108
 
109
+ def search_for_token?
110
+ @search_type == :token
111
+ end
112
+
113
+ def search_for_node?
114
+ @search_type == :node
115
+ end
116
+
101
117
  attr_reader :collector
102
118
  attr_reader :filter_reader
103
119
  attr_reader :pattern
@@ -107,33 +123,55 @@ module CqlRuby
107
123
  end
108
124
  end
109
125
 
110
- CqlRuby::Crumb = Struct.new(:full_name, :ancestors, :source_reader) do
111
- def line_no
112
- ancestors.last.location.expression.line
113
- end
126
+ module CqlRuby
127
+ class Crumb
128
+ def initialize(node, ancestors, source_reader)
129
+ @node = node
130
+ @ancestors = ancestors
131
+ @source_reader = source_reader
132
+ end
114
133
 
115
- def line_col_no
116
- ancestors.last.location.expression.column
117
- end
134
+ def line_no
135
+ anchor.location.expression.line
136
+ end
118
137
 
119
- def source
120
- source_reader.source_line(line_no)
121
- end
138
+ def line_col_no
139
+ anchor.location.expression.column
140
+ end
122
141
 
123
- def surrounding_line(offset)
124
- source_reader.source_line(line_no + offset)
125
- end
142
+ def source
143
+ source_reader.source_line(line_no)
144
+ end
126
145
 
127
- def file_name
128
- source_reader.file
129
- end
146
+ def surrounding_line(offset)
147
+ source_reader.source_line(line_no + offset)
148
+ end
130
149
 
131
- def expression_size
132
- ancestors.last.location.expression.size
133
- end
150
+ def file_name
151
+ source_reader.file
152
+ end
153
+
154
+ def expression_size
155
+ anchor.location.expression.size
156
+ end
157
+
158
+ def type
159
+ anchor.type
160
+ end
161
+
162
+ private
163
+
164
+ def anchor
165
+ if node.is_a?(Symbol)
166
+ ancestors.last
167
+ else
168
+ node
169
+ end
170
+ end
134
171
 
135
- def type
136
- ancestors.last.type
172
+ attr_reader :node
173
+ attr_reader :ancestors
174
+ attr_reader :source_reader
137
175
  end
138
176
  end
139
177
 
@@ -3,18 +3,18 @@
3
3
  module CqlRuby
4
4
  class FilterEvaluator
5
5
  class << self
6
- def pass?(filter_reader, node, ancestors)
6
+ def pass?(filter_reader, ancestors, node)
7
7
  [
8
8
  pass_type?(filter_reader, ancestors),
9
9
  pass_nesting?(filter_reader, ancestors),
10
- pass_has?(filter_reader, node, ancestors),
10
+ pass_has?(filter_reader, ancestors, node),
11
11
  ].all?
12
12
  end
13
13
 
14
14
  private
15
15
 
16
16
  #
17
- # @param [Cqlruby::FilterReader] filter_reader
17
+ # @param [CqlRuby::FilterReader] filter_reader
18
18
  # @param [Array<Parser::AST::Node>] ancestors
19
19
  #
20
20
  # @return [Boolean]
@@ -39,7 +39,6 @@ module CqlRuby
39
39
  next false unless ancestor.type.to_s == nest_rule.type
40
40
  next true unless nest_rule.restrict_name?
41
41
 
42
- # TODO Make a proper matcher class.
43
42
  if %w[class module].include?(nest_rule.type)
44
43
  CqlRuby::PatternMatcher.match?(nest_rule.name, ancestor.children[0].children[1])
45
44
  elsif %[def].include?(nest_rule.type)
@@ -53,14 +52,19 @@ module CqlRuby
53
52
 
54
53
  #
55
54
  # @param [CqlRuby::FilterReader] filter_reader
56
- # @param [Parser::AST::Node] node
57
55
  # @param [Array<Parser::AST::Node>] ancestors
56
+ # @param [Any<Parser::AST::Node, Symbol>] node
58
57
  #
59
- def pass_has?(filter_reader, node, ancestors)
58
+ def pass_has?(filter_reader, ancestors, node)
60
59
  return true unless filter_reader.restrict_children?
61
60
 
62
61
  filter_reader.has_leaves.all? do |has_rule|
63
- anchor_node = try_get_class(ancestors) || try_get_module(ancestors) || try_get_def(ancestors)
62
+ anchor_node = if node.is_a?(Symbol)
63
+ # TODO: Expand this to other wrappers (loops, conditions, etc).
64
+ try_get_class(ancestors) || try_get_module(ancestors) || try_get_def(ancestors)
65
+ else
66
+ node
67
+ end
64
68
  next false unless anchor_node
65
69
 
66
70
  has_node_with_name?(anchor_node, has_rule)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cql_ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.10
4
+ version: 0.0.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - itarato