rubocop-ast 0.7.1 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fe900ddbeb79b1f5bc64a232c83261e6c947d95639886ecc727a14e928521d87
4
- data.tar.gz: d35cd600bbe5b2653e34c3726a7bd860130088dc30d21a4e3838fd1cc05f88db
3
+ metadata.gz: ec19a654b5c38c2a70e811bd7c16a86473bd9fec2444774e9d8ef3cfa9d9de58
4
+ data.tar.gz: 6a0ef9e9d121c5ba9991bb7cd534f8e180d84718ef093fc0be8099175bf027b4
5
5
  SHA512:
6
- metadata.gz: bf280eb45f3b9e6554202cbfd57edb386a5736881251d9492190f29b5c539ac1ad3bf95f39c449cddcda318968e4f09019b70cbc703d3d59044721e5576dee5b
7
- data.tar.gz: 85344e01afb0f05836e63c50f7031249fffa2fd5bbf4e8fc8a09f16a05eecaeb339b65b15acc677340621c71955fc295408b7fbabbc24e8d365839700ec515a3
6
+ metadata.gz: a720f20464156e92dddd9979ae2dff71a7a5deca84b2e8c717b9165fa9e831b4c15c5f7cba3c33cd8986a2618822ee0e82386e58d1586b1ebefbb5a74f9972b6
7
+ data.tar.gz: bf26e4aa780bf56d029ca9272825a4a91d8c76f87d88e7c43de8167e06d8e2687d2d450e28f989b77c3d61169a0d91866bfe1504f2069aee3d0d2e16e2ce4444
@@ -484,10 +484,11 @@ module RuboCop
484
484
  def_node_matcher :global_const?, '(const {nil? cbase} %1)'
485
485
 
486
486
  def_node_matcher :class_constructor?, <<~PATTERN
487
- { (send #global_const?({:Class :Module}) :new ...)
488
- (block (send #global_const?({:Class :Module}) :new ...) ...)}
487
+ { (send #global_const?({:Class :Module :Struct}) :new ...)
488
+ (block (send #global_const?({:Class :Module :Struct}) :new ...) ...)}
489
489
  PATTERN
490
490
 
491
+ # @deprecated Use `:class_constructor?`
491
492
  def_node_matcher :struct_constructor?, <<~PATTERN
492
493
  (block (send #global_const?(:Struct) :new ...) _ $_)
493
494
  PATTERN
@@ -58,6 +58,11 @@ module RuboCop
58
58
  def receiver
59
59
  children[-4]
60
60
  end
61
+
62
+ # @return [Boolean] if the definition is without an `end` or not.
63
+ def endless?
64
+ !loc.end
65
+ end
61
66
  end
62
67
  end
63
68
  end
@@ -145,16 +145,19 @@ module RuboCop
145
145
  #
146
146
  # @return [Array<Node>] an array of branch nodes
147
147
  def branches
148
- branches = [if_branch]
149
-
150
- return branches unless else?
151
-
152
- other_branches = if elsif_conditional?
153
- else_branch.branches
154
- else
155
- [else_branch]
156
- end
157
- branches.concat(other_branches)
148
+ if ternary?
149
+ [if_branch, else_branch]
150
+ elsif !else?
151
+ [if_branch]
152
+ else
153
+ branches = [if_branch]
154
+ other_branches = if elsif_conditional?
155
+ else_branch.branches
156
+ else
157
+ [else_branch]
158
+ end
159
+ branches.concat(other_branches)
160
+ end
158
161
  end
159
162
 
160
163
  # @deprecated Use `branches.each`
@@ -174,7 +174,7 @@ module RuboCop
174
174
  # @return [Boolean] whether the dispatched method is a `def` modifier
175
175
  def def_modifier?
176
176
  send_type? &&
177
- [self, *each_descendant(:send)].any?(&:adjacent_def_modifier?)
177
+ adjacent_def_modifier? || each_child_node(:send).any?(&:def_modifier?)
178
178
  end
179
179
 
180
180
  # Checks whether this is a lambda. Some versions of parser parses
@@ -6,7 +6,7 @@ module RuboCop
6
6
  module AST
7
7
  # This class performs a pattern-matching operation on an AST node.
8
8
  #
9
- # Detailed syntax: /doc/modules/ROOT/pages/node_pattern.md
9
+ # Detailed syntax: /docs/modules/ROOT/pages/node_pattern.adoc
10
10
  #
11
11
  # Initialize a new `NodePattern` with `NodePattern.new(pattern_string)`, then
12
12
  # pass an AST node to `NodePattern#match`. Alternatively, use one of the class
@@ -6,7 +6,7 @@ module RuboCop
6
6
  # Responsible to build the AST nodes for `NodePattern`
7
7
  #
8
8
  # Doc on how this fits in the compiling process:
9
- # /doc/modules/ROOT/pages/node_pattern.md
9
+ # /docs/modules/ROOT/pages/node_pattern.adoc
10
10
  class Builder
11
11
  def emit_capture(capture_token, node)
12
12
  return node if capture_token.nil?
@@ -7,7 +7,7 @@ module RuboCop
7
7
  # Defers work to its subcompilers
8
8
  #
9
9
  # Doc on how this fits in the compiling process:
10
- # /doc/modules/ROOT/pages/node_pattern.md
10
+ # /docs/modules/ROOT/pages/node_pattern.adoc
11
11
  class Compiler
12
12
  extend Forwardable
13
13
  attr_reader :captures, :named_parameters, :positional_parameters, :binding
@@ -8,7 +8,7 @@ module RuboCop
8
8
  # This value responds to `===`.
9
9
  #
10
10
  # Doc on how this fits in the compiling process:
11
- # /doc/modules/ROOT/pages/node_pattern.md
11
+ # /docs/modules/ROOT/pages/node_pattern.adoc
12
12
  class AtomSubcompiler < Subcompiler
13
13
  private
14
14
 
@@ -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 # rubocop:disable Metrics/BlockLength
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)
@@ -9,7 +9,7 @@ module RuboCop
9
9
  # or it's `node.type` if `seq_head` is true
10
10
  #
11
11
  # Doc on how this fits in the compiling process:
12
- # /doc/modules/ROOT/pages/node_pattern.md
12
+ # /docs/modules/ROOT/pages/node_pattern.adoc
13
13
  class NodePatternSubcompiler < Subcompiler
14
14
  attr_reader :access, :seq_head
15
15
 
@@ -11,7 +11,7 @@ module RuboCop
11
11
  # Assumes the given `var` is a `::RuboCop::AST::Node`
12
12
  #
13
13
  # Doc on how this fits in the compiling process:
14
- # /doc/modules/ROOT/pages/node_pattern.md
14
+ # /docs/modules/ROOT/pages/node_pattern.adoc
15
15
  #
16
16
  # rubocop:disable Metrics/ClassLength
17
17
  class SequenceSubcompiler < Subcompiler
@@ -8,7 +8,7 @@ module RuboCop
8
8
  # Implements visitor pattern
9
9
  #
10
10
  # Doc on how this fits in the compiling process:
11
- # /doc/modules/ROOT/pages/node_pattern.md
11
+ # /docs/modules/ROOT/pages/node_pattern.adoc
12
12
  class Subcompiler
13
13
  attr_reader :compiler
14
14
 
@@ -14,7 +14,7 @@ module RuboCop
14
14
  # Lexer class for `NodePattern`
15
15
  #
16
16
  # Doc on how this fits in the compiling process:
17
- # /doc/modules/ROOT/pages/node_pattern.md
17
+ # /docs/modules/ROOT/pages/node_pattern.adoc
18
18
  class Lexer < LexerRex
19
19
  Error = ScanError
20
20
 
@@ -11,8 +11,10 @@
11
11
  class RuboCop::AST::NodePattern::LexerRex
12
12
 
13
13
  macros
14
+ CONST_NAME /[A-Z:][a-zA-Z_:]+/
14
15
  SYMBOL_NAME /[\w+@*\/?!<>=~|%^-]+|\[\]=?/
15
16
  IDENTIFIER /[a-zA-Z_][a-zA-Z0-9_-]*/
17
+ CALL /(?:#{CONST_NAME}\.)?#{IDENTIFIER}[!?]?/
16
18
  REGEXP_BODY /(?:[^\/]|\\\/)*/
17
19
  REGEXP /\/(#{REGEXP_BODY})(?<!\\)\/([imxo]*)/
18
20
  rules
@@ -25,12 +27,12 @@ rules
25
27
  %w"( ) { | } [ ] < > $ ! ^ ` ... + * ? ,"
26
28
  )}/o { emit ss.matched, &:to_sym }
27
29
  /#{REGEXP}/o { emit_regexp }
28
- /%([A-Z:][a-zA-Z_:]+)/ { emit :tPARAM_CONST }
30
+ /%(#{CONST_NAME})/o { emit :tPARAM_CONST }
29
31
  /%([a-z_]+)/ { emit :tPARAM_NAMED }
30
32
  /%(\d*)/ { emit(:tPARAM_NUMBER) { |s| s.empty? ? 1 : s.to_i } } # Map `%` to `%1`
31
33
  /_(#{IDENTIFIER})/o { emit :tUNIFY }
32
34
  /_/o { emit :tWILDCARD }
33
- /\#(#{IDENTIFIER}[!?]?)/o { @state = :ARG; emit :tFUNCTION_CALL, &:to_sym }
35
+ /\#(#{CALL})/o { @state = :ARG; emit :tFUNCTION_CALL, &:to_sym }
34
36
  /#{IDENTIFIER}\?/o { @state = :ARG; emit :tPREDICATE, &:to_sym }
35
37
  /#{IDENTIFIER}/o { emit :tNODE_TYPE, &:to_sym }
36
38
  :ARG /\(/ { @state = nil; emit :tARG_LIST }
@@ -24,8 +24,10 @@ class RuboCop::AST::NodePattern::LexerRex
24
24
  require 'strscan'
25
25
 
26
26
  # :stopdoc:
27
+ CONST_NAME = /[A-Z:][a-zA-Z_:]+/
27
28
  SYMBOL_NAME = /[\w+@*\/?!<>=~|%^-]+|\[\]=?/
28
29
  IDENTIFIER = /[a-zA-Z_][a-zA-Z0-9_-]*/
30
+ CALL = /(?:#{CONST_NAME}\.)?#{IDENTIFIER}[!?]?/
29
31
  REGEXP_BODY = /(?:[^\/]|\\\/)*/
30
32
  REGEXP = /\/(#{REGEXP_BODY})(?<!\\)\/([imxo]*)/
31
33
  # :startdoc:
@@ -132,7 +134,7 @@ class RuboCop::AST::NodePattern::LexerRex
132
134
  action { emit ss.matched, &:to_sym }
133
135
  when ss.skip(/#{REGEXP}/o) then
134
136
  action { emit_regexp }
135
- when ss.skip(/%([A-Z:][a-zA-Z_:]+)/) then
137
+ when ss.skip(/%(#{CONST_NAME})/o) then
136
138
  action { emit :tPARAM_CONST }
137
139
  when ss.skip(/%([a-z_]+)/) then
138
140
  action { emit :tPARAM_NAMED }
@@ -142,7 +144,7 @@ class RuboCop::AST::NodePattern::LexerRex
142
144
  action { emit :tUNIFY }
143
145
  when ss.skip(/_/o) then
144
146
  action { emit :tWILDCARD }
145
- when ss.skip(/\#(#{IDENTIFIER}[!?]?)/o) then
147
+ when ss.skip(/\#(#{CALL})/o) then
146
148
  action { @state = :ARG; emit :tFUNCTION_CALL, &:to_sym }
147
149
  when ss.skip(/#{IDENTIFIER}\?/o) then
148
150
  action { @state = :ARG; emit :tPREDICATE, &:to_sym }
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
  #
3
3
  # DO NOT MODIFY!!!!
4
- # This file is automatically generated by Racc 1.5.0
4
+ # This file is automatically generated by Racc 1.4.16
5
5
  # from Racc grammar file "".
6
6
  #
7
7
 
@@ -9,7 +9,7 @@ module RuboCop
9
9
  # Note: class reopened in `parser.racc`
10
10
  #
11
11
  # Doc on how this fits in the compiling process:
12
- # /doc/modules/ROOT/pages/node_pattern.md
12
+ # /docs/modules/ROOT/pages/node_pattern.adoc
13
13
  class Parser < Racc::Parser
14
14
  extend Forwardable
15
15
 
@@ -20,6 +20,9 @@ module RuboCop
20
20
  new(file, ruby_version, path)
21
21
  end
22
22
 
23
+ INVALID_LEVELS = %i[error fatal].freeze
24
+ private_constant :INVALID_LEVELS
25
+
23
26
  def initialize(source, ruby_version, path = nil)
24
27
  # Defaults source encoding to UTF-8, regardless of the encoding it has
25
28
  # been read with, which could be non-utf8 depending on the default
@@ -64,7 +67,7 @@ module RuboCop
64
67
  def valid_syntax?
65
68
  return false if @parser_error
66
69
 
67
- @diagnostics.none? { |d| %i[error fatal].include?(d.level) }
70
+ @diagnostics.none? { |d| INVALID_LEVELS.include?(d.level) }
68
71
  end
69
72
 
70
73
  # Raw source checksum for tracking infinite loops.
@@ -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(:"on_#{node.type}", node)
20
+ send(TYPE_TO_METHOD[node.type], node)
15
21
  nil
16
22
  end
17
23
 
18
- NO_CHILD_NODES = %i[true false nil int float complex
19
- rational str sym regopt self lvar
20
- ivar cvar gvar nth_ref back_ref cbase
21
- arg restarg blockarg shadowarg
22
- kwrestarg zsuper redo retry
23
- forward_args forwarded_args
24
- match_var match_nil_pattern empty_else
25
- forward_arg lambda procarg0 __ENCODING__].freeze
26
- ONE_CHILD_NODE = %i[splat kwsplat block_pass not break next
27
- preexe postexe match_current_line defined?
28
- arg_expr pin match_rest if_guard unless_guard
29
- match_with_trailing_comma].freeze
30
- MANY_CHILD_NODES = %i[dstr dsym xstr regexp array hash pair
31
- mlhs masgn or_asgn and_asgn rasgn mrasgn
32
- undef alias args super yield or and
33
- while_post until_post iflipflop eflipflop
34
- match_with_lvasgn begin kwbegin return
35
- in_match match_alt
36
- match_as array_pattern array_pattern_with_tail
37
- hash_pattern const_pattern find_pattern
38
- index indexasgn].freeze
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)
52
- end
53
- end
54
- RUBY
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
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) # def on_send(node)
43
+ #{body} # # body ...
44
+ nil # nil
45
+ end # end
46
+ RUBY
47
+ aliases.each do |m|
48
+ alias_method "on_#{m}", "on_#{type}"
62
49
  end
63
- RUBY
64
- end
50
+ end
65
51
 
66
- SECOND_CHILD_ONLY.each do |type|
67
- # Guard clause is for nodes nested within mlhs
68
- module_eval(<<~RUBY, __FILE__, __LINE__ + 1)
69
- def on_#{type}(node)
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
- end
74
- RUBY
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
- def on_class(node)
94
- children = node.children
95
- child = children[0] # always const???
96
- send(:"on_#{child.type}", child)
97
- if (child = children[1])
98
- send(:"on_#{child.type}", child)
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
- def on_send(node)
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
- send(:"on_#{child.type}", child) if child
158
+ #{send_code} if child
118
159
  end
119
- nil
120
- end
121
-
122
- alias on_csend on_send
123
-
124
- def on_op_asgn(node)
125
- children = node.children
126
- child = children[0]
127
- send(:"on_#{child.type}", child)
128
- child = children[2]
129
- send(:"on_#{child.type}", child)
130
- end
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
- send(:"on_#{child.type}", child) if child
174
+ next unless child.class == Node
175
+ #{send_code}
180
176
  end
181
- nil
182
- end
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
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module AST
5
5
  module Version
6
- STRING = '0.7.1'
6
+ STRING = '1.1.1'
7
7
  end
8
8
  end
9
9
  end
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.7.1
4
+ version: 1.1.1
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-09-28 00:00:00.000000000 Z
13
+ date: 2020-11-04 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.0.8
168
+ rubygems_version: 3.1.4
169
169
  signing_key:
170
170
  specification_version: 4
171
171
  summary: RuboCop tools to deal with Ruby code AST.