rubocop-ast 0.7.1 → 0.8.0
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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d9e5dc8ddfc947af0653c81c0579c18eba59551567d977dc3bf0a4ca4ea92d59
|
4
|
+
data.tar.gz: 4110b86811007a0a28e52c090f45b34449a0e3a6ede4cec97463052c1fb3a8a2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 827480d8986177910eee3a5d7bebafd9eb169818d755d79c16f9d4a3f89ebcd486494f35f07fbf3e0f6ffcc2a82da74a86b0475504c79b4fb1de3b3ad732a78f
|
7
|
+
data.tar.gz: 6daa124ee67a95fbc6e91cf5cc0a1179e9aad6718fd8a3f9d54b48c5209b45ede07875e1becbdae84ee2106458bb23538fc144a8aefcc35e922204fcb4c30619
|
@@ -43,7 +43,7 @@ module RuboCop
|
|
43
43
|
|
44
44
|
# Result of a NodePattern run against a particular AST
|
45
45
|
# Consider constructor is private
|
46
|
-
Result = Struct.new(:colorizer, :trace, :returned, :ruby_ast) do
|
46
|
+
Result = Struct.new(:colorizer, :trace, :returned, :ruby_ast) do
|
47
47
|
# @return [String] a Rainbow colorized version of ruby
|
48
48
|
def colorize(color_scheme = COLOR_SCHEME)
|
49
49
|
map = color_map(color_scheme)
|
@@ -1,6 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
# rubocop:disable Metrics/ModuleLength
|
4
3
|
module RuboCop
|
5
4
|
module AST
|
6
5
|
# Provides methods for traversing an AST.
|
@@ -8,198 +7,175 @@ module RuboCop
|
|
8
7
|
# Override methods to perform custom processing. Remember to call `super`
|
9
8
|
# if you want to recursively process descendant nodes.
|
10
9
|
module Traversal
|
10
|
+
# Only for debugging.
|
11
|
+
# @api private
|
12
|
+
class DebugError < RuntimeError
|
13
|
+
end
|
14
|
+
|
15
|
+
TYPE_TO_METHOD = Hash.new { |h, type| h[type] = :"on_#{type}" }
|
16
|
+
|
11
17
|
def walk(node)
|
12
18
|
return if node.nil?
|
13
19
|
|
14
|
-
send(
|
20
|
+
send(TYPE_TO_METHOD[node.type], node)
|
15
21
|
nil
|
16
22
|
end
|
17
23
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
SECOND_CHILD_ONLY = %i[lvasgn ivasgn cvasgn gvasgn optarg kwarg
|
40
|
-
kwoptarg].freeze
|
41
|
-
private_constant :NO_CHILD_NODES, :ONE_CHILD_NODE, :MANY_CHILD_NODES, :SECOND_CHILD_ONLY
|
42
|
-
|
43
|
-
NO_CHILD_NODES.each do |type|
|
44
|
-
module_eval("def on_#{type}(node); end", __FILE__, __LINE__)
|
45
|
-
end
|
46
|
-
|
47
|
-
ONE_CHILD_NODE.each do |type|
|
48
|
-
module_eval(<<~RUBY, __FILE__, __LINE__ + 1)
|
49
|
-
def on_#{type}(node)
|
50
|
-
if (child = node.children[0])
|
51
|
-
send(:"on_\#{child.type}", child)
|
24
|
+
# @api private
|
25
|
+
module CallbackCompiler
|
26
|
+
SEND = 'send(TYPE_TO_METHOD[child.type], child)'
|
27
|
+
assign_code = 'child = node.children[%<index>i]'
|
28
|
+
code = "#{assign_code}\n#{SEND}"
|
29
|
+
TEMPLATE = {
|
30
|
+
skip: '',
|
31
|
+
always: code,
|
32
|
+
nil?: "#{code} if child"
|
33
|
+
}.freeze
|
34
|
+
|
35
|
+
def def_callback(type, *signature,
|
36
|
+
arity: signature.size..signature.size,
|
37
|
+
arity_check: ENV['RUBOCOP_DEBUG'] && self.arity_check(arity),
|
38
|
+
body: self.body(signature, arity_check))
|
39
|
+
type, *aliases = type
|
40
|
+
lineno = caller_locations(1, 1).first.lineno
|
41
|
+
module_eval(<<~RUBY, __FILE__, lineno) # rubocop:disable Style/EvalWithLocation
|
42
|
+
def on_#{type}(node)
|
43
|
+
#{body}
|
44
|
+
nil
|
52
45
|
end
|
46
|
+
RUBY
|
47
|
+
aliases.each do |m|
|
48
|
+
alias_method "on_#{m}", "on_#{type}"
|
53
49
|
end
|
54
|
-
|
55
|
-
end
|
56
|
-
|
57
|
-
MANY_CHILD_NODES.each do |type|
|
58
|
-
module_eval(<<~RUBY, __FILE__, __LINE__ + 1)
|
59
|
-
def on_#{type}(node)
|
60
|
-
node.children.each { |child| send(:"on_\#{child.type}", child) }
|
61
|
-
nil
|
62
|
-
end
|
63
|
-
RUBY
|
64
|
-
end
|
50
|
+
end
|
65
51
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
if (child = node.children[1])
|
71
|
-
send(:"on_\#{child.type}", child)
|
52
|
+
def body(signature, prelude)
|
53
|
+
signature
|
54
|
+
.map.with_index do |arg, i|
|
55
|
+
TEMPLATE[arg].gsub('%<index>i', i.to_s)
|
72
56
|
end
|
73
|
-
|
74
|
-
|
75
|
-
end
|
76
|
-
|
77
|
-
def on_const(node)
|
78
|
-
return unless (child = node.children[0])
|
79
|
-
|
80
|
-
send(:"on_#{child.type}", child)
|
81
|
-
end
|
82
|
-
|
83
|
-
def on_casgn(node)
|
84
|
-
children = node.children
|
85
|
-
if (child = children[0]) # always const???
|
86
|
-
send(:"on_#{child.type}", child)
|
57
|
+
.unshift(prelude)
|
58
|
+
.join("\n")
|
87
59
|
end
|
88
|
-
return unless (child = children[2])
|
89
|
-
|
90
|
-
send(:"on_#{child.type}", child)
|
91
|
-
end
|
92
60
|
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
61
|
+
def arity_check(range)
|
62
|
+
<<~RUBY
|
63
|
+
n = node.children.size
|
64
|
+
raise DebugError, [
|
65
|
+
'Expected #{range} children, got',
|
66
|
+
n, 'for', node.inspect
|
67
|
+
].join(' ') unless (#{range}).cover?(node.children.size)
|
68
|
+
RUBY
|
99
69
|
end
|
100
|
-
return unless (child = children[2])
|
101
|
-
|
102
|
-
send(:"on_#{child.type}", child)
|
103
|
-
end
|
104
|
-
|
105
|
-
def on_def(node)
|
106
|
-
children = node.children
|
107
|
-
on_args(children[1])
|
108
|
-
return unless (child = children[2])
|
109
|
-
|
110
|
-
send(:"on_#{child.type}", child)
|
111
70
|
end
|
112
|
-
|
113
|
-
|
71
|
+
private_constant :CallbackCompiler
|
72
|
+
extend CallbackCompiler
|
73
|
+
send_code = CallbackCompiler::SEND
|
74
|
+
|
75
|
+
### arity == 0
|
76
|
+
no_children = %i[true false nil self cbase zsuper redo retry
|
77
|
+
forward_args forwarded_args match_nil_pattern
|
78
|
+
forward_arg lambda empty_else kwnilarg
|
79
|
+
__FILE__ __LINE__ __ENCODING__]
|
80
|
+
|
81
|
+
### arity == 0..1
|
82
|
+
opt_symbol_child = %i[restarg kwrestarg]
|
83
|
+
opt_node_child = %i[splat kwsplat match_rest]
|
84
|
+
|
85
|
+
### arity == 1
|
86
|
+
literal_child = %i[int float complex
|
87
|
+
rational str sym lvar
|
88
|
+
ivar cvar gvar nth_ref back_ref
|
89
|
+
arg blockarg shadowarg
|
90
|
+
kwarg match_var]
|
91
|
+
|
92
|
+
many_symbol_children = %i[regopt]
|
93
|
+
|
94
|
+
node_child = %i[block_pass not
|
95
|
+
match_current_line defined?
|
96
|
+
arg_expr pin if_guard unless_guard
|
97
|
+
match_with_trailing_comma]
|
98
|
+
node_or_nil_child = %i[preexe postexe]
|
99
|
+
|
100
|
+
NO_CHILD_NODES = (no_children + opt_symbol_child + literal_child).to_set.freeze
|
101
|
+
private_constant :NO_CHILD_NODES # Used by Commissioner
|
102
|
+
|
103
|
+
### arity > 1
|
104
|
+
symbol_then_opt_node = %i[lvasgn ivasgn cvasgn gvasgn]
|
105
|
+
symbol_then_node_or_nil = %i[optarg kwoptarg]
|
106
|
+
node_then_opt_node = %i[while until module sclass]
|
107
|
+
|
108
|
+
### variable arity
|
109
|
+
many_node_children = %i[dstr dsym xstr regexp array hash pair
|
110
|
+
mlhs masgn or_asgn and_asgn rasgn mrasgn
|
111
|
+
undef alias args super yield or and
|
112
|
+
while_post until_post iflipflop eflipflop
|
113
|
+
match_with_lvasgn begin kwbegin return
|
114
|
+
in_match match_alt break next
|
115
|
+
match_as array_pattern array_pattern_with_tail
|
116
|
+
hash_pattern const_pattern find_pattern
|
117
|
+
index indexasgn procarg0]
|
118
|
+
many_opt_node_children = %i[case rescue resbody ensure for when
|
119
|
+
case_match in_pattern irange erange]
|
120
|
+
|
121
|
+
### Callbacks for above
|
122
|
+
def_callback no_children
|
123
|
+
def_callback opt_symbol_child, :skip, arity: 0..1
|
124
|
+
def_callback opt_node_child, :nil?, arity: 0..1
|
125
|
+
|
126
|
+
def_callback literal_child, :skip
|
127
|
+
def_callback node_child, :always
|
128
|
+
def_callback node_or_nil_child, :nil?
|
129
|
+
|
130
|
+
def_callback symbol_then_opt_node, :skip, :nil?, arity: 1..2
|
131
|
+
def_callback symbol_then_node_or_nil, :skip, :nil?
|
132
|
+
def_callback node_then_opt_node, :always, :nil?
|
133
|
+
|
134
|
+
def_callback many_symbol_children, :skip, arity_check: nil
|
135
|
+
def_callback many_node_children, body: <<~RUBY
|
136
|
+
node.children.each { |child| #{send_code} }
|
137
|
+
RUBY
|
138
|
+
def_callback many_opt_node_children,
|
139
|
+
body: <<~RUBY
|
140
|
+
node.children.each { |child| #{send_code} if child }
|
141
|
+
RUBY
|
142
|
+
|
143
|
+
### Other particular cases
|
144
|
+
def_callback :const, :nil?, :skip
|
145
|
+
def_callback :casgn, :nil?, :skip, :nil?, arity: 2..3
|
146
|
+
def_callback :class, :always, :nil?, :nil?
|
147
|
+
def_callback :def, :skip, :always, :nil?
|
148
|
+
def_callback :op_asgn, :always, :skip, :always
|
149
|
+
def_callback :if, :always, :nil?, :nil?
|
150
|
+
def_callback :block, :always, :always, :nil?
|
151
|
+
def_callback :numblock, :always, :skip, :nil?
|
152
|
+
def_callback :defs, :always, :skip, :always, :nil?
|
153
|
+
|
154
|
+
def_callback %i[send csend], body: <<~RUBY
|
114
155
|
node.children.each_with_index do |child, i|
|
115
156
|
next if i == 1
|
116
157
|
|
117
|
-
|
158
|
+
#{send_code} if child
|
118
159
|
end
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
def on_defs(node)
|
133
|
-
children = node.children
|
134
|
-
child = children[0]
|
135
|
-
send(:"on_#{child.type}", child)
|
136
|
-
on_args(children[2])
|
137
|
-
return unless (child = children[3])
|
138
|
-
|
139
|
-
send(:"on_#{child.type}", child)
|
140
|
-
end
|
141
|
-
|
142
|
-
def on_if(node)
|
143
|
-
children = node.children
|
144
|
-
child = children[0]
|
145
|
-
send(:"on_#{child.type}", child)
|
146
|
-
if (child = children[1])
|
147
|
-
send(:"on_#{child.type}", child)
|
148
|
-
end
|
149
|
-
return unless (child = children[2])
|
150
|
-
|
151
|
-
send(:"on_#{child.type}", child)
|
152
|
-
end
|
153
|
-
|
154
|
-
def on_while(node)
|
155
|
-
children = node.children
|
156
|
-
child = children[0]
|
157
|
-
send(:"on_#{child.type}", child)
|
158
|
-
return unless (child = children[1])
|
159
|
-
|
160
|
-
send(:"on_#{child.type}", child)
|
161
|
-
end
|
162
|
-
|
163
|
-
alias on_until on_while
|
164
|
-
alias on_module on_while
|
165
|
-
alias on_sclass on_while
|
166
|
-
|
167
|
-
def on_block(node)
|
168
|
-
children = node.children
|
169
|
-
child = children[0]
|
170
|
-
send(:"on_#{child.type}", child) # can be send, zsuper...
|
171
|
-
on_args(children[1])
|
172
|
-
return unless (child = children[2])
|
173
|
-
|
174
|
-
send(:"on_#{child.type}", child)
|
175
|
-
end
|
176
|
-
|
177
|
-
def on_case(node)
|
160
|
+
RUBY
|
161
|
+
|
162
|
+
### generic processing of any other node (forward compatibility)
|
163
|
+
defined = instance_methods(false)
|
164
|
+
.grep(/^on_/)
|
165
|
+
.map { |s| s.to_s[3..-1].to_sym } # :on_foo => :foo
|
166
|
+
|
167
|
+
to_define = ::Parser::Meta::NODE_TYPES.to_a
|
168
|
+
to_define -= defined
|
169
|
+
to_define -= %i[numargs ident] # transient
|
170
|
+
to_define -= %i[blockarg_expr restarg_expr] # obsolete
|
171
|
+
to_define -= %i[objc_kwarg objc_restarg objc_varargs] # mac_ruby
|
172
|
+
def_callback to_define, body: <<~RUBY
|
178
173
|
node.children.each do |child|
|
179
|
-
|
174
|
+
next unless child.class == Node
|
175
|
+
#{send_code}
|
180
176
|
end
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
alias on_rescue on_case
|
185
|
-
alias on_resbody on_case
|
186
|
-
alias on_ensure on_case
|
187
|
-
alias on_for on_case
|
188
|
-
alias on_when on_case
|
189
|
-
alias on_case_match on_case
|
190
|
-
alias on_in_pattern on_case
|
191
|
-
alias on_irange on_case
|
192
|
-
alias on_erange on_case
|
193
|
-
|
194
|
-
def on_numblock(node)
|
195
|
-
children = node.children
|
196
|
-
child = children[0]
|
197
|
-
send(:"on_#{child.type}", child)
|
198
|
-
return unless (child = children[2])
|
199
|
-
|
200
|
-
send(:"on_#{child.type}", child)
|
201
|
-
end
|
177
|
+
RUBY
|
178
|
+
MISSING = to_define if ENV['RUBOCOP_DEBUG']
|
202
179
|
end
|
203
180
|
end
|
204
181
|
end
|
205
|
-
# rubocop:enable Metrics/ModuleLength
|
data/lib/rubocop/ast/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rubocop-ast
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.8.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bozhidar Batsov
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2020-
|
13
|
+
date: 2020-10-12 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: parser
|
@@ -165,7 +165,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
165
165
|
- !ruby/object:Gem::Version
|
166
166
|
version: '0'
|
167
167
|
requirements: []
|
168
|
-
rubygems_version: 3.
|
168
|
+
rubygems_version: 3.1.2
|
169
169
|
signing_key:
|
170
170
|
specification_version: 4
|
171
171
|
summary: RuboCop tools to deal with Ruby code AST.
|