rubocop-ast 1.8.0 → 1.11.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: 9c8abd574665322b04af26974900562ce6224cdccb59fc1d8926971e39f0503f
4
- data.tar.gz: 87096e43215bc016c8afc66194a93dc22567960e3bfebfaa99468e8736e35b36
3
+ metadata.gz: fc07fcefa159e288e9bfe924e4f974841ce03bc2bd46cca4baf4ea600296b2e8
4
+ data.tar.gz: 7e0eaf77c2f769a2ed491c171b591e53c6e14744d197616792fc45d9b063a9f9
5
5
  SHA512:
6
- metadata.gz: 6a92317d5065b2b344c0130eaf37e77a410f4e0652cc30b9724ed44f0de05173213418bf9fd990b52de44e437547e90c0c6198f237c37ad8c8f99c9f4d0f2d9a
7
- data.tar.gz: 9a304655d736e46e9f2f8a2094e4f0ac5ed7bcd1aa0ad19b16ad954f3b4d170d965203b24552478505454ac1f38650a393c8a0aede4c9412c083259756bf9ea1
6
+ metadata.gz: 5795ea28b4acae46dfe11ccfbd596c87ef0edc9ee20d2a248979bcbbeabefdf812d7a981f9ad648879730114767019a0e894894a6154c456e39fbe0ea9562c29
7
+ data.tar.gz: 5395f0a9f747811636ed1f62d0aecdd43d18d2aac7d00b550a40c81a2f82feb082ad62b381265e9b803df5eef21f15fa3f10d51d3ca2b76517356d338ec94dc4
@@ -20,6 +20,7 @@ module RuboCop
20
20
  # @api private
21
21
  NODE_MAP = {
22
22
  and: AndNode,
23
+ and_asgn: AndAsgnNode,
23
24
  alias: AliasNode,
24
25
  arg: ArgNode,
25
26
  blockarg: ArgNode,
@@ -32,10 +33,15 @@ module RuboCop
32
33
  shadowarg: ArgNode,
33
34
  args: ArgsNode,
34
35
  array: ArrayNode,
36
+ lvasgn: AsgnNode,
37
+ ivasgn: AsgnNode,
38
+ cvasgn: AsgnNode,
39
+ gvasgn: AsgnNode,
35
40
  block: BlockNode,
36
41
  numblock: BlockNode,
37
42
  break: BreakNode,
38
43
  case_match: CaseMatchNode,
44
+ casgn: CasgnNode,
39
45
  case: CaseNode,
40
46
  class: ClassNode,
41
47
  const: ConstNode,
@@ -60,6 +66,8 @@ module RuboCop
60
66
  lambda: LambdaNode,
61
67
  module: ModuleNode,
62
68
  next: NextNode,
69
+ op_asgn: OpAsgnNode,
70
+ or_asgn: OrAsgnNode,
63
71
  or: OrNode,
64
72
  pair: PairNode,
65
73
  procarg0: Procarg0Node,
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module AST
5
+ # A node extension for `op_asgn` 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 AndAsgnNode < OpAsgnNode
9
+ # The operator being used for assignment as a symbol.
10
+ #
11
+ # @return [Symbol] the assignment operator
12
+ def operator
13
+ :'&&'
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module AST
5
+ # A node extension for `lvasgn`, `ivasgn`, `cvasgn`, and `gvasgn` 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 AsgnNode < Node
9
+ # The name of the variable being assigned as a symbol.
10
+ #
11
+ # @return [Symbol] the name of the variable being assigned
12
+ def name
13
+ node_parts[0]
14
+ end
15
+
16
+ # The expression being assigned to the variable.
17
+ #
18
+ # @return [Node] the expression being assigned.
19
+ def expression
20
+ node_parts[1]
21
+ end
22
+ end
23
+ end
24
+ end
@@ -47,6 +47,7 @@ module RuboCop
47
47
  # Returns the else branch of the `case` statement, if any.
48
48
  #
49
49
  # @return [Node] the else branch node of the `case` statement
50
+ # @return [EmptyElse] the empty else branch node of the `case` statement
50
51
  # @return [nil] if the case statement does not have an else branch.
51
52
  def else_branch
52
53
  node_parts[-1]
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module AST
5
+ # A node extension for `casgn` 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 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
15
+
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
22
+
23
+ # The expression being assigned to the variable.
24
+ #
25
+ # @return [Node] the expression being assigned.
26
+ def expression
27
+ node_parts[2]
28
+ end
29
+ end
30
+ end
31
+ end
@@ -4,8 +4,6 @@ module RuboCop
4
4
  module AST
5
5
  # A node extension for `const` nodes.
6
6
  class ConstNode < Node
7
- # The `send` node associated with this block.
8
- #
9
7
  # @return [Node, nil] the node associated with the scope (e.g. cbase, const, ...)
10
8
  def namespace
11
9
  children[0]
@@ -16,8 +14,6 @@ module RuboCop
16
14
  children[1]
17
15
  end
18
16
 
19
- # The body of this block.
20
- #
21
17
  # @return [Boolean] if the constant is a Module / Class, according to the standard convention.
22
18
  # Note: some classes might have uppercase in which case this method
23
19
  # returns false
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module AST
5
+ # A node extension for `op_asgn` 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 OpAsgnNode < Node
9
+ # @return [AsgnNode] the assignment node
10
+ def assignment_node
11
+ node_parts[0]
12
+ end
13
+
14
+ # The name of the variable being assigned as a symbol.
15
+ #
16
+ # @return [Symbol] the name of the variable being assigned
17
+ def name
18
+ assignment_node.name
19
+ end
20
+
21
+ # The operator being used for assignment as a symbol.
22
+ #
23
+ # @return [Symbol] the assignment operator
24
+ def operator
25
+ node_parts[1]
26
+ end
27
+
28
+ # The expression being assigned to the variable.
29
+ #
30
+ # @return [Node] the expression being assigned.
31
+ def expression
32
+ node_parts.last
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module AST
5
+ # A node extension for `op_asgn` 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 OrAsgnNode < OpAsgnNode
9
+ # The operator being used for assignment as a symbol.
10
+ #
11
+ # @return [Symbol] the assignment operator
12
+ def operator
13
+ :'||'
14
+ end
15
+ end
16
+ end
17
+ end
@@ -317,7 +317,11 @@ module RuboCop
317
317
  # returns nil if answer cannot be determined
318
318
  ancestors = each_ancestor(:class, :module, :sclass, :casgn, :block)
319
319
  result = ancestors.map do |ancestor|
320
- parent_module_name_part(ancestor) { |full_name| return full_name }
320
+ parent_module_name_part(ancestor) do |full_name|
321
+ return nil unless full_name
322
+
323
+ full_name
324
+ end
321
325
  end.compact.reverse.join('::')
322
326
  result.empty? ? 'Object' : result
323
327
  end
@@ -503,20 +507,20 @@ module RuboCop
503
507
  # @deprecated Use `:class_constructor?`
504
508
  # @!method struct_constructor?(node = self)
505
509
  def_node_matcher :struct_constructor?, <<~PATTERN
506
- (block (send #global_const?(:Struct) :new ...) _ $_)
510
+ ({block numblock} (send #global_const?(:Struct) :new ...) _ $_)
507
511
  PATTERN
508
512
 
509
513
  # @!method class_definition?(node = self)
510
514
  def_node_matcher :class_definition?, <<~PATTERN
511
515
  {(class _ _ $_)
512
516
  (sclass _ $_)
513
- (block (send #global_const?({:Struct :Class}) :new ...) _ $_)}
517
+ ({block numblock} (send #global_const?({:Struct :Class}) :new ...) _ $_)}
514
518
  PATTERN
515
519
 
516
520
  # @!method module_definition?(node = self)
517
521
  def_node_matcher :module_definition?, <<~PATTERN
518
522
  {(module _ $_)
519
- (block (send #global_const?(:Module) :new ...) _ $_)}
523
+ ({block numblock} (send #global_const?(:Module) :new ...) _ $_)}
520
524
  PATTERN
521
525
 
522
526
  # Some expressions are evaluated for their value, some for their side
@@ -54,7 +54,7 @@ module RuboCop
54
54
  children_nodes.sum(&:nb_captures)
55
55
  end
56
56
 
57
- # @return [Boolean] returns true iff matches variable number of elements
57
+ # @return [Boolean] returns whether it matches a variable number of elements
58
58
  def variadic?
59
59
  arity.is_a?(Range)
60
60
  end
@@ -80,23 +80,23 @@ module RuboCop
80
80
  "#<#{self.class} #{pattern}>"
81
81
  end
82
82
 
83
- def marshal_load(pattern) #:nodoc:
83
+ def marshal_load(pattern) # :nodoc:
84
84
  initialize pattern
85
85
  end
86
86
 
87
- def marshal_dump #:nodoc:
87
+ def marshal_dump # :nodoc:
88
88
  pattern
89
89
  end
90
90
 
91
- def as_json(_options = nil) #:nodoc:
91
+ def as_json(_options = nil) # :nodoc:
92
92
  pattern
93
93
  end
94
94
 
95
- def encode_with(coder) #:nodoc:
95
+ def encode_with(coder) # :nodoc:
96
96
  coder['pattern'] = pattern
97
97
  end
98
98
 
99
- def init_with(coder) #:nodoc:
99
+ def init_with(coder) # :nodoc:
100
100
  initialize(coder['pattern'])
101
101
  end
102
102
 
@@ -177,6 +177,14 @@ module RuboCop
177
177
  sorted_tokens[last_token_index(range_or_node)]
178
178
  end
179
179
 
180
+ # The tokens list is always sorted by token position, except for cases when heredoc
181
+ # is passed as a method argument. In this case tokens are interleaved by
182
+ # heredoc contents' tokens.
183
+ def sorted_tokens
184
+ # Use stable sort.
185
+ @sorted_tokens ||= tokens.sort_by.with_index { |token, i| [token.begin_pos, i] }
186
+ end
187
+
180
188
  private
181
189
 
182
190
  def comment_index
@@ -272,14 +280,6 @@ module RuboCop
272
280
  sorted_tokens.bsearch_index { |token| token.end_pos >= end_pos }
273
281
  end
274
282
 
275
- # The tokens list is always sorted by token position, except for cases when heredoc
276
- # is passed as a method argument. In this case tokens are interleaved by
277
- # heredoc contents' tokens.
278
- def sorted_tokens
279
- # Use stable sort.
280
- @sorted_tokens ||= tokens.sort_by.with_index { |token, i| [token.begin_pos, i] }
281
- end
282
-
283
283
  def source_range(range_or_node)
284
284
  if range_or_node.respond_to?(:source_range)
285
285
  range_or_node.source_range
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module AST
5
5
  module Version
6
- STRING = '1.8.0'
6
+ STRING = '1.11.0'
7
7
  end
8
8
  end
9
9
  end
data/lib/rubocop/ast.rb CHANGED
@@ -39,10 +39,12 @@ require_relative 'ast/node/and_node'
39
39
  require_relative 'ast/node/arg_node'
40
40
  require_relative 'ast/node/args_node'
41
41
  require_relative 'ast/node/array_node'
42
+ require_relative 'ast/node/asgn_node'
42
43
  require_relative 'ast/node/block_node'
43
44
  require_relative 'ast/node/break_node'
44
45
  require_relative 'ast/node/case_match_node'
45
46
  require_relative 'ast/node/case_node'
47
+ require_relative 'ast/node/casgn_node'
46
48
  require_relative 'ast/node/class_node'
47
49
  require_relative 'ast/node/const_node'
48
50
  require_relative 'ast/node/def_node'
@@ -61,6 +63,9 @@ require_relative 'ast/node/keyword_splat_node'
61
63
  require_relative 'ast/node/lambda_node'
62
64
  require_relative 'ast/node/module_node'
63
65
  require_relative 'ast/node/next_node'
66
+ require_relative 'ast/node/op_asgn_node'
67
+ require_relative 'ast/node/and_asgn_node'
68
+ require_relative 'ast/node/or_asgn_node'
64
69
  require_relative 'ast/node/or_node'
65
70
  require_relative 'ast/node/pair_node'
66
71
  require_relative 'ast/node/procarg0_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.8.0
4
+ version: 1.11.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: 2021-07-14 00:00:00.000000000 Z
13
+ date: 2021-08-24 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: parser
@@ -63,14 +63,17 @@ files:
63
63
  - lib/rubocop/ast/ext/range_min_max.rb
64
64
  - lib/rubocop/ast/node.rb
65
65
  - lib/rubocop/ast/node/alias_node.rb
66
+ - lib/rubocop/ast/node/and_asgn_node.rb
66
67
  - lib/rubocop/ast/node/and_node.rb
67
68
  - lib/rubocop/ast/node/arg_node.rb
68
69
  - lib/rubocop/ast/node/args_node.rb
69
70
  - lib/rubocop/ast/node/array_node.rb
71
+ - lib/rubocop/ast/node/asgn_node.rb
70
72
  - lib/rubocop/ast/node/block_node.rb
71
73
  - lib/rubocop/ast/node/break_node.rb
72
74
  - lib/rubocop/ast/node/case_match_node.rb
73
75
  - lib/rubocop/ast/node/case_node.rb
76
+ - lib/rubocop/ast/node/casgn_node.rb
74
77
  - lib/rubocop/ast/node/class_node.rb
75
78
  - lib/rubocop/ast/node/const_node.rb
76
79
  - lib/rubocop/ast/node/def_node.rb
@@ -102,6 +105,8 @@ files:
102
105
  - lib/rubocop/ast/node/mixin/predicate_operator_node.rb
103
106
  - lib/rubocop/ast/node/module_node.rb
104
107
  - lib/rubocop/ast/node/next_node.rb
108
+ - lib/rubocop/ast/node/op_asgn_node.rb
109
+ - lib/rubocop/ast/node/or_asgn_node.rb
105
110
  - lib/rubocop/ast/node/or_node.rb
106
111
  - lib/rubocop/ast/node/pair_node.rb
107
112
  - lib/rubocop/ast/node/procarg0_node.rb