rubocop-ast 1.50.0 → 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: 21fa08d16f9a915d62568fbf85b25efeefbf23fba1bf710b772130bcdcced47d
4
- data.tar.gz: 2789ea768313b62f16dc6a6f77e3f4b02e5d1af01a7d765483284fb3cd65c44c
3
+ metadata.gz: 852caeb016762e91a5ba54a7f395eca116212e1b44024db303924c7991d1b657
4
+ data.tar.gz: b4dda6fc3370f3edb5e6f24cebee0b19f2ef4f39274071a07e8a0a0f9255305f
5
5
  SHA512:
6
- metadata.gz: 53cd0e762e23e4394cbd01ed3d2ae6a86983e22f0a18d7782207874d691140a6eca839e67f2ee746ffb942863c65c3278cde553b408cf8c1fdfb0da736e393e4
7
- data.tar.gz: d8657ab6d0d89f0d9d963e13379283c7aba9245fb9fa3227316f4b7955d6faab37a2da0539814461c5b39e5d8e5c7c12d715c8ebcdd2406e606e7e6f350e0160
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
@@ -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
@@ -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.
@@ -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
@@ -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.
@@ -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
 
@@ -536,6 +536,28 @@ module RuboCop
536
536
  parent&.call_type? && equal?(parent.receiver)
537
537
  end
538
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
559
+ end
560
+
539
561
  def argument?
540
562
  return false unless parent&.send_type?
541
563
 
@@ -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)
@@ -264,25 +264,33 @@ module RuboCop
264
264
  sorted_tokens[last_token_index(range_or_node)]
265
265
  end
266
266
 
267
- # The tokens list is always sorted by token position, except for cases when heredoc
268
- # is passed as a method argument. In this case tokens are interleaved by
269
- # 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.
270
271
  def sorted_tokens
271
- # Most sources have their tokens already in order, in which case
272
- # sorting can be skipped entirely. Callers only ever read from the
273
- # returned array, so it is safe to reuse `tokens` as is.
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
274
  @sorted_tokens ||= if tokens_sorted?
275
275
  tokens
276
276
  else
277
- # Use stable sort.
278
- tokens.sort_by.with_index { |token, i| [token.begin_pos, i] }
277
+ sort_tokens
279
278
  end
280
279
  end
281
280
 
282
281
  private
283
282
 
284
283
  def tokens_sorted?
285
- tokens.each_cons(2).all? { |a, b| a.begin_pos <= b.begin_pos }
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 }
286
294
  end
287
295
 
288
296
  # `__END__` only starts a data section when it isn't nested inside a
@@ -341,6 +349,13 @@ module RuboCop
341
349
  # All errors are in diagnostics. No need to handle exception.
342
350
  comments = []
343
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 = []
344
359
  end
345
360
 
346
361
  ast&.complete!
@@ -365,7 +380,7 @@ module RuboCop
365
380
  @parser_tokens
366
381
  end
367
382
 
368
- # rubocop:disable Lint/FloatComparison, Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
383
+ # rubocop:disable-next Lint/FloatComparison, Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
369
384
  def parser_class(ruby_version, parser_engine)
370
385
  case parser_engine
371
386
  when :parser_whitequark
@@ -432,7 +447,6 @@ module RuboCop
432
447
  end
433
448
  end
434
449
  end
435
- # rubocop:enable Lint/FloatComparison, Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
436
450
 
437
451
  def builder_class(parser_engine)
438
452
  case parser_engine
@@ -443,7 +457,7 @@ module RuboCop
443
457
  end
444
458
  end
445
459
 
446
- # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
460
+ # rubocop:disable-next Metrics/AbcSize, Metrics/MethodLength
447
461
  def create_parser(ruby_version, parser_engine, prism_result)
448
462
  builder = builder_class(parser_engine).new
449
463
 
@@ -474,7 +488,6 @@ module RuboCop
474
488
  end
475
489
  end
476
490
  end
477
- # rubocop:enable Metrics/AbcSize, Metrics/MethodLength
478
491
 
479
492
  def normalize_parser_engine(parser_engine, ruby_version)
480
493
  parser_engine = parser_engine.to_sym
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module AST
5
5
  module Version
6
- STRING = '1.50.0'
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.50.0
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: []