rubocop-ast 1.41.0 → 1.44.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: 19ee463e3a3cb36f4cac11ef8a15c891d15ab7a7b616cab272bcef1e5d1511ad
4
- data.tar.gz: d01d708fe51840b874ac59409db40e12860f04d639159606633bcaf31b73a22a
3
+ metadata.gz: 9c81addbafdef9eafbc9cff169bf71dd053d5afac7b5f60a048b4c2f03f3660a
4
+ data.tar.gz: cae0da568f2f9a1d29876369cc1a5158879148a3fd3d773fd73f0fe765836efa
5
5
  SHA512:
6
- metadata.gz: 3172e1d4d9ec2d01a910ef63dbb2593b8c685936e9399ecc6d701cbb808969f25eadf6a7b46bce1993202693c03c11b9bc724be0c2c0e4018213a96ffc41e3b0
7
- data.tar.gz: 33871fb889a66acdc8c39c7007f3b829cd2158f3a3c6cc111a924f1350bb1b0bcafa17de4e2c4216f0065449455b29cbec12fa3483cea9eb5623301a9060219e
6
+ metadata.gz: 5a05ff6ffe86fc1fe0376f541f48df20d6f6df15bf0236757358f9c1c7b8231feff8efc6bd9b6aaff5f44c5eee8787c5540cde7ec14a85f346e832cc41ebe079
7
+ data.tar.gz: f02aba449bf7a41ad37dbd4b6efb0781555a908e3cba6459c2f88f182f36e9385ef13747e9af94740ad7c6d46d27ab5bf635231baedaa711a704a3f7ff8e8d52
@@ -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,
@@ -513,6 +516,10 @@ module RuboCop
513
516
  parent&.send_type? && parent.arguments.include?(self)
514
517
  end
515
518
 
519
+ def any_def_type?
520
+ GROUP_FOR_TYPE[type] == :any_def
521
+ end
522
+
516
523
  def argument_type?
517
524
  GROUP_FOR_TYPE[type] == :argument
518
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,11 +303,10 @@ 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
312
  require 'prism/translation/parser33'
@@ -319,6 +314,9 @@ module RuboCop
319
314
  when 3.4
320
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}"
@@ -331,49 +329,17 @@ module RuboCop
331
329
  when :parser_whitequark
332
330
  RuboCop::AST::Builder
333
331
  when :parser_prism
334
- require_prism
335
- require_relative 'builder_prism'
336
332
  RuboCop::AST::BuilderPrism
337
333
  end
338
334
  end
339
335
 
340
- # Prism is a native extension, a `LoadError` will be raised if linked to an incompatible
341
- # Ruby version. Only raise if it really was caused by Prism not being present.
342
- # rubocop:disable Metrics/MethodLength
343
- def require_prism
344
- require 'prism'
345
- required_prism_version = '1.4.0'
346
- if required_prism_version > Prism::VERSION
347
- # While Prism is not yet a dependency, users may run with outdated versions that
348
- # don't have all the parsers.
349
- warn <<~MESSAGE
350
- Error: Prism version #{Prism::VERSION} was loaded, but rubocop-ast requires #{required_prism_version}.
351
- * If you're using Bundler and don't yet have `gem 'prism'` as a dependency, add it now.
352
- * If you're using Bundler and already have `gem 'prism'` as a dependency, update it to the most recent version.
353
- * If you don't use Bundler, run `gem update prism`.
354
- MESSAGE
355
- exit!
356
- end
357
- rescue LoadError => e
358
- raise unless e.path == 'prism'
359
-
360
- warn "Error: Unable to load Prism. Add `gem 'prism'` to your Gemfile."
361
- exit!
362
- end
363
- # rubocop:enable Metrics/MethodLength
364
-
365
336
  # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
366
337
  def create_parser(ruby_version, parser_engine, prism_result)
367
338
  builder = builder_class(parser_engine).new
368
339
 
369
340
  parser_class = parser_class(ruby_version, parser_engine)
370
341
 
371
- # NOTE: Check if the `Prism#initialize` method has the `:parser` keyword argument.
372
- # The `:parser` keyword argument cannot be used to switch parsers because older versions of
373
- # Prism do not support it.
374
- parser_switch_available = parser_class.instance_method(:initialize).parameters.assoc(:key)
375
-
376
- parser_instance = if prism_result && parser_switch_available
342
+ parser_instance = if prism_result
377
343
  # NOTE: Since it is intended for use with Ruby LSP, it targets only Prism.
378
344
  # If there is no reuse of a pre-parsed result, such as in Ruby LSP,
379
345
  # regular parsing with Prism occurs, and `else` branch will be executed.
@@ -397,6 +363,31 @@ module RuboCop
397
363
  end
398
364
  # rubocop:enable Metrics/AbcSize, Metrics/MethodLength
399
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
+
400
391
  def first_token_index(range_or_node)
401
392
  begin_pos = source_range(range_or_node).begin_pos
402
393
  sorted_tokens.bsearch_index { |token| token.begin_pos >= begin_pos }
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module AST
5
5
  module Version
6
- STRING = '1.41.0'
6
+ STRING = '1.44.0'
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.41.0
4
+ version: 1.44.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bozhidar Batsov
@@ -9,7 +9,7 @@ authors:
9
9
  - Yuji Nakayama
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2025-03-20 00:00:00.000000000 Z
12
+ date: 2025-04-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: parser
@@ -25,6 +25,20 @@ dependencies:
25
25
  - - ">="
26
26
  - !ruby/object:Gem::Version
27
27
  version: 3.3.7.2
28
+ - !ruby/object:Gem::Dependency
29
+ name: prism
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '1.4'
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '1.4'
28
42
  description: " RuboCop's Node and NodePattern classes.\n"
29
43
  email: rubocop@googlegroups.com
30
44
  executables: []