rubocop-ast 1.24.0 → 1.26.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: 78554cccfe256fe1f9c47816aee520d541d7cc9cc1e6da0fedc3aaa881a788c4
4
- data.tar.gz: fd568b92e24931d617f4ee51153215a8ebd49bc3942cab17df4e64611b89748f
3
+ metadata.gz: bc10e7c795dab5de6b3af9e4256ac4312e5c3593dac55ac4bf290386813d8375
4
+ data.tar.gz: 4f5712401b5ce837a33b44a9e1e5061ca134fe794a04a9bdb46d7941564be73d
5
5
  SHA512:
6
- metadata.gz: 46fb9ce602917ce32b77592da9ebfa0fc84ba5fd22c236d7afc70cd7a8bbc4b74855cdc54d69592c97cfb03977124dc400b4c4cca5b6c353af835ee69e84c529
7
- data.tar.gz: 4834a36fc8d5976d0e10007be2777d7f0a395d41dd47a0668c1a03062cfd79ea4b972c70988be90e76624e26ce3757f3f18e34e6e3041eef70f0b0a20f11c2b7
6
+ metadata.gz: db2491014949e08395d008635c29a22d56b6495d020adc3ecc83071bf9f8614be7ff00088a69e82cc4add3ea118a71a28f3903ed3905012312191988cb61ec92
7
+ data.tar.gz: 7ba42b76e1fa3721ac7d41e83d9ad4a87ec214c02938af1b6f2162e80ec55a4706698bee254591cdf50c3467648a95d972ad615fc559f0e8669a8de64552aa83
@@ -75,7 +75,7 @@ module RuboCop
75
75
  rescue: RescueNode,
76
76
  resbody: ResbodyNode,
77
77
  return: ReturnNode,
78
- csend: SendNode,
78
+ csend: CsendNode,
79
79
  send: SendNode,
80
80
  str: StrNode,
81
81
  xstr: StrNode,
@@ -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
@@ -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
@@ -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
@@ -211,7 +211,7 @@ module RuboCop
211
211
  #
212
212
  # @return [Boolean] whether this method is a lambda literal
213
213
  def lambda_literal?
214
- block_literal? && loc.expression && loc.expression.source == '->'
214
+ loc.expression.source == '->' && block_literal?
215
215
  end
216
216
 
217
217
  # Checks whether this is a unary operation.
@@ -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.
@@ -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?`
@@ -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,6 +12,9 @@ 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
+
15
18
  attr_reader :path, :buffer, :ast, :comments, :tokens, :diagnostics,
16
19
  :parser_error, :raw_source, :ruby_version
17
20
 
@@ -20,9 +23,6 @@ module RuboCop
20
23
  new(file, ruby_version, path)
21
24
  end
22
25
 
23
- INVALID_LEVELS = %i[error fatal].freeze
24
- private_constant :INVALID_LEVELS
25
-
26
26
  def initialize(source, ruby_version, path = nil)
27
27
  # Defaults source encoding to UTF-8, regardless of the encoding it has
28
28
  # been read with, which could be non-utf8 depending on the default
@@ -265,6 +265,9 @@ module RuboCop
265
265
  when 3.2
266
266
  require 'parser/ruby32'
267
267
  Parser::Ruby32
268
+ when 3.3
269
+ require 'parser/ruby33'
270
+ Parser::Ruby33
268
271
  else
269
272
  raise ArgumentError,
270
273
  "RuboCop found unknown Ruby version: #{ruby_version.inspect}"
@@ -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)
@@ -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?
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module AST
5
5
  module Version
6
- STRING = '1.24.0'
6
+ STRING = '1.26.0'
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.24.0
4
+ version: 1.26.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: 2022-11-30 00:00:00.000000000 Z
13
+ date: 2023-02-11 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: parser
@@ -18,14 +18,14 @@ dependencies:
18
18
  requirements:
19
19
  - - ">="
20
20
  - !ruby/object:Gem::Version
21
- version: 3.1.1.0
21
+ version: 3.2.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
28
+ version: 3.2.1.0
29
29
  - !ruby/object:Gem::Dependency
30
30
  name: bundler
31
31
  requirement: !ruby/object:Gem::Requirement
@@ -76,6 +76,7 @@ files:
76
76
  - lib/rubocop/ast/node/casgn_node.rb
77
77
  - lib/rubocop/ast/node/class_node.rb
78
78
  - lib/rubocop/ast/node/const_node.rb
79
+ - lib/rubocop/ast/node/csend_node.rb
79
80
  - lib/rubocop/ast/node/def_node.rb
80
81
  - lib/rubocop/ast/node/defined_node.rb
81
82
  - lib/rubocop/ast/node/dstr_node.rb
@@ -175,7 +176,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
175
176
  - !ruby/object:Gem::Version
176
177
  version: '0'
177
178
  requirements: []
178
- rubygems_version: 3.3.3
179
+ rubygems_version: 3.4.1
179
180
  signing_key:
180
181
  specification_version: 4
181
182
  summary: RuboCop tools to deal with Ruby code AST.