rubocop-ast 1.32.2 → 1.33.0

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: a2dd457b35c9658041396de931f65b32bb032b029e403c98c7e1da559497e217
4
- data.tar.gz: 948fddf25b05f66a0ae568a4143edc2418f99bda4875fec16be4b07a40636656
3
+ metadata.gz: 01f8352e57bd480b219c3219e60a2e7750e03dfbec0e34c0c0235139d11c0870
4
+ data.tar.gz: 390a70bd777f6525138ab82faf2242d90ee1c1ace80dc7219e420e4d8780e296
5
5
  SHA512:
6
- metadata.gz: 97e596f863ead1485b8e87835ae7443c4bbb82b9a5d03f9d04310fec54f07251407c9ce222c9fae50b022a294973017bb191f066579ccdec201b87c6f0b7169a
7
- data.tar.gz: f91ff65c507970aeb2924e648e7a82ecab3538d8c3b4c4c083d12f3343244ca62cca311762d5e272107b5608e9899d47d9846280a1a2d328f29d7a840440f2ea
6
+ metadata.gz: b949d204d6c94ac8b4a3ac2546eb25f21a317cb74ea2a8b668c06e504a8b37775c4b6b3257ee59a15231cfbcc97d5c20500680d244e722173dffd4dd80620baf
7
+ data.tar.gz: b0cbd03ab6d36c40a450e1faccd8eeecdb1b8edfcb0d8dae0e3e3c0adb5050a88967b8fdccb1a553e2f5e63fcfadebb5327e54c25d941df02633071d21a3b449
@@ -65,6 +65,8 @@ module RuboCop
65
65
  kwargs: HashNode,
66
66
  kwsplat: KeywordSplatNode,
67
67
  lambda: LambdaNode,
68
+ masgn: MasgnNode,
69
+ mlhs: MlhsNode,
68
70
  module: ModuleNode,
69
71
  next: NextNode,
70
72
  op_asgn: OpAsgnNode,
@@ -87,6 +89,10 @@ module RuboCop
87
89
  sym: SymbolNode,
88
90
  until: UntilNode,
89
91
  until_post: UntilNode,
92
+ lvar: VarNode,
93
+ ivar: VarNode,
94
+ cvar: VarNode,
95
+ gvar: VarNode,
90
96
  when: WhenNode,
91
97
  while: WhileNode,
92
98
  while_post: WhileNode,
@@ -90,14 +90,14 @@ module RuboCop
90
90
  #
91
91
  # @return [Boolean] whether the `block` literal is enclosed in braces
92
92
  def braces?
93
- loc.end&.is?('}')
93
+ loc.end.is?('}')
94
94
  end
95
95
 
96
96
  # Checks whether the `block` literal is delimited by `do`-`end` keywords.
97
97
  #
98
98
  # @return [Boolean] whether the `block` literal is enclosed in `do`-`end`
99
99
  def keywords?
100
- loc.end&.is?('end')
100
+ loc.end.is?('end')
101
101
  end
102
102
 
103
103
  # The delimiters for this `block` literal.
@@ -6,19 +6,9 @@ module RuboCop
6
6
  # This will be used in place of a plain node when the builder constructs
7
7
  # the AST, making its methods available to all assignment nodes within RuboCop.
8
8
  class CasgnNode < Node
9
- # The namespace of the constant being assigned.
10
- #
11
- # @return [Node, nil] the node associated with the scope (e.g. cbase, const, ...)
12
- def namespace
13
- node_parts[0]
14
- end
9
+ include ConstantNode
15
10
 
16
- # The name of the variable being assigned as a symbol.
17
- #
18
- # @return [Symbol] the name of the variable being assigned
19
- def name
20
- node_parts[1]
21
- end
11
+ alias name short_name
22
12
 
23
13
  # The expression being assigned to the variable.
24
14
  #
@@ -4,58 +4,7 @@ module RuboCop
4
4
  module AST
5
5
  # A node extension for `const` nodes.
6
6
  class ConstNode < Node
7
- # @return [Node, nil] the node associated with the scope (e.g. cbase, const, ...)
8
- def namespace
9
- children[0]
10
- end
11
-
12
- # @return [Symbol] the demodulized name of the constant: "::Foo::Bar" => :Bar
13
- def short_name
14
- children[1]
15
- end
16
-
17
- # @return [Boolean] if the constant is a Module / Class, according to the standard convention.
18
- # Note: some classes might have uppercase in which case this method
19
- # returns false
20
- def module_name?
21
- short_name.match?(/[[:lower:]]/)
22
- end
23
- alias class_name? module_name?
24
-
25
- # @return [Boolean] if the constant starts with `::` (aka s(:cbase))
26
- def absolute?
27
- return false unless namespace
28
-
29
- each_path.first.cbase_type?
30
- end
31
-
32
- # @return [Boolean] if the constant does not start with `::` (aka s(:cbase))
33
- def relative?
34
- !absolute?
35
- end
36
-
37
- # Yield nodes for the namespace
38
- #
39
- # For `::Foo::Bar::BAZ` => yields:
40
- # s(:cbase), then
41
- # s(:const, :Foo), then
42
- # s(:const, s(:const, :Foo), :Bar)
43
- def each_path(&block)
44
- return to_enum(__method__) unless block
45
-
46
- descendants = []
47
- last = self
48
- loop do
49
- last = last.children.first
50
- break if last.nil?
51
-
52
- descendants << last
53
- break unless last.const_type?
54
- end
55
- descendants.reverse_each(&block)
56
-
57
- self
58
- end
7
+ include ConstantNode
59
8
  end
60
9
  end
61
10
  end
@@ -13,7 +13,7 @@ module RuboCop
13
13
  #
14
14
  # @return [Boolean] whether the `def` node body is a void context
15
15
  def void_context?
16
- method?(:initialize) || assignment_method?
16
+ (def_type? && method?(:initialize)) || assignment_method?
17
17
  end
18
18
 
19
19
  # Checks whether this method definition node forwards its arguments
@@ -102,7 +102,7 @@ module RuboCop
102
102
  #
103
103
  # @return [Boolean] whether the `if` node has at least one `elsif` branch
104
104
  def elsif_conditional?
105
- else_branch&.if_type? && else_branch&.elsif?
105
+ else_branch&.if_type? && else_branch.elsif?
106
106
  end
107
107
 
108
108
  # Returns the branch of the `if` node that gets evaluated when its
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module AST
5
+ # A node extension for `masgn` nodes.
6
+ # This will be used in place of a plain node when the builder constructs
7
+ # the AST, making its methods available to all assignment nodes within RuboCop.
8
+ class MasgnNode < Node
9
+ # @return [MlhsNode] the `mlhs` node
10
+ def lhs
11
+ # The first child is a `mlhs` node
12
+ node_parts[0]
13
+ end
14
+
15
+ # @return [Array<Node>] the assignment nodes of the multiple assignment
16
+ def assignments
17
+ lhs.assignments
18
+ end
19
+
20
+ # @return [Array<Symbol>] names of all the variables being assigned
21
+ def names
22
+ assignments.map do |assignment|
23
+ if assignment.send_type? || assignment.indexasgn_type?
24
+ assignment.method_name
25
+ else
26
+ assignment.name
27
+ end
28
+ end
29
+ end
30
+
31
+ # The RHS (right hand side) of the multiple assignment. This returns
32
+ # the nodes as parsed: either a single node if the RHS has a single value,
33
+ # or an `array` node containing multiple nodes.
34
+ #
35
+ # NOTE: Due to how parsing works, `expression` will return the same for
36
+ # `a, b = x, y` and `a, b = [x, y]`.
37
+ #
38
+ # @return [Node] the right hand side of a multiple assignment.
39
+ def expression
40
+ node_parts[1]
41
+ end
42
+ alias rhs expression
43
+
44
+ # In contrast to `expression`, `values` always returns a Ruby array
45
+ # containing all the nodes being assigned on the RHS.
46
+ #
47
+ # Literal arrays are considered a singular value; but unlike `expression`,
48
+ # implied `array` nodes from assigning multiple values on the RHS are treated
49
+ # as separate.
50
+ #
51
+ # @return [Array<Node>] individual values being assigned on the RHS of the multiple assignment
52
+ def values
53
+ multiple_rhs? ? expression.children : [expression]
54
+ end
55
+
56
+ private
57
+
58
+ def multiple_rhs?
59
+ expression.array_type? && !expression.bracketed?
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module AST
5
+ # Common functionality for nodes that deal with constants:
6
+ # `const`, `casgn`.
7
+ module ConstantNode
8
+ # @return [Node, nil] the node associated with the scope (e.g. cbase, const, ...)
9
+ def namespace
10
+ children[0]
11
+ end
12
+
13
+ # @return [Symbol] the demodulized name of the constant: "::Foo::Bar" => :Bar
14
+ def short_name
15
+ children[1]
16
+ end
17
+
18
+ # @return [Boolean] if the constant is a Module / Class, according to the standard convention.
19
+ # Note: some classes might have uppercase in which case this method
20
+ # returns false
21
+ def module_name?
22
+ short_name.match?(/[[:lower:]]/)
23
+ end
24
+ alias class_name? module_name?
25
+
26
+ # @return [Boolean] if the constant starts with `::` (aka s(:cbase))
27
+ def absolute?
28
+ return false unless namespace
29
+
30
+ each_path.first.cbase_type?
31
+ end
32
+
33
+ # @return [Boolean] if the constant does not start with `::` (aka s(:cbase))
34
+ def relative?
35
+ !absolute?
36
+ end
37
+
38
+ # Yield nodes for the namespace
39
+ #
40
+ # For `::Foo::Bar::BAZ` => yields:
41
+ # s(:cbase), then
42
+ # s(:const, :Foo), then
43
+ # s(:const, s(:const, :Foo), :Bar)
44
+ def each_path(&block)
45
+ return to_enum(__method__) unless block
46
+
47
+ descendants = []
48
+ last = self
49
+ loop do
50
+ last = last.children.first
51
+ break if last.nil?
52
+
53
+ descendants << last
54
+ break unless last.const_type?
55
+ end
56
+ descendants.reverse_each(&block)
57
+
58
+ self
59
+ end
60
+ end
61
+ end
62
+ end
@@ -117,7 +117,7 @@ module RuboCop
117
117
  #
118
118
  # @return [Boolean] whether the method was called with a connecting dot
119
119
  def dot?
120
- loc.respond_to?(:dot) && loc.dot && loc.dot.is?('.')
120
+ loc.respond_to?(:dot) && loc.dot&.is?('.')
121
121
  end
122
122
 
123
123
  # Checks whether the dispatched method uses a double colon to connect the
@@ -125,7 +125,7 @@ module RuboCop
125
125
  #
126
126
  # @return [Boolean] whether the method was called with a connecting dot
127
127
  def double_colon?
128
- loc.respond_to?(:dot) && loc.dot && loc.dot.is?('::')
128
+ loc.respond_to?(:dot) && loc.dot&.is?('::')
129
129
  end
130
130
 
131
131
  # Checks whether the dispatched method uses a safe navigation operator to
@@ -133,7 +133,7 @@ module RuboCop
133
133
  #
134
134
  # @return [Boolean] whether the method was called with a connecting dot
135
135
  def safe_navigation?
136
- loc.respond_to?(:dot) && loc.dot && loc.dot.is?('&.')
136
+ loc.respond_to?(:dot) && loc.dot&.is?('&.')
137
137
  end
138
138
 
139
139
  # Checks whether the *explicit* receiver of this method dispatch is
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module AST
5
+ # A node extension for `mlhs` nodes.
6
+ # This will be used in place of a plain node when the builder constructs
7
+ # the AST, making its methods available to all assignment nodes within RuboCop.
8
+ class MlhsNode < Node
9
+ # Returns all the assignment nodes on the left hand side (LHS) of a multiple assignment.
10
+ # These are generally assignment nodes (`lvasgn`, `ivasgn`, `cvasgn`, `gvasgn`, `casgn`)
11
+ # but can also be `send` nodes in case of `foo.bar, ... =` or `foo[:bar], ... =`.
12
+ #
13
+ # @return [Array<Node>] the assignment nodes of the multiple assignment LHS
14
+ def assignments
15
+ child_nodes.flat_map do |node|
16
+ if node.splat_type?
17
+ node.child_nodes.first
18
+ elsif node.mlhs_type?
19
+ node.assignments
20
+ else
21
+ node
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -9,7 +9,7 @@ module RuboCop
9
9
  include BasicLiteralNode
10
10
 
11
11
  def character_literal?
12
- loc.respond_to?(:begin) && loc.begin && loc.begin.is?('?')
12
+ loc.respond_to?(:begin) && loc.begin&.is?('?')
13
13
  end
14
14
 
15
15
  def heredoc?
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module AST
5
+ # A node extension for `lvar`, `ivar`, `cvar` and `gvar` nodes.
6
+ # This will be used in place of a plain node when the builder constructs
7
+ # the AST, making its methods available to all assignment nodes within RuboCop.
8
+ class VarNode < Node
9
+ # @return [Symbol] The name of the variable.
10
+ def name
11
+ node_parts[0]
12
+ end
13
+ end
14
+ end
15
+ end
@@ -460,7 +460,7 @@ module RuboCop
460
460
  end
461
461
 
462
462
  def parenthesized_call?
463
- loc.respond_to?(:begin) && loc.begin && loc.begin.is?('(')
463
+ loc.respond_to?(:begin) && loc.begin&.is?('(')
464
464
  end
465
465
 
466
466
  def call_type?
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module AST
5
5
  module Version
6
- STRING = '1.32.2'
6
+ STRING = '1.33.0'
7
7
  end
8
8
  end
9
9
  end
data/lib/rubocop/ast.rb CHANGED
@@ -27,6 +27,7 @@ require_relative 'ast/node/mixin/method_identifier_predicates'
27
27
  require_relative 'ast/node/mixin/binary_operator_node'
28
28
  require_relative 'ast/node/mixin/collection_node'
29
29
  require_relative 'ast/node/mixin/conditional_node'
30
+ require_relative 'ast/node/mixin/constant_node'
30
31
  require_relative 'ast/node/mixin/hash_element_node'
31
32
  require_relative 'ast/node/mixin/method_dispatch_node'
32
33
  require_relative 'ast/node/mixin/modifier_node'
@@ -61,6 +62,8 @@ require_relative 'ast/node/indexasgn_node'
61
62
  require_relative 'ast/node/int_node'
62
63
  require_relative 'ast/node/keyword_splat_node'
63
64
  require_relative 'ast/node/lambda_node'
65
+ require_relative 'ast/node/masgn_node'
66
+ require_relative 'ast/node/mlhs_node'
64
67
  require_relative 'ast/node/module_node'
65
68
  require_relative 'ast/node/next_node'
66
69
  require_relative 'ast/node/op_asgn_node'
@@ -83,6 +86,7 @@ require_relative 'ast/node/dstr_node'
83
86
  require_relative 'ast/node/super_node'
84
87
  require_relative 'ast/node/symbol_node'
85
88
  require_relative 'ast/node/until_node'
89
+ require_relative 'ast/node/var_node'
86
90
  require_relative 'ast/node/when_node'
87
91
  require_relative 'ast/node/while_node'
88
92
  require_relative 'ast/node/yield_node'
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.32.2
4
+ version: 1.33.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: 2024-09-03 00:00:00.000000000 Z
13
+ date: 2024-10-29 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: parser
@@ -71,10 +71,12 @@ files:
71
71
  - lib/rubocop/ast/node/int_node.rb
72
72
  - lib/rubocop/ast/node/keyword_splat_node.rb
73
73
  - lib/rubocop/ast/node/lambda_node.rb
74
+ - lib/rubocop/ast/node/masgn_node.rb
74
75
  - lib/rubocop/ast/node/mixin/basic_literal_node.rb
75
76
  - lib/rubocop/ast/node/mixin/binary_operator_node.rb
76
77
  - lib/rubocop/ast/node/mixin/collection_node.rb
77
78
  - lib/rubocop/ast/node/mixin/conditional_node.rb
79
+ - lib/rubocop/ast/node/mixin/constant_node.rb
78
80
  - lib/rubocop/ast/node/mixin/descendence.rb
79
81
  - lib/rubocop/ast/node/mixin/hash_element_node.rb
80
82
  - lib/rubocop/ast/node/mixin/method_dispatch_node.rb
@@ -83,6 +85,7 @@ files:
83
85
  - lib/rubocop/ast/node/mixin/numeric_node.rb
84
86
  - lib/rubocop/ast/node/mixin/parameterized_node.rb
85
87
  - lib/rubocop/ast/node/mixin/predicate_operator_node.rb
88
+ - lib/rubocop/ast/node/mlhs_node.rb
86
89
  - lib/rubocop/ast/node/module_node.rb
87
90
  - lib/rubocop/ast/node/next_node.rb
88
91
  - lib/rubocop/ast/node/op_asgn_node.rb
@@ -102,6 +105,7 @@ files:
102
105
  - lib/rubocop/ast/node/super_node.rb
103
106
  - lib/rubocop/ast/node/symbol_node.rb
104
107
  - lib/rubocop/ast/node/until_node.rb
108
+ - lib/rubocop/ast/node/var_node.rb
105
109
  - lib/rubocop/ast/node/when_node.rb
106
110
  - lib/rubocop/ast/node/while_node.rb
107
111
  - lib/rubocop/ast/node/yield_node.rb