rubocop-ast 1.5.0 → 1.9.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: a4e1919e281d2cc09eca1c8f18effe89cd4c818f7981070e5cb222bde40d6be5
4
- data.tar.gz: e7368a014cc2c9881552f935b6a841357275940e63ee567655f3bb151b118977
3
+ metadata.gz: e5f8cfde2fd4d1e7e8e61926e4bca24ca426274253cff583338688b38810ad8f
4
+ data.tar.gz: a9f1d7174de586df8f9004da23ce67738f43f2ffcc3309983be8cedb23960758
5
5
  SHA512:
6
- metadata.gz: b1c16200ecf6bc69027525ac88c1214d9fb69ce8840307422560ac9c6e314c6d720e278100640da86a406e0d4ae7d229d3f943b4dd5a3446739584baa205336a
7
- data.tar.gz: eafb7d829dd9c17182b43bc9ed091933bcb4ce7904b499a12a79054f76ecc25896917ae4f043f84790c2b2754f6563ab1457ce86fd58b8ddf490f8188263479a
6
+ metadata.gz: baeed38e039ead920084a839a7e216b3567bad48ebe10aa4577c43e666f06cc9156cff4b47a7a9994a27f6a5c9cb4f08c0cd745bc28ce7468e434328e4389c53
7
+ data.tar.gz: 652edd75af0f4d8f4e30c19350a8a6e5cdd95e3fbdd0cd2247a7fb15bdea32fa337548411f75e68cc68445ea218fc9217ad83c442ec0a849c51ef6eb3b09edde
data/lib/rubocop/ast.rb CHANGED
@@ -53,6 +53,7 @@ require_relative 'ast/node/forward_args_node'
53
53
  require_relative 'ast/node/float_node'
54
54
  require_relative 'ast/node/hash_node'
55
55
  require_relative 'ast/node/if_node'
56
+ require_relative 'ast/node/in_pattern_node'
56
57
  require_relative 'ast/node/index_node'
57
58
  require_relative 'ast/node/indexasgn_node'
58
59
  require_relative 'ast/node/int_node'
@@ -49,6 +49,7 @@ module RuboCop
49
49
  float: FloatNode,
50
50
  hash: HashNode,
51
51
  if: IfNode,
52
+ in_pattern: InPatternNode,
52
53
  int: IntNode,
53
54
  index: IndexNode,
54
55
  indexasgn: IndexasgnNode,
@@ -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,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
@@ -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
@@ -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
 
@@ -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)
@@ -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:
@@ -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
@@ -272,14 +280,6 @@ module RuboCop
272
280
  sorted_tokens.bsearch_index { |token| token.end_pos >= end_pos }
273
281
  end
274
282
 
275
- # The tokens list is always sorted by token position, except for cases when heredoc
276
- # is passed as a method argument. In this case tokens are interleaved by
277
- # heredoc contents' tokens.
278
- def sorted_tokens
279
- # Use stable sort.
280
- @sorted_tokens ||= tokens.sort_by.with_index { |token, i| [token.begin_pos, i] }
281
- end
282
-
283
283
  def source_range(range_or_node)
284
284
  if range_or_node.respond_to?(:source_range)
285
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.5.0'
6
+ STRING = '1.9.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.5.0
4
+ version: 1.9.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-05-02 00:00:00.000000000 Z
13
+ date: 2021-08-06 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: parser
@@ -82,6 +82,7 @@ files:
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