rubocop-ast 1.39.0 → 1.40.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: 7d2a487128abbc36ae15a961ea8b7d1e5c63a266efb81cfbb815965d4f77a4e0
4
- data.tar.gz: 10af6d44e0a7b22a6e43f3e14e720c0df6991be9efc2e38ad47c5526d170d935
3
+ metadata.gz: 572b1323e4358215b5e093fa0326fb04dc3d7986746143b75bd0d58223d2dc9e
4
+ data.tar.gz: 96d40dd742d28e480ca3be7dd7fd16bf9f410e7631373d4c441a74530bfc0314
5
5
  SHA512:
6
- metadata.gz: 4eec6dc6fbb933a3c8628cfc54332e1b019b7423b298ac155899cfc62db3e52ddfd1a3308d20bea5e856250ed56af0a84ae856e4ef482e7368e0f1c1be264639
7
- data.tar.gz: 1e20432910c02a763ccc0d99ac18771b4ac0ac578c2d749ecc77e9084ec4784551fb8778ee3bbdf24709a9ec00f046320cb37c9975038c04f5674a24db943c04
6
+ metadata.gz: fd75e5325a907aead5239937ee34447d16da764f1fe2a31d140c9d4f5398b4c4a81c5f88a697bb62149845fc24691b53d526cc5ac05b1060472e92fceaceefae
7
+ data.tar.gz: d04d2d4dcf76bd6b23e9f6b2a44af18562d8bed8d9767b0511e58feabe762f90fba42f73869500fbe8c34ec1d0e4b6bebe74e7b3bbee18a83b284a97c05d477c
@@ -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 = {
@@ -107,7 +100,7 @@ module RuboCop
107
100
  node_klass(type).new(type, children, location: source_map)
108
101
  end
109
102
 
110
- # TODO: Figure out what to do about literal encoding handling...
103
+ # Overwrite the base method to allow strings with invalid encoding
111
104
  # More details here https://github.com/whitequark/parser/issues/283
112
105
  def string_value(token)
113
106
  value(token)
@@ -119,5 +112,20 @@ module RuboCop
119
112
  NODE_MAP[type] || Node
120
113
  end
121
114
  end
115
+
116
+ # `RuboCop::AST::Builder` is an AST builder that is utilized to let `Parser`
117
+ # generate ASTs with {RuboCop::AST::Node}.
118
+ #
119
+ # @example
120
+ # buffer = Parser::Source::Buffer.new('(string)')
121
+ # buffer.source = 'puts :foo'
122
+ #
123
+ # builder = RuboCop::AST::Builder.new
124
+ # require 'parser/ruby25'
125
+ # parser = Parser::Ruby25.new(builder)
126
+ # root_node = parser.parse(buffer)
127
+ class Builder < Parser::Builders::Default
128
+ include BuilderExtensions
129
+ end
122
130
  end
123
131
  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
@@ -314,10 +314,10 @@ module RuboCop
314
314
 
315
315
  case ruby_version
316
316
  when 3.3
317
- require_prism_translation_parser(ruby_version)
317
+ require 'prism/translation/parser33'
318
318
  Prism::Translation::Parser33
319
319
  when 3.4
320
- require_prism_translation_parser(ruby_version)
320
+ require 'prism/translation/parser34'
321
321
  Prism::Translation::Parser34
322
322
  else
323
323
  raise ArgumentError, 'RuboCop supports target Ruby versions 3.3 and above with Prism. ' \
@@ -326,34 +326,45 @@ module RuboCop
326
326
  end
327
327
  end
328
328
 
329
+ def builder_class(parser_engine)
330
+ case parser_engine
331
+ when :parser_whitequark
332
+ RuboCop::AST::Builder
333
+ when :parser_prism
334
+ require_prism
335
+ require_relative 'builder_prism'
336
+ RuboCop::AST::BuilderPrism
337
+ end
338
+ end
339
+
329
340
  # Prism is a native extension, a `LoadError` will be raised if linked to an incompatible
330
341
  # Ruby version. Only raise if it really was caused by Prism not being present.
342
+ # rubocop:disable Metrics/MethodLength
331
343
  def require_prism
332
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
333
357
  rescue LoadError => e
334
358
  raise unless e.path == 'prism'
335
359
 
336
360
  warn "Error: Unable to load Prism. Add `gem 'prism'` to your Gemfile."
337
361
  exit!
338
362
  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!
352
- end
363
+ # rubocop:enable Metrics/MethodLength
353
364
 
354
365
  # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
355
366
  def create_parser(ruby_version, parser_engine, prism_result)
356
- builder = RuboCop::AST::Builder.new
367
+ builder = builder_class(parser_engine).new
357
368
 
358
369
  parser_class = parser_class(ruby_version, parser_engine)
359
370
 
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module AST
5
5
  module Version
6
- STRING = '1.39.0'
6
+ STRING = '1.40.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.39.0
4
+ version: 1.40.0
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-03-19 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: parser
@@ -39,6 +39,7 @@ files:
39
39
  - lib/rubocop-ast.rb
40
40
  - lib/rubocop/ast.rb
41
41
  - lib/rubocop/ast/builder.rb
42
+ - lib/rubocop/ast/builder_prism.rb
42
43
  - lib/rubocop/ast/ext/range.rb
43
44
  - lib/rubocop/ast/node.rb
44
45
  - lib/rubocop/ast/node/alias_node.rb