rubocop-ast 1.49.1 → 1.51.0

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: aad4bf15828f1db50f117c1a983e1f4c7f0cff233878a7f1a1686416ab21feff
4
- data.tar.gz: 8ac8ad74b061c5fcd733e21420068edc69d0a9bfba532dc766e01dbb80be5c40
3
+ metadata.gz: 852caeb016762e91a5ba54a7f395eca116212e1b44024db303924c7991d1b657
4
+ data.tar.gz: b4dda6fc3370f3edb5e6f24cebee0b19f2ef4f39274071a07e8a0a0f9255305f
5
5
  SHA512:
6
- metadata.gz: 75f3882c819f2e10f7ef893481fbe9feddedfcdc91678c1c695d6ef36d94798dedf5667427cbfc6c022494e7e0c9581f54b4e1dcc9f05f2aea677668fe645b04
7
- data.tar.gz: 94b37b0490cbf1ab81ab0c4c98a5bd8e6037b6ae5210af6ba2cbd23d4a263a3e4cfccad3f99fe621a6ab62d8356fc007fd066adb2d2379e02f5b07580ef2b2d4
6
+ metadata.gz: 07db780992c4c952ce6a32f3dea9e25b86652ebf2c2244adc68048a5a8c4c9fe6afc1dba5450ab68aa76216de794b272ab3c38285f6202f439adf4b70b8a8b24
7
+ data.tar.gz: 16c6acc46fffa394620be118f9fcdebd2eaac6293e9ef206123e1f7c6b03d41f6e09844e49441b3734507b9fd87cf7fe8400911c52edf4f8555af5515bcc1513
@@ -20,6 +20,37 @@ module RuboCop
20
20
  def line_span(exclude_end: false)
21
21
  ::Range.new(first_line, last_line, exclude_end)
22
22
  end
23
+
24
+ # @param [Parser::Source::Range, RuboCop::AST::Node, RuboCop::AST::Token,
25
+ # Parser::Source::Comment] other
26
+ # @return [Boolean] whether this range starts on the same line as `other`
27
+ def same_line?(other)
28
+ other_line = if other.respond_to?(:line)
29
+ other.line
30
+ elsif other.respond_to?(:loc)
31
+ other.loc.line
32
+ end
33
+
34
+ !other_line.nil? && line == other_line
35
+ end
36
+
37
+ # Arbitrarily chosen value, should be enough to cover
38
+ # the most nested source code in real world projects.
39
+ MAX_LINE_BEGINS_REGEX_INDEX = 50
40
+ LINE_BEGINS_REGEX_CACHE = Hash.new do |hash, index|
41
+ hash[index] = /^\s{#{index}}\S/ if index <= MAX_LINE_BEGINS_REGEX_INDEX
42
+ end
43
+ private_constant :MAX_LINE_BEGINS_REGEX_INDEX, :LINE_BEGINS_REGEX_CACHE
44
+
45
+ # @return [Boolean] whether this range begins its line, i.e. is preceded
46
+ # only by whitespace
47
+ def begins_its_line?
48
+ if (regex = LINE_BEGINS_REGEX_CACHE[column])
49
+ source_line.match?(regex)
50
+ else
51
+ source_line.index(/\S/) == column
52
+ end
53
+ end
23
54
  end
24
55
  end
25
56
  end
@@ -2,9 +2,9 @@
2
2
 
3
3
  module RuboCop
4
4
  module AST
5
- # A node extension for `until` nodes. This will be used in place of a plain
5
+ # A node extension for `and` nodes. This will be used in place of a plain
6
6
  # node when the builder constructs the AST, making its methods available
7
- # to all `until` nodes within RuboCop.
7
+ # to all `and` nodes within RuboCop.
8
8
  class AndNode < Node
9
9
  include BinaryOperatorNode
10
10
  include PredicateOperatorNode
@@ -130,7 +130,7 @@ module RuboCop
130
130
  #
131
131
  # @return [Boolean] whether the `block` literal is on a single line
132
132
  def single_line?
133
- loc.begin.line == loc.end.line
133
+ loc.begin.same_line?(loc.end)
134
134
  end
135
135
 
136
136
  # Checks whether this is a multiline block. This is overridden here
@@ -17,7 +17,7 @@ module RuboCop
17
17
 
18
18
  # @deprecated Use `in_pattern_branches.each`
19
19
  def each_in_pattern(&block)
20
- return in_pattern_branches.to_enum(__method__) unless block
20
+ return to_enum(__method__) unless block
21
21
 
22
22
  in_pattern_branches.each(&block)
23
23
 
@@ -17,7 +17,7 @@ module RuboCop
17
17
 
18
18
  # @deprecated Use `when_branches.each`
19
19
  def each_when(&block)
20
- return when_branches.to_enum(__method__) unless block
20
+ return to_enum(__method__) unless block
21
21
 
22
22
  when_branches.each(&block)
23
23
 
@@ -8,7 +8,7 @@ module RuboCop
8
8
  class ForNode < Node
9
9
  # Returns the keyword of the `for` statement as a string.
10
10
  #
11
- # @return [String] the keyword of the `until` statement
11
+ # @return [String] the keyword of the `for` statement
12
12
  def keyword
13
13
  'for'
14
14
  end
@@ -56,10 +56,10 @@ module RuboCop
56
56
  #
57
57
  # @return [self] if a block is given
58
58
  # @return [Enumerator] if no block is given
59
- def each_key(&block)
60
- return pairs.map(&:key).to_enum unless block
59
+ def each_key
60
+ return to_enum(__method__) unless block_given?
61
61
 
62
- pairs.map(&:key).each(&block)
62
+ each_child_node(:pair) { |pair| yield pair.key }
63
63
 
64
64
  self
65
65
  end
@@ -80,10 +80,10 @@ module RuboCop
80
80
  #
81
81
  # @return [self] if a block is given
82
82
  # @return [Enumerator] if no block is given
83
- def each_value(&block)
84
- return pairs.map(&:value).to_enum unless block
83
+ def each_value
84
+ return to_enum(__method__) unless block_given?
85
85
 
86
- pairs.map(&:value).each(&block)
86
+ each_child_node(:pair) { |pair| yield pair.value }
87
87
 
88
88
  self
89
89
  end
@@ -22,7 +22,9 @@ module RuboCop
22
22
  #
23
23
  # @return [Boolean] whether the node is an `unless` statement
24
24
  def unless?
25
- keyword == 'unless'
25
+ # Compare the keyword length to avoid allocating the keyword source;
26
+ # a non-ternary `if` node's keyword can only be `if`, `elsif` or `unless`.
27
+ !ternary? && loc.keyword.length == 6
26
28
  end
27
29
 
28
30
  # Checks whether the `if` node has an `then` clause.
@@ -120,7 +122,7 @@ module RuboCop
120
122
  # @return [Node] the truthy branch node of the `if` node
121
123
  # @return [nil] if the truthy branch is empty
122
124
  def if_branch
123
- node_parts[1]
125
+ unless? ? children[2] : children[1]
124
126
  end
125
127
 
126
128
  # Returns the branch of the `if` node that gets evaluated when its
@@ -131,7 +133,7 @@ module RuboCop
131
133
  # @return [Node] the falsey branch node of the `if` node
132
134
  # @return [nil] when there is no else branch
133
135
  def else_branch
134
- node_parts[2]
136
+ unless? ? children[1] : children[2]
135
137
  end
136
138
 
137
139
  # Custom destructuring method. This is used to normalize the branches
@@ -139,13 +141,7 @@ module RuboCop
139
141
  #
140
142
  # @return [Array<Node>] the different parts of the `if` statement
141
143
  def node_parts
142
- if unless?
143
- condition, false_branch, true_branch = *self
144
- else
145
- condition, true_branch, false_branch = *self
146
- end
147
-
148
- [condition, true_branch, false_branch]
144
+ [condition, if_branch, else_branch]
149
145
  end
150
146
 
151
147
  # Returns an array of all the branches in the conditional statement.
@@ -169,7 +165,7 @@ module RuboCop
169
165
 
170
166
  # @deprecated Use `branches.each`
171
167
  def each_branch(&block)
172
- return branches.to_enum(__method__) unless block
168
+ return to_enum(__method__) unless block
173
169
 
174
170
  branches.each(&block)
175
171
  end
@@ -11,7 +11,7 @@ module RuboCop
11
11
  #
12
12
  # @return [Boolean] whether the condition is on a single line
13
13
  def single_line_condition?
14
- loc.keyword.line == condition.source_range.line
14
+ loc.keyword.same_line?(condition)
15
15
  end
16
16
 
17
17
  # Checks whether the condition of the node is written on more than
@@ -22,12 +22,12 @@ module RuboCop
22
22
  !single_line_condition?
23
23
  end
24
24
 
25
- # Returns the condition of the node. This works together with each node's
26
- # custom destructuring method to select the correct part of the node.
25
+ # Returns the condition of the node. The condition is the first child
26
+ # for all conditional node types, including normalized `unless` nodes.
27
27
  #
28
28
  # @return [Node, nil] the condition of the node
29
29
  def condition
30
- node_parts[0]
30
+ children[0]
31
31
  end
32
32
 
33
33
  # Returns the body associated with the condition. This works together with
@@ -25,7 +25,7 @@ module RuboCop
25
25
  children.each do |child|
26
26
  next unless child.is_a?(::AST::Node)
27
27
 
28
- yield child if types.empty? || child.type?(*types)
28
+ yield child if types.empty? || child.type_in?(types)
29
29
  end
30
30
 
31
31
  self
@@ -73,6 +73,31 @@ module RuboCop
73
73
  each_descendant.to_a
74
74
  end
75
75
 
76
+ # Checks whether any descendant node matches. The types and the optional
77
+ # block are interpreted the same way as for `each_descendant`.
78
+ #
79
+ # @overload any_descendant?
80
+ # Checks whether there is any descendant node.
81
+ # @overload any_descendant?(type, ...)
82
+ # Checks whether there is any descendant node matching any of the types.
83
+ # @param [Symbol] type a node type
84
+ # @yieldparam [Node] node descendant nodes matching the types, if a block is given
85
+ # @return [Boolean] whether any (matching) descendant node was found
86
+ # (for which the block returned a truthy value, if given)
87
+ def any_descendant?(*types)
88
+ if block_given?
89
+ each_descendant(*types) do |descendant|
90
+ return true if yield(descendant)
91
+ end
92
+ else
93
+ each_descendant(*types) do # rubocop:disable Lint/UnreachableLoop
94
+ return true
95
+ end
96
+ end
97
+
98
+ false
99
+ end
100
+
76
101
  # Calls the given block for the receiver and each descendant node in
77
102
  # depth-first order.
78
103
  # If no block is given, an `Enumerator` is returned.
@@ -95,7 +120,7 @@ module RuboCop
95
120
  def each_node(*types, &block)
96
121
  return to_enum(__method__, *types) unless block
97
122
 
98
- yield self if types.empty? || type?(*types)
123
+ yield self if types.empty? || type_in?(types)
99
124
 
100
125
  visit_descendants(types, &block)
101
126
 
@@ -105,11 +130,28 @@ module RuboCop
105
130
  protected
106
131
 
107
132
  def visit_descendants(types, &block)
133
+ if types.empty?
134
+ visit_all_descendants(&block)
135
+ else
136
+ visit_descendants_of_types(types, &block)
137
+ end
138
+ end
139
+
140
+ def visit_all_descendants(&block)
141
+ children.each do |child|
142
+ next unless child.is_a?(::AST::Node)
143
+
144
+ yield child
145
+ child.visit_all_descendants(&block)
146
+ end
147
+ end
148
+
149
+ def visit_descendants_of_types(types, &block)
108
150
  children.each do |child|
109
151
  next unless child.is_a?(::AST::Node)
110
152
 
111
- yield child if types.empty? || child.type?(*types)
112
- child.visit_descendants(types, &block)
153
+ yield child if child.type_in?(types)
154
+ child.visit_descendants_of_types(types, &block)
113
155
  end
114
156
  end
115
157
  end
@@ -99,9 +99,8 @@ module RuboCop
99
99
 
100
100
  def valid_argument_types?
101
101
  [first, second].all? do |argument|
102
- # rubocop:disable InternalAffairs/NodeTypeMultiplePredicates
102
+ # rubocop:disable-next InternalAffairs/NodeTypeMultiplePredicates
103
103
  argument.pair_type? || argument.kwsplat_type?
104
- # rubocop:enable InternalAffairs/NodeTypeMultiplePredicates
105
104
  end
106
105
  end
107
106
 
@@ -165,7 +165,7 @@ module RuboCop
165
165
  #
166
166
  # @return [Boolean] whether the dispatched method has a block
167
167
  def block_literal?
168
- parent&.any_block_type? && eql?(parent.send_node)
168
+ parent&.any_block_type? && equal?(parent.send_node)
169
169
  end
170
170
 
171
171
  # Checks whether this node is an arithmetic operation
@@ -140,7 +140,7 @@ module RuboCop
140
140
  #
141
141
  # @return [Boolean] whether the method is an assignment
142
142
  def assignment_method?
143
- !comparison_method? && method_name.to_s.end_with?('=')
143
+ !comparison_method? && method_name.end_with?('=')
144
144
  end
145
145
 
146
146
  # Checks whether the method is an enumerator method.
@@ -148,7 +148,7 @@ module RuboCop
148
148
  # @return [Boolean] whether the method is an enumerator
149
149
  def enumerator_method?
150
150
  ENUMERATOR_METHODS.include?(method_name) ||
151
- method_name.to_s.start_with?('each_')
151
+ method_name.start_with?('each_')
152
152
  end
153
153
 
154
154
  # Checks whether the method is an Enumerable method.
@@ -162,14 +162,14 @@ module RuboCop
162
162
  #
163
163
  # @return [Boolean] whether the method is a predicate method
164
164
  def predicate_method?
165
- method_name.to_s.end_with?('?')
165
+ method_name.end_with?('?')
166
166
  end
167
167
 
168
168
  # Checks whether the method is a bang method.
169
169
  #
170
170
  # @return [Boolean] whether the method is a bang method
171
171
  def bang_method?
172
- method_name.to_s.end_with?('!')
172
+ method_name.end_with?('!')
173
173
  end
174
174
 
175
175
  # Checks whether the method is a camel case method,
@@ -177,7 +177,7 @@ module RuboCop
177
177
  #
178
178
  # @return [Boolean] whether the method is a camel case method
179
179
  def camel_case_method?
180
- method_name.to_s =~ /\A[A-Z]/
180
+ method_name.match?(/\A[A-Z]/)
181
181
  end
182
182
 
183
183
  # Checks whether the *explicit* receiver of this node is `self`.
@@ -7,7 +7,7 @@ module RuboCop
7
7
  SIGN_REGEX = /\A[+-]/.freeze
8
8
  private_constant :SIGN_REGEX
9
9
 
10
- # Checks whether this is literal has a sign.
10
+ # Checks whether this literal has a sign.
11
11
  #
12
12
  # @example
13
13
  #
@@ -32,7 +32,7 @@ module RuboCop
32
32
  end
33
33
 
34
34
  # Returns the delimiter of the `pair` as a string. Returns `=>` for a
35
- # colon delimited `pair` and `:` for a hash rocket delimited `pair`.
35
+ # hash rocket delimited `pair` and `:` for a colon delimited `pair`.
36
36
  #
37
37
  # @param [Boolean] with_spacing whether to include spacing
38
38
  # @return [String] the delimiter of the `pair`
@@ -2,7 +2,7 @@
2
2
 
3
3
  module RuboCop
4
4
  module AST
5
- # A node extension for `sym` nodes. This will be used in place of a
5
+ # A node extension for `sym` nodes. This will be used in place of a
6
6
  # plain node when the builder constructs the AST, making its methods
7
7
  # available to all `sym` nodes within RuboCop.
8
8
  class SymbolNode < Node
@@ -15,7 +15,7 @@ module RuboCop
15
15
 
16
16
  # @deprecated Use `conditions.each`
17
17
  def each_condition(&block)
18
- return conditions.to_enum(__method__) unless block
18
+ return to_enum(__method__) unless block
19
19
 
20
20
  conditions.each(&block)
21
21
 
@@ -24,9 +24,9 @@ module RuboCop
24
24
  'until'
25
25
  end
26
26
 
27
- # Checks whether the `until` node has a `do` keyword.
27
+ # Checks whether the `while` node has a `do` keyword.
28
28
  #
29
- # @return [Boolean] whether the `until` node has a `do` keyword
29
+ # @return [Boolean] whether the `while` node has a `do` keyword
30
30
  def do?
31
31
  loc_is?(:begin, 'do')
32
32
  end
@@ -76,6 +76,13 @@ module RuboCop
76
76
  OPERATOR_KEYWORDS = %i[and or].to_set.freeze
77
77
  # @api private
78
78
  SPECIAL_KEYWORDS = %w[__FILE__ __LINE__ __ENCODING__].to_set.freeze
79
+ # The node types which could have a source matching `SPECIAL_KEYWORDS`:
80
+ # `__FILE__` parses as `str`, `__LINE__` as `int` and `__ENCODING__` as
81
+ # `const` (or as their own node types when the builder emits them);
82
+ # `str` and `sym` also cover word/symbol array elements, string parts
83
+ # and hash labels spelling out one of these keywords.
84
+ SPECIAL_KEYWORD_TYPES = %i[str sym int const __FILE__ __LINE__ __ENCODING__].to_set.freeze
85
+ private_constant :SPECIAL_KEYWORD_TYPES
79
86
 
80
87
  LITERAL_RECURSIVE_METHODS = (COMPARISON_OPERATORS + %i[* ! <=>]).freeze
81
88
  LITERAL_RECURSIVE_TYPES = (OPERATOR_KEYWORDS + COMPOSITE_LITERALS + %i[begin pair]).freeze
@@ -172,6 +179,13 @@ module RuboCop
172
179
  # Allows specific single node types, as well as "grouped" types
173
180
  # (e.g. `:boolean` for `:true` or `:false`)
174
181
  def type?(*types)
182
+ type_in?(types)
183
+ end
184
+
185
+ # Non-splatting variant of `type?`, used by the traversal hot paths to
186
+ # avoid allocating an array per visited node.
187
+ # @api private
188
+ def type_in?(types)
175
189
  return true if types.include?(type)
176
190
 
177
191
  group_type = GROUP_FOR_TYPE[type]
@@ -287,7 +301,7 @@ module RuboCop
287
301
  # Some node types override this with their own custom
288
302
  # destructuring method.
289
303
  #
290
- # @return [Array<Node>] the different parts of the ndde
304
+ # @return [Array<Node>] the different parts of the node
291
305
  alias node_parts to_a
292
306
 
293
307
  # Calls the given block for each ancestor node from parent to root.
@@ -503,7 +517,7 @@ module RuboCop
503
517
  end
504
518
 
505
519
  def special_keyword?
506
- SPECIAL_KEYWORDS.include?(source)
520
+ SPECIAL_KEYWORD_TYPES.include?(type) && SPECIAL_KEYWORDS.include?(source)
507
521
  end
508
522
 
509
523
  def operator_keyword?
@@ -519,11 +533,35 @@ module RuboCop
519
533
  end
520
534
 
521
535
  def chained?
522
- parent&.call_type? && eql?(parent.receiver)
536
+ parent&.call_type? && equal?(parent.receiver)
537
+ end
538
+
539
+ # Returns the receiver at the base of this node's call chain, e.g. the
540
+ # `if` node for `(cond ? a : b).foo.bar`. Returns `self` for non-call
541
+ # nodes and `nil` when the chain starts with a receiverless call
542
+ # (e.g. for `foo.bar.baz`).
543
+ #
544
+ # @return [Node, nil] the node at the base of this call chain
545
+ def first_part_of_call_chain
546
+ node = self
547
+
548
+ while node
549
+ if node.call_type?
550
+ node = node.receiver
551
+ elsif node.any_block_type?
552
+ node = node.send_node
553
+ else
554
+ break
555
+ end
556
+ end
557
+
558
+ node
523
559
  end
524
560
 
525
561
  def argument?
526
- parent&.send_type? && parent.arguments.include?(self)
562
+ return false unless parent&.send_type?
563
+
564
+ parent.arguments.any? { |argument| argument.equal?(self) }
527
565
  end
528
566
 
529
567
  def any_def_type?
@@ -695,9 +733,16 @@ module RuboCop
695
733
  def visit_ancestors(types)
696
734
  last_node = self
697
735
 
698
- while (current_node = last_node.parent)
699
- yield current_node if types.empty? || current_node.type?(*types)
700
- last_node = current_node
736
+ if types.empty?
737
+ while (current_node = last_node.parent)
738
+ yield current_node
739
+ last_node = current_node
740
+ end
741
+ else
742
+ while (current_node = last_node.parent)
743
+ yield current_node if current_node.type_in?(types)
744
+ last_node = current_node
745
+ end
701
746
  end
702
747
  end
703
748
 
@@ -15,12 +15,11 @@ module RuboCop
15
15
  @visit = {}
16
16
  end
17
17
 
18
- # rubocop:disable Naming/PredicateMethod
18
+ # rubocop:disable-next Naming/PredicateMethod
19
19
  def enter(node_id)
20
20
  @visit[node_id] = false
21
21
  true
22
22
  end
23
- # rubocop:enable Naming/PredicateMethod
24
23
 
25
24
  def success(node_id)
26
25
  @visit[node_id] = true
@@ -72,8 +72,7 @@ module RuboCop
72
72
 
73
73
  def visit_intersection
74
74
  multiple_access(:intersection) do
75
- node.children.map { |child| compile(child) }
76
- .join(' && ')
75
+ node.children.map { |child| compile(child) }.join(' && ')
77
76
  end
78
77
  end
79
78
 
@@ -29,7 +29,7 @@ module RuboCop
29
29
  end
30
30
 
31
31
  def compile_sequence
32
- # rubocop:disable Layout/CommentIndentation
32
+ # rubocop:disable-next Layout/CommentIndentation
33
33
  compiler.with_temp_variables do |cur_child, cur_index, previous_index|
34
34
  @cur_child_var = cur_child # To hold the current child node
35
35
  @cur_index_var = cur_index # To hold the current child index (always >= 0)
@@ -51,13 +51,36 @@ module RuboCop
51
51
  # Must be true if `@cur_index` is `:variadic_mode`
52
52
  compile_terms
53
53
  end
54
- # rubocop:enable Layout/CommentIndentation
55
54
  end
56
55
 
57
56
  private
58
57
 
59
58
  private :compile # Not meant to be called from outside
60
59
 
60
+ def compile_terms(children = @seq.children, last_arity = 0..0)
61
+ arities = remaining_arities(children, last_arity)
62
+ total_arity = arities.shift
63
+ guard = compile_child_nb_guard(total_arity)
64
+ return guard if children.empty?
65
+
66
+ @remaining_arity = total_arity
67
+ terms = children.map do |child|
68
+ use_index_from_end
69
+ @remaining_arity = arities.shift
70
+ handle_prev { compile(child) }
71
+ end
72
+ [guard, terms].join(" &&\n")
73
+ end
74
+
75
+ # yield `sync_code` iff not already in sync
76
+ def sync
77
+ return if @in_sync
78
+
79
+ code = compile_loop_advance("= #{compile_cur_index}")
80
+ @in_sync = true
81
+ yield code
82
+ end
83
+
61
84
  # Single node patterns are all handled here
62
85
  def visit_other_type
63
86
  access = case @cur_index
@@ -228,30 +251,6 @@ module RuboCop
228
251
  code
229
252
  end
230
253
 
231
- def compile_terms(children = @seq.children, last_arity = 0..0)
232
- arities = remaining_arities(children, last_arity)
233
- total_arity = arities.shift
234
- guard = compile_child_nb_guard(total_arity)
235
- return guard if children.empty?
236
-
237
- @remaining_arity = total_arity
238
- terms = children.map do |child|
239
- use_index_from_end
240
- @remaining_arity = arities.shift
241
- handle_prev { compile(child) }
242
- end
243
- [guard, terms].join(" &&\n")
244
- end
245
-
246
- # yield `sync_code` iff not already in sync
247
- def sync
248
- return if @in_sync
249
-
250
- code = compile_loop_advance("= #{compile_cur_index}")
251
- @in_sync = true
252
- yield code
253
- end
254
-
255
254
  # @api private
256
255
  attr_reader :in_sync, :cur_index
257
256
 
@@ -48,7 +48,7 @@ module RuboCop
48
48
  end
49
49
 
50
50
  def def_helper(base, method_name, **defaults)
51
- location = caller_locations(3, 1).first
51
+ location = definition_location || caller_locations(3, 1).first
52
52
  unless defaults.empty?
53
53
  call = :"without_defaults_#{method_name}"
54
54
  base.send :define_method, method_name, &wrapping_block(call, **defaults)
@@ -26,6 +26,18 @@ module RuboCop
26
26
  class NodePattern
27
27
  class Invalid < StandardError; end
28
28
 
29
+ # When changed to `true`, methods defined with the `Macros` compile
30
+ # their pattern on first invocation instead of at definition time.
31
+ # Compiling the patterns is a significant part of loading a large body
32
+ # of cops, and most of them are never invoked in a given run.
33
+ # Note that with lazy compilation an invalid pattern raises
34
+ # `NodePattern::Invalid` when the method is first called, not when it
35
+ # is defined; set to `false` (default) to get errors at definition-time.
36
+ class << self
37
+ attr_accessor :lazy_compilation
38
+ end
39
+ self.lazy_compilation = false
40
+
29
41
  # Helpers for defining methods based on a pattern string
30
42
  module Macros
31
43
  # Define a method which applies a pattern to an AST node
@@ -36,7 +48,12 @@ module RuboCop
36
48
  # If the node matches, and no block is provided, the new method will
37
49
  # return the captures, or `true` if there were none.
38
50
  def def_node_matcher(method_name, pattern_str, **keyword_defaults)
39
- NodePattern.new(pattern_str).def_node_matcher(self, method_name, **keyword_defaults)
51
+ if NodePattern.lazy_compilation
52
+ def_node_pattern_method_lazily(:def_node_matcher, method_name, pattern_str,
53
+ keyword_defaults, caller_locations(1, 1).first)
54
+ else
55
+ NodePattern.new(pattern_str).def_node_matcher(self, method_name, **keyword_defaults)
56
+ end
40
57
  end
41
58
 
42
59
  # Define a method which recurses over the descendants of an AST node,
@@ -46,7 +63,41 @@ module RuboCop
46
63
  # as soon as it finds a descendant which matches. Otherwise, it will
47
64
  # yield all descendants which match.
48
65
  def def_node_search(method_name, pattern_str, **keyword_defaults)
49
- NodePattern.new(pattern_str).def_node_search(self, method_name, **keyword_defaults)
66
+ if NodePattern.lazy_compilation
67
+ def_node_pattern_method_lazily(:def_node_search, method_name, pattern_str,
68
+ keyword_defaults, caller_locations(1, 1).first)
69
+ else
70
+ NodePattern.new(pattern_str).def_node_search(self, method_name, **keyword_defaults)
71
+ end
72
+ end
73
+
74
+ private
75
+
76
+ # Defines a stub that compiles the pattern and replaces itself on
77
+ # first invocation. The compiled method is invoked through the owner's
78
+ # instance method rather than a regular dispatch, so that overrides
79
+ # calling `super` keep working.
80
+ def def_node_pattern_method_lazily(definer, method_name, pattern_str, keyword_defaults,
81
+ location)
82
+ base = self
83
+
84
+ define_method(method_name) do |*args, **kwargs, &block|
85
+ # Remove the stub before defining the compiled method in its
86
+ # place, so that no method redefinition warning is emitted.
87
+ begin
88
+ base.send(:remove_method, method_name)
89
+ rescue NameError
90
+ nil # the method was already compiled concurrently
91
+ end
92
+
93
+ pattern = NodePattern.new(pattern_str)
94
+ pattern.definition_location = location
95
+ pattern.public_send(definer, base, method_name, **keyword_defaults)
96
+
97
+ base.instance_method(method_name).bind_call(self, *args, **kwargs, &block)
98
+ end
99
+
100
+ method_name
50
101
  end
51
102
  end
52
103
 
@@ -73,6 +124,11 @@ module RuboCop
73
124
 
74
125
  attr_reader :pattern, :ast, :match_code
75
126
 
127
+ # The location to attribute methods defined from this pattern to, when
128
+ # it can't be determined from the call stack (e.g. for lazily compiled
129
+ # patterns). Used by `MethodDefiner`.
130
+ attr_accessor :definition_location
131
+
76
132
  def_delegators :@compiler, :captures, :named_parameters, :positional_parameters
77
133
 
78
134
  def initialize(str, compiler: Compiler.new)
@@ -25,6 +25,44 @@ module RuboCop
25
25
  end
26
26
  end
27
27
 
28
+ # Extends the prism translation parsers so that the conversion of tokens
29
+ # into the `parser` gem's format is deferred until the tokens are first
30
+ # accessed. Building the tokens is a significant part of the translation
31
+ # cost, and not every caller needs them.
32
+ # @api private
33
+ module PrismLazyTokens
34
+ # Same contract as `Parser::Base#tokenize`, except the tokens are
35
+ # returned as a callable that performs the conversion when invoked.
36
+ def tokenize_deferred(source_buffer)
37
+ @source_buffer = source_buffer
38
+ source = source_buffer.source
39
+
40
+ offset_cache = build_offset_cache(source)
41
+ result = unwrap(@parser.parse_lex(source, **prism_options), offset_cache)
42
+
43
+ program, tokens = result.value
44
+ ast = build_ast(program, offset_cache) if result.success?
45
+ comments = build_comments(result.comments, offset_cache)
46
+
47
+ [ast, comments, deferred_tokens(source_buffer, tokens, offset_cache)]
48
+ ensure
49
+ @source_buffer = nil
50
+ end
51
+
52
+ private
53
+
54
+ def deferred_tokens(source_buffer, tokens, offset_cache)
55
+ lambda do
56
+ @source_buffer = source_buffer
57
+ begin
58
+ build_tokens(tokens, offset_cache)
59
+ ensure
60
+ @source_buffer = nil
61
+ end
62
+ end
63
+ end
64
+ end
65
+
28
66
  # ProcessedSource contains objects which are generated by Parser
29
67
  # and other information such as disabled lines for cops.
30
68
  # It also provides a convenient way to access source lines.
@@ -38,7 +76,7 @@ module RuboCop
38
76
  PARSER_ENGINES = %i[default parser_whitequark parser_prism].freeze
39
77
  private_constant :PARSER_ENGINES
40
78
 
41
- attr_reader :path, :buffer, :ast, :comments, :tokens, :diagnostics,
79
+ attr_reader :path, :buffer, :ast, :comments, :diagnostics,
42
80
  :parser_error, :raw_source, :ruby_version, :parser_engine
43
81
 
44
82
  def self.from_file(path, ruby_version, parser_engine: :default)
@@ -46,6 +84,13 @@ module RuboCop
46
84
  new(file, ruby_version, path, parser_engine: parser_engine)
47
85
  end
48
86
 
87
+ # Subclasses of the prism translation parsers with lazily built tokens.
88
+ # @api private
89
+ def self.lazy_tokens_parser_class(base)
90
+ @lazy_tokens_parser_classes ||= {}
91
+ @lazy_tokens_parser_classes[base] ||= Class.new(base) { include PrismLazyTokens }
92
+ end
93
+
49
94
  def initialize(
50
95
  source, ruby_version, path = nil, parser_engine: :default, prism_result: nil
51
96
  )
@@ -54,7 +99,7 @@ module RuboCop
54
99
  # Defaults source encoding to UTF-8, regardless of the encoding it has
55
100
  # been read with, which could be non-utf8 depending on the default
56
101
  # external encoding.
57
- (+source).force_encoding(Encoding::UTF_8) unless source.encoding == Encoding::UTF_8
102
+ source = (+source).force_encoding(Encoding::UTF_8) unless source.encoding == Encoding::UTF_8
58
103
 
59
104
  @raw_source = source
60
105
  @path = path
@@ -77,14 +122,13 @@ module RuboCop
77
122
  def lines
78
123
  @lines ||= begin
79
124
  all_lines = @buffer.source_lines
80
- last_token_line = tokens.any? ? tokens.last.line : all_lines.size
81
- result = []
82
- all_lines.each_with_index do |line, ix|
83
- break if ix >= last_token_line && line == '__END__'
84
-
85
- result << line
125
+ if all_lines.include?('__END__')
126
+ trim_lines_after_data_marker(all_lines)
127
+ else
128
+ # Don't consult the tokens (which may have to be built first) when
129
+ # the source can't contain a data section.
130
+ all_lines.dup
86
131
  end
87
- result
88
132
  end
89
133
  end
90
134
 
@@ -100,7 +144,7 @@ module RuboCop
100
144
 
101
145
  # Raw source checksum for tracking infinite loops.
102
146
  def checksum
103
- Digest::SHA1.hexdigest(@raw_source)
147
+ @checksum ||= Digest::SHA1.hexdigest(@raw_source)
104
148
  end
105
149
 
106
150
  # @deprecated Use `comments.each`
@@ -173,6 +217,8 @@ module RuboCop
173
217
  end
174
218
 
175
219
  def preceding_line(token)
220
+ return nil if token.line < 2
221
+
176
222
  lines[token.line - 2]
177
223
  end
178
224
 
@@ -191,6 +237,19 @@ module RuboCop
191
237
  .length
192
238
  end
193
239
 
240
+ # The tokens of the source. With the prism engine the tokens are built
241
+ # lazily on first access, since their conversion is costly and not
242
+ # every caller needs them.
243
+ def tokens
244
+ @tokens ||= begin
245
+ tokens = parser_tokens.map { |t| Token.from_parser_token(t) }
246
+ # The parser tokens are no longer needed, and the deferred conversion
247
+ # holds on to the parser instance, so release both.
248
+ @parser_tokens = nil
249
+ tokens
250
+ end
251
+ end
252
+
194
253
  def tokens_within(range_or_node)
195
254
  begin_index = first_token_index(range_or_node)
196
255
  end_index = last_token_index(range_or_node)
@@ -205,16 +264,48 @@ module RuboCop
205
264
  sorted_tokens[last_token_index(range_or_node)]
206
265
  end
207
266
 
208
- # The tokens list is always sorted by token position, except for cases when heredoc
209
- # is passed as a method argument. In this case tokens are interleaved by
210
- # heredoc contents' tokens.
267
+ # The tokens list is out of order when a heredoc is passed as a method
268
+ # argument (the heredoc contents' tokens are interleaved) and, more
269
+ # commonly, whenever the source contains comments, whose tokens the
270
+ # lexer emits out of band.
211
271
  def sorted_tokens
212
- # Use stable sort.
213
- @sorted_tokens ||= tokens.sort_by.with_index { |token, i| [token.begin_pos, i] }
272
+ # Callers only ever read from the returned array, so it is safe to
273
+ # reuse `tokens` as is when it's already in order.
274
+ @sorted_tokens ||= if tokens_sorted?
275
+ tokens
276
+ else
277
+ sort_tokens
278
+ end
214
279
  end
215
280
 
216
281
  private
217
282
 
283
+ def tokens_sorted?
284
+ # `each_cons` allocates an array per pair, which would defeat the
285
+ # point of checking before sorting.
286
+ (1...tokens.size).all? { |i| tokens[i - 1].begin_pos <= tokens[i].begin_pos }
287
+ end
288
+
289
+ def sort_tokens
290
+ # Combine the position and the index into a single integer to get a
291
+ # stable sort without allocating an array per token for the key.
292
+ size = tokens.size
293
+ tokens.sort_by.with_index { |token, i| (token.begin_pos * size) + i }
294
+ end
295
+
296
+ # `__END__` only starts a data section when it isn't nested inside a
297
+ # string or heredoc, which is what the last token's line disambiguates.
298
+ def trim_lines_after_data_marker(all_lines)
299
+ last_token_line = tokens.any? ? tokens.last.line : all_lines.size
300
+ result = []
301
+ all_lines.each_with_index do |line, ix|
302
+ break if ix >= last_token_line && line == '__END__'
303
+
304
+ result << line
305
+ end
306
+ result
307
+ end
308
+
218
309
  def comment_index
219
310
  @comment_index ||= {}.tap do |hash|
220
311
  comments.each { |c| hash[c.location.line] = c }
@@ -222,8 +313,7 @@ module RuboCop
222
313
  end
223
314
 
224
315
  def parse(source, ruby_version, parser_engine, prism_result)
225
- buffer_name = @path || STRING_SOURCE_NAME
226
- @buffer = Parser::Source::Buffer.new(buffer_name, 1)
316
+ @buffer = Parser::Source::Buffer.new(@path || STRING_SOURCE_NAME, 1)
227
317
 
228
318
  begin
229
319
  @buffer.source = source
@@ -237,26 +327,60 @@ module RuboCop
237
327
 
238
328
  parser = create_parser(ruby_version, parser_engine, prism_result)
239
329
 
240
- @ast, @comments, @tokens = tokenize(parser)
330
+ @ast, @comments, tokens = tokenize(parser)
331
+ store_tokens(tokens)
332
+ end
333
+
334
+ # The tokens may be an already converted array, or a deferred conversion
335
+ # to be performed when the tokens are first accessed.
336
+ def store_tokens(tokens)
337
+ if tokens.is_a?(Proc)
338
+ @deferred_parser_tokens = tokens
339
+ else
340
+ @parser_tokens = tokens
341
+ end
241
342
  end
242
343
 
243
344
  def tokenize(parser)
244
345
  begin
245
- ast, comments, tokens = parser.tokenize(@buffer)
346
+ ast, comments, tokens = parse_and_lex(parser)
246
347
  ast ||= nil # force `false` to `nil`, see https://github.com/whitequark/parser/pull/722
247
348
  rescue Parser::SyntaxError
248
349
  # All errors are in diagnostics. No need to handle exception.
249
350
  comments = []
250
351
  tokens = []
352
+ rescue StandardError => e
353
+ # The parser gem may fail on invalid sources with an exception other than `Parser::SyntaxError`
354
+ # and without a diagnostic, e.g. with `RegexpError` for `/\c\xFF/ =~ ""`. Keep the exception
355
+ # so that it can be reported instead of crashing the caller.
356
+ @parser_error = e
357
+ comments = []
358
+ tokens = []
251
359
  end
252
360
 
253
361
  ast&.complete!
254
- tokens.map! { |t| Token.from_parser_token(t) }
255
362
 
256
363
  [ast, comments, tokens]
257
364
  end
258
365
 
259
- # rubocop:disable Lint/FloatComparison, Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
366
+ def parse_and_lex(parser)
367
+ if parser.respond_to?(:tokenize_deferred)
368
+ parser.tokenize_deferred(@buffer)
369
+ else
370
+ parser.tokenize(@buffer)
371
+ end
372
+ end
373
+
374
+ def parser_tokens
375
+ if @deferred_parser_tokens
376
+ @parser_tokens = @deferred_parser_tokens.call
377
+ @deferred_parser_tokens = nil
378
+ end
379
+
380
+ @parser_tokens
381
+ end
382
+
383
+ # rubocop:disable-next Lint/FloatComparison, Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
260
384
  def parser_class(ruby_version, parser_engine)
261
385
  case parser_engine
262
386
  when :parser_whitequark
@@ -323,7 +447,6 @@ module RuboCop
323
447
  end
324
448
  end
325
449
  end
326
- # rubocop:enable Lint/FloatComparison, Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
327
450
 
328
451
  def builder_class(parser_engine)
329
452
  case parser_engine
@@ -334,12 +457,15 @@ module RuboCop
334
457
  end
335
458
  end
336
459
 
337
- # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
460
+ # rubocop:disable-next Metrics/AbcSize, Metrics/MethodLength
338
461
  def create_parser(ruby_version, parser_engine, prism_result)
339
462
  builder = builder_class(parser_engine).new
340
463
 
341
464
  parser_class = parser_class(ruby_version, parser_engine)
342
465
 
466
+ parser_class = self.class.lazy_tokens_parser_class(parser_class) if
467
+ parser_engine == :parser_prism
468
+
343
469
  parser_instance = if parser_engine == :parser_prism && prism_result
344
470
  # NOTE: Since it is intended for use with Ruby LSP, it targets only Prism.
345
471
  # If there is no reuse of a pre-parsed result, such as in Ruby LSP,
@@ -362,7 +488,6 @@ module RuboCop
362
488
  end
363
489
  end
364
490
  end
365
- # rubocop:enable Metrics/AbcSize, Metrics/MethodLength
366
491
 
367
492
  def normalize_parser_engine(parser_engine, ruby_version)
368
493
  parser_engine = parser_engine.to_sym
@@ -378,11 +503,11 @@ module RuboCop
378
503
  end
379
504
  end
380
505
 
381
- # The Parser gem does not support Ruby 3.5 or later.
382
- # It is also not fully compatible with Ruby 3.4 but for
383
- # now respects using parser for backwards compatibility.
506
+ # Prism is used for all Ruby versions it can parse (3.3 and later);
507
+ # the Parser gem does not support Ruby 3.5 or later and is not fully
508
+ # compatible with Ruby 3.4.
384
509
  def default_parser_engine(ruby_version)
385
- if ruby_version >= 3.4
510
+ if ruby_version >= 3.3
386
511
  :parser_prism
387
512
  else
388
513
  :parser_whitequark
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module AST
5
5
  module Version
6
- STRING = '1.49.1'
6
+ STRING = '1.51.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.49.1
4
+ version: 1.51.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bozhidar Batsov
@@ -29,14 +29,14 @@ dependencies:
29
29
  name: prism
30
30
  requirement: !ruby/object:Gem::Requirement
31
31
  requirements:
32
- - - "~>"
32
+ - - ">="
33
33
  - !ruby/object:Gem::Version
34
34
  version: '1.7'
35
35
  type: :runtime
36
36
  prerelease: false
37
37
  version_requirements: !ruby/object:Gem::Requirement
38
38
  requirements:
39
- - - "~>"
39
+ - - ">="
40
40
  - !ruby/object:Gem::Version
41
41
  version: '1.7'
42
42
  description: " RuboCop's Node and NodePattern classes.\n"
@@ -176,7 +176,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
176
176
  - !ruby/object:Gem::Version
177
177
  version: '0'
178
178
  requirements: []
179
- rubygems_version: 4.0.3
179
+ rubygems_version: 4.0.17
180
180
  specification_version: 4
181
181
  summary: RuboCop tools to deal with Ruby code AST.
182
182
  test_files: []