rubocop-ast 1.4.0 → 1.7.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: 34e6f8d5799eb4f6871ac4666b174d020ca3131b801184b31324d7488abc64b8
4
- data.tar.gz: f0be9d5375bc11ae205a2b6343aa2db37afdd330bbde6d22c82ee54b1d6ae03d
3
+ metadata.gz: 25e84f96e233a9c966ad15efd834e109fea1c11d0998e2913ffd81310568d2b2
4
+ data.tar.gz: f6b8a033d2fd90518fb102f2cfaa27a5ef32bcfb892783ef79e2b9ad33ab4235
5
5
  SHA512:
6
- metadata.gz: aa8d27ca9cb6cb156f4a5cc1a951860fa2c04ef0cdf425c4ce2255825a08e664c0255e24c75495c66028f8aeadcf59f524b14b409c6d644129ff95e7a2c56194
7
- data.tar.gz: 36c4874bf4e4dd2c8d076243ee08f670dcf17711ebced45f200f71dd720faf4cd1e119b9df491fcee140d33d94bc7ddc3fd88f8818665505702c24ac54e0b91e
6
+ metadata.gz: 326d28041a4c30d64cac801ceec9677eea264c7a98754217ae27b026bf9779122803402cdddd8306881320e8ac185ce3ff708d4b6e2c869840622303b50b9ab1
7
+ data.tar.gz: 293b516ba03824c5c9fc56557943993688cf0796bd7ffe567434f7633ca71aa24aec8ebcac89284c3b7b9fb7d38302cf666b1e7b0bb844700d70bad8c65cc767
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'
@@ -54,6 +53,7 @@ require_relative 'ast/node/forward_args_node'
54
53
  require_relative 'ast/node/float_node'
55
54
  require_relative 'ast/node/hash_node'
56
55
  require_relative 'ast/node/if_node'
56
+ require_relative 'ast/node/in_pattern_node'
57
57
  require_relative 'ast/node/index_node'
58
58
  require_relative 'ast/node/indexasgn_node'
59
59
  require_relative 'ast/node/int_node'
@@ -72,6 +72,7 @@ require_relative 'ast/node/return_node'
72
72
  require_relative 'ast/node/self_class_node'
73
73
  require_relative 'ast/node/send_node'
74
74
  require_relative 'ast/node/str_node'
75
+ require_relative 'ast/node/dstr_node'
75
76
  require_relative 'ast/node/super_node'
76
77
  require_relative 'ast/node/symbol_node'
77
78
  require_relative 'ast/node/until_node'
@@ -42,12 +42,14 @@ module RuboCop
42
42
  def: DefNode,
43
43
  defined?: DefinedNode,
44
44
  defs: DefNode,
45
+ dstr: DstrNode,
45
46
  ensure: EnsureNode,
46
47
  for: ForNode,
47
48
  forward_args: ForwardArgsNode,
48
49
  float: FloatNode,
49
50
  hash: HashNode,
50
51
  if: IfNode,
52
+ in_pattern: InPatternNode,
51
53
  int: IntNode,
52
54
  index: IndexNode,
53
55
  indexasgn: IndexasgnNode,
@@ -68,7 +70,6 @@ module RuboCop
68
70
  csend: SendNode,
69
71
  send: SendNode,
70
72
  str: StrNode,
71
- dstr: StrNode,
72
73
  xstr: StrNode,
73
74
  sclass: SelfClassNode,
74
75
  super: SuperNode,
@@ -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 $_ $_) ...)
@@ -334,6 +337,7 @@ module RuboCop
334
337
  end
335
338
 
336
339
  # Some cops treat the shovel operator as a kind of assignment.
340
+ # @!method assignment_or_similar?(node = self)
337
341
  def_node_matcher :assignment_or_similar?, <<~PATTERN
338
342
  {assignment? (send _recv :<< ...)}
339
343
  PATTERN
@@ -469,37 +473,47 @@ module RuboCop
469
473
  node.match_guard_clause?
470
474
  end
471
475
 
476
+ # @!method match_guard_clause?(node = self)
472
477
  def_node_matcher :match_guard_clause?, <<~PATTERN
473
478
  [${(send nil? {:raise :fail} ...) return break next} single_line?]
474
479
  PATTERN
475
480
 
481
+ # @!method proc?(node = self)
476
482
  def_node_matcher :proc?, <<~PATTERN
477
483
  {(block (send nil? :proc) ...)
478
484
  (block (send #global_const?(:Proc) :new) ...)
479
485
  (send #global_const?(:Proc) :new)}
480
486
  PATTERN
481
487
 
488
+ # @!method lambda?(node = self)
482
489
  def_node_matcher :lambda?, '({block numblock} (send nil? :lambda) ...)'
490
+
491
+ # @!method lambda_or_proc?(node = self)
483
492
  def_node_matcher :lambda_or_proc?, '{lambda? proc?}'
484
493
 
494
+ # @!method global_const?(node = self, name)
485
495
  def_node_matcher :global_const?, '(const {nil? cbase} %1)'
486
496
 
497
+ # @!method class_constructor?(node = self)
487
498
  def_node_matcher :class_constructor?, <<~PATTERN
488
499
  { (send #global_const?({:Class :Module :Struct}) :new ...)
489
500
  (block (send #global_const?({:Class :Module :Struct}) :new ...) ...)}
490
501
  PATTERN
491
502
 
492
503
  # @deprecated Use `:class_constructor?`
504
+ # @!method struct_constructor?(node = self)
493
505
  def_node_matcher :struct_constructor?, <<~PATTERN
494
506
  (block (send #global_const?(:Struct) :new ...) _ $_)
495
507
  PATTERN
496
508
 
509
+ # @!method class_definition?(node = self)
497
510
  def_node_matcher :class_definition?, <<~PATTERN
498
511
  {(class _ _ $_)
499
512
  (sclass _ $_)
500
513
  (block (send #global_const?({:Struct :Class}) :new ...) _ $_)}
501
514
  PATTERN
502
515
 
516
+ # @!method module_definition?(node = self)
503
517
  def_node_matcher :module_definition?, <<~PATTERN
504
518
  {(module _ $_)
505
519
  (block (send #global_const?(:Module) :new ...) _ $_)}
@@ -633,6 +647,7 @@ module RuboCop
633
647
  end
634
648
  end
635
649
 
650
+ # @!method new_class_or_module_block?(node = self)
636
651
  def_node_matcher :new_class_or_module_block?, <<~PATTERN
637
652
  ^(casgn _ _ (block (send (const _ {:Class :Module}) :new) ...))
638
653
  PATTERN
@@ -24,9 +24,9 @@ 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
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module AST
5
+ # A node extension for `dstr` nodes. This will be used
6
+ # in place of a plain node when the builder constructs the AST, making
7
+ # its methods available to all `dstr` nodes within RuboCop.
8
+ class DstrNode < StrNode
9
+ def value
10
+ child_nodes.map do |child|
11
+ child.respond_to?(:value) ? child.value : child.source
12
+ end.join
13
+ end
14
+ end
15
+ end
16
+ end
@@ -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
@@ -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
@@ -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
  (_ _ _ _ ...)]
@@ -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
@@ -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
@@ -236,6 +236,9 @@ module RuboCop
236
236
  when 2.8, 3.0
237
237
  require 'parser/ruby30'
238
238
  Parser::Ruby30
239
+ when 3.1
240
+ require 'parser/ruby31'
241
+ Parser::Ruby31
239
242
  else
240
243
  raise ArgumentError,
241
244
  "RuboCop found unknown Ruby version: #{ruby_version.inspect}"
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module AST
5
5
  module Version
6
- STRING = '1.4.0'
6
+ STRING = '1.7.0'
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: 1.4.0
4
+ version: 1.7.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-01 00:00:00.000000000 Z
13
+ date: 2021-05-28 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,7 +61,6 @@ 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
67
66
  - lib/rubocop/ast/node/and_node.rb
@@ -76,12 +75,14 @@ files:
76
75
  - lib/rubocop/ast/node/const_node.rb
77
76
  - lib/rubocop/ast/node/def_node.rb
78
77
  - lib/rubocop/ast/node/defined_node.rb
78
+ - lib/rubocop/ast/node/dstr_node.rb
79
79
  - lib/rubocop/ast/node/ensure_node.rb
80
80
  - lib/rubocop/ast/node/float_node.rb
81
81
  - lib/rubocop/ast/node/for_node.rb
82
82
  - lib/rubocop/ast/node/forward_args_node.rb
83
83
  - lib/rubocop/ast/node/hash_node.rb
84
84
  - lib/rubocop/ast/node/if_node.rb
85
+ - lib/rubocop/ast/node/in_pattern_node.rb
85
86
  - lib/rubocop/ast/node/index_node.rb
86
87
  - lib/rubocop/ast/node/indexasgn_node.rb
87
88
  - lib/rubocop/ast/node/int_node.rb
@@ -161,7 +162,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
161
162
  requirements:
162
163
  - - ">="
163
164
  - !ruby/object:Gem::Version
164
- version: 2.4.0
165
+ version: 2.5.0
165
166
  required_rubygems_version: !ruby/object:Gem::Requirement
166
167
  requirements:
167
168
  - - ">="
@@ -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