rubocop-ast 1.39.0 → 1.44.1

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: 7d2a487128abbc36ae15a961ea8b7d1e5c63a266efb81cfbb815965d4f77a4e0
4
- data.tar.gz: 10af6d44e0a7b22a6e43f3e14e720c0df6991be9efc2e38ad47c5526d170d935
3
+ metadata.gz: 9310771bd0730e0408e6db665c1f0db5d52ae370aa7eb761ac3942777f4d6cee
4
+ data.tar.gz: ccd0f8b884bc0f3d8404e46db57cabd956ac8e808873ead64c4ba38a2d2387fe
5
5
  SHA512:
6
- metadata.gz: 4eec6dc6fbb933a3c8628cfc54332e1b019b7423b298ac155899cfc62db3e52ddfd1a3308d20bea5e856250ed56af0a84ae856e4ef482e7368e0f1c1be264639
7
- data.tar.gz: 1e20432910c02a763ccc0d99ac18771b4ac0ac578c2d749ecc77e9084ec4784551fb8778ee3bbdf24709a9ec00f046320cb37c9975038c04f5674a24db943c04
6
+ metadata.gz: 74c053c8bcba554f9816b44b58f1d8b531c643d615961c9ffb56e79177d59a596477565b13d6317de7e7132dd7c70d9428ae8381a1757c9836073515edad8740
7
+ data.tar.gz: cd6d7101a68f5ccb8657acf28d094c07a44bbd6312b8a381e16f34f8a4211929a00c26d6f5adced13ef7da085d704ffd8d303d7ae0ade38d41de0792aa5c6898
@@ -2,20 +2,13 @@
2
2
 
3
3
  module RuboCop
4
4
  module AST
5
- # `RuboCop::AST::Builder` is an AST builder that is utilized to let `Parser`
6
- # generate ASTs with {RuboCop::AST::Node}.
7
- #
8
- # @example
9
- # buffer = Parser::Source::Buffer.new('(string)')
10
- # buffer.source = 'puts :foo'
11
- #
12
- # builder = RuboCop::AST::Builder.new
13
- # require 'parser/ruby25'
14
- # parser = Parser::Ruby25.new(builder)
15
- # root_node = parser.parse(buffer)
16
- class Builder < Parser::Builders::Default
17
- self.emit_forward_arg = true if respond_to?(:emit_forward_arg=)
18
- self.emit_match_pattern = true if respond_to?(:emit_match_pattern=)
5
+ # Common functionality between the parser and prism builder
6
+ # @api private
7
+ module BuilderExtensions
8
+ def self.included(base)
9
+ base.emit_forward_arg = true
10
+ base.emit_match_pattern = true
11
+ end
19
12
 
20
13
  # @api private
21
14
  NODE_MAP = {
@@ -39,6 +32,7 @@ module RuboCop
39
32
  gvasgn: AsgnNode,
40
33
  block: BlockNode,
41
34
  numblock: BlockNode,
35
+ itblock: BlockNode,
42
36
  break: BreakNode,
43
37
  case_match: CaseMatchNode,
44
38
  casgn: CasgnNode,
@@ -107,7 +101,7 @@ module RuboCop
107
101
  node_klass(type).new(type, children, location: source_map)
108
102
  end
109
103
 
110
- # TODO: Figure out what to do about literal encoding handling...
104
+ # Overwrite the base method to allow strings with invalid encoding
111
105
  # More details here https://github.com/whitequark/parser/issues/283
112
106
  def string_value(token)
113
107
  value(token)
@@ -119,5 +113,20 @@ module RuboCop
119
113
  NODE_MAP[type] || Node
120
114
  end
121
115
  end
116
+
117
+ # `RuboCop::AST::Builder` is an AST builder that is utilized to let `Parser`
118
+ # generate ASTs with {RuboCop::AST::Node}.
119
+ #
120
+ # @example
121
+ # buffer = Parser::Source::Buffer.new('(string)')
122
+ # buffer.source = 'puts :foo'
123
+ #
124
+ # builder = RuboCop::AST::Builder.new
125
+ # require 'parser/ruby25'
126
+ # parser = Parser::Ruby25.new(builder)
127
+ # root_node = parser.parse(buffer)
128
+ class Builder < Parser::Builders::Default
129
+ include BuilderExtensions
130
+ end
122
131
  end
123
132
  end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module AST
5
+ # A parser builder, based on the one provided by prism,
6
+ # which is capable of emitting AST for more recent Rubies.
7
+ class BuilderPrism < Prism::Translation::Parser::Builder
8
+ include BuilderExtensions
9
+ end
10
+ end
11
+ end
@@ -11,6 +11,8 @@ module RuboCop
11
11
  class BlockNode < Node
12
12
  include MethodIdentifierPredicates
13
13
 
14
+ IT_BLOCK_ARGUMENT = [ArgNode.new(:arg, [:it])].freeze
15
+ private_constant :IT_BLOCK_ARGUMENT
14
16
  VOID_CONTEXT_METHODS = %i[each tap].freeze
15
17
  private_constant :VOID_CONTEXT_METHODS
16
18
 
@@ -46,10 +48,10 @@ module RuboCop
46
48
  #
47
49
  # @return [Array<Node>]
48
50
  def arguments
49
- if numblock_type?
50
- [].freeze # Numbered parameters have no block arguments.
51
- else
51
+ if block_type?
52
52
  node_parts[1]
53
+ else
54
+ [].freeze # Numblocks and itblocks have no explicit block arguments.
53
55
  end
54
56
  end
55
57
 
@@ -60,6 +62,8 @@ module RuboCop
60
62
  def argument_list
61
63
  if numblock_type?
62
64
  numbered_arguments
65
+ elsif itblock_type?
66
+ IT_BLOCK_ARGUMENT
63
67
  else
64
68
  arguments.argument_list
65
69
  end
@@ -153,10 +157,7 @@ module RuboCop
153
157
 
154
158
  private
155
159
 
156
- # Numbered arguments of this `numblock`.
157
160
  def numbered_arguments
158
- return [].freeze unless numblock_type?
159
-
160
161
  max_param = children[1]
161
162
  1.upto(max_param).map do |i|
162
163
  ArgNode.new(:arg, [:"_#{i}"])
@@ -6,11 +6,24 @@ module RuboCop
6
6
  # node when the builder constructs the AST, making its methods available
7
7
  # to all `ensure` nodes within RuboCop.
8
8
  class EnsureNode < Node
9
+ DEPRECATION_WARNING_LOCATION_CACHE = [] # rubocop:disable Style/MutableConstant
10
+ private_constant :DEPRECATION_WARNING_LOCATION_CACHE
11
+
9
12
  # Returns the body of the `ensure` clause.
10
13
  #
11
14
  # @return [Node, nil] The body of the `ensure`.
12
15
  # @deprecated Use `EnsureNode#branch`
13
16
  def body
17
+ first_caller = caller(1..1).first
18
+
19
+ unless DEPRECATION_WARNING_LOCATION_CACHE.include?(first_caller)
20
+ warn '`EnsureNode#body` is deprecated and will be changed in the next major version of ' \
21
+ 'rubocop-ast. Use `EnsureNode#branch` instead to get the body of the `ensure` branch.'
22
+ warn "Called from:\n#{caller.join("\n")}\n\n"
23
+
24
+ DEPRECATION_WARNING_LOCATION_CACHE << first_caller
25
+ end
26
+
14
27
  branch
15
28
  end
16
29
 
@@ -39,10 +39,10 @@ module RuboCop
39
39
  end
40
40
  end
41
41
 
42
- # The `block` or `numblock` node associated with this method dispatch, if any.
42
+ # The `block`, `numblock`, or `itblock` node associated with this method dispatch, if any.
43
43
  #
44
- # @return [BlockNode, nil] the `block` or `numblock` node associated with this method
45
- # call or `nil`
44
+ # @return [BlockNode, nil] the `block`, `numblock`, or `itblock` node associated with this
45
+ # method call or `nil`
46
46
  def block_node
47
47
  parent if block_literal?
48
48
  end
@@ -200,8 +200,7 @@ module RuboCop
200
200
  arg = node.children[2]
201
201
 
202
202
  return unless node.send_type? && node.receiver.nil? && arg.is_a?(::AST::Node)
203
-
204
- return arg if arg.type?(:def, :defs)
203
+ return arg if arg.any_def_type?
205
204
 
206
205
  def_modifier(arg)
207
206
  end
@@ -271,7 +270,7 @@ module RuboCop
271
270
 
272
271
  # @!method adjacent_def_modifier?(node = self)
273
272
  def_node_matcher :adjacent_def_modifier?, <<~PATTERN
274
- (send nil? _ ({def defs} ...))
273
+ (send nil? _ (any_def ...))
275
274
  PATTERN
276
275
 
277
276
  # @!method bare_access_modifier_declaration?(node = self)
@@ -87,6 +87,9 @@ module RuboCop
87
87
 
88
88
  # @api private
89
89
  GROUP_FOR_TYPE = {
90
+ def: :any_def,
91
+ defs: :any_def,
92
+
90
93
  arg: :argument,
91
94
  optarg: :argument,
92
95
  restarg: :argument,
@@ -112,7 +115,8 @@ module RuboCop
112
115
  csend: :call,
113
116
 
114
117
  block: :any_block,
115
- numblock: :any_block
118
+ numblock: :any_block,
119
+ itblock: :any_block
116
120
  }.freeze
117
121
  private_constant :GROUP_FOR_TYPE
118
122
 
@@ -512,6 +516,10 @@ module RuboCop
512
516
  parent&.send_type? && parent.arguments.include?(self)
513
517
  end
514
518
 
519
+ def any_def_type?
520
+ GROUP_FOR_TYPE[type] == :any_def
521
+ end
522
+
515
523
  def argument_type?
516
524
  GROUP_FOR_TYPE[type] == :argument
517
525
  end
@@ -109,14 +109,7 @@ module RuboCop
109
109
  private
110
110
 
111
111
  def ruby_ast(ruby)
112
- buffer = ::Parser::Source::Buffer.new('(ruby)', source: ruby)
113
- ruby_parser.parse(buffer)
114
- end
115
-
116
- def ruby_parser
117
- require 'parser/current'
118
- builder = ::RuboCop::AST::Builder.new
119
- ::Parser::CurrentRuby.new(builder)
112
+ ProcessedSource.new(ruby, RUBY_VERSION.to_f, '(ruby)').ast
120
113
  end
121
114
  end
122
115
 
@@ -35,25 +35,21 @@ module RuboCop
35
35
  INVALID_LEVELS = %i[error fatal].freeze
36
36
  private_constant :INVALID_LEVELS
37
37
 
38
- PARSER_ENGINES = %i[parser_whitequark parser_prism].freeze
38
+ PARSER_ENGINES = %i[default parser_whitequark parser_prism].freeze
39
39
  private_constant :PARSER_ENGINES
40
40
 
41
41
  attr_reader :path, :buffer, :ast, :comments, :tokens, :diagnostics,
42
42
  :parser_error, :raw_source, :ruby_version, :parser_engine
43
43
 
44
- def self.from_file(path, ruby_version, parser_engine: :parser_whitequark)
44
+ def self.from_file(path, ruby_version, parser_engine: :default)
45
45
  file = File.read(path, mode: 'rb')
46
46
  new(file, ruby_version, path, parser_engine: parser_engine)
47
47
  end
48
48
 
49
49
  def initialize(
50
- source, ruby_version, path = nil, parser_engine: :parser_whitequark, prism_result: nil
50
+ source, ruby_version, path = nil, parser_engine: :default, prism_result: nil
51
51
  )
52
- parser_engine = parser_engine.to_sym
53
- unless PARSER_ENGINES.include?(parser_engine)
54
- raise ArgumentError, 'The keyword argument `parser_engine` accepts `parser_whitequark` ' \
55
- "or `parser_prism`, but `#{parser_engine}` was passed."
56
- end
52
+ parser_engine = normalize_parser_engine(parser_engine, ruby_version)
57
53
 
58
54
  # Defaults source encoding to UTF-8, regardless of the encoding it has
59
55
  # been read with, which could be non-utf8 depending on the default
@@ -307,18 +303,20 @@ module RuboCop
307
303
  require 'parser/ruby34'
308
304
  Parser::Ruby34
309
305
  else
310
- raise ArgumentError, "RuboCop found unknown Ruby version: #{ruby_version.inspect}"
306
+ raise ArgumentError, 'RuboCop supports target Ruby versions 3.4 and below with ' \
307
+ "`parser`. Specified target Ruby version: #{ruby_version.inspect}"
311
308
  end
312
309
  when :parser_prism
313
- require_prism
314
-
315
310
  case ruby_version
316
311
  when 3.3
317
- require_prism_translation_parser(ruby_version)
312
+ require 'prism/translation/parser33'
318
313
  Prism::Translation::Parser33
319
314
  when 3.4
320
- require_prism_translation_parser(ruby_version)
315
+ require 'prism/translation/parser34'
321
316
  Prism::Translation::Parser34
317
+ when 3.5
318
+ require 'prism/translation/parser35'
319
+ Prism::Translation::Parser35
322
320
  else
323
321
  raise ArgumentError, 'RuboCop supports target Ruby versions 3.3 and above with Prism. ' \
324
322
  "Specified target Ruby version: #{ruby_version.inspect}"
@@ -326,43 +324,22 @@ module RuboCop
326
324
  end
327
325
  end
328
326
 
329
- # Prism is a native extension, a `LoadError` will be raised if linked to an incompatible
330
- # Ruby version. Only raise if it really was caused by Prism not being present.
331
- def require_prism
332
- require 'prism'
333
- rescue LoadError => e
334
- raise unless e.path == 'prism'
335
-
336
- warn "Error: Unable to load Prism. Add `gem 'prism'` to your Gemfile."
337
- exit!
338
- end
339
-
340
- # While Prism is not yet a dependency, users may run with outdated versions that
341
- # don't have all the parsers.
342
- def require_prism_translation_parser(version)
343
- require "prism/translation/parser#{version.to_s.delete('.')}"
344
- rescue LoadError
345
- warn <<~MESSAGE
346
- Error: Unable to load Prism parser for Ruby #{version}.
347
- * If you're using Bundler and don't yet have `gem 'prism'` as a dependency, add it now.
348
- * If you're using Bundler and already have `gem 'prism'` as a dependency, update it to the most recent version.
349
- * If you don't use Bundler, run `gem update prism`.
350
- MESSAGE
351
- exit!
327
+ def builder_class(parser_engine)
328
+ case parser_engine
329
+ when :parser_whitequark
330
+ RuboCop::AST::Builder
331
+ when :parser_prism
332
+ RuboCop::AST::BuilderPrism
333
+ end
352
334
  end
353
335
 
354
336
  # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
355
337
  def create_parser(ruby_version, parser_engine, prism_result)
356
- builder = RuboCop::AST::Builder.new
338
+ builder = builder_class(parser_engine).new
357
339
 
358
340
  parser_class = parser_class(ruby_version, parser_engine)
359
341
 
360
- # NOTE: Check if the `Prism#initialize` method has the `:parser` keyword argument.
361
- # The `:parser` keyword argument cannot be used to switch parsers because older versions of
362
- # Prism do not support it.
363
- parser_switch_available = parser_class.instance_method(:initialize).parameters.assoc(:key)
364
-
365
- parser_instance = if prism_result && parser_switch_available
342
+ parser_instance = if prism_result
366
343
  # NOTE: Since it is intended for use with Ruby LSP, it targets only Prism.
367
344
  # If there is no reuse of a pre-parsed result, such as in Ruby LSP,
368
345
  # regular parsing with Prism occurs, and `else` branch will be executed.
@@ -386,6 +363,31 @@ module RuboCop
386
363
  end
387
364
  # rubocop:enable Metrics/AbcSize, Metrics/MethodLength
388
365
 
366
+ def normalize_parser_engine(parser_engine, ruby_version)
367
+ parser_engine = parser_engine.to_sym
368
+ unless PARSER_ENGINES.include?(parser_engine)
369
+ raise ArgumentError, 'The keyword argument `parser_engine` accepts `default`, ' \
370
+ "`parser_whitequark`, or `parser_prism`, but `#{parser_engine}` " \
371
+ 'was passed.'
372
+ end
373
+ if parser_engine == :default
374
+ default_parser_engine(ruby_version)
375
+ else
376
+ parser_engine
377
+ end
378
+ end
379
+
380
+ # The Parser gem does not support Ruby 3.5 or later.
381
+ # It is also not fully compatible with Ruby 3.4 but for
382
+ # now respects using parser for backwards compatibility.
383
+ def default_parser_engine(ruby_version)
384
+ if ruby_version >= 3.4
385
+ :parser_prism
386
+ else
387
+ :parser_whitequark
388
+ end
389
+ end
390
+
389
391
  def first_token_index(range_or_node)
390
392
  begin_pos = source_range(range_or_node).begin_pos
391
393
  sorted_tokens.bsearch_index { |token| token.begin_pos >= begin_pos }
@@ -118,7 +118,7 @@ module RuboCop
118
118
  many_node_children = %i[dstr dsym xstr regexp array hash pair
119
119
  mlhs masgn or_asgn and_asgn rasgn mrasgn
120
120
  undef alias args super yield or and
121
- while_post until_post iflipflop eflipflop
121
+ while_post until_post
122
122
  match_with_lvasgn begin kwbegin return
123
123
  in_match match_alt break next
124
124
  match_as array_pattern array_pattern_with_tail
@@ -126,7 +126,7 @@ module RuboCop
126
126
  index indexasgn procarg0 kwargs]
127
127
  many_opt_node_children = %i[case rescue resbody ensure for when
128
128
  case_match in_pattern irange erange
129
- match_pattern match_pattern_p]
129
+ match_pattern match_pattern_p iflipflop eflipflop]
130
130
 
131
131
  ### Callbacks for above
132
132
  def_callback no_children
@@ -159,6 +159,7 @@ module RuboCop
159
159
  def_callback :if, :always, :nil?, :nil?
160
160
  def_callback :block, :always, :always, :nil?
161
161
  def_callback :numblock, :always, :skip, :nil?
162
+ def_callback :itblock, :always, :skip, :nil?
162
163
  def_callback :defs, :always, :skip, :always, :nil?
163
164
 
164
165
  def_callback %i[send csend], body: <<~RUBY
@@ -176,7 +177,7 @@ module RuboCop
176
177
 
177
178
  to_define = ::Parser::Meta::NODE_TYPES.to_a
178
179
  to_define -= defined
179
- to_define -= %i[numargs ident] # transient
180
+ to_define -= %i[numargs itarg ident] # transient
180
181
  to_define -= %i[blockarg_expr restarg_expr] # obsolete
181
182
  to_define -= %i[objc_kwarg objc_restarg objc_varargs] # mac_ruby
182
183
  def_callback to_define, body: <<~RUBY
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module AST
5
5
  module Version
6
- STRING = '1.39.0'
6
+ STRING = '1.44.1'
7
7
  end
8
8
  end
9
9
  end
data/lib/rubocop/ast.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'parser'
4
+ require 'prism'
4
5
  require 'forwardable'
5
6
  require 'set'
6
7
 
@@ -92,6 +93,7 @@ require_relative 'ast/node/when_node'
92
93
  require_relative 'ast/node/while_node'
93
94
  require_relative 'ast/node/yield_node'
94
95
  require_relative 'ast/builder'
96
+ require_relative 'ast/builder_prism'
95
97
  require_relative 'ast/processed_source'
96
98
  require_relative 'ast/rubocop_compatibility'
97
99
  require_relative 'ast/token'
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.39.0
4
+ version: 1.44.1
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: 2025-03-16 00:00:00.000000000 Z
13
+ date: 2025-04-11 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: parser
@@ -18,14 +18,28 @@ dependencies:
18
18
  requirements:
19
19
  - - ">="
20
20
  - !ruby/object:Gem::Version
21
- version: 3.3.1.0
21
+ version: 3.3.7.2
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.3.1.0
28
+ version: 3.3.7.2
29
+ - !ruby/object:Gem::Dependency
30
+ name: prism
31
+ requirement: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - "~>"
34
+ - !ruby/object:Gem::Version
35
+ version: '1.4'
36
+ type: :runtime
37
+ prerelease: false
38
+ version_requirements: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - "~>"
41
+ - !ruby/object:Gem::Version
42
+ version: '1.4'
29
43
  description: " RuboCop's Node and NodePattern classes.\n"
30
44
  email: rubocop@googlegroups.com
31
45
  executables: []
@@ -39,6 +53,7 @@ files:
39
53
  - lib/rubocop-ast.rb
40
54
  - lib/rubocop/ast.rb
41
55
  - lib/rubocop/ast/builder.rb
56
+ - lib/rubocop/ast/builder_prism.rb
42
57
  - lib/rubocop/ast/ext/range.rb
43
58
  - lib/rubocop/ast/node.rb
44
59
  - lib/rubocop/ast/node/alias_node.rb