rubocop-ast 1.4.1 → 1.13.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: 514cdc7bddc33ed7ef5123a27b9a8c7f4977a6d5f79b677371e8ec9b0f8641c3
4
- data.tar.gz: 66670e0fa5cf3e0710154714eb854624a592d72d219aee0e425e11be009892ab
3
+ metadata.gz: b3a3081c514303a8fb2f6ac3e4616da1d5f41ac4bd21d63407f9281534443385
4
+ data.tar.gz: dabd9d626ee4dd67946126b81f1a8852da16bb561caaabc91fa4b89648186fa6
5
5
  SHA512:
6
- metadata.gz: cc1dc3353b12e90a8166d02d7ddf95e80a966290c6f611500cc5b446a707456a6acbd15947d92399b3ddd004195e8c8da4b23dd3ca80c91226920dd269326621
7
- data.tar.gz: 962af11aa6ad1d1b32117758661faa0e43f632cf6540df082e2e0aedabfecd7fb98adc9b30ff4ee5beb63674b2ad42a3dcc7b07a821e109e77b49868efce8b69
6
+ metadata.gz: f678e9d3b6da22c1be2861fd023121c50b161dfff5986ed14f49199c251bb041a2d2c3e93cacb86c9368dd71f8ed7575620984eeb54e65eaf183f062ffa11dee
7
+ data.tar.gz: 948888c0e83bca6acf73b06a0e5030bab2fde65cafa6366c6a3054cb07eca722d938f28a8b1e1bb226c8f48c9bb483b9d66704d432fdc87e23a4d2461809bb38
data/README.md CHANGED
@@ -1,11 +1,11 @@
1
1
  # RuboCop AST
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/rubocop-ast.svg)](https://badge.fury.io/rb/rubocop-ast)
4
- [![CI](https://github.com/rubocop-hq/rubocop-ast/workflows/CI/badge.svg)](https://github.com/rubocop-hq/rubocop-ast/actions?query=workflow%3ACI)
5
- [![Test Coverage](https://api.codeclimate.com/v1/badges/a29666e6373bc41bc0a9/test_coverage)](https://codeclimate.com/github/rubocop-hq/rubocop-ast/test_coverage)
6
- [![Maintainability](https://api.codeclimate.com/v1/badges/a29666e6373bc41bc0a9/maintainability)](https://codeclimate.com/github/rubocop-hq/rubocop-ast/maintainability)
4
+ [![CI](https://github.com/rubocop/rubocop-ast/workflows/CI/badge.svg)](https://github.com/rubocop/rubocop-ast/actions?query=workflow%3ACI)
5
+ [![Test Coverage](https://api.codeclimate.com/v1/badges/a29666e6373bc41bc0a9/test_coverage)](https://codeclimate.com/github/rubocop/rubocop-ast/test_coverage)
6
+ [![Maintainability](https://api.codeclimate.com/v1/badges/a29666e6373bc41bc0a9/maintainability)](https://codeclimate.com/github/rubocop/rubocop-ast/maintainability)
7
7
 
8
- Contains the classes needed by [RuboCop](https://github.com/rubocop-hq/rubocop) to deal with Ruby's AST, in particular:
8
+ Contains the classes needed by [RuboCop](https://github.com/rubocop/rubocop) to deal with Ruby's AST, in particular:
9
9
 
10
10
  * `RuboCop::AST::Node` ([doc](docs/modules/ROOT/pages/node_types.adoc))
11
11
  * `RuboCop::AST::NodePattern` ([doc](docs/modules/ROOT/pages/node_pattern.adoc))
@@ -20,6 +20,7 @@ module RuboCop
20
20
  # @api private
21
21
  NODE_MAP = {
22
22
  and: AndNode,
23
+ and_asgn: AndAsgnNode,
23
24
  alias: AliasNode,
24
25
  arg: ArgNode,
25
26
  blockarg: ArgNode,
@@ -32,10 +33,15 @@ module RuboCop
32
33
  shadowarg: ArgNode,
33
34
  args: ArgsNode,
34
35
  array: ArrayNode,
36
+ lvasgn: AsgnNode,
37
+ ivasgn: AsgnNode,
38
+ cvasgn: AsgnNode,
39
+ gvasgn: AsgnNode,
35
40
  block: BlockNode,
36
41
  numblock: BlockNode,
37
42
  break: BreakNode,
38
43
  case_match: CaseMatchNode,
44
+ casgn: CasgnNode,
39
45
  case: CaseNode,
40
46
  class: ClassNode,
41
47
  const: ConstNode,
@@ -49,6 +55,7 @@ module RuboCop
49
55
  float: FloatNode,
50
56
  hash: HashNode,
51
57
  if: IfNode,
58
+ in_pattern: InPatternNode,
52
59
  int: IntNode,
53
60
  index: IndexNode,
54
61
  indexasgn: IndexasgnNode,
@@ -59,6 +66,8 @@ module RuboCop
59
66
  lambda: LambdaNode,
60
67
  module: ModuleNode,
61
68
  next: NextNode,
69
+ op_asgn: OpAsgnNode,
70
+ or_asgn: OrAsgnNode,
62
71
  or: OrNode,
63
72
  pair: PairNode,
64
73
  procarg0: Procarg0Node,
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module AST
5
+ # A node extension for `op_asgn` nodes.
6
+ # This will be used in place of a plain node when the builder constructs
7
+ # the AST, making its methods available to all assignment nodes within RuboCop.
8
+ class AndAsgnNode < OpAsgnNode
9
+ # The operator being used for assignment as a symbol.
10
+ #
11
+ # @return [Symbol] the assignment operator
12
+ def operator
13
+ :'&&'
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module AST
5
+ # A node extension for `lvasgn`, `ivasgn`, `cvasgn`, and `gvasgn` nodes.
6
+ # This will be used in place of a plain node when the builder constructs
7
+ # the AST, making its methods available to all assignment nodes within RuboCop.
8
+ class AsgnNode < Node
9
+ # The name of the variable being assigned as a symbol.
10
+ #
11
+ # @return [Symbol] the name of the variable being assigned
12
+ def name
13
+ node_parts[0]
14
+ end
15
+
16
+ # The expression being assigned to the variable.
17
+ #
18
+ # @return [Node] the expression being assigned.
19
+ def expression
20
+ node_parts[1]
21
+ end
22
+ end
23
+ end
24
+ end
@@ -24,16 +24,30 @@ module RuboCop
24
24
  self
25
25
  end
26
26
 
27
- # Returns an array of all the when branches in the `case` statement.
27
+ # Returns an array of all the `in` pattern branches in the `case` statement.
28
28
  #
29
- # @return [Array<Node>] an array of `in_pattern` nodes
29
+ # @return [Array<InPatternNode>] an array of `in_pattern` nodes
30
30
  def in_pattern_branches
31
31
  node_parts[1...-1]
32
32
  end
33
33
 
34
+ # Returns an array of all the when branches in the `case` statement.
35
+ #
36
+ # @return [Array<Node, nil>] an array of the bodies of the `in` branches
37
+ # and the `else` (if any). Note that these bodies could be nil.
38
+ def branches
39
+ bodies = in_pattern_branches.map(&:body)
40
+ if else?
41
+ # `empty-else` node sets nil because it has no body.
42
+ else_branch.empty_else_type? ? bodies.push(nil) : bodies.push(else_branch)
43
+ end
44
+ bodies
45
+ end
46
+
34
47
  # Returns the else branch of the `case` statement, if any.
35
48
  #
36
49
  # @return [Node] the else branch node of the `case` statement
50
+ # @return [EmptyElse] the empty else branch node of the `case` statement
37
51
  # @return [nil] if the case statement does not have an else branch.
38
52
  def else_branch
39
53
  node_parts[-1]
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module AST
5
+ # A node extension for `casgn` nodes.
6
+ # This will be used in place of a plain node when the builder constructs
7
+ # the AST, making its methods available to all assignment nodes within RuboCop.
8
+ class CasgnNode < Node
9
+ # The namespace of the constant being assigned.
10
+ #
11
+ # @return [Node, nil] the node associated with the scope (e.g. cbase, const, ...)
12
+ def namespace
13
+ node_parts[0]
14
+ end
15
+
16
+ # The name of the variable being assigned as a symbol.
17
+ #
18
+ # @return [Symbol] the name of the variable being assigned
19
+ def name
20
+ node_parts[1]
21
+ end
22
+
23
+ # The expression being assigned to the variable.
24
+ #
25
+ # @return [Node] the expression being assigned.
26
+ def expression
27
+ node_parts[2]
28
+ end
29
+ end
30
+ end
31
+ end
@@ -4,8 +4,6 @@ module RuboCop
4
4
  module AST
5
5
  # A node extension for `const` nodes.
6
6
  class ConstNode < Node
7
- # The `send` node associated with this block.
8
- #
9
7
  # @return [Node, nil] the node associated with the scope (e.g. cbase, const, ...)
10
8
  def namespace
11
9
  children[0]
@@ -16,8 +14,6 @@ module RuboCop
16
14
  children[1]
17
15
  end
18
16
 
19
- # The body of this block.
20
- #
21
17
  # @return [Boolean] if the constant is a Module / Class, according to the standard convention.
22
18
  # Note: some classes might have uppercase in which case this method
23
19
  # returns false
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module AST
5
+ # A node extension for `in` nodes. This will be used in place of a plain
6
+ # node when the builder constructs the AST, making its methods available
7
+ # to all `in` nodes within RuboCop.
8
+ class InPatternNode < Node
9
+ # Returns a node of the pattern in the `in` branch.
10
+ #
11
+ # @return [Node] a pattern node
12
+ def pattern
13
+ node_parts.first
14
+ end
15
+
16
+ # Returns the index of the `in` branch within the `case` statement.
17
+ #
18
+ # @return [Integer] the index of the `in` branch
19
+ def branch_index
20
+ parent.in_pattern_branches.index(self)
21
+ end
22
+
23
+ # Checks whether the `in` node has a `then` keyword.
24
+ #
25
+ # @return [Boolean] whether the `in` node has a `then` keyword
26
+ def then?
27
+ loc.begin&.is?('then')
28
+ end
29
+
30
+ # Returns the body of the `in` node.
31
+ #
32
+ # @return [Node, nil] the body of the `in` node
33
+ def body
34
+ node_parts[-1]
35
+ end
36
+ end
37
+ end
38
+ end
@@ -13,13 +13,9 @@ module RuboCop
13
13
  #
14
14
  # @overload each_child_node
15
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, ...)
16
+ # @overload each_child_node(type, ...)
20
17
  # Yield only nodes matching any of the types.
21
- # @param [Symbol] type_a a node type
22
- # @param [Symbol] type_b a node type
18
+ # @param [Symbol] type a node type
23
19
  # @yieldparam [Node] node each child node
24
20
  # @return [self] if a block is given
25
21
  # @return [Enumerator] if no block is given
@@ -28,9 +28,9 @@ module RuboCop
28
28
  node_parts[1]
29
29
  end
30
30
 
31
- # The `block` node associated with this method dispatch, if any.
31
+ # The `block` or `numblock` node associated with this method dispatch, if any.
32
32
  #
33
- # @return [BlockNode, nil] the `block` node associated with this method
33
+ # @return [BlockNode, nil] the `block` or `numblock` node associated with this method
34
34
  # call or `nil`
35
35
  def block_node
36
36
  parent if block_literal?
@@ -154,7 +154,7 @@ module RuboCop
154
154
  #
155
155
  # @return [Boolean] whether the dispatched method has a block
156
156
  def block_literal?
157
- parent&.block_type? && eql?(parent.send_node)
157
+ (parent&.block_type? || parent&.numblock_type?) && eql?(parent.send_node)
158
158
  end
159
159
 
160
160
  # Checks whether this node is an arithmetic operation
@@ -165,16 +165,34 @@ module RuboCop
165
165
  ARITHMETIC_OPERATORS.include?(method_name)
166
166
  end
167
167
 
168
- # Checks if this node is part of a chain of `def` modifiers.
168
+ # Checks if this node is part of a chain of `def` or `defs` modifiers.
169
169
  #
170
170
  # @example
171
171
  #
172
172
  # private def foo; end
173
173
  #
174
- # @return [Boolean] whether the dispatched method is a `def` modifier
175
- def def_modifier?
176
- send_type? &&
177
- adjacent_def_modifier? || each_child_node(:send).any?(&:def_modifier?)
174
+ # @return wether the `def|defs` node is a modifier or not.
175
+ # See also `def_modifier` that returns the node or `nil`
176
+ def def_modifier?(node = self)
177
+ !!def_modifier(node)
178
+ end
179
+
180
+ # Checks if this node is part of a chain of `def` or `defs` modifiers.
181
+ #
182
+ # @example
183
+ #
184
+ # private def foo; end
185
+ #
186
+ # @return [Node | nil] returns the `def|defs` node this is a modifier for,
187
+ # or `nil` if it isn't a def modifier
188
+ def def_modifier(node = self)
189
+ arg = node.children[2]
190
+
191
+ return unless node.send_type? && node.receiver.nil? && arg.is_a?(::AST::Node)
192
+
193
+ return arg if arg.def_type? || arg.defs_type?
194
+
195
+ def_modifier(arg)
178
196
  end
179
197
 
180
198
  # Checks whether this is a lambda. Some versions of parser parses
@@ -224,6 +242,7 @@ module RuboCop
224
242
 
225
243
  private
226
244
 
245
+ # @!method in_macro_scope?(node = self)
227
246
  def_node_matcher :in_macro_scope?, <<~PATTERN
228
247
  {
229
248
  root? # Either a root node,
@@ -239,14 +258,17 @@ module RuboCop
239
258
  }
240
259
  PATTERN
241
260
 
261
+ # @!method adjacent_def_modifier?(node = self)
242
262
  def_node_matcher :adjacent_def_modifier?, <<~PATTERN
243
263
  (send nil? _ ({def defs} ...))
244
264
  PATTERN
245
265
 
266
+ # @!method bare_access_modifier_declaration?(node = self)
246
267
  def_node_matcher :bare_access_modifier_declaration?, <<~PATTERN
247
268
  (send nil? {:public :protected :private :module_function})
248
269
  PATTERN
249
270
 
271
+ # @!method non_bare_access_modifier_declaration?(node = self)
250
272
  def_node_matcher :non_bare_access_modifier_declaration?, <<~PATTERN
251
273
  (send nil? {:public :protected :private :module_function} _)
252
274
  PATTERN
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module AST
5
+ # A node extension for `op_asgn` nodes.
6
+ # This will be used in place of a plain node when the builder constructs
7
+ # the AST, making its methods available to all assignment nodes within RuboCop.
8
+ class OpAsgnNode < Node
9
+ # @return [AsgnNode] the assignment node
10
+ def assignment_node
11
+ node_parts[0]
12
+ end
13
+
14
+ # The name of the variable being assigned as a symbol.
15
+ #
16
+ # @return [Symbol] the name of the variable being assigned
17
+ def name
18
+ assignment_node.name
19
+ end
20
+
21
+ # The operator being used for assignment as a symbol.
22
+ #
23
+ # @return [Symbol] the assignment operator
24
+ def operator
25
+ node_parts[1]
26
+ end
27
+
28
+ # The expression being assigned to the variable.
29
+ #
30
+ # @return [Node] the expression being assigned.
31
+ def expression
32
+ node_parts.last
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module AST
5
+ # A node extension for `op_asgn` nodes.
6
+ # This will be used in place of a plain node when the builder constructs
7
+ # the AST, making its methods available to all assignment nodes within RuboCop.
8
+ class OrAsgnNode < OpAsgnNode
9
+ # The operator being used for assignment as a symbol.
10
+ #
11
+ # @return [Symbol] the assignment operator
12
+ def operator
13
+ :'||'
14
+ end
15
+ end
16
+ end
17
+ end
@@ -9,6 +9,7 @@ module RuboCop
9
9
  include ParameterizedNode::RestArguments
10
10
  include MethodDispatchNode
11
11
 
12
+ # @!method attribute_accessor?(node = self)
12
13
  def_node_matcher :attribute_accessor?, <<~PATTERN
13
14
  [(send nil? ${:attr_reader :attr_writer :attr_accessor :attr} $...)
14
15
  (_ _ _ _ ...)]
@@ -272,10 +272,12 @@ module RuboCop
272
272
 
273
273
  ## Destructuring
274
274
 
275
+ # @!method receiver(node = self)
275
276
  def_node_matcher :receiver, <<~PATTERN
276
277
  {(send $_ ...) ({block numblock} (send $_ ...) ...)}
277
278
  PATTERN
278
279
 
280
+ # @!method str_content(node = self)
279
281
  def_node_matcher :str_content, '(str $_)'
280
282
 
281
283
  def const_name
@@ -289,6 +291,7 @@ module RuboCop
289
291
  end
290
292
  end
291
293
 
294
+ # @!method defined_module0(node = self)
292
295
  def_node_matcher :defined_module0, <<~PATTERN
293
296
  {(class (const $_ $_) ...)
294
297
  (module (const $_ $_) ...)
@@ -314,7 +317,11 @@ module RuboCop
314
317
  # returns nil if answer cannot be determined
315
318
  ancestors = each_ancestor(:class, :module, :sclass, :casgn, :block)
316
319
  result = ancestors.map do |ancestor|
317
- parent_module_name_part(ancestor) { |full_name| return full_name }
320
+ parent_module_name_part(ancestor) do |full_name|
321
+ return nil unless full_name
322
+
323
+ full_name
324
+ end
318
325
  end.compact.reverse.join('::')
319
326
  result.empty? ? 'Object' : result
320
327
  end
@@ -334,6 +341,7 @@ module RuboCop
334
341
  end
335
342
 
336
343
  # Some cops treat the shovel operator as a kind of assignment.
344
+ # @!method assignment_or_similar?(node = self)
337
345
  def_node_matcher :assignment_or_similar?, <<~PATTERN
338
346
  {assignment? (send _recv :<< ...)}
339
347
  PATTERN
@@ -417,7 +425,7 @@ module RuboCop
417
425
  end
418
426
 
419
427
  def keyword?
420
- return true if special_keyword? || send_type? && prefix_not?
428
+ return true if special_keyword? || (send_type? && prefix_not?)
421
429
  return false unless KEYWORDS.include?(type)
422
430
 
423
431
  !OPERATOR_KEYWORDS.include?(type) || loc.operator.is?(type.to_s)
@@ -456,7 +464,7 @@ module RuboCop
456
464
  end
457
465
 
458
466
  def numeric_type?
459
- int_type? || float_type?
467
+ int_type? || float_type? || rational_type? || complex_type?
460
468
  end
461
469
 
462
470
  def range_type?
@@ -469,40 +477,50 @@ module RuboCop
469
477
  node.match_guard_clause?
470
478
  end
471
479
 
480
+ # @!method match_guard_clause?(node = self)
472
481
  def_node_matcher :match_guard_clause?, <<~PATTERN
473
482
  [${(send nil? {:raise :fail} ...) return break next} single_line?]
474
483
  PATTERN
475
484
 
485
+ # @!method proc?(node = self)
476
486
  def_node_matcher :proc?, <<~PATTERN
477
487
  {(block (send nil? :proc) ...)
478
488
  (block (send #global_const?(:Proc) :new) ...)
479
489
  (send #global_const?(:Proc) :new)}
480
490
  PATTERN
481
491
 
492
+ # @!method lambda?(node = self)
482
493
  def_node_matcher :lambda?, '({block numblock} (send nil? :lambda) ...)'
494
+
495
+ # @!method lambda_or_proc?(node = self)
483
496
  def_node_matcher :lambda_or_proc?, '{lambda? proc?}'
484
497
 
498
+ # @!method global_const?(node = self, name)
485
499
  def_node_matcher :global_const?, '(const {nil? cbase} %1)'
486
500
 
501
+ # @!method class_constructor?(node = self)
487
502
  def_node_matcher :class_constructor?, <<~PATTERN
488
503
  { (send #global_const?({:Class :Module :Struct}) :new ...)
489
504
  (block (send #global_const?({:Class :Module :Struct}) :new ...) ...)}
490
505
  PATTERN
491
506
 
492
507
  # @deprecated Use `:class_constructor?`
508
+ # @!method struct_constructor?(node = self)
493
509
  def_node_matcher :struct_constructor?, <<~PATTERN
494
- (block (send #global_const?(:Struct) :new ...) _ $_)
510
+ ({block numblock} (send #global_const?(:Struct) :new ...) _ $_)
495
511
  PATTERN
496
512
 
513
+ # @!method class_definition?(node = self)
497
514
  def_node_matcher :class_definition?, <<~PATTERN
498
515
  {(class _ _ $_)
499
516
  (sclass _ $_)
500
- (block (send #global_const?({:Struct :Class}) :new ...) _ $_)}
517
+ ({block numblock} (send #global_const?({:Struct :Class}) :new ...) _ $_)}
501
518
  PATTERN
502
519
 
520
+ # @!method module_definition?(node = self)
503
521
  def_node_matcher :module_definition?, <<~PATTERN
504
522
  {(module _ $_)
505
- (block (send #global_const?(:Module) :new ...) _ $_)}
523
+ ({block numblock} (send #global_const?(:Module) :new ...) _ $_)}
506
524
  PATTERN
507
525
 
508
526
  # Some expressions are evaluated for their value, some for their side
@@ -633,6 +651,7 @@ module RuboCop
633
651
  end
634
652
  end
635
653
 
654
+ # @!method new_class_or_module_block?(node = self)
636
655
  def_node_matcher :new_class_or_module_block?, <<~PATTERN
637
656
  ^(casgn _ _ (block (send (const _ {:Class :Module}) :new) ...))
638
657
  PATTERN
@@ -347,7 +347,7 @@ module RuboCop
347
347
  def compile_loop_advance(to = '+=1')
348
348
  # The `#{@cur_child_var} ||` is just to avoid unused variable warning
349
349
  "(#{@cur_child_var} = #{@seq_var}.children[#{@cur_index_var} #{to}]; " \
350
- "#{@cur_child_var} || true)"
350
+ "#{@cur_child_var} || true)"
351
351
  end
352
352
 
353
353
  def compile_loop(term)
@@ -2,7 +2,7 @@
2
2
  # encoding: UTF-8
3
3
  #--
4
4
  # This file is automatically generated. Do not modify it.
5
- # Generated by: oedipus_lex version 2.5.2.
5
+ # Generated by: oedipus_lex version 2.6.0.
6
6
  # Source: lib/rubocop/ast/node_pattern/lexer.rex
7
7
  #++
8
8
 
@@ -54,7 +54,7 @@ module RuboCop
54
54
  children_nodes.sum(&:nb_captures)
55
55
  end
56
56
 
57
- # @return [Boolean] returns true iff matches variable number of elements
57
+ # @return [Boolean] returns whether it matches a variable number of elements
58
58
  def variadic?
59
59
  arity.is_a?(Range)
60
60
  end
@@ -52,7 +52,7 @@ module RuboCop
52
52
 
53
53
  detail = node.loc&.expression&.source || node.to_s
54
54
  raise NodePattern::Invalid, 'parse error, expected unary node pattern ' \
55
- "but got expression matching multiple elements: #{detail}"
55
+ "but got expression matching multiple elements: #{detail}"
56
56
  end
57
57
 
58
58
  # Overrides Racc::Parser's method:
@@ -28,7 +28,7 @@ module RuboCop
28
28
  module Macros
29
29
  # Define a method which applies a pattern to an AST node
30
30
  #
31
- # The new method will return nil if the node does not match
31
+ # The new method will return nil if the node does not match.
32
32
  # If the node matches, and a block is provided, the new method will
33
33
  # yield to the block (passing any captures as block arguments).
34
34
  # If the node matches, and no block is provided, the new method will
@@ -80,23 +80,23 @@ module RuboCop
80
80
  "#<#{self.class} #{pattern}>"
81
81
  end
82
82
 
83
- def marshal_load(pattern) #:nodoc:
83
+ def marshal_load(pattern) # :nodoc:
84
84
  initialize pattern
85
85
  end
86
86
 
87
- def marshal_dump #:nodoc:
87
+ def marshal_dump # :nodoc:
88
88
  pattern
89
89
  end
90
90
 
91
- def as_json(_options = nil) #:nodoc:
91
+ def as_json(_options = nil) # :nodoc:
92
92
  pattern
93
93
  end
94
94
 
95
- def encode_with(coder) #:nodoc:
95
+ def encode_with(coder) # :nodoc:
96
96
  coder['pattern'] = pattern
97
97
  end
98
98
 
99
- def init_with(coder) #:nodoc:
99
+ def init_with(coder) # :nodoc:
100
100
  initialize(coder['pattern'])
101
101
  end
102
102
 
@@ -41,7 +41,7 @@ module RuboCop
41
41
  def ast_with_comments
42
42
  return if !ast || !comments
43
43
 
44
- @ast_with_comments ||= Parser::Source::Comment.associate(ast, comments)
44
+ @ast_with_comments ||= Parser::Source::Comment.associate_by_identity(ast, comments)
45
45
  end
46
46
 
47
47
  # Returns the source lines, line break characters removed, excluding a
@@ -177,6 +177,14 @@ module RuboCop
177
177
  sorted_tokens[last_token_index(range_or_node)]
178
178
  end
179
179
 
180
+ # The tokens list is always sorted by token position, except for cases when heredoc
181
+ # is passed as a method argument. In this case tokens are interleaved by
182
+ # heredoc contents' tokens.
183
+ def sorted_tokens
184
+ # Use stable sort.
185
+ @sorted_tokens ||= tokens.sort_by.with_index { |token, i| [token.begin_pos, i] }
186
+ end
187
+
180
188
  private
181
189
 
182
190
  def comment_index
@@ -236,6 +244,9 @@ module RuboCop
236
244
  when 2.8, 3.0
237
245
  require 'parser/ruby30'
238
246
  Parser::Ruby30
247
+ when 3.1
248
+ require 'parser/ruby31'
249
+ Parser::Ruby31
239
250
  else
240
251
  raise ArgumentError,
241
252
  "RuboCop found unknown Ruby version: #{ruby_version.inspect}"
@@ -269,14 +280,6 @@ module RuboCop
269
280
  sorted_tokens.bsearch_index { |token| token.end_pos >= end_pos }
270
281
  end
271
282
 
272
- # The tokens list is always sorted by token position, except for cases when heredoc
273
- # is passed as a method argument. In this case tokens are interleaved by
274
- # heredoc contents' tokens.
275
- def sorted_tokens
276
- # Use stable sort.
277
- @sorted_tokens ||= tokens.sort_by.with_index { |token, i| [token.begin_pos, i] }
278
- end
279
-
280
283
  def source_range(range_or_node)
281
284
  if range_or_node.respond_to?(:source_range)
282
285
  range_or_node.source_range
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module AST
5
5
  module Version
6
- STRING = '1.4.1'
6
+ STRING = '1.13.0'
7
7
  end
8
8
  end
9
9
  end
data/lib/rubocop/ast.rb CHANGED
@@ -6,7 +6,6 @@ require 'set'
6
6
 
7
7
  require_relative 'ast/ext/range'
8
8
  require_relative 'ast/ext/range_min_max'
9
- require_relative 'ast/ext/set'
10
9
  require_relative 'ast/node_pattern/method_definer'
11
10
  require_relative 'ast/node_pattern'
12
11
  require_relative 'ast/node/mixin/descendence'
@@ -40,10 +39,12 @@ require_relative 'ast/node/and_node'
40
39
  require_relative 'ast/node/arg_node'
41
40
  require_relative 'ast/node/args_node'
42
41
  require_relative 'ast/node/array_node'
42
+ require_relative 'ast/node/asgn_node'
43
43
  require_relative 'ast/node/block_node'
44
44
  require_relative 'ast/node/break_node'
45
45
  require_relative 'ast/node/case_match_node'
46
46
  require_relative 'ast/node/case_node'
47
+ require_relative 'ast/node/casgn_node'
47
48
  require_relative 'ast/node/class_node'
48
49
  require_relative 'ast/node/const_node'
49
50
  require_relative 'ast/node/def_node'
@@ -54,6 +55,7 @@ require_relative 'ast/node/forward_args_node'
54
55
  require_relative 'ast/node/float_node'
55
56
  require_relative 'ast/node/hash_node'
56
57
  require_relative 'ast/node/if_node'
58
+ require_relative 'ast/node/in_pattern_node'
57
59
  require_relative 'ast/node/index_node'
58
60
  require_relative 'ast/node/indexasgn_node'
59
61
  require_relative 'ast/node/int_node'
@@ -61,6 +63,9 @@ require_relative 'ast/node/keyword_splat_node'
61
63
  require_relative 'ast/node/lambda_node'
62
64
  require_relative 'ast/node/module_node'
63
65
  require_relative 'ast/node/next_node'
66
+ require_relative 'ast/node/op_asgn_node'
67
+ require_relative 'ast/node/and_asgn_node'
68
+ require_relative 'ast/node/or_asgn_node'
64
69
  require_relative 'ast/node/or_node'
65
70
  require_relative 'ast/node/pair_node'
66
71
  require_relative 'ast/node/procarg0_node'
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: 1.4.1
4
+ version: 1.13.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: 2021-01-23 00:00:00.000000000 Z
13
+ date: 2021-11-07 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: parser
@@ -18,14 +18,14 @@ dependencies:
18
18
  requirements:
19
19
  - - ">="
20
20
  - !ruby/object:Gem::Version
21
- version: 2.7.1.5
21
+ version: 3.0.1.1
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
25
25
  requirements:
26
26
  - - ">="
27
27
  - !ruby/object:Gem::Version
28
- version: 2.7.1.5
28
+ version: 3.0.1.1
29
29
  - !ruby/object:Gem::Dependency
30
30
  name: bundler
31
31
  requirement: !ruby/object:Gem::Requirement
@@ -61,17 +61,19 @@ files:
61
61
  - lib/rubocop/ast/builder.rb
62
62
  - lib/rubocop/ast/ext/range.rb
63
63
  - lib/rubocop/ast/ext/range_min_max.rb
64
- - lib/rubocop/ast/ext/set.rb
65
64
  - lib/rubocop/ast/node.rb
66
65
  - lib/rubocop/ast/node/alias_node.rb
66
+ - lib/rubocop/ast/node/and_asgn_node.rb
67
67
  - lib/rubocop/ast/node/and_node.rb
68
68
  - lib/rubocop/ast/node/arg_node.rb
69
69
  - lib/rubocop/ast/node/args_node.rb
70
70
  - lib/rubocop/ast/node/array_node.rb
71
+ - lib/rubocop/ast/node/asgn_node.rb
71
72
  - lib/rubocop/ast/node/block_node.rb
72
73
  - lib/rubocop/ast/node/break_node.rb
73
74
  - lib/rubocop/ast/node/case_match_node.rb
74
75
  - lib/rubocop/ast/node/case_node.rb
76
+ - lib/rubocop/ast/node/casgn_node.rb
75
77
  - lib/rubocop/ast/node/class_node.rb
76
78
  - lib/rubocop/ast/node/const_node.rb
77
79
  - lib/rubocop/ast/node/def_node.rb
@@ -83,6 +85,7 @@ files:
83
85
  - lib/rubocop/ast/node/forward_args_node.rb
84
86
  - lib/rubocop/ast/node/hash_node.rb
85
87
  - lib/rubocop/ast/node/if_node.rb
88
+ - lib/rubocop/ast/node/in_pattern_node.rb
86
89
  - lib/rubocop/ast/node/index_node.rb
87
90
  - lib/rubocop/ast/node/indexasgn_node.rb
88
91
  - lib/rubocop/ast/node/int_node.rb
@@ -102,6 +105,8 @@ files:
102
105
  - lib/rubocop/ast/node/mixin/predicate_operator_node.rb
103
106
  - lib/rubocop/ast/node/module_node.rb
104
107
  - lib/rubocop/ast/node/next_node.rb
108
+ - lib/rubocop/ast/node/op_asgn_node.rb
109
+ - lib/rubocop/ast/node/or_asgn_node.rb
105
110
  - lib/rubocop/ast/node/or_node.rb
106
111
  - lib/rubocop/ast/node/pair_node.rb
107
112
  - lib/rubocop/ast/node/procarg0_node.rb
@@ -145,15 +150,15 @@ files:
145
150
  - lib/rubocop/ast/token.rb
146
151
  - lib/rubocop/ast/traversal.rb
147
152
  - lib/rubocop/ast/version.rb
148
- homepage: https://github.com/rubocop-hq/rubocop-ast
153
+ homepage: https://github.com/rubocop/rubocop-ast
149
154
  licenses:
150
155
  - MIT
151
156
  metadata:
152
157
  homepage_uri: https://www.rubocop.org/
153
- changelog_uri: https://github.com/rubocop-hq/rubocop-ast/blob/master/CHANGELOG.md
154
- source_code_uri: https://github.com/rubocop-hq/rubocop-ast/
158
+ changelog_uri: https://github.com/rubocop/rubocop-ast/blob/master/CHANGELOG.md
159
+ source_code_uri: https://github.com/rubocop/rubocop-ast/
155
160
  documentation_uri: https://docs.rubocop.org/rubocop-ast/
156
- bug_tracker_uri: https://github.com/rubocop-hq/rubocop-ast/issues
161
+ bug_tracker_uri: https://github.com/rubocop/rubocop-ast/issues
157
162
  post_install_message:
158
163
  rdoc_options: []
159
164
  require_paths:
@@ -162,7 +167,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
162
167
  requirements:
163
168
  - - ">="
164
169
  - !ruby/object:Gem::Version
165
- version: 2.4.0
170
+ version: 2.5.0
166
171
  required_rubygems_version: !ruby/object:Gem::Requirement
167
172
  requirements:
168
173
  - - ">="
@@ -1,12 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- test = :foo
4
- case test
5
- when Set[:foo]
6
- # ok, RUBY_VERSION > 2.4
7
- else
8
- # Harmonize `Set#===`
9
- class Set
10
- alias === include?
11
- end
12
- end