rubocop-ast 1.32.3 → 1.49.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.
Files changed (51) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +0 -2
  3. data/lib/rubocop/ast/builder.rb +32 -15
  4. data/lib/rubocop/ast/builder_prism.rb +11 -0
  5. data/lib/rubocop/ast/node/args_node.rb +1 -1
  6. data/lib/rubocop/ast/node/array_node.rb +4 -4
  7. data/lib/rubocop/ast/node/asgn_node.rb +2 -0
  8. data/lib/rubocop/ast/node/block_node.rb +9 -8
  9. data/lib/rubocop/ast/node/casgn_node.rb +4 -12
  10. data/lib/rubocop/ast/node/complex_node.rb +13 -0
  11. data/lib/rubocop/ast/node/const_node.rb +1 -52
  12. data/lib/rubocop/ast/node/ensure_node.rb +28 -0
  13. data/lib/rubocop/ast/node/for_node.rb +1 -1
  14. data/lib/rubocop/ast/node/hash_node.rb +1 -1
  15. data/lib/rubocop/ast/node/if_node.rb +10 -3
  16. data/lib/rubocop/ast/node/in_pattern_node.rb +1 -1
  17. data/lib/rubocop/ast/node/keyword_begin_node.rb +44 -0
  18. data/lib/rubocop/ast/node/masgn_node.rb +63 -0
  19. data/lib/rubocop/ast/node/mixin/basic_literal_node.rb +1 -1
  20. data/lib/rubocop/ast/node/mixin/constant_node.rb +62 -0
  21. data/lib/rubocop/ast/node/mixin/descendence.rb +3 -3
  22. data/lib/rubocop/ast/node/mixin/hash_element_node.rb +2 -0
  23. data/lib/rubocop/ast/node/mixin/method_dispatch_node.rb +13 -14
  24. data/lib/rubocop/ast/node/mixin/numeric_node.rb +2 -2
  25. data/lib/rubocop/ast/node/mixin/parameterized_node.rb +3 -2
  26. data/lib/rubocop/ast/node/mixin/predicate_operator_node.rb +7 -2
  27. data/lib/rubocop/ast/node/mlhs_node.rb +29 -0
  28. data/lib/rubocop/ast/node/op_asgn_node.rb +3 -1
  29. data/lib/rubocop/ast/node/str_node.rb +37 -1
  30. data/lib/rubocop/ast/node/until_node.rb +1 -1
  31. data/lib/rubocop/ast/node/var_node.rb +15 -0
  32. data/lib/rubocop/ast/node/when_node.rb +1 -1
  33. data/lib/rubocop/ast/node/while_node.rb +1 -1
  34. data/lib/rubocop/ast/node.rb +110 -24
  35. data/lib/rubocop/ast/node_pattern/compiler/binding.rb +6 -0
  36. data/lib/rubocop/ast/node_pattern/compiler/debug.rb +3 -8
  37. data/lib/rubocop/ast/node_pattern/compiler/node_pattern_subcompiler.rb +1 -1
  38. data/lib/rubocop/ast/node_pattern/compiler/sequence_subcompiler.rb +15 -3
  39. data/lib/rubocop/ast/node_pattern/compiler.rb +1 -0
  40. data/lib/rubocop/ast/node_pattern/lexer.rex +1 -1
  41. data/lib/rubocop/ast/node_pattern/lexer.rex.rb +2 -3
  42. data/lib/rubocop/ast/node_pattern/node.rb +8 -2
  43. data/lib/rubocop/ast/node_pattern/parser.racc.rb +42 -40
  44. data/lib/rubocop/ast/node_pattern.rb +2 -1
  45. data/lib/rubocop/ast/processed_source.rb +92 -25
  46. data/lib/rubocop/ast/token.rb +2 -1
  47. data/lib/rubocop/ast/traversal.rb +35 -24
  48. data/lib/rubocop/ast/utilities/simple_forwardable.rb +16 -5
  49. data/lib/rubocop/ast/version.rb +1 -1
  50. data/lib/rubocop/ast.rb +8 -0
  51. metadata +26 -5
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 289c6acb690241440018fb6d23e23759c6cf52100b2bf373b47226ee71cad215
4
- data.tar.gz: 45cd10ff396ddf599ac11277b5d14a2e7cede79b267a40b2e53a83ccd2580479
3
+ metadata.gz: aad4bf15828f1db50f117c1a983e1f4c7f0cff233878a7f1a1686416ab21feff
4
+ data.tar.gz: 8ac8ad74b061c5fcd733e21420068edc69d0a9bfba532dc766e01dbb80be5c40
5
5
  SHA512:
6
- metadata.gz: 137d5b737b87e1a136b23c6ae6b591334e5971e923c01727c37280b42e96a0a41fc534b54e8ea1b83074256b5d6645f86c079977fd72ffd6404d9f4eb61d92c6
7
- data.tar.gz: d7c4e19d909f3591b4f7460a54e50f77555d37b517baa86359c635a2b32eeb119804464fb17c95adc69f23147f3023322142d44117f0e9ea0262e8c1fdd3aec2
6
+ metadata.gz: 75f3882c819f2e10f7ef893481fbe9feddedfcdc91678c1c695d6ef36d94798dedf5667427cbfc6c022494e7e0c9581f54b4e1dcc9f05f2aea677668fe645b04
7
+ data.tar.gz: 94b37b0490cbf1ab81ab0c4c98a5bd8e6037b6ae5210af6ba2cbd23d4a263a3e4cfccad3f99fe621a6ab62d8356fc007fd066adb2d2379e02f5b07580ef2b2d4
data/README.md CHANGED
@@ -2,8 +2,6 @@
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/rubocop-ast.svg)](https://badge.fury.io/rb/rubocop-ast)
4
4
  [![CI](https://github.com/rubocop/rubocop-ast/actions/workflows/rubocop.yml/badge.svg)](https://github.com/rubocop/rubocop-ast/actions/workflows/rubocop.yml)
5
- [![Test Coverage](https://api.codeclimate.com/v1/badges/a29666e6373bc41bc0a9/test_coverage)](https://codeclimate.com/github/rubocop/rubocop-ast/test_coverage)
6
- [![Maintainability](https://api.codeclimate.com/v1/badges/a29666e6373bc41bc0a9/maintainability)](https://codeclimate.com/github/rubocop/rubocop-ast/maintainability)
7
5
 
8
6
  Contains the classes needed by [RuboCop](https://github.com/rubocop/rubocop) to deal with Ruby's AST, in particular:
9
7
 
@@ -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,11 +32,13 @@ 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,
45
39
  case: CaseNode,
46
40
  class: ClassNode,
41
+ complex: ComplexNode,
47
42
  const: ConstNode,
48
43
  def: DefNode,
49
44
  defined?: DefinedNode,
@@ -63,8 +58,11 @@ module RuboCop
63
58
  irange: RangeNode,
64
59
  erange: RangeNode,
65
60
  kwargs: HashNode,
61
+ kwbegin: KeywordBeginNode,
66
62
  kwsplat: KeywordSplatNode,
67
63
  lambda: LambdaNode,
64
+ masgn: MasgnNode,
65
+ mlhs: MlhsNode,
68
66
  module: ModuleNode,
69
67
  next: NextNode,
70
68
  op_asgn: OpAsgnNode,
@@ -87,6 +85,10 @@ module RuboCop
87
85
  sym: SymbolNode,
88
86
  until: UntilNode,
89
87
  until_post: UntilNode,
88
+ lvar: VarNode,
89
+ ivar: VarNode,
90
+ cvar: VarNode,
91
+ gvar: VarNode,
90
92
  when: WhenNode,
91
93
  while: WhileNode,
92
94
  while_post: WhileNode,
@@ -100,7 +102,7 @@ module RuboCop
100
102
  node_klass(type).new(type, children, location: source_map)
101
103
  end
102
104
 
103
- # TODO: Figure out what to do about literal encoding handling...
105
+ # Overwrite the base method to allow strings with invalid encoding
104
106
  # More details here https://github.com/whitequark/parser/issues/283
105
107
  def string_value(token)
106
108
  value(token)
@@ -112,5 +114,20 @@ module RuboCop
112
114
  NODE_MAP[type] || Node
113
115
  end
114
116
  end
117
+
118
+ # `RuboCop::AST::Builder` is an AST builder that is utilized to let `Parser`
119
+ # generate ASTs with {RuboCop::AST::Node}.
120
+ #
121
+ # @example
122
+ # buffer = Parser::Source::Buffer.new('(string)')
123
+ # buffer.source = 'puts :foo'
124
+ #
125
+ # builder = RuboCop::AST::Builder.new
126
+ # require 'parser/ruby25'
127
+ # parser = Parser::Ruby25.new(builder)
128
+ # root_node = parser.parse(buffer)
129
+ class Builder < Parser::Builders::Default
130
+ include BuilderExtensions
131
+ end
115
132
  end
116
133
  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
@@ -32,7 +32,7 @@ module RuboCop
32
32
  #
33
33
  # @return [Array<Node>] array of argument nodes.
34
34
  def argument_list
35
- each_descendant(*ARGUMENT_TYPES).to_a.freeze
35
+ each_descendant(:argument).to_a.freeze
36
36
  end
37
37
  end
38
38
  end
@@ -7,8 +7,8 @@ module RuboCop
7
7
  # to all `array` nodes within RuboCop.
8
8
  class ArrayNode < Node
9
9
  PERCENT_LITERAL_TYPES = {
10
- string: /^%[wW]/,
11
- symbol: /^%[iI]/
10
+ string: /\A%[wW]/,
11
+ symbol: /\A%[iI]/
12
12
  }.freeze
13
13
  private_constant :PERCENT_LITERAL_TYPES
14
14
 
@@ -34,7 +34,7 @@ module RuboCop
34
34
  #
35
35
  # @return [Boolean] whether the array is enclosed in square brackets
36
36
  def square_brackets?
37
- loc.begin&.is?('[')
37
+ loc_is?(:begin, '[')
38
38
  end
39
39
 
40
40
  # Checks whether the `array` literal is delimited by percent brackets.
@@ -50,7 +50,7 @@ module RuboCop
50
50
  # @return [Boolean] whether the array is enclosed in percent brackets
51
51
  def percent_literal?(type = nil)
52
52
  if type
53
- loc.begin && loc.begin.source =~ PERCENT_LITERAL_TYPES[type]
53
+ loc.begin&.source&.match?(PERCENT_LITERAL_TYPES.fetch(type))
54
54
  else
55
55
  loc.begin&.source&.start_with?('%')
56
56
  end
@@ -12,6 +12,7 @@ module RuboCop
12
12
  def name
13
13
  node_parts[0]
14
14
  end
15
+ alias lhs name
15
16
 
16
17
  # The expression being assigned to the variable.
17
18
  #
@@ -19,6 +20,7 @@ module RuboCop
19
20
  def expression
20
21
  node_parts[1]
21
22
  end
23
+ alias rhs expression
22
24
  end
23
25
  end
24
26
  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
@@ -90,14 +94,14 @@ module RuboCop
90
94
  #
91
95
  # @return [Boolean] whether the `block` literal is enclosed in braces
92
96
  def braces?
93
- loc.end&.is?('}')
97
+ loc.end.is?('}')
94
98
  end
95
99
 
96
100
  # Checks whether the `block` literal is delimited by `do`-`end` keywords.
97
101
  #
98
102
  # @return [Boolean] whether the `block` literal is enclosed in `do`-`end`
99
103
  def keywords?
100
- loc.end&.is?('end')
104
+ loc.end.is?('end')
101
105
  end
102
106
 
103
107
  # The delimiters for this `block` literal.
@@ -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,19 +6,10 @@ 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
12
+ alias lhs short_name
22
13
 
23
14
  # The expression being assigned to the variable.
24
15
  #
@@ -26,6 +17,7 @@ module RuboCop
26
17
  def expression
27
18
  node_parts[2]
28
19
  end
20
+ alias rhs expression
29
21
  end
30
22
  end
31
23
  end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module AST
5
+ # A node extension for `complex` nodes. This will be used in place of a plain
6
+ # node when the builder constructs the AST, making its methods available to
7
+ # all `complex` nodes within RuboCop.
8
+ class ComplexNode < Node
9
+ include BasicLiteralNode
10
+ include NumericNode
11
+ end
12
+ end
13
+ end
@@ -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
@@ -6,13 +6,41 @@ 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`.
15
+ # @deprecated Use `EnsureNode#branch`
12
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
+
27
+ branch
28
+ end
29
+
30
+ # Returns an the ensure branch in the exception handling statement.
31
+ #
32
+ # @return [Node, nil] the body of the ensure branch.
33
+ def branch
13
34
  node_parts[1]
14
35
  end
15
36
 
37
+ # Returns the `rescue` node of the `ensure`, if present.
38
+ #
39
+ # @return [Node, nil] The `rescue` node.
40
+ def rescue_node
41
+ node_parts[0] if node_parts[0].rescue_type?
42
+ end
43
+
16
44
  # Checks whether this node body is a void context.
17
45
  # Always `true` for `ensure`.
18
46
  #
@@ -17,7 +17,7 @@ module RuboCop
17
17
  #
18
18
  # @return [Boolean] whether the `for` node has a `do` keyword
19
19
  def do?
20
- loc.begin&.is?('do')
20
+ loc_is?(:begin, 'do')
21
21
  end
22
22
 
23
23
  # Checks whether this node body is a void context.
@@ -115,7 +115,7 @@ module RuboCop
115
115
  #
116
116
  # @return [Boolean] whether the `hash` literal is enclosed in braces
117
117
  def braces?
118
- loc.end&.is?('}')
118
+ loc_is?(:end, '}')
119
119
  end
120
120
  end
121
121
  end
@@ -25,6 +25,13 @@ module RuboCop
25
25
  keyword == 'unless'
26
26
  end
27
27
 
28
+ # Checks whether the `if` node has an `then` clause.
29
+ #
30
+ # @return [Boolean] whether the node has an `then` clause
31
+ def then?
32
+ loc_is?(:begin, 'then')
33
+ end
34
+
28
35
  # Checks whether the `if` is an `elsif`. Parser handles these by nesting
29
36
  # `if` nodes in the `else` branch.
30
37
  #
@@ -40,14 +47,14 @@ module RuboCop
40
47
  #
41
48
  # @return [Boolean] whether the node has an `else` clause
42
49
  def else?
43
- loc.respond_to?(:else) && loc.else
50
+ loc?(:else)
44
51
  end
45
52
 
46
53
  # Checks whether the `if` node is a ternary operator.
47
54
  #
48
55
  # @return [Boolean] whether the `if` node is a ternary operator
49
56
  def ternary?
50
- loc.respond_to?(:question)
57
+ loc?(:question)
51
58
  end
52
59
 
53
60
  # Returns the keyword of the `if` statement as a string. Returns an empty
@@ -102,7 +109,7 @@ module RuboCop
102
109
  #
103
110
  # @return [Boolean] whether the `if` node has at least one `elsif` branch
104
111
  def elsif_conditional?
105
- else_branch&.if_type? && else_branch&.elsif?
112
+ else_branch&.if_type? && else_branch.elsif?
106
113
  end
107
114
 
108
115
  # Returns the branch of the `if` node that gets evaluated when its
@@ -24,7 +24,7 @@ module RuboCop
24
24
  #
25
25
  # @return [Boolean] whether the `in` node has a `then` keyword
26
26
  def then?
27
- loc.begin&.is?('then')
27
+ loc_is?(:begin, 'then')
28
28
  end
29
29
 
30
30
  # Returns the body of the `in` node.
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module AST
5
+ # A node extension for `kwbegin` 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 `kwbegin` nodes within RuboCop.
8
+ class KeywordBeginNode < Node
9
+ # Returns the body of the `kwbegin` block. Returns `self` if the `kwbegin` contains
10
+ # multiple nodes.
11
+ #
12
+ # @return [Node, nil] The body of the `kwbegin`.
13
+ def body
14
+ return unless node_parts.any?
15
+
16
+ if rescue_node
17
+ rescue_node.body
18
+ elsif ensure_node
19
+ ensure_node.node_parts[0]
20
+ elsif node_parts.one?
21
+ node_parts[0]
22
+ else
23
+ self
24
+ end
25
+ end
26
+
27
+ # Returns the `rescue` node of the `kwbegin` block, if one is present.
28
+ #
29
+ # @return [Node, nil] The `rescue` node within `kwbegin`.
30
+ def ensure_node
31
+ node_parts[0] if node_parts[0]&.ensure_type?
32
+ end
33
+
34
+ # Returns the `rescue` node of the `kwbegin` block, if one is present.
35
+ #
36
+ # @return [Node, nil] The `rescue` node within `kwbegin`.
37
+ def rescue_node
38
+ return ensure_node&.rescue_node if ensure_node&.rescue_node
39
+
40
+ node_parts[0] if node_parts[0]&.rescue_type?
41
+ end
42
+ end
43
+ end
44
+ end
@@ -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.type?(:send, :indexasgn)
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
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module AST
5
5
  # Common functionality for primitive literal nodes: `sym`, `str`,
6
- # `int`, `float`, `rational`...
6
+ # `int`, `float`, `rational`, `complex`...
7
7
  module BasicLiteralNode
8
8
  # Returns the value of the literal.
9
9
  #
@@ -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
@@ -25,7 +25,7 @@ module RuboCop
25
25
  children.each do |child|
26
26
  next unless child.is_a?(::AST::Node)
27
27
 
28
- yield child if types.empty? || types.include?(child.type)
28
+ yield child if types.empty? || child.type?(*types)
29
29
  end
30
30
 
31
31
  self
@@ -95,7 +95,7 @@ module RuboCop
95
95
  def each_node(*types, &block)
96
96
  return to_enum(__method__, *types) unless block
97
97
 
98
- yield self if types.empty? || types.include?(type)
98
+ yield self if types.empty? || type?(*types)
99
99
 
100
100
  visit_descendants(types, &block)
101
101
 
@@ -108,7 +108,7 @@ module RuboCop
108
108
  children.each do |child|
109
109
  next unless child.is_a?(::AST::Node)
110
110
 
111
- yield child if types.empty? || types.include?(child.type)
111
+ yield child if types.empty? || child.type?(*types)
112
112
  child.visit_descendants(types, &block)
113
113
  end
114
114
  end
@@ -99,7 +99,9 @@ module RuboCop
99
99
 
100
100
  def valid_argument_types?
101
101
  [first, second].all? do |argument|
102
+ # rubocop:disable InternalAffairs/NodeTypeMultiplePredicates
102
103
  argument.pair_type? || argument.kwsplat_type?
104
+ # rubocop:enable InternalAffairs/NodeTypeMultiplePredicates
103
105
  end
104
106
  end
105
107