rubocop-ast 1.23.0 → 1.31.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: cfec7f927c495d16f847400f123da08ff90e6a94ff6319a84ce21fb0b261b7a5
4
- data.tar.gz: c770260ba7f50b0bb233d8e95d43e1d8fa1674a2a11af4548a37f919b5ce9cc8
3
+ metadata.gz: 56067f459b491d4b3f10deb7f4d198b5ad9cffbc52739a53dec9d3b644c6b8a6
4
+ data.tar.gz: d45cf7be143799cea97c0af7c35e05d8ccdc7b5fc920ba05d2504bb1eacb745f
5
5
  SHA512:
6
- metadata.gz: efbc6b544362492884000f55fbb1b216c6f4e55bb1087c88e3218bb595f91129afd4c45ecaa3e3d02784d7cd714ad21765c376aaad5e0768917384c366657e48
7
- data.tar.gz: 7f98a811c53a27eba7211f05b6a368b7f058dd285e6771c7ed2d97230688e88498c6f57b1d7e585ff83b5f0d3f85b928bb67ba4bbbeaa06fa7c0eb5aa7e5d20a
6
+ metadata.gz: c7f375ab38218879cf28118dd9b8144f2ce00832dc13aa6d23338adefaf531fbae4af141b9a9b1332a402b1d1c6e9105a1fe45ba1afe18c971aee0a713f2b695
7
+ data.tar.gz: 3d01a913bb5e333f8e605492574b2492238cf1208bac5b6a0fc0e880e76e0be391a6bf187c51ebdafbbef35f2702255b166cfe85e6a9ce7d0cedc4a868938c91
@@ -19,76 +19,77 @@ module RuboCop
19
19
 
20
20
  # @api private
21
21
  NODE_MAP = {
22
- and: AndNode,
23
- and_asgn: AndAsgnNode,
24
- alias: AliasNode,
25
- arg: ArgNode,
26
- blockarg: ArgNode,
27
- forward_arg: ArgNode,
28
- kwarg: ArgNode,
29
- kwoptarg: ArgNode,
30
- kwrestarg: ArgNode,
31
- optarg: ArgNode,
32
- restarg: ArgNode,
33
- shadowarg: ArgNode,
34
- args: ArgsNode,
35
- array: ArrayNode,
36
- lvasgn: AsgnNode,
37
- ivasgn: AsgnNode,
38
- cvasgn: AsgnNode,
39
- gvasgn: AsgnNode,
40
- block: BlockNode,
41
- numblock: BlockNode,
42
- break: BreakNode,
43
- case_match: CaseMatchNode,
44
- casgn: CasgnNode,
45
- case: CaseNode,
46
- class: ClassNode,
47
- const: ConstNode,
48
- def: DefNode,
49
- defined?: DefinedNode,
50
- defs: DefNode,
51
- dstr: DstrNode,
52
- ensure: EnsureNode,
53
- for: ForNode,
54
- forward_args: ForwardArgsNode,
55
- float: FloatNode,
56
- hash: HashNode,
57
- if: IfNode,
58
- in_pattern: InPatternNode,
59
- int: IntNode,
60
- index: IndexNode,
61
- indexasgn: IndexasgnNode,
62
- irange: RangeNode,
63
- erange: RangeNode,
64
- kwargs: HashNode,
65
- kwsplat: KeywordSplatNode,
66
- lambda: LambdaNode,
67
- module: ModuleNode,
68
- next: NextNode,
69
- op_asgn: OpAsgnNode,
70
- or_asgn: OrAsgnNode,
71
- or: OrNode,
72
- pair: PairNode,
73
- procarg0: Procarg0Node,
74
- regexp: RegexpNode,
75
- rescue: RescueNode,
76
- resbody: ResbodyNode,
77
- return: ReturnNode,
78
- csend: SendNode,
79
- send: SendNode,
80
- str: StrNode,
81
- xstr: StrNode,
82
- sclass: SelfClassNode,
83
- super: SuperNode,
84
- zsuper: SuperNode,
85
- sym: SymbolNode,
86
- until: UntilNode,
87
- until_post: UntilNode,
88
- when: WhenNode,
89
- while: WhileNode,
90
- while_post: WhileNode,
91
- yield: YieldNode
22
+ and: AndNode,
23
+ and_asgn: AndAsgnNode,
24
+ alias: AliasNode,
25
+ arg: ArgNode,
26
+ blockarg: ArgNode,
27
+ forward_arg: ArgNode,
28
+ kwarg: ArgNode,
29
+ kwoptarg: ArgNode,
30
+ kwrestarg: ArgNode,
31
+ optarg: ArgNode,
32
+ restarg: ArgNode,
33
+ shadowarg: ArgNode,
34
+ args: ArgsNode,
35
+ array: ArrayNode,
36
+ lvasgn: AsgnNode,
37
+ ivasgn: AsgnNode,
38
+ cvasgn: AsgnNode,
39
+ gvasgn: AsgnNode,
40
+ block: BlockNode,
41
+ numblock: BlockNode,
42
+ break: BreakNode,
43
+ case_match: CaseMatchNode,
44
+ casgn: CasgnNode,
45
+ case: CaseNode,
46
+ class: ClassNode,
47
+ const: ConstNode,
48
+ def: DefNode,
49
+ defined?: DefinedNode,
50
+ defs: DefNode,
51
+ dstr: DstrNode,
52
+ ensure: EnsureNode,
53
+ for: ForNode,
54
+ forward_args: ForwardArgsNode,
55
+ forwarded_kwrestarg: KeywordSplatNode,
56
+ float: FloatNode,
57
+ hash: HashNode,
58
+ if: IfNode,
59
+ in_pattern: InPatternNode,
60
+ int: IntNode,
61
+ index: IndexNode,
62
+ indexasgn: IndexasgnNode,
63
+ irange: RangeNode,
64
+ erange: RangeNode,
65
+ kwargs: HashNode,
66
+ kwsplat: KeywordSplatNode,
67
+ lambda: LambdaNode,
68
+ module: ModuleNode,
69
+ next: NextNode,
70
+ op_asgn: OpAsgnNode,
71
+ or_asgn: OrAsgnNode,
72
+ or: OrNode,
73
+ pair: PairNode,
74
+ procarg0: Procarg0Node,
75
+ regexp: RegexpNode,
76
+ rescue: RescueNode,
77
+ resbody: ResbodyNode,
78
+ return: ReturnNode,
79
+ csend: CsendNode,
80
+ send: SendNode,
81
+ str: StrNode,
82
+ xstr: StrNode,
83
+ sclass: SelfClassNode,
84
+ super: SuperNode,
85
+ zsuper: SuperNode,
86
+ sym: SymbolNode,
87
+ until: UntilNode,
88
+ until_post: UntilNode,
89
+ when: WhenNode,
90
+ while: WhileNode,
91
+ while_post: WhileNode,
92
+ yield: YieldNode
92
93
  }.freeze
93
94
 
94
95
  # Generates {Node} from the given information.
@@ -15,8 +15,8 @@ module RuboCop
15
15
  # :bar
16
16
  # ]
17
17
  #
18
- # node.loc.begin.line_span # => 1..1
19
- # node.loc.expression.line_span(exclude_end: true) # => 1...4
18
+ # node.loc.begin.line_span # => 1..1
19
+ # node.source_range.line_span(exclude_end: true) # => 1...4
20
20
  def line_span(exclude_end: false)
21
21
  ::Range.new(first_line, last_line, exclude_end)
22
22
  end
@@ -25,4 +25,4 @@ module RuboCop
25
25
  end
26
26
  end
27
27
 
28
- ::Parser::Source::Range.include ::RuboCop::AST::Ext::Range
28
+ Parser::Source::Range.include RuboCop::AST::Ext::Range
@@ -21,6 +21,24 @@ module RuboCop
21
21
  node_parts[0]
22
22
  end
23
23
 
24
+ # A shorthand for getting the first argument of this block.
25
+ # Equivalent to `arguments.first`.
26
+ #
27
+ # @return [Node, nil] the first argument of this block,
28
+ # or `nil` if there are no arguments
29
+ def first_argument
30
+ arguments[0]
31
+ end
32
+
33
+ # A shorthand for getting the last argument of this block.
34
+ # Equivalent to `arguments.last`.
35
+ #
36
+ # @return [Node, nil] the last argument of this block,
37
+ # or `nil` if there are no arguments
38
+ def last_argument
39
+ arguments[-1]
40
+ end
41
+
24
42
  # The arguments of this block.
25
43
  # Note that if the block has destructured arguments, `arguments` will
26
44
  # return a `mlhs` node, whereas `argument_list` will return only
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module AST
5
+ # A node extension for `csend` 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 `csend` nodes within RuboCop.
8
+ class CsendNode < SendNode
9
+ def send_type?
10
+ false
11
+ end
12
+ end
13
+ end
14
+ end
@@ -81,7 +81,7 @@ module RuboCop
81
81
  (if? || unless?) && super
82
82
  end
83
83
 
84
- # Chacks whether the `if` node has nested `if` nodes in any of its
84
+ # Checks whether the `if` node has nested `if` nodes in any of its
85
85
  # branches.
86
86
  #
87
87
  # @note This performs a shallow search.
@@ -2,9 +2,9 @@
2
2
 
3
3
  module RuboCop
4
4
  module AST
5
- # A node extension for `kwsplat` nodes. This will be used in place of a
6
- # plain node when the builder constructs the AST, making its methods
7
- # available to all `kwsplat` nodes within RuboCop.
5
+ # A node extension for `kwsplat` and `forwarded_kwrestarg` nodes. This will be used in
6
+ # place of a plain node when the builder constructs the AST, making its methods available to
7
+ # all `kwsplat` and `forwarded_kwrestarg` nodes within RuboCop.
8
8
  class KeywordSplatNode < Node
9
9
  include HashElementNode
10
10
 
@@ -41,6 +41,13 @@ module RuboCop
41
41
  def node_parts
42
42
  [self, self]
43
43
  end
44
+
45
+ # This provides `forwarded_kwrestarg` node to return true to be compatible with `kwsplat` node.
46
+ #
47
+ # @return [true]
48
+ def kwsplat_type?
49
+ true
50
+ end
44
51
  end
45
52
  end
46
53
  end
@@ -36,7 +36,10 @@ module RuboCop
36
36
  #
37
37
  # @return [Array<Node>] an array of child nodes
38
38
  def child_nodes
39
- each_child_node.to_a
39
+ # Iterate child nodes directly to avoid allocating an Enumerator.
40
+ nodes = []
41
+ each_child_node { |node| nodes << node }
42
+ nodes
40
43
  end
41
44
 
42
45
  # Calls the given block for each descendant node with depth first order.
@@ -102,7 +105,9 @@ module RuboCop
102
105
  protected
103
106
 
104
107
  def visit_descendants(types, &block)
105
- each_child_node do |child|
108
+ children.each do |child|
109
+ next unless child.is_a?(::AST::Node)
110
+
106
111
  yield child if types.empty? || types.include?(child.type)
107
112
  child.visit_descendants(types, &block)
108
113
  end
@@ -5,7 +5,7 @@ module RuboCop
5
5
  # Common functionality for nodes that are a kind of method dispatch:
6
6
  # `send`, `csend`, `super`, `zsuper`, `yield`, `defined?`,
7
7
  # and (modern only): `index`, `indexasgn`, `lambda`
8
- module MethodDispatchNode
8
+ module MethodDispatchNode # rubocop:disable Metrics/ModuleLength
9
9
  extend NodePattern::Macros
10
10
  include MethodIdentifierPredicates
11
11
 
@@ -28,6 +28,17 @@ module RuboCop
28
28
  node_parts[1]
29
29
  end
30
30
 
31
+ # The source range for the method name or keyword that dispatches this call.
32
+ #
33
+ # @return [Parser::Source::Range] the source range for the method name or keyword
34
+ def selector
35
+ if loc.respond_to? :keyword
36
+ loc.keyword
37
+ else
38
+ loc.selector
39
+ end
40
+ end
41
+
31
42
  # The `block` or `numblock` node associated with this method dispatch, if any.
32
43
  #
33
44
  # @return [BlockNode, nil] the `block` or `numblock` node associated with this method
@@ -147,7 +158,7 @@ module RuboCop
147
158
  #
148
159
  # @return [Boolean] whether the method is the implicit form of `#call`
149
160
  def implicit_call?
150
- method?(:call) && !loc.selector
161
+ method?(:call) && !selector
151
162
  end
152
163
 
153
164
  # Whether this method dispatch has an explicit block.
@@ -211,7 +222,7 @@ module RuboCop
211
222
  #
212
223
  # @return [Boolean] whether this method is a lambda literal
213
224
  def lambda_literal?
214
- block_literal? && loc.expression && loc.expression.source == '->'
225
+ loc.expression.source == '->' && block_literal?
215
226
  end
216
227
 
217
228
  # Checks whether this is a unary operation.
@@ -222,9 +233,9 @@ module RuboCop
222
233
  #
223
234
  # @return [Boolean] whether this method is a unary operation
224
235
  def unary_operation?
225
- return false unless loc.selector
236
+ return false unless selector
226
237
 
227
- operator_method? && loc.expression.begin_pos == loc.selector.begin_pos
238
+ operator_method? && loc.expression.begin_pos == selector.begin_pos
228
239
  end
229
240
 
230
241
  # Checks whether this is a binary operation.
@@ -235,9 +246,9 @@ module RuboCop
235
246
  #
236
247
  # @return [Boolean] whether this method is a binary operation
237
248
  def binary_operation?
238
- return false unless loc.selector
249
+ return false unless selector
239
250
 
240
- operator_method? && loc.expression.begin_pos != loc.selector.begin_pos
251
+ operator_method? && loc.expression.begin_pos != selector.begin_pos
241
252
  end
242
253
 
243
254
  private
@@ -82,9 +82,17 @@ module RuboCop
82
82
  # and optimizes other calls
83
83
  module RestArguments
84
84
  include ParameterizedNode
85
+
86
+ EMPTY_ARGUMENTS = [].freeze
87
+
85
88
  # @return [Array<Node>] arguments, if any
86
89
  def arguments
87
- children[first_argument_index..].freeze
90
+ if arguments?
91
+ children.drop(first_argument_index).freeze
92
+ else
93
+ # Skip unneeded Array allocation.
94
+ EMPTY_ARGUMENTS
95
+ end
88
96
  end
89
97
 
90
98
  # A shorthand for getting the first argument of the node.
@@ -15,6 +15,10 @@ module RuboCop
15
15
  (_ _ _ _ ...)]
16
16
  PATTERN
17
17
 
18
+ def send_type?
19
+ true
20
+ end
21
+
18
22
  private
19
23
 
20
24
  def first_argument_index
@@ -84,8 +84,12 @@ module RuboCop
84
84
  LITERAL_RECURSIVE_TYPES = (OPERATOR_KEYWORDS + COMPOSITE_LITERALS + %i[begin pair]).freeze
85
85
  private_constant :LITERAL_RECURSIVE_METHODS, :LITERAL_RECURSIVE_TYPES
86
86
 
87
+ EMPTY_CHILDREN = [].freeze
88
+ EMPTY_PROPERTIES = {}.freeze
89
+ private_constant :EMPTY_CHILDREN, :EMPTY_PROPERTIES
90
+
87
91
  # @see https://www.rubydoc.info/gems/ast/AST/Node:initialize
88
- def initialize(type, children = [], properties = {})
92
+ def initialize(type, children = EMPTY_CHILDREN, properties = EMPTY_PROPERTIES)
89
93
  @mutable_attributes = {}
90
94
 
91
95
  # ::AST::Node#initialize freezes itself.
@@ -101,11 +105,19 @@ module RuboCop
101
105
  end
102
106
  end
103
107
 
104
- Parser::Meta::NODE_TYPES.each do |node_type|
108
+ (Parser::Meta::NODE_TYPES - [:send]).each do |node_type|
105
109
  method_name = "#{node_type.to_s.gsub(/\W/, '')}_type?"
106
- define_method(method_name) do
107
- type == node_type
108
- end
110
+ class_eval <<~RUBY, __FILE__, __LINE__ + 1
111
+ def #{method_name} # def block_type?
112
+ @type == :#{node_type} # @type == :block
113
+ end # end
114
+ RUBY
115
+ end
116
+
117
+ # Most nodes are of 'send' type, so this method is defined
118
+ # separately to make this check as fast as possible.
119
+ def send_type?
120
+ false
109
121
  end
110
122
 
111
123
  # Returns the parent node, or `nil` if the receiver is a root node.
@@ -203,9 +215,7 @@ module RuboCop
203
215
  # destructuring method.
204
216
  #
205
217
  # @return [Array<Node>] the different parts of the ndde
206
- def node_parts
207
- to_a
208
- end
218
+ alias node_parts to_a
209
219
 
210
220
  # Calls the given block for each ancestor node from parent to root.
211
221
  # If no block is given, an `Enumerator` is returned.
@@ -316,13 +326,13 @@ module RuboCop
316
326
  # what class or module is this method/constant/etc definition in?
317
327
  # returns nil if answer cannot be determined
318
328
  ancestors = each_ancestor(:class, :module, :sclass, :casgn, :block)
319
- result = ancestors.map do |ancestor|
329
+ result = ancestors.filter_map do |ancestor|
320
330
  parent_module_name_part(ancestor) do |full_name|
321
331
  return nil unless full_name
322
332
 
323
333
  full_name
324
334
  end
325
- end.compact.reverse.join('::')
335
+ end.reverse.join('::')
326
336
  result.empty? ? 'Object' : result
327
337
  end
328
338
 
@@ -500,8 +510,14 @@ module RuboCop
500
510
 
501
511
  # @!method class_constructor?(node = self)
502
512
  def_node_matcher :class_constructor?, <<~PATTERN
503
- { (send #global_const?({:Class :Module :Struct}) :new ...)
504
- (block (send #global_const?({:Class :Module :Struct}) :new ...) ...)}
513
+ {
514
+ (send #global_const?({:Class :Module :Struct}) :new ...)
515
+ (send #global_const?(:Data) :define ...)
516
+ ({block numblock} {
517
+ (send #global_const?({:Class :Module :Struct}) :new ...)
518
+ (send #global_const?(:Data) :define ...)
519
+ } ...)
520
+ }
505
521
  PATTERN
506
522
 
507
523
  # @deprecated Use `:class_constructor?`
@@ -38,7 +38,7 @@ module RuboCop
38
38
  end
39
39
 
40
40
  def visit_set
41
- set = node.children.map(&:child).to_set.freeze
41
+ set = node.children.to_set(&:child).freeze
42
42
  NodePattern::Sets[set]
43
43
  end
44
44
 
@@ -47,7 +47,7 @@ module RuboCop
47
47
  # @return [String] a Rainbow colorized version of ruby
48
48
  def colorize(color_scheme = COLOR_SCHEME)
49
49
  map = color_map(color_scheme)
50
- ast.loc.expression.source_buffer.source.chars.map.with_index do |char, i|
50
+ ast.source_range.source_buffer.source.chars.map.with_index do |char, i|
51
51
  Rainbow(char).color(map[i])
52
52
  end.join
53
53
  end
@@ -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.0.
5
+ # Generated by: oedipus_lex version 2.6.1.
6
6
  # Source: lib/rubocop/ast/node_pattern/lexer.rex
7
7
  #++
8
8
 
@@ -75,6 +75,10 @@ module RuboCop
75
75
  self.class.new(type, children, { location: location })
76
76
  end
77
77
 
78
+ def source_range
79
+ loc.expression
80
+ end
81
+
78
82
  INT_TO_RANGE = Hash.new { |h, k| h[k] = k..k }
79
83
  private_constant :INT_TO_RANGE
80
84
 
@@ -203,7 +207,7 @@ module RuboCop
203
207
  include ForbidInSeqHead
204
208
 
205
209
  def arity
206
- min, max = children.map(&:arity_range).map(&:minmax).transpose.map(&:sum)
210
+ min, max = children.map { |child| child.arity_range.minmax }.transpose.map(&:sum)
207
211
  min == max ? min || 0 : min..max # NOTE: || 0 for empty case, where min == max == nil.
208
212
  end
209
213
 
@@ -219,7 +223,7 @@ module RuboCop
219
223
  # Node class for `{ ... }`
220
224
  class Union < Node
221
225
  def arity
222
- minima, maxima = children.map(&:arity_range).map(&:minmax).transpose
226
+ minima, maxima = children.map { |child| child.arity_range.minmax }.transpose
223
227
  min = minima.min
224
228
  max = maxima.max
225
229
  min == max ? min : min..max
@@ -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.5.1
4
+ # This file is automatically generated by Racc 1.7.1
5
5
  # from Racc grammar file "".
6
6
  #
7
7
 
@@ -239,6 +239,7 @@ 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)
242
243
 
243
244
  Racc_token_to_s_table = [
244
245
  "$end",
@@ -289,6 +290,7 @@ Racc_token_to_s_table = [
289
290
  "opt_rest",
290
291
  "rest",
291
292
  "arg_list" ]
293
+ Ractor.make_shareable(Racc_token_to_s_table) if defined?(Ractor)
292
294
 
293
295
  Racc_debug_parser = false
294
296
 
@@ -48,12 +48,12 @@ module RuboCop
48
48
 
49
49
  def emit_unary_op(type, operator_t = nil, *children)
50
50
  children[-1] = children[-1].first if children[-1].is_a?(Array) # token?
51
- map = source_map(children.first.loc.expression, operator_t: operator_t)
51
+ map = source_map(children.first.source_range, operator_t: operator_t)
52
52
  n(type, children, map)
53
53
  end
54
54
 
55
55
  def emit_list(type, begin_t, children, end_t)
56
- expr = children.first.loc.expression.join(children.last.loc.expression)
56
+ expr = children.first.source_range.join(children.last.source_range)
57
57
  map = source_map(expr, begin_t: begin_t, end_t: end_t)
58
58
  n(type, children, map)
59
59
  end
@@ -79,8 +79,7 @@ module RuboCop
79
79
  end
80
80
 
81
81
  def join_exprs(left_expr, right_expr)
82
- left_expr.loc.expression
83
- .join(right_expr.loc.expression)
82
+ left_expr.source_range.join(right_expr.source_range)
84
83
  end
85
84
 
86
85
  def source_map(token_or_range, begin_t: nil, end_t: nil, operator_t: nil, selector_t: nil)
@@ -54,6 +54,22 @@ module RuboCop
54
54
 
55
55
  VAR = 'node'
56
56
 
57
+ # Yields its argument and any descendants, depth-first.
58
+ #
59
+ def self.descend(element, &block)
60
+ return to_enum(__method__, element) unless block
61
+
62
+ yield element
63
+
64
+ if element.is_a?(::RuboCop::AST::Node)
65
+ element.children.each do |child|
66
+ descend(child, &block)
67
+ end
68
+ end
69
+
70
+ nil
71
+ end
72
+
57
73
  attr_reader :pattern, :ast, :match_code
58
74
 
59
75
  def_delegators :@compiler, :captures, :named_parameters, :positional_parameters
@@ -100,22 +116,6 @@ module RuboCop
100
116
  initialize(coder['pattern'])
101
117
  end
102
118
 
103
- # Yields its argument and any descendants, depth-first.
104
- #
105
- def self.descend(element, &block)
106
- return to_enum(__method__, element) unless block
107
-
108
- yield element
109
-
110
- if element.is_a?(::RuboCop::AST::Node)
111
- element.children.each do |child|
112
- descend(child, &block)
113
- end
114
- end
115
-
116
- nil
117
- end
118
-
119
119
  def freeze
120
120
  @match_code.freeze
121
121
  @compiler.freeze
@@ -12,30 +12,40 @@ module RuboCop
12
12
  # @api private
13
13
  STRING_SOURCE_NAME = '(string)'
14
14
 
15
+ INVALID_LEVELS = %i[error fatal].freeze
16
+ private_constant :INVALID_LEVELS
17
+
18
+ PARSER_ENGINES = %i[parser_whitequark parser_prism].freeze
19
+ private_constant :PARSER_ENGINES
20
+
15
21
  attr_reader :path, :buffer, :ast, :comments, :tokens, :diagnostics,
16
- :parser_error, :raw_source, :ruby_version
22
+ :parser_error, :raw_source, :ruby_version, :parser_engine
17
23
 
18
- def self.from_file(path, ruby_version)
24
+ def self.from_file(path, ruby_version, parser_engine: :parser_whitequark)
19
25
  file = File.read(path, mode: 'rb')
20
- new(file, ruby_version, path)
26
+ new(file, ruby_version, path, parser_engine: parser_engine)
21
27
  end
22
28
 
23
- INVALID_LEVELS = %i[error fatal].freeze
24
- private_constant :INVALID_LEVELS
29
+ def initialize(source, ruby_version, path = nil, parser_engine: :parser_whitequark)
30
+ parser_engine = parser_engine.to_sym
31
+ unless PARSER_ENGINES.include?(parser_engine)
32
+ raise ArgumentError, 'The keyword argument `parser_engine` accepts `parser_whitequark` ' \
33
+ "or `parser_prism`, but `#{parser_engine}` was passed."
34
+ end
25
35
 
26
- def initialize(source, ruby_version, path = nil)
27
36
  # Defaults source encoding to UTF-8, regardless of the encoding it has
28
37
  # been read with, which could be non-utf8 depending on the default
29
38
  # external encoding.
30
- source.force_encoding(Encoding::UTF_8) unless source.encoding == Encoding::UTF_8
39
+ (+source).force_encoding(Encoding::UTF_8) unless source.encoding == Encoding::UTF_8
31
40
 
32
41
  @raw_source = source
33
42
  @path = path
34
43
  @diagnostics = []
35
44
  @ruby_version = ruby_version
45
+ @parser_engine = parser_engine
36
46
  @parser_error = nil
37
47
 
38
- parse(source, ruby_version)
48
+ parse(source, ruby_version, parser_engine)
39
49
  end
40
50
 
41
51
  def ast_with_comments
@@ -193,13 +203,13 @@ module RuboCop
193
203
  end
194
204
  end
195
205
 
196
- def parse(source, ruby_version)
206
+ def parse(source, ruby_version, parser_engine)
197
207
  buffer_name = @path || STRING_SOURCE_NAME
198
208
  @buffer = Parser::Source::Buffer.new(buffer_name, 1)
199
209
 
200
210
  begin
201
211
  @buffer.source = source
202
- rescue EncodingError => e
212
+ rescue EncodingError, Parser::UnknownEncodingInMagicComment => e
203
213
  @parser_error = e
204
214
  @ast = nil
205
215
  @comments = []
@@ -207,7 +217,7 @@ module RuboCop
207
217
  return
208
218
  end
209
219
 
210
- @ast, @comments, @tokens = tokenize(create_parser(ruby_version))
220
+ @ast, @comments, @tokens = tokenize(create_parser(ruby_version, parser_engine))
211
221
  end
212
222
 
213
223
  def tokenize(parser)
@@ -227,55 +237,82 @@ module RuboCop
227
237
  end
228
238
 
229
239
  # 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
- else
269
- raise ArgumentError,
270
- "RuboCop found unknown Ruby version: #{ruby_version.inspect}"
240
+ def parser_class(ruby_version, parser_engine)
241
+ case parser_engine
242
+ when :parser_whitequark
243
+ case ruby_version
244
+ when 1.9
245
+ require 'parser/ruby19'
246
+ Parser::Ruby19
247
+ when 2.0
248
+ require 'parser/ruby20'
249
+ Parser::Ruby20
250
+ when 2.1
251
+ require 'parser/ruby21'
252
+ Parser::Ruby21
253
+ when 2.2
254
+ require 'parser/ruby22'
255
+ Parser::Ruby22
256
+ when 2.3
257
+ require 'parser/ruby23'
258
+ Parser::Ruby23
259
+ when 2.4
260
+ require 'parser/ruby24'
261
+ Parser::Ruby24
262
+ when 2.5
263
+ require 'parser/ruby25'
264
+ Parser::Ruby25
265
+ when 2.6
266
+ require 'parser/ruby26'
267
+ Parser::Ruby26
268
+ when 2.7
269
+ require 'parser/ruby27'
270
+ Parser::Ruby27
271
+ when 2.8, 3.0
272
+ require 'parser/ruby30'
273
+ Parser::Ruby30
274
+ when 3.1
275
+ require 'parser/ruby31'
276
+ Parser::Ruby31
277
+ when 3.2
278
+ require 'parser/ruby32'
279
+ Parser::Ruby32
280
+ when 3.3
281
+ require 'parser/ruby33'
282
+ Parser::Ruby33
283
+ when 3.4
284
+ require 'parser/ruby34'
285
+ Parser::Ruby34
286
+ else
287
+ raise ArgumentError, "RuboCop found unknown Ruby version: #{ruby_version.inspect}"
288
+ end
289
+ when :parser_prism
290
+ begin
291
+ require 'prism'
292
+ rescue LoadError
293
+ warn "Error: Unable to load Prism. Add `gem 'prism'` to your Gemfile."
294
+ exit!
295
+ end
296
+
297
+ case ruby_version
298
+ when 3.3
299
+ require 'prism/translation/parser33'
300
+ Prism::Translation::Parser33
301
+ when 3.4
302
+ require 'prism/translation/parser34'
303
+ Prism::Translation::Parser34
304
+ else
305
+ raise ArgumentError, 'RuboCop supports target Ruby versions 3.3 and above with Prism. ' \
306
+ "Specified target Ruby version: #{ruby_version.inspect}"
307
+ end
271
308
  end
272
309
  end
273
310
  # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
274
311
 
275
- def create_parser(ruby_version)
312
+ def create_parser(ruby_version, parser_engine)
276
313
  builder = RuboCop::AST::Builder.new
277
314
 
278
- parser_class(ruby_version).new(builder).tap do |parser|
315
+ parser_class(ruby_version, parser_engine).new(builder).tap do |parser|
279
316
  # On JRuby there's a risk that we hang in tokenize() if we
280
317
  # don't set the all errors as fatal flag. The problem is caused by a bug
281
318
  # in Racc that is discussed in issue #93 of the whitequark/parser
@@ -9,7 +9,8 @@ module RuboCop
9
9
  module Sexp
10
10
  # Creates a {Node} with type `type` and children `children`.
11
11
  def s(type, *children)
12
- Node.new(type, children)
12
+ klass = Builder::NODE_MAP[type] || Node
13
+ klass.new(type, children)
13
14
  end
14
15
  end
15
16
  end
@@ -4,6 +4,8 @@ module RuboCop
4
4
  module AST
5
5
  # A basic wrapper around Parser's tokens.
6
6
  class Token
7
+ LEFT_PAREN_TYPES = %i[tLPAREN tLPAREN2].freeze
8
+
7
9
  attr_reader :pos, :type, :text
8
10
 
9
11
  def self.from_parser_token(parser_token)
@@ -81,7 +83,7 @@ module RuboCop
81
83
  end
82
84
 
83
85
  def left_curly_brace?
84
- type == :tLCURLY
86
+ type == :tLCURLY || type == :tLAMBEG
85
87
  end
86
88
 
87
89
  def right_curly_brace?
@@ -89,7 +91,7 @@ module RuboCop
89
91
  end
90
92
 
91
93
  def left_parens?
92
- %i[tLPAREN tLPAREN2].include?(type)
94
+ LEFT_PAREN_TYPES.include?(type)
93
95
  end
94
96
 
95
97
  def right_parens?
@@ -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
 
@@ -75,7 +75,8 @@ module RuboCop
75
75
  ### arity == 0
76
76
  no_children = %i[true false nil self cbase zsuper redo retry
77
77
  forward_args forwarded_args match_nil_pattern
78
- forward_arg lambda empty_else kwnilarg
78
+ forward_arg forwarded_restarg forwarded_kwrestarg
79
+ lambda empty_else kwnilarg
79
80
  __FILE__ __LINE__ __ENCODING__]
80
81
 
81
82
  ### arity == 0..1
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module AST
5
5
  module Version
6
- STRING = '1.23.0'
6
+ STRING = '1.31.3'
7
7
  end
8
8
  end
9
9
  end
data/lib/rubocop/ast.rb CHANGED
@@ -76,6 +76,7 @@ require_relative 'ast/node/resbody_node'
76
76
  require_relative 'ast/node/return_node'
77
77
  require_relative 'ast/node/self_class_node'
78
78
  require_relative 'ast/node/send_node'
79
+ require_relative 'ast/node/csend_node'
79
80
  require_relative 'ast/node/str_node'
80
81
  require_relative 'ast/node/dstr_node'
81
82
  require_relative 'ast/node/super_node'
@@ -91,5 +92,5 @@ require_relative 'ast/token'
91
92
  require_relative 'ast/traversal'
92
93
  require_relative 'ast/version'
93
94
 
94
- ::RuboCop::AST::NodePattern::Parser.autoload :WithMeta, "#{__dir__}/ast/node_pattern/with_meta"
95
- ::RuboCop::AST::NodePattern::Compiler.autoload :Debug, "#{__dir__}/ast/node_pattern/compiler/debug"
95
+ RuboCop::AST::NodePattern::Parser.autoload :WithMeta, "#{__dir__}/ast/node_pattern/with_meta"
96
+ RuboCop::AST::NodePattern::Compiler.autoload :Debug, "#{__dir__}/ast/node_pattern/compiler/debug"
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.23.0
4
+ version: 1.31.3
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: 2022-10-21 00:00:00.000000000 Z
13
+ date: 2024-04-29 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: parser
@@ -18,34 +18,14 @@ dependencies:
18
18
  requirements:
19
19
  - - ">="
20
20
  - !ruby/object:Gem::Version
21
- version: 3.1.1.0
21
+ version: 3.3.1.0
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
25
25
  requirements:
26
26
  - - ">="
27
27
  - !ruby/object:Gem::Version
28
- version: 3.1.1.0
29
- - !ruby/object:Gem::Dependency
30
- name: bundler
31
- requirement: !ruby/object:Gem::Requirement
32
- requirements:
33
- - - ">="
34
- - !ruby/object:Gem::Version
35
- version: 1.15.0
36
- - - "<"
37
- - !ruby/object:Gem::Version
38
- version: '3.0'
39
- type: :development
40
- prerelease: false
41
- version_requirements: !ruby/object:Gem::Requirement
42
- requirements:
43
- - - ">="
44
- - !ruby/object:Gem::Version
45
- version: 1.15.0
46
- - - "<"
47
- - !ruby/object:Gem::Version
48
- version: '3.0'
28
+ version: 3.3.1.0
49
29
  description: " RuboCop's Node and NodePattern classes.\n"
50
30
  email: rubocop@googlegroups.com
51
31
  executables: []
@@ -76,6 +56,7 @@ files:
76
56
  - lib/rubocop/ast/node/casgn_node.rb
77
57
  - lib/rubocop/ast/node/class_node.rb
78
58
  - lib/rubocop/ast/node/const_node.rb
59
+ - lib/rubocop/ast/node/csend_node.rb
79
60
  - lib/rubocop/ast/node/def_node.rb
80
61
  - lib/rubocop/ast/node/defined_node.rb
81
62
  - lib/rubocop/ast/node/dstr_node.rb
@@ -168,14 +149,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
168
149
  requirements:
169
150
  - - ">="
170
151
  - !ruby/object:Gem::Version
171
- version: 2.6.0
152
+ version: 2.7.0
172
153
  required_rubygems_version: !ruby/object:Gem::Requirement
173
154
  requirements:
174
155
  - - ">="
175
156
  - !ruby/object:Gem::Version
176
157
  version: '0'
177
158
  requirements: []
178
- rubygems_version: 3.3.3
159
+ rubygems_version: 3.5.3
179
160
  signing_key:
180
161
  specification_version: 4
181
162
  summary: RuboCop tools to deal with Ruby code AST.