rubocop-ast 0.6.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/rubocop/ast.rb +17 -0
- data/lib/rubocop/ast/builder.rb +1 -0
- data/lib/rubocop/ast/node.rb +47 -127
- data/lib/rubocop/ast/node/array_node.rb +1 -0
- data/lib/rubocop/ast/node/block_node.rb +1 -0
- data/lib/rubocop/ast/node/def_node.rb +5 -0
- data/lib/rubocop/ast/node/keyword_splat_node.rb +1 -0
- data/lib/rubocop/ast/node/mixin/collection_node.rb +1 -0
- data/lib/rubocop/ast/node/mixin/descendence.rb +116 -0
- data/lib/rubocop/ast/node/mixin/method_dispatch_node.rb +3 -1
- data/lib/rubocop/ast/node/mixin/method_identifier_predicates.rb +9 -0
- data/lib/rubocop/ast/node/mixin/numeric_node.rb +1 -0
- data/lib/rubocop/ast/node/mixin/predicate_operator_node.rb +7 -3
- data/lib/rubocop/ast/node/pair_node.rb +4 -0
- data/lib/rubocop/ast/node/regexp_node.rb +1 -0
- data/lib/rubocop/ast/node_pattern.rb +44 -870
- data/lib/rubocop/ast/node_pattern/builder.rb +72 -0
- data/lib/rubocop/ast/node_pattern/comment.rb +45 -0
- data/lib/rubocop/ast/node_pattern/compiler.rb +104 -0
- data/lib/rubocop/ast/node_pattern/compiler/atom_subcompiler.rb +56 -0
- data/lib/rubocop/ast/node_pattern/compiler/binding.rb +78 -0
- data/lib/rubocop/ast/node_pattern/compiler/debug.rb +168 -0
- data/lib/rubocop/ast/node_pattern/compiler/node_pattern_subcompiler.rb +146 -0
- data/lib/rubocop/ast/node_pattern/compiler/sequence_subcompiler.rb +420 -0
- data/lib/rubocop/ast/node_pattern/compiler/subcompiler.rb +57 -0
- data/lib/rubocop/ast/node_pattern/lexer.rb +70 -0
- data/lib/rubocop/ast/node_pattern/lexer.rex +39 -0
- data/lib/rubocop/ast/node_pattern/lexer.rex.rb +182 -0
- data/lib/rubocop/ast/node_pattern/method_definer.rb +143 -0
- data/lib/rubocop/ast/node_pattern/node.rb +275 -0
- data/lib/rubocop/ast/node_pattern/parser.racc.rb +470 -0
- data/lib/rubocop/ast/node_pattern/parser.rb +66 -0
- data/lib/rubocop/ast/node_pattern/parser.y +103 -0
- data/lib/rubocop/ast/node_pattern/sets.rb +37 -0
- data/lib/rubocop/ast/node_pattern/with_meta.rb +111 -0
- data/lib/rubocop/ast/processed_source.rb +5 -1
- data/lib/rubocop/ast/traversal.rb +149 -172
- data/lib/rubocop/ast/version.rb +1 -1
- metadata +23 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d8575eee0ddbed5fb9eb553bbc33346ec8dc75216403501a46a946f825e79102
|
4
|
+
data.tar.gz: 5762b5f73e28c3dfd687feb0308d4248d87b90488ff99c9ddeebd49dced31941
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c7b6f9c6f1366ec8ddcb852ce8af9abdd0f945d41c17c9221ea27e9442b324912a1ff530e9faa36a4d48242e282b5786095cf714b38180e6b9fb121e97f4d703
|
7
|
+
data.tar.gz: e43be065343ccc65a3c776b7073c6647c4cbf406121ec8e31782ced1b2d218937597f70a35b014d9c5681cdf7e76a8045755c4133394160ab3ed710e87639972
|
data/lib/rubocop/ast.rb
CHANGED
@@ -6,7 +6,21 @@ require 'set'
|
|
6
6
|
|
7
7
|
require_relative 'ast/ext/range'
|
8
8
|
require_relative 'ast/ext/set'
|
9
|
+
require_relative 'ast/node_pattern/method_definer'
|
9
10
|
require_relative 'ast/node_pattern'
|
11
|
+
require_relative 'ast/node/mixin/descendence'
|
12
|
+
require_relative 'ast/node_pattern/builder'
|
13
|
+
require_relative 'ast/node_pattern/comment'
|
14
|
+
require_relative 'ast/node_pattern/compiler'
|
15
|
+
require_relative 'ast/node_pattern/compiler/subcompiler'
|
16
|
+
require_relative 'ast/node_pattern/compiler/atom_subcompiler'
|
17
|
+
require_relative 'ast/node_pattern/compiler/binding'
|
18
|
+
require_relative 'ast/node_pattern/compiler/node_pattern_subcompiler'
|
19
|
+
require_relative 'ast/node_pattern/compiler/sequence_subcompiler'
|
20
|
+
require_relative 'ast/node_pattern/lexer'
|
21
|
+
require_relative 'ast/node_pattern/node'
|
22
|
+
require_relative 'ast/node_pattern/parser'
|
23
|
+
require_relative 'ast/node_pattern/sets'
|
10
24
|
require_relative 'ast/sexp'
|
11
25
|
require_relative 'ast/node'
|
12
26
|
require_relative 'ast/node/mixin/method_identifier_predicates'
|
@@ -67,3 +81,6 @@ require_relative 'ast/rubocop_compatibility'
|
|
67
81
|
require_relative 'ast/token'
|
68
82
|
require_relative 'ast/traversal'
|
69
83
|
require_relative 'ast/version'
|
84
|
+
|
85
|
+
::RuboCop::AST::NodePattern::Parser.autoload :WithMeta, "#{__dir__}/ast/node_pattern/with_meta"
|
86
|
+
::RuboCop::AST::NodePattern::Compiler.autoload :Debug, "#{__dir__}/ast/node_pattern/compiler/debug"
|
data/lib/rubocop/ast/builder.rb
CHANGED
data/lib/rubocop/ast/node.rb
CHANGED
@@ -21,41 +21,67 @@ module RuboCop
|
|
21
21
|
class Node < Parser::AST::Node # rubocop:disable Metrics/ClassLength
|
22
22
|
include RuboCop::AST::Sexp
|
23
23
|
extend NodePattern::Macros
|
24
|
+
include RuboCop::AST::Descendence
|
24
25
|
|
26
|
+
# @api private
|
25
27
|
# <=> isn't included here, because it doesn't return a boolean.
|
26
|
-
COMPARISON_OPERATORS = %i[== === != <= >= > <].freeze
|
28
|
+
COMPARISON_OPERATORS = %i[== === != <= >= > <].to_set.freeze
|
27
29
|
|
30
|
+
# @api private
|
28
31
|
TRUTHY_LITERALS = %i[str dstr xstr int float sym dsym array
|
29
32
|
hash regexp true irange erange complex
|
30
|
-
rational regopt].freeze
|
31
|
-
|
33
|
+
rational regopt].to_set.freeze
|
34
|
+
# @api private
|
35
|
+
FALSEY_LITERALS = %i[false nil].to_set.freeze
|
36
|
+
# @api private
|
32
37
|
LITERALS = (TRUTHY_LITERALS + FALSEY_LITERALS).freeze
|
38
|
+
# @api private
|
33
39
|
COMPOSITE_LITERALS = %i[dstr xstr dsym array hash irange
|
34
|
-
erange regexp].freeze
|
40
|
+
erange regexp].to_set.freeze
|
41
|
+
# @api private
|
35
42
|
BASIC_LITERALS = (LITERALS - COMPOSITE_LITERALS).freeze
|
43
|
+
# @api private
|
36
44
|
MUTABLE_LITERALS = %i[str dstr xstr array hash
|
37
|
-
regexp irange erange].freeze
|
45
|
+
regexp irange erange].to_set.freeze
|
46
|
+
# @api private
|
38
47
|
IMMUTABLE_LITERALS = (LITERALS - MUTABLE_LITERALS).freeze
|
39
48
|
|
49
|
+
# @api private
|
40
50
|
EQUALS_ASSIGNMENTS = %i[lvasgn ivasgn cvasgn gvasgn
|
41
|
-
casgn masgn rasgn mrasgn].freeze
|
42
|
-
|
51
|
+
casgn masgn rasgn mrasgn].to_set.freeze
|
52
|
+
# @api private
|
53
|
+
SHORTHAND_ASSIGNMENTS = %i[op_asgn or_asgn and_asgn].to_set.freeze
|
54
|
+
# @api private
|
43
55
|
ASSIGNMENTS = (EQUALS_ASSIGNMENTS + SHORTHAND_ASSIGNMENTS).freeze
|
44
56
|
|
45
|
-
|
46
|
-
|
47
|
-
|
57
|
+
# @api private
|
58
|
+
BASIC_CONDITIONALS = %i[if while until].to_set.freeze
|
59
|
+
# @api private
|
60
|
+
CONDITIONALS = (BASIC_CONDITIONALS + [:case]).freeze
|
61
|
+
# @api private
|
62
|
+
POST_CONDITION_LOOP_TYPES = %i[while_post until_post].to_set.freeze
|
63
|
+
# @api private
|
48
64
|
LOOP_TYPES = (POST_CONDITION_LOOP_TYPES + %i[while until for]).freeze
|
49
|
-
|
50
|
-
|
65
|
+
# @api private
|
66
|
+
VARIABLES = %i[ivar gvar cvar lvar].to_set.freeze
|
67
|
+
# @api private
|
68
|
+
REFERENCES = %i[nth_ref back_ref].to_set.freeze
|
69
|
+
# @api private
|
51
70
|
KEYWORDS = %i[alias and break case class def defs defined?
|
52
71
|
kwbegin do else ensure for if module next
|
53
72
|
not or postexe redo rescue retry return self
|
54
73
|
super zsuper then undef until when while
|
55
|
-
yield].freeze
|
56
|
-
|
57
|
-
|
58
|
-
|
74
|
+
yield].to_set.freeze
|
75
|
+
# @api private
|
76
|
+
OPERATOR_KEYWORDS = %i[and or].to_set.freeze
|
77
|
+
# @api private
|
78
|
+
SPECIAL_KEYWORDS = %w[__FILE__ __LINE__ __ENCODING__].to_set.freeze
|
79
|
+
# @api private
|
80
|
+
ARGUMENT_TYPES = %i[arg optarg restarg kwarg kwoptarg kwrestarg blockarg].to_set.freeze
|
81
|
+
|
82
|
+
LITERAL_RECURSIVE_METHODS = (COMPARISON_OPERATORS + %i[* ! <=>]).freeze
|
83
|
+
LITERAL_RECURSIVE_TYPES = (OPERATOR_KEYWORDS + COMPOSITE_LITERALS + %i[begin pair]).freeze
|
84
|
+
private_constant :LITERAL_RECURSIVE_METHODS, :LITERAL_RECURSIVE_TYPES
|
59
85
|
|
60
86
|
# @see https://www.rubydoc.info/gems/ast/AST/Node:initialize
|
61
87
|
def initialize(type, children = [], properties = {})
|
@@ -211,104 +237,6 @@ module RuboCop
|
|
211
237
|
each_ancestor.to_a
|
212
238
|
end
|
213
239
|
|
214
|
-
# Calls the given block for each child node.
|
215
|
-
# If no block is given, an `Enumerator` is returned.
|
216
|
-
#
|
217
|
-
# Note that this is different from `node.children.each { |child| ... }`
|
218
|
-
# which yields all children including non-node elements.
|
219
|
-
#
|
220
|
-
# @overload each_child_node
|
221
|
-
# Yield all nodes.
|
222
|
-
# @overload each_child_node(type)
|
223
|
-
# Yield only nodes matching the type.
|
224
|
-
# @param [Symbol] type a node type
|
225
|
-
# @overload each_child_node(type_a, type_b, ...)
|
226
|
-
# Yield only nodes matching any of the types.
|
227
|
-
# @param [Symbol] type_a a node type
|
228
|
-
# @param [Symbol] type_b a node type
|
229
|
-
# @yieldparam [Node] node each child node
|
230
|
-
# @return [self] if a block is given
|
231
|
-
# @return [Enumerator] if no block is given
|
232
|
-
def each_child_node(*types)
|
233
|
-
return to_enum(__method__, *types) unless block_given?
|
234
|
-
|
235
|
-
children.each do |child|
|
236
|
-
next unless child.is_a?(Node)
|
237
|
-
|
238
|
-
yield child if types.empty? || types.include?(child.type)
|
239
|
-
end
|
240
|
-
|
241
|
-
self
|
242
|
-
end
|
243
|
-
|
244
|
-
# Returns an array of child nodes.
|
245
|
-
# This is a shorthand for `node.each_child_node.to_a`.
|
246
|
-
#
|
247
|
-
# @return [Array<Node>] an array of child nodes
|
248
|
-
def child_nodes
|
249
|
-
each_child_node.to_a
|
250
|
-
end
|
251
|
-
|
252
|
-
# Calls the given block for each descendant node with depth first order.
|
253
|
-
# If no block is given, an `Enumerator` is returned.
|
254
|
-
#
|
255
|
-
# @overload each_descendant
|
256
|
-
# Yield all nodes.
|
257
|
-
# @overload each_descendant(type)
|
258
|
-
# Yield only nodes matching the type.
|
259
|
-
# @param [Symbol] type a node type
|
260
|
-
# @overload each_descendant(type_a, type_b, ...)
|
261
|
-
# Yield only nodes matching any of the types.
|
262
|
-
# @param [Symbol] type_a a node type
|
263
|
-
# @param [Symbol] type_b a node type
|
264
|
-
# @yieldparam [Node] node each descendant node
|
265
|
-
# @return [self] if a block is given
|
266
|
-
# @return [Enumerator] if no block is given
|
267
|
-
def each_descendant(*types, &block)
|
268
|
-
return to_enum(__method__, *types) unless block_given?
|
269
|
-
|
270
|
-
visit_descendants(types, &block)
|
271
|
-
|
272
|
-
self
|
273
|
-
end
|
274
|
-
|
275
|
-
# Returns an array of descendant nodes.
|
276
|
-
# This is a shorthand for `node.each_descendant.to_a`.
|
277
|
-
#
|
278
|
-
# @return [Array<Node>] an array of descendant nodes
|
279
|
-
def descendants
|
280
|
-
each_descendant.to_a
|
281
|
-
end
|
282
|
-
|
283
|
-
# Calls the given block for the receiver and each descendant node in
|
284
|
-
# depth-first order.
|
285
|
-
# If no block is given, an `Enumerator` is returned.
|
286
|
-
#
|
287
|
-
# This method would be useful when you treat the receiver node as the root
|
288
|
-
# of a tree and want to iterate over all nodes in the tree.
|
289
|
-
#
|
290
|
-
# @overload each_node
|
291
|
-
# Yield all nodes.
|
292
|
-
# @overload each_node(type)
|
293
|
-
# Yield only nodes matching the type.
|
294
|
-
# @param [Symbol] type a node type
|
295
|
-
# @overload each_node(type_a, type_b, ...)
|
296
|
-
# Yield only nodes matching any of the types.
|
297
|
-
# @param [Symbol] type_a a node type
|
298
|
-
# @param [Symbol] type_b a node type
|
299
|
-
# @yieldparam [Node] node each node
|
300
|
-
# @return [self] if a block is given
|
301
|
-
# @return [Enumerator] if no block is given
|
302
|
-
def each_node(*types, &block)
|
303
|
-
return to_enum(__method__, *types) unless block_given?
|
304
|
-
|
305
|
-
yield self if types.empty? || types.include?(type)
|
306
|
-
|
307
|
-
visit_descendants(types, &block)
|
308
|
-
|
309
|
-
self
|
310
|
-
end
|
311
|
-
|
312
240
|
# Note: Some rare nodes may have no source, like `s(:args)` in `foo {}`
|
313
241
|
# @return [String, nil]
|
314
242
|
def source
|
@@ -439,10 +367,10 @@ module RuboCop
|
|
439
367
|
define_method(recursive_kind) do
|
440
368
|
case type
|
441
369
|
when :send
|
442
|
-
|
370
|
+
LITERAL_RECURSIVE_METHODS.include?(method_name) &&
|
443
371
|
receiver.send(recursive_kind) &&
|
444
372
|
arguments.all?(&recursive_kind)
|
445
|
-
when
|
373
|
+
when LITERAL_RECURSIVE_TYPES
|
446
374
|
children.compact.all?(&recursive_kind)
|
447
375
|
else
|
448
376
|
send(kind_filter)
|
@@ -556,10 +484,11 @@ module RuboCop
|
|
556
484
|
def_node_matcher :global_const?, '(const {nil? cbase} %1)'
|
557
485
|
|
558
486
|
def_node_matcher :class_constructor?, <<~PATTERN
|
559
|
-
{ (send #global_const?({:Class :Module}) :new ...)
|
560
|
-
(block (send #global_const?({:Class :Module}) :new ...) ...)}
|
487
|
+
{ (send #global_const?({:Class :Module :Struct}) :new ...)
|
488
|
+
(block (send #global_const?({:Class :Module :Struct}) :new ...) ...)}
|
561
489
|
PATTERN
|
562
490
|
|
491
|
+
# @deprecated Use `:class_constructor?`
|
563
492
|
def_node_matcher :struct_constructor?, <<~PATTERN
|
564
493
|
(block (send #global_const?(:Struct) :new ...) _ $_)
|
565
494
|
PATTERN
|
@@ -631,15 +560,6 @@ module RuboCop
|
|
631
560
|
end
|
632
561
|
end
|
633
562
|
|
634
|
-
protected
|
635
|
-
|
636
|
-
def visit_descendants(types, &block)
|
637
|
-
each_child_node do |child|
|
638
|
-
yield child if types.empty? || types.include?(child.type)
|
639
|
-
child.visit_descendants(types, &block)
|
640
|
-
end
|
641
|
-
end
|
642
|
-
|
643
563
|
private
|
644
564
|
|
645
565
|
def visit_ancestors(types)
|
@@ -0,0 +1,116 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module AST
|
5
|
+
# Common functionality for primitive literal nodes: `sym`, `str`,
|
6
|
+
# `int`, `float`, ...
|
7
|
+
module Descendence
|
8
|
+
# Calls the given block for each child node.
|
9
|
+
# If no block is given, an `Enumerator` is returned.
|
10
|
+
#
|
11
|
+
# Note that this is different from `node.children.each { |child| ... }`
|
12
|
+
# which yields all children including non-node elements.
|
13
|
+
#
|
14
|
+
# @overload each_child_node
|
15
|
+
# Yield all nodes.
|
16
|
+
# @overload each_child_node(type)
|
17
|
+
# Yield only nodes matching the type.
|
18
|
+
# @param [Symbol] type a node type
|
19
|
+
# @overload each_child_node(type_a, type_b, ...)
|
20
|
+
# Yield only nodes matching any of the types.
|
21
|
+
# @param [Symbol] type_a a node type
|
22
|
+
# @param [Symbol] type_b a node type
|
23
|
+
# @yieldparam [Node] node each child node
|
24
|
+
# @return [self] if a block is given
|
25
|
+
# @return [Enumerator] if no block is given
|
26
|
+
def each_child_node(*types)
|
27
|
+
return to_enum(__method__, *types) unless block_given?
|
28
|
+
|
29
|
+
children.each do |child|
|
30
|
+
next unless child.is_a?(::AST::Node)
|
31
|
+
|
32
|
+
yield child if types.empty? || types.include?(child.type)
|
33
|
+
end
|
34
|
+
|
35
|
+
self
|
36
|
+
end
|
37
|
+
|
38
|
+
# Returns an array of child nodes.
|
39
|
+
# This is a shorthand for `node.each_child_node.to_a`.
|
40
|
+
#
|
41
|
+
# @return [Array<Node>] an array of child nodes
|
42
|
+
def child_nodes
|
43
|
+
each_child_node.to_a
|
44
|
+
end
|
45
|
+
|
46
|
+
# Calls the given block for each descendant node with depth first order.
|
47
|
+
# If no block is given, an `Enumerator` is returned.
|
48
|
+
#
|
49
|
+
# @overload each_descendant
|
50
|
+
# Yield all nodes.
|
51
|
+
# @overload each_descendant(type)
|
52
|
+
# Yield only nodes matching the type.
|
53
|
+
# @param [Symbol] type a node type
|
54
|
+
# @overload each_descendant(type_a, type_b, ...)
|
55
|
+
# Yield only nodes matching any of the types.
|
56
|
+
# @param [Symbol] type_a a node type
|
57
|
+
# @param [Symbol] type_b a node type
|
58
|
+
# @yieldparam [Node] node each descendant node
|
59
|
+
# @return [self] if a block is given
|
60
|
+
# @return [Enumerator] if no block is given
|
61
|
+
def each_descendant(*types, &block)
|
62
|
+
return to_enum(__method__, *types) unless block_given?
|
63
|
+
|
64
|
+
visit_descendants(types, &block)
|
65
|
+
|
66
|
+
self
|
67
|
+
end
|
68
|
+
|
69
|
+
# Returns an array of descendant nodes.
|
70
|
+
# This is a shorthand for `node.each_descendant.to_a`.
|
71
|
+
#
|
72
|
+
# @return [Array<Node>] an array of descendant nodes
|
73
|
+
def descendants
|
74
|
+
each_descendant.to_a
|
75
|
+
end
|
76
|
+
|
77
|
+
# Calls the given block for the receiver and each descendant node in
|
78
|
+
# depth-first order.
|
79
|
+
# If no block is given, an `Enumerator` is returned.
|
80
|
+
#
|
81
|
+
# This method would be useful when you treat the receiver node as the root
|
82
|
+
# of a tree and want to iterate over all nodes in the tree.
|
83
|
+
#
|
84
|
+
# @overload each_node
|
85
|
+
# Yield all nodes.
|
86
|
+
# @overload each_node(type)
|
87
|
+
# Yield only nodes matching the type.
|
88
|
+
# @param [Symbol] type a node type
|
89
|
+
# @overload each_node(type_a, type_b, ...)
|
90
|
+
# Yield only nodes matching any of the types.
|
91
|
+
# @param [Symbol] type_a a node type
|
92
|
+
# @param [Symbol] type_b a node type
|
93
|
+
# @yieldparam [Node] node each node
|
94
|
+
# @return [self] if a block is given
|
95
|
+
# @return [Enumerator] if no block is given
|
96
|
+
def each_node(*types, &block)
|
97
|
+
return to_enum(__method__, *types) unless block_given?
|
98
|
+
|
99
|
+
yield self if types.empty? || types.include?(type)
|
100
|
+
|
101
|
+
visit_descendants(types, &block)
|
102
|
+
|
103
|
+
self
|
104
|
+
end
|
105
|
+
|
106
|
+
protected
|
107
|
+
|
108
|
+
def visit_descendants(types, &block)
|
109
|
+
each_child_node do |child|
|
110
|
+
yield child if types.empty? || types.include?(child.type)
|
111
|
+
child.visit_descendants(types, &block)
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
@@ -10,7 +10,9 @@ module RuboCop
|
|
10
10
|
include MethodIdentifierPredicates
|
11
11
|
|
12
12
|
ARITHMETIC_OPERATORS = %i[+ - * / % **].freeze
|
13
|
+
private_constant :ARITHMETIC_OPERATORS
|
13
14
|
SPECIAL_MODIFIERS = %w[private protected].freeze
|
15
|
+
private_constant :SPECIAL_MODIFIERS
|
14
16
|
|
15
17
|
# The receiving node of the method dispatch.
|
16
18
|
#
|
@@ -172,7 +174,7 @@ module RuboCop
|
|
172
174
|
# @return [Boolean] whether the dispatched method is a `def` modifier
|
173
175
|
def def_modifier?
|
174
176
|
send_type? &&
|
175
|
-
|
177
|
+
adjacent_def_modifier? || each_child_node(:send).any?(&:def_modifier?)
|
176
178
|
end
|
177
179
|
|
178
180
|
# Checks whether this is a lambda. Some versions of parser parses
|