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 +4 -4
- data/bin/cql_ruby +8 -1
- data/lib/cql_ruby/abstract_printer.rb +1 -1
- data/lib/cql_ruby/console_printer.rb +2 -2
- data/lib/cql_ruby/crumb_collector.rb +1 -1
- data/lib/cql_ruby/executor.rb +62 -24
- data/lib/cql_ruby/filter_evaluator.rb +11 -7
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d117740a94a14a7c3f8a6a62f7bb22ae3dbbc450a909de2c9d5c3a9d5eb6fb91
|
4
|
+
data.tar.gz: 1850b47222e7d5be2caf3b50221f2facececedff355873f4b752d08a9aabf096
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 37a8dc083b6a7ee31d93ad01e01a1028bf58a0e5cf4ddee3fdb676ac3af606aeab679d6f9e8498865b42797cb159da93fa65479bce432811ead57f1c8827e0d9
|
7
|
+
data.tar.gz: a78b4f787e661d21e02f914058b6877ef863de7ed21beef8502f1ab278fa61799da32dd0a3956f54f4c0a7a9d3af95f34e7203819090f4f1189879333f0d4b70
|
data/bin/cql_ruby
CHANGED
@@ -7,7 +7,8 @@ def show_help
|
|
7
7
|
puts <<~HELP
|
8
8
|
|
9
9
|
\tSYNOPSIS
|
10
|
-
\t\tcql_ruby
|
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: #{$!}"
|
@@ -21,7 +21,7 @@ module CqlRuby
|
|
21
21
|
end
|
22
22
|
|
23
23
|
#
|
24
|
-
# @param 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 [
|
74
|
+
# @param [CqlRuby::Crumb] crumb
|
75
75
|
# @return [String]
|
76
76
|
def decorate_source_line(crumb)
|
77
77
|
source = crumb.source
|
data/lib/cql_ruby/executor.rb
CHANGED
@@ -20,7 +20,7 @@ end
|
|
20
20
|
#
|
21
21
|
# Executes search and dumps results into the collector.
|
22
22
|
#
|
23
|
-
# @param collector [
|
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,
|
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
|
111
|
-
|
112
|
-
ancestors
|
113
|
-
|
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
|
-
|
116
|
-
|
117
|
-
|
134
|
+
def line_no
|
135
|
+
anchor.location.expression.line
|
136
|
+
end
|
118
137
|
|
119
|
-
|
120
|
-
|
121
|
-
|
138
|
+
def line_col_no
|
139
|
+
anchor.location.expression.column
|
140
|
+
end
|
122
141
|
|
123
|
-
|
124
|
-
|
125
|
-
|
142
|
+
def source
|
143
|
+
source_reader.source_line(line_no)
|
144
|
+
end
|
126
145
|
|
127
|
-
|
128
|
-
|
129
|
-
|
146
|
+
def surrounding_line(offset)
|
147
|
+
source_reader.source_line(line_no + offset)
|
148
|
+
end
|
130
149
|
|
131
|
-
|
132
|
-
|
133
|
-
|
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
|
-
|
136
|
-
ancestors
|
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,
|
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,
|
10
|
+
pass_has?(filter_reader, ancestors, node),
|
11
11
|
].all?
|
12
12
|
end
|
13
13
|
|
14
14
|
private
|
15
15
|
|
16
16
|
#
|
17
|
-
# @param [
|
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,
|
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 =
|
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)
|