rubocop-ast 1.30.0 → 1.32.3

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: 84c917625cddd9b79c0979e269fdd324f8119b9d2b3c6081fae0ef449669bea5
4
- data.tar.gz: 8d053cac9c670216326df54b5b7b6a8961bf89614defa16e93ad1c557a8b25a5
3
+ metadata.gz: 289c6acb690241440018fb6d23e23759c6cf52100b2bf373b47226ee71cad215
4
+ data.tar.gz: 45cd10ff396ddf599ac11277b5d14a2e7cede79b267a40b2e53a83ccd2580479
5
5
  SHA512:
6
- metadata.gz: c95bd42a576592153d70f61367aafacd9677021515534a487706b736cc02cfcf717fc6db39e3df7212088301383cb7d60af9b929b4ca65b7baa5a66f5eca059e
7
- data.tar.gz: f0d99d3695e651ec10709dd729696a2b137c3b11603598164b433f61583218b16807c44ea1637ebe1922b496b8472795e4d54a4dc1312908f049e6f43b242ff8
6
+ metadata.gz: 137d5b737b87e1a136b23c6ae6b591334e5971e923c01727c37280b42e96a0a41fc534b54e8ea1b83074256b5d6645f86c079977fd72ffd6404d9f4eb61d92c6
7
+ data.tar.gz: d7c4e19d909f3591b4f7460a54e50f77555d37b517baa86359c635a2b32eeb119804464fb17c95adc69f23147f3023322142d44117f0e9ea0262e8c1fdd3aec2
data/README.md CHANGED
@@ -1,7 +1,7 @@
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/rubocop-ast/workflows/CI/badge.svg)](https://github.com/rubocop/rubocop-ast/actions?query=workflow%3ACI)
4
+ [![CI](https://github.com/rubocop/rubocop-ast/actions/workflows/rubocop.yml/badge.svg)](https://github.com/rubocop/rubocop-ast/actions/workflows/rubocop.yml)
5
5
  [![Test Coverage](https://api.codeclimate.com/v1/badges/a29666e6373bc41bc0a9/test_coverage)](https://codeclimate.com/github/rubocop/rubocop-ast/test_coverage)
6
6
  [![Maintainability](https://api.codeclimate.com/v1/badges/a29666e6373bc41bc0a9/maintainability)](https://codeclimate.com/github/rubocop/rubocop-ast/maintainability)
7
7
 
@@ -72,6 +72,7 @@ module RuboCop
72
72
  or: OrNode,
73
73
  pair: PairNode,
74
74
  procarg0: Procarg0Node,
75
+ rational: RationalNode,
75
76
  regexp: RegexpNode,
76
77
  rescue: RescueNode,
77
78
  resbody: ResbodyNode,
@@ -17,7 +17,11 @@ module RuboCop
17
17
  # @return [Array<Node>] an array of value nodes
18
18
  alias values children
19
19
 
20
- # @deprecated Use `values.each` (a.k.a. `children.each`)
20
+ # Calls the given block for each `value` node in the `array` literal.
21
+ # If no block is given, an `Enumerator` is returned.
22
+ #
23
+ # @return [self] if a block is given
24
+ # @return [Enumerator] if no block is given
21
25
  def each_value(&block)
22
26
  return to_enum(__method__) unless block
23
27
 
@@ -13,7 +13,7 @@ module RuboCop
13
13
  #
14
14
  # @return [Boolean] whether the `def` node body is a void context
15
15
  def void_context?
16
- method?(:initialize) || assignment_method?
16
+ (def_type? && method?(:initialize)) || assignment_method?
17
17
  end
18
18
 
19
19
  # Checks whether this method definition node forwards its arguments
@@ -12,6 +12,14 @@ module RuboCop
12
12
  def body
13
13
  node_parts[1]
14
14
  end
15
+
16
+ # Checks whether this node body is a void context.
17
+ # Always `true` for `ensure`.
18
+ #
19
+ # @return [true] whether the `ensure` node body is a void context
20
+ def void_context?
21
+ true
22
+ end
15
23
  end
16
24
  end
17
25
  end
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module AST
5
5
  # Common functionality for primitive literal nodes: `sym`, `str`,
6
- # `int`, `float`, ...
6
+ # `int`, `float`, `rational`...
7
7
  module BasicLiteralNode
8
8
  # Returns the value of the literal.
9
9
  #
@@ -4,7 +4,7 @@ module RuboCop
4
4
  module AST
5
5
  # A mixin that helps give collection nodes array polymorphism.
6
6
  module CollectionNode
7
- extend Forwardable
7
+ extend SimpleForwardable
8
8
 
9
9
  ARRAY_METHODS =
10
10
  (Array.instance_methods - Object.instance_methods - [:to_a]).freeze
@@ -2,7 +2,7 @@
2
2
 
3
3
  module RuboCop
4
4
  module AST
5
- # Common functionality for primitive numeric nodes: `int`, `float`, ...
5
+ # Common functionality for primitive numeric nodes: `int`, `float`, `rational`...
6
6
  module NumericNode
7
7
  SIGN_REGEX = /\A[+-]/.freeze
8
8
  private_constant :SIGN_REGEX
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module AST
5
+ # A node extension for `rational` nodes. This will be used in place of a plain
6
+ # node when the builder constructs the AST, making its methods available to
7
+ # all `rational` nodes within RuboCop.
8
+ class RationalNode < Node
9
+ include BasicLiteralNode
10
+ include NumericNode
11
+ end
12
+ end
13
+ end
@@ -88,6 +88,27 @@ module RuboCop
88
88
  EMPTY_PROPERTIES = {}.freeze
89
89
  private_constant :EMPTY_CHILDREN, :EMPTY_PROPERTIES
90
90
 
91
+ # Define a +recursive_?+ predicate method for the given node kind.
92
+ private_class_method def self.def_recursive_literal_predicate(kind) # rubocop:disable Metrics/MethodLength
93
+ recursive_kind = "recursive_#{kind}?"
94
+ kind_filter = "#{kind}?"
95
+
96
+ class_eval <<~RUBY, __FILE__, __LINE__ + 1
97
+ def #{recursive_kind} # def recursive_literal?
98
+ case type # case type
99
+ when :send # when :send
100
+ LITERAL_RECURSIVE_METHODS.include?(method_name) && # LITERAL_RECURSIVE_METHODS.include?(method_name) &&
101
+ receiver.send(:#{recursive_kind}) && # receiver.send(:recursive_literal?) &&
102
+ arguments.all?(&:#{recursive_kind}) # arguments.all?(&:recursive_literal?)
103
+ when LITERAL_RECURSIVE_TYPES # when LITERAL_RECURSIVE_TYPES
104
+ children.compact.all?(&:#{recursive_kind}) # children.compact.all?(&:recursive_literal?)
105
+ else # else
106
+ send(:#{kind_filter}) # send(:literal?)
107
+ end # end
108
+ end # end
109
+ RUBY
110
+ end
111
+
91
112
  # @see https://www.rubydoc.info/gems/ast/AST/Node:initialize
92
113
  def initialize(type, children = EMPTY_CHILDREN, properties = EMPTY_PROPERTIES)
93
114
  @mutable_attributes = {}
@@ -326,13 +347,13 @@ module RuboCop
326
347
  # what class or module is this method/constant/etc definition in?
327
348
  # returns nil if answer cannot be determined
328
349
  ancestors = each_ancestor(:class, :module, :sclass, :casgn, :block)
329
- result = ancestors.map do |ancestor|
350
+ result = ancestors.filter_map do |ancestor|
330
351
  parent_module_name_part(ancestor) do |full_name|
331
352
  return nil unless full_name
332
353
 
333
354
  full_name
334
355
  end
335
- end.compact.reverse.join('::')
356
+ end.reverse.join('::')
336
357
  result.empty? ? 'Object' : result
337
358
  end
338
359
 
@@ -380,22 +401,11 @@ module RuboCop
380
401
  IMMUTABLE_LITERALS.include?(type)
381
402
  end
382
403
 
383
- %i[literal basic_literal].each do |kind|
384
- recursive_kind = :"recursive_#{kind}?"
385
- kind_filter = :"#{kind}?"
386
- define_method(recursive_kind) do
387
- case type
388
- when :send
389
- LITERAL_RECURSIVE_METHODS.include?(method_name) &&
390
- receiver.send(recursive_kind) &&
391
- arguments.all?(&recursive_kind)
392
- when LITERAL_RECURSIVE_TYPES
393
- children.compact.all?(&recursive_kind)
394
- else
395
- send(kind_filter)
396
- end
397
- end
398
- end
404
+ # @!macro [attach] def_recursive_literal_predicate
405
+ # @!method recursive_$1?
406
+ # @return [Boolean]
407
+ def_recursive_literal_predicate :literal
408
+ def_recursive_literal_predicate :basic_literal
399
409
 
400
410
  def variable?
401
411
  VARIABLES.include?(type)
@@ -547,8 +557,7 @@ module RuboCop
547
557
  # So, does the return value of this node matter? If we changed it to
548
558
  # `(...; nil)`, might that affect anything?
549
559
  #
550
- # rubocop:disable Metrics/MethodLength
551
- def value_used?
560
+ def value_used? # rubocop:disable Metrics/MethodLength
552
561
  # Be conservative and return true if we're not sure.
553
562
  return false if parent.nil?
554
563
 
@@ -569,7 +578,6 @@ module RuboCop
569
578
  true
570
579
  end
571
580
  end
572
- # rubocop:enable Metrics/MethodLength
573
581
 
574
582
  # Some expressions are evaluated for their value, some for their side
575
583
  # effects, and some for both.
@@ -26,8 +26,9 @@ module RuboCop
26
26
  var
27
27
  end
28
28
 
29
- # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
30
- def union_bind(enum)
29
+ # Yields for each branch of the given union, forbidding unification of
30
+ # bindings which only appear in a subset of the union.
31
+ def union_bind(enum) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
31
32
  # We need to reset @bound before each branch is processed.
32
33
  # Moreover we need to keep track of newly encountered wildcards.
33
34
  # Var `newly_bound_intersection` will hold those that are encountered
@@ -62,7 +63,6 @@ module RuboCop
62
63
 
63
64
  result
64
65
  end
65
- # rubocop:enable Metrics/MethodLength, Metrics/AbcSize
66
66
 
67
67
  private
68
68
 
@@ -13,8 +13,8 @@ module RuboCop
13
13
  # Doc on how this fits in the compiling process:
14
14
  # /docs/modules/ROOT/pages/node_pattern.adoc
15
15
  #
16
- # rubocop:disable Metrics/ClassLength
17
- class SequenceSubcompiler < Subcompiler
16
+ class SequenceSubcompiler < Subcompiler # rubocop:disable Metrics/ClassLength
17
+ # Shift of 1 from standard Ruby indices
18
18
  DELTA = 1
19
19
  POSITIVE = :positive?.to_proc
20
20
  private_constant :POSITIVE
@@ -413,7 +413,6 @@ module RuboCop
413
413
  @in_sync = sub_compilers.all?(&:in_sync)
414
414
  end
415
415
  end
416
- # rubocop:enable Metrics/ClassLength
417
416
  end
418
417
  end
419
418
  end
@@ -9,7 +9,7 @@ module RuboCop
9
9
  # Doc on how this fits in the compiling process:
10
10
  # /docs/modules/ROOT/pages/node_pattern.adoc
11
11
  class Compiler
12
- extend Forwardable
12
+ extend SimpleForwardable
13
13
  attr_reader :captures, :named_parameters, :positional_parameters, :binding
14
14
 
15
15
  def initialize
@@ -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.6.1.
5
+ # Generated by: oedipus_lex version 2.6.0.
6
6
  # Source: lib/rubocop/ast/node_pattern/lexer.rex
7
7
  #++
8
8
 
@@ -5,9 +5,8 @@ module RuboCop
5
5
  class NodePattern
6
6
  # Base class for AST Nodes of a `NodePattern`
7
7
  class Node < ::Parser::AST::Node
8
- extend Forwardable
8
+ extend SimpleForwardable
9
9
  include ::RuboCop::AST::Descendence
10
- using Ext::RangeMinMax
11
10
 
12
11
  MATCHES_WITHIN_SET = %i[symbol number string].to_set.freeze
13
12
  private_constant :MATCHES_WITHIN_SET
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
  #
3
3
  # DO NOT MODIFY!!!!
4
- # This file is automatically generated by Racc 1.7.1
4
+ # This file is automatically generated by Racc 1.5.0
5
5
  # from Racc grammar file "".
6
6
  #
7
7
 
@@ -14,15 +14,15 @@ module RuboCop
14
14
 
15
15
  racc_action_table = [
16
16
  14, 15, 16, 21, 18, 17, 19, 10, 11, 12,
17
- 60, 22, 20, 4, 24, 5, 40, 6, 7, 8,
18
- 28, 23, 56, 50, 40, 61, 43, 66, 51, 51,
17
+ 60, 22, 20, 4, 40, 5, 43, 6, 7, 8,
18
+ 28, 23, 56, 50, 66, 61, 24, 51, 51, 40,
19
19
  58, 14, 15, 16, 21, 18, 17, 19, 10, 11,
20
20
  12, nil, 22, 20, 4, nil, 5, nil, 6, 7,
21
21
  8, 28, 23, nil, nil, -34, 14, 15, 16, 21,
22
22
  18, 17, 19, 10, 11, 12, nil, 22, 20, 4,
23
23
  nil, 5, nil, 6, 7, 8, 9, 23, 14, 15,
24
24
  16, 21, 18, 17, 19, 10, 11, 12, nil, 22,
25
- 20, 4, nil, 5, nil, 6, 7, 8, 28, 23,
25
+ 20, 4, nil, 5, nil, 6, 7, 8, 9, 23,
26
26
  14, 15, 16, 21, 18, 17, 19, 10, 11, 12,
27
27
  nil, 22, 20, 4, nil, 5, nil, 6, 7, 8,
28
28
  9, 23, 14, 15, 16, 21, 18, 17, 19, 10,
@@ -33,7 +33,7 @@ racc_action_table = [
33
33
  18, 17, 19, 10, 11, 12, nil, 22, 20, 4,
34
34
  nil, 5, nil, 6, 7, 8, 9, 23, 14, 15,
35
35
  16, 21, 18, 17, 19, 10, 11, 12, nil, 22,
36
- 20, 4, nil, 5, nil, 6, 7, 8, 9, 23,
36
+ 20, 4, nil, 5, nil, 6, 7, 8, 28, 23,
37
37
  14, 15, 16, 21, 18, 17, 19, 10, 11, 12,
38
38
  nil, 22, 20, 4, 44, 5, nil, 6, 7, 8,
39
39
  28, 23, 14, 15, 16, 21, 18, 17, 19, 10,
@@ -47,31 +47,31 @@ racc_action_table = [
47
47
  20, 4, nil, 5, nil, 6, 7, 8, 9, 23,
48
48
  14, 15, 16, 21, 18, 17, 19, 10, 11, 12,
49
49
  nil, 22, 20, 4, nil, 5, nil, 6, 7, 8,
50
- 9, 23, -1, -1, -1, -2, -2, -2, 47, 48,
51
- 49 ]
50
+ 9, 23, 47, 48, 49, -1, -1, -1, -2, -2,
51
+ -2 ]
52
52
 
53
53
  racc_action_check = [
54
54
  42, 42, 42, 42, 42, 42, 42, 42, 42, 42,
55
- 54, 42, 42, 42, 1, 42, 10, 42, 42, 42,
56
- 42, 42, 42, 30, 11, 54, 24, 62, 30, 63,
55
+ 54, 42, 42, 42, 11, 42, 24, 42, 42, 42,
56
+ 42, 42, 42, 30, 62, 54, 1, 63, 30, 10,
57
57
  42, 59, 59, 59, 59, 59, 59, 59, 59, 59,
58
58
  59, nil, 59, 59, 59, nil, 59, nil, 59, 59,
59
- 59, 59, 59, nil, nil, 59, 0, 0, 0, 0,
59
+ 59, 59, 59, nil, nil, 59, 5, 5, 5, 5,
60
+ 5, 5, 5, 5, 5, 5, nil, 5, 5, 5,
61
+ nil, 5, nil, 5, 5, 5, 5, 5, 6, 6,
62
+ 6, 6, 6, 6, 6, 6, 6, 6, nil, 6,
63
+ 6, 6, nil, 6, nil, 6, 6, 6, 6, 6,
64
+ 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
65
+ nil, 7, 7, 7, nil, 7, nil, 7, 7, 7,
66
+ 7, 7, 8, 8, 8, 8, 8, 8, 8, 8,
67
+ 8, 8, nil, 8, 8, 8, nil, 8, nil, 8,
68
+ 8, 8, 8, 8, 9, 9, 9, 9, 9, 9,
69
+ 9, 9, 9, 9, nil, 9, 9, 9, nil, 9,
70
+ nil, 9, 9, 9, 9, 9, 0, 0, 0, 0,
60
71
  0, 0, 0, 0, 0, 0, nil, 0, 0, 0,
61
72
  nil, 0, nil, 0, 0, 0, 0, 0, 4, 4,
62
73
  4, 4, 4, 4, 4, 4, 4, 4, nil, 4,
63
74
  4, 4, nil, 4, nil, 4, 4, 4, 4, 4,
64
- 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
65
- nil, 5, 5, 5, nil, 5, nil, 5, 5, 5,
66
- 5, 5, 6, 6, 6, 6, 6, 6, 6, 6,
67
- 6, 6, nil, 6, 6, 6, nil, 6, nil, 6,
68
- 6, 6, 6, 6, 7, 7, 7, 7, 7, 7,
69
- 7, 7, 7, 7, nil, 7, 7, 7, nil, 7,
70
- nil, 7, 7, 7, 7, 7, 8, 8, 8, 8,
71
- 8, 8, 8, 8, 8, 8, nil, 8, 8, 8,
72
- nil, 8, nil, 8, 8, 8, 8, 8, 9, 9,
73
- 9, 9, 9, 9, 9, 9, 9, 9, nil, 9,
74
- 9, 9, nil, 9, nil, 9, 9, 9, 9, 9,
75
75
  27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
76
76
  nil, 27, 27, 27, 27, 27, nil, 27, 27, 27,
77
77
  27, 27, 28, 28, 28, 28, 28, 28, 28, 28,
@@ -85,17 +85,17 @@ racc_action_check = [
85
85
  50, 50, nil, 50, nil, 50, 50, 50, 50, 50,
86
86
  61, 61, 61, 61, 61, 61, 61, 61, 61, 61,
87
87
  nil, 61, 61, 61, nil, 61, nil, 61, 61, 61,
88
- 61, 61, 25, 25, 25, 26, 26, 26, 29, 29,
89
- 29 ]
88
+ 61, 61, 29, 29, 29, 25, 25, 25, 26, 26,
89
+ 26 ]
90
90
 
91
91
  racc_action_pointer = [
92
- 54, 14, nil, nil, 76, 98, 120, 142, 164, 186,
93
- 4, 12, nil, nil, nil, nil, nil, nil, nil, nil,
94
- nil, nil, nil, nil, 26, 315, 318, 208, 230, 321,
92
+ 164, 26, nil, nil, 186, 54, 76, 98, 120, 142,
93
+ 17, 2, nil, nil, nil, nil, nil, nil, nil, nil,
94
+ nil, nil, nil, nil, 16, 318, 321, 208, 230, 315,
95
95
  -2, nil, nil, 252, nil, nil, nil, nil, nil, nil,
96
96
  274, nil, -2, nil, nil, nil, nil, nil, nil, nil,
97
97
  296, nil, nil, nil, -6, nil, nil, nil, nil, 29,
98
- nil, 318, 1, -1, nil, nil, nil ]
98
+ nil, 318, -2, -3, nil, nil, nil ]
99
99
 
100
100
  racc_action_default = [
101
101
  -47, -47, -1, -2, -31, -47, -47, -47, -47, -47,
@@ -107,26 +107,26 @@ racc_action_default = [
107
107
  -37, -47, -47, -47, -35, -39, -26 ]
108
108
 
109
109
  racc_goto_table = [
110
- 1, 33, 27, 25, 26, 34, 35, 36, 37, 38,
111
- 42, 32, 39, 41, 46, 63, 62, 64, 54, nil,
112
- nil, nil, nil, nil, nil, nil, 25, 26, 38, nil,
113
- nil, nil, nil, 53, 45, nil, nil, nil, nil, nil,
114
- 55, 25, 26, nil, nil, nil, 59, nil, nil, 57,
110
+ 1, 33, 64, 32, 25, 34, 35, 36, 37, 38,
111
+ 54, 26, 39, 41, 27, 42, 46, 63, 62, nil,
112
+ nil, nil, nil, nil, nil, nil, 45, 25, 38, nil,
113
+ nil, nil, nil, 53, 26, nil, nil, nil, nil, nil,
114
+ 55, 57, 25, nil, nil, nil, 59, nil, nil, 26,
115
115
  34, nil, nil, nil, nil, nil, nil, nil, nil, 53,
116
116
  nil, 65 ]
117
117
 
118
118
  racc_goto_check = [
119
- 1, 5, 4, 2, 3, 1, 1, 1, 1, 1,
120
- 8, 9, 6, 6, 10, 11, 12, 13, 14, nil,
121
- nil, nil, nil, nil, nil, nil, 2, 3, 1, nil,
122
- nil, nil, nil, 1, 9, nil, nil, nil, nil, nil,
123
- 1, 2, 3, nil, nil, nil, 5, nil, nil, 9,
119
+ 1, 5, 13, 9, 2, 1, 1, 1, 1, 1,
120
+ 14, 3, 6, 6, 4, 8, 10, 11, 12, nil,
121
+ nil, nil, nil, nil, nil, nil, 9, 2, 1, nil,
122
+ nil, nil, nil, 1, 3, nil, nil, nil, nil, nil,
123
+ 1, 9, 2, nil, nil, nil, 5, nil, nil, 3,
124
124
  1, nil, nil, nil, nil, nil, nil, nil, nil, 1,
125
125
  nil, 1 ]
126
126
 
127
127
  racc_goto_pointer = [
128
- nil, 0, -1, 0, -2, -4, 2, nil, -13, 7,
129
- -15, -44, -43, -42, -22 ]
128
+ nil, 0, 0, 7, 10, -4, 2, nil, -8, -1,
129
+ -13, -42, -41, -57, -30 ]
130
130
 
131
131
  racc_goto_default = [
132
132
  nil, 29, 2, 3, nil, nil, nil, 13, nil, nil,
@@ -239,7 +239,6 @@ Racc_arg = [
239
239
  racc_shift_n,
240
240
  racc_reduce_n,
241
241
  racc_use_result_var ]
242
- Ractor.make_shareable(Racc_arg) if defined?(Ractor)
243
242
 
244
243
  Racc_token_to_s_table = [
245
244
  "$end",
@@ -290,7 +289,6 @@ Racc_token_to_s_table = [
290
289
  "opt_rest",
291
290
  "rest",
292
291
  "arg_list" ]
293
- Ractor.make_shareable(Racc_token_to_s_table) if defined?(Ractor)
294
292
 
295
293
  Racc_debug_parser = false
296
294
 
@@ -11,7 +11,7 @@ module RuboCop
11
11
  # Doc on how this fits in the compiling process:
12
12
  # /docs/modules/ROOT/pages/node_pattern.adoc
13
13
  class Parser < Racc::Parser
14
- extend Forwardable
14
+ extend SimpleForwardable
15
15
 
16
16
  Builder = NodePattern::Builder
17
17
  Lexer = NodePattern::Lexer
@@ -48,7 +48,7 @@ module RuboCop
48
48
  end
49
49
  end
50
50
 
51
- extend Forwardable
51
+ extend SimpleForwardable
52
52
  include MethodDefiner
53
53
  Invalid = Class.new(StandardError)
54
54
 
@@ -2,28 +2,36 @@
2
2
 
3
3
  require 'digest/sha1'
4
4
 
5
- # rubocop:disable Metrics/ClassLength
6
5
  module RuboCop
7
6
  module AST
8
7
  # ProcessedSource contains objects which are generated by Parser
9
8
  # and other information such as disabled lines for cops.
10
9
  # It also provides a convenient way to access source lines.
11
- class ProcessedSource
10
+ class ProcessedSource # rubocop:disable Metrics/ClassLength
12
11
  # @api private
13
12
  STRING_SOURCE_NAME = '(string)'
14
13
 
15
14
  INVALID_LEVELS = %i[error fatal].freeze
16
15
  private_constant :INVALID_LEVELS
17
16
 
17
+ PARSER_ENGINES = %i[parser_whitequark parser_prism].freeze
18
+ private_constant :PARSER_ENGINES
19
+
18
20
  attr_reader :path, :buffer, :ast, :comments, :tokens, :diagnostics,
19
- :parser_error, :raw_source, :ruby_version
21
+ :parser_error, :raw_source, :ruby_version, :parser_engine
20
22
 
21
- def self.from_file(path, ruby_version)
23
+ def self.from_file(path, ruby_version, parser_engine: :parser_whitequark)
22
24
  file = File.read(path, mode: 'rb')
23
- new(file, ruby_version, path)
25
+ new(file, ruby_version, path, parser_engine: parser_engine)
24
26
  end
25
27
 
26
- def initialize(source, ruby_version, path = nil)
28
+ def initialize(source, ruby_version, path = nil, parser_engine: :parser_whitequark)
29
+ parser_engine = parser_engine.to_sym
30
+ unless PARSER_ENGINES.include?(parser_engine)
31
+ raise ArgumentError, 'The keyword argument `parser_engine` accepts `parser_whitequark` ' \
32
+ "or `parser_prism`, but `#{parser_engine}` was passed."
33
+ end
34
+
27
35
  # Defaults source encoding to UTF-8, regardless of the encoding it has
28
36
  # been read with, which could be non-utf8 depending on the default
29
37
  # external encoding.
@@ -33,9 +41,10 @@ module RuboCop
33
41
  @path = path
34
42
  @diagnostics = []
35
43
  @ruby_version = ruby_version
44
+ @parser_engine = parser_engine
36
45
  @parser_error = nil
37
46
 
38
- parse(source, ruby_version)
47
+ parse(source, ruby_version, parser_engine)
39
48
  end
40
49
 
41
50
  def ast_with_comments
@@ -193,13 +202,13 @@ module RuboCop
193
202
  end
194
203
  end
195
204
 
196
- def parse(source, ruby_version)
205
+ def parse(source, ruby_version, parser_engine)
197
206
  buffer_name = @path || STRING_SOURCE_NAME
198
207
  @buffer = Parser::Source::Buffer.new(buffer_name, 1)
199
208
 
200
209
  begin
201
210
  @buffer.source = source
202
- rescue EncodingError => e
211
+ rescue EncodingError, Parser::UnknownEncodingInMagicComment => e
203
212
  @parser_error = e
204
213
  @ast = nil
205
214
  @comments = []
@@ -207,7 +216,7 @@ module RuboCop
207
216
  return
208
217
  end
209
218
 
210
- @ast, @comments, @tokens = tokenize(create_parser(ruby_version))
219
+ @ast, @comments, @tokens = tokenize(create_parser(ruby_version, parser_engine))
211
220
  end
212
221
 
213
222
  def tokenize(parser)
@@ -226,59 +235,81 @@ module RuboCop
226
235
  [ast, comments, tokens]
227
236
  end
228
237
 
229
- # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
230
- def parser_class(ruby_version)
231
- case ruby_version
232
- when 1.9
233
- require 'parser/ruby19'
234
- Parser::Ruby19
235
- when 2.0
236
- require 'parser/ruby20'
237
- Parser::Ruby20
238
- when 2.1
239
- require 'parser/ruby21'
240
- Parser::Ruby21
241
- when 2.2
242
- require 'parser/ruby22'
243
- Parser::Ruby22
244
- when 2.3
245
- require 'parser/ruby23'
246
- Parser::Ruby23
247
- when 2.4
248
- require 'parser/ruby24'
249
- Parser::Ruby24
250
- when 2.5
251
- require 'parser/ruby25'
252
- Parser::Ruby25
253
- when 2.6
254
- require 'parser/ruby26'
255
- Parser::Ruby26
256
- when 2.7
257
- require 'parser/ruby27'
258
- Parser::Ruby27
259
- when 2.8, 3.0
260
- require 'parser/ruby30'
261
- Parser::Ruby30
262
- when 3.1
263
- require 'parser/ruby31'
264
- Parser::Ruby31
265
- when 3.2
266
- require 'parser/ruby32'
267
- Parser::Ruby32
268
- when 3.3
269
- require 'parser/ruby33'
270
- Parser::Ruby33
271
- else
272
- raise ArgumentError,
273
- "RuboCop found unknown Ruby version: #{ruby_version.inspect}"
238
+ def parser_class(ruby_version, parser_engine) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
239
+ case parser_engine
240
+ when :parser_whitequark
241
+ case ruby_version
242
+ when 1.9
243
+ require 'parser/ruby19'
244
+ Parser::Ruby19
245
+ when 2.0
246
+ require 'parser/ruby20'
247
+ Parser::Ruby20
248
+ when 2.1
249
+ require 'parser/ruby21'
250
+ Parser::Ruby21
251
+ when 2.2
252
+ require 'parser/ruby22'
253
+ Parser::Ruby22
254
+ when 2.3
255
+ require 'parser/ruby23'
256
+ Parser::Ruby23
257
+ when 2.4
258
+ require 'parser/ruby24'
259
+ Parser::Ruby24
260
+ when 2.5
261
+ require 'parser/ruby25'
262
+ Parser::Ruby25
263
+ when 2.6
264
+ require 'parser/ruby26'
265
+ Parser::Ruby26
266
+ when 2.7
267
+ require 'parser/ruby27'
268
+ Parser::Ruby27
269
+ when 2.8, 3.0
270
+ require 'parser/ruby30'
271
+ Parser::Ruby30
272
+ when 3.1
273
+ require 'parser/ruby31'
274
+ Parser::Ruby31
275
+ when 3.2
276
+ require 'parser/ruby32'
277
+ Parser::Ruby32
278
+ when 3.3
279
+ require 'parser/ruby33'
280
+ Parser::Ruby33
281
+ when 3.4
282
+ require 'parser/ruby34'
283
+ Parser::Ruby34
284
+ else
285
+ raise ArgumentError, "RuboCop found unknown Ruby version: #{ruby_version.inspect}"
286
+ end
287
+ when :parser_prism
288
+ begin
289
+ require 'prism'
290
+ rescue LoadError
291
+ warn "Error: Unable to load Prism. Add `gem 'prism'` to your Gemfile."
292
+ exit!
293
+ end
294
+
295
+ case ruby_version
296
+ when 3.3
297
+ require 'prism/translation/parser33'
298
+ Prism::Translation::Parser33
299
+ when 3.4
300
+ require 'prism/translation/parser34'
301
+ Prism::Translation::Parser34
302
+ else
303
+ raise ArgumentError, 'RuboCop supports target Ruby versions 3.3 and above with Prism. ' \
304
+ "Specified target Ruby version: #{ruby_version.inspect}"
305
+ end
274
306
  end
275
307
  end
276
- # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
277
308
 
278
- def create_parser(ruby_version)
309
+ def create_parser(ruby_version, parser_engine)
279
310
  builder = RuboCop::AST::Builder.new
280
311
 
281
- parser_class(ruby_version).new(builder).tap do |parser|
312
+ parser_class(ruby_version, parser_engine).new(builder).tap do |parser|
282
313
  # On JRuby there's a risk that we hang in tokenize() if we
283
314
  # don't set the all errors as fatal flag. The problem is caused by a bug
284
315
  # in Racc that is discussed in issue #93 of the whitequark/parser
@@ -311,4 +342,3 @@ module RuboCop
311
342
  end
312
343
  end
313
344
  end
314
- # rubocop:enable Metrics/ClassLength
@@ -83,7 +83,7 @@ module RuboCop
83
83
  end
84
84
 
85
85
  def left_curly_brace?
86
- type == :tLCURLY
86
+ type == :tLCURLY || type == :tLAMBEG
87
87
  end
88
88
 
89
89
  def right_curly_brace?
@@ -38,14 +38,14 @@ module RuboCop
38
38
  body: self.body(signature, arity_check))
39
39
  type, *aliases = type
40
40
  lineno = caller_locations(1, 1).first.lineno
41
- module_eval(<<~RUBY, __FILE__, lineno) # rubocop:disable Style/EvalWithLocation
41
+ module_eval(<<~RUBY, __FILE__, lineno)
42
42
  def on_#{type}(node) # def on_send(node)
43
43
  #{body} # # body ...
44
44
  nil # nil
45
45
  end # end
46
46
  RUBY
47
47
  aliases.each do |m|
48
- alias_method "on_#{m}", "on_#{type}"
48
+ alias_method :"on_#{m}", :"on_#{type}"
49
49
  end
50
50
  end
51
51
 
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ # Similar to `Forwardable#def_delegators`, but simpler & faster
5
+ module SimpleForwardable
6
+ def def_delegators(accessor, *methods)
7
+ methods.each do |method|
8
+ class_eval(<<~RUBY, __FILE__, __LINE__ + 1)
9
+ def #{method}(...) # def example(...)
10
+ #{accessor}.#{method}(...) # foo.example(...)
11
+ end # end
12
+ RUBY
13
+ end
14
+ end
15
+ end
16
+ end
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module AST
5
5
  module Version
6
- STRING = '1.30.0'
6
+ STRING = '1.32.3'
7
7
  end
8
8
  end
9
9
  end
data/lib/rubocop/ast.rb CHANGED
@@ -5,7 +5,7 @@ require 'forwardable'
5
5
  require 'set'
6
6
 
7
7
  require_relative 'ast/ext/range'
8
- require_relative 'ast/ext/range_min_max'
8
+ require_relative 'ast/utilities/simple_forwardable'
9
9
  require_relative 'ast/node_pattern/method_definer'
10
10
  require_relative 'ast/node_pattern'
11
11
  require_relative 'ast/node/mixin/descendence'
@@ -70,6 +70,7 @@ require_relative 'ast/node/or_node'
70
70
  require_relative 'ast/node/pair_node'
71
71
  require_relative 'ast/node/procarg0_node'
72
72
  require_relative 'ast/node/range_node'
73
+ require_relative 'ast/node/rational_node'
73
74
  require_relative 'ast/node/regexp_node'
74
75
  require_relative 'ast/node/rescue_node'
75
76
  require_relative 'ast/node/resbody_node'
metadata CHANGED
@@ -1,16 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop-ast
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.30.0
4
+ version: 1.32.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bozhidar Batsov
8
8
  - Jonas Arvidsson
9
9
  - Yuji Nakayama
10
- autorequire:
11
10
  bindir: bin
12
11
  cert_chain: []
13
- date: 2023-10-26 00:00:00.000000000 Z
12
+ date: 2024-09-04 00:00:00.000000000 Z
14
13
  dependencies:
15
14
  - !ruby/object:Gem::Dependency
16
15
  name: parser
@@ -18,14 +17,14 @@ dependencies:
18
17
  requirements:
19
18
  - - ">="
20
19
  - !ruby/object:Gem::Version
21
- version: 3.2.1.0
20
+ version: 3.3.1.0
22
21
  type: :runtime
23
22
  prerelease: false
24
23
  version_requirements: !ruby/object:Gem::Requirement
25
24
  requirements:
26
25
  - - ">="
27
26
  - !ruby/object:Gem::Version
28
- version: 3.2.1.0
27
+ version: 3.3.1.0
29
28
  description: " RuboCop's Node and NodePattern classes.\n"
30
29
  email: rubocop@googlegroups.com
31
30
  executables: []
@@ -40,7 +39,6 @@ files:
40
39
  - lib/rubocop/ast.rb
41
40
  - lib/rubocop/ast/builder.rb
42
41
  - lib/rubocop/ast/ext/range.rb
43
- - lib/rubocop/ast/ext/range_min_max.rb
44
42
  - lib/rubocop/ast/node.rb
45
43
  - lib/rubocop/ast/node/alias_node.rb
46
44
  - lib/rubocop/ast/node/and_asgn_node.rb
@@ -92,6 +90,7 @@ files:
92
90
  - lib/rubocop/ast/node/pair_node.rb
93
91
  - lib/rubocop/ast/node/procarg0_node.rb
94
92
  - lib/rubocop/ast/node/range_node.rb
93
+ - lib/rubocop/ast/node/rational_node.rb
95
94
  - lib/rubocop/ast/node/regexp_node.rb
96
95
  - lib/rubocop/ast/node/resbody_node.rb
97
96
  - lib/rubocop/ast/node/rescue_node.rb
@@ -130,6 +129,7 @@ files:
130
129
  - lib/rubocop/ast/sexp.rb
131
130
  - lib/rubocop/ast/token.rb
132
131
  - lib/rubocop/ast/traversal.rb
132
+ - lib/rubocop/ast/utilities/simple_forwardable.rb
133
133
  - lib/rubocop/ast/version.rb
134
134
  homepage: https://github.com/rubocop/rubocop-ast
135
135
  licenses:
@@ -141,7 +141,6 @@ metadata:
141
141
  documentation_uri: https://docs.rubocop.org/rubocop-ast/
142
142
  bug_tracker_uri: https://github.com/rubocop/rubocop-ast/issues
143
143
  rubygems_mfa_required: 'true'
144
- post_install_message:
145
144
  rdoc_options: []
146
145
  require_paths:
147
146
  - lib
@@ -149,15 +148,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
149
148
  requirements:
150
149
  - - ">="
151
150
  - !ruby/object:Gem::Version
152
- version: 2.6.0
151
+ version: 2.7.0
153
152
  required_rubygems_version: !ruby/object:Gem::Requirement
154
153
  requirements:
155
154
  - - ">="
156
155
  - !ruby/object:Gem::Version
157
156
  version: '0'
158
157
  requirements: []
159
- rubygems_version: 3.4.10
160
- signing_key:
158
+ rubygems_version: 3.6.0.dev
161
159
  specification_version: 4
162
160
  summary: RuboCop tools to deal with Ruby code AST.
163
161
  test_files: []
@@ -1,18 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module RuboCop
4
- module AST
5
- module Ext
6
- # Refinement to circumvent broken `Range#minmax` for infinity ranges in 2.6-
7
- module RangeMinMax
8
- if ::Range.instance_method(:minmax).owner != ::Range
9
- refine ::Range do
10
- def minmax
11
- [min, max]
12
- end
13
- end
14
- end
15
- end
16
- end
17
- end
18
- end