rubocop-ast 1.7.0 → 1.13.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: 25e84f96e233a9c966ad15efd834e109fea1c11d0998e2913ffd81310568d2b2
4
- data.tar.gz: f6b8a033d2fd90518fb102f2cfaa27a5ef32bcfb892783ef79e2b9ad33ab4235
3
+ metadata.gz: b3a3081c514303a8fb2f6ac3e4616da1d5f41ac4bd21d63407f9281534443385
4
+ data.tar.gz: dabd9d626ee4dd67946126b81f1a8852da16bb561caaabc91fa4b89648186fa6
5
5
  SHA512:
6
- metadata.gz: 326d28041a4c30d64cac801ceec9677eea264c7a98754217ae27b026bf9779122803402cdddd8306881320e8ac185ce3ff708d4b6e2c869840622303b50b9ab1
7
- data.tar.gz: 293b516ba03824c5c9fc56557943993688cf0796bd7ffe567434f7633ca71aa24aec8ebcac89284c3b7b9fb7d38302cf666b1e7b0bb844700d70bad8c65cc767
6
+ metadata.gz: f678e9d3b6da22c1be2861fd023121c50b161dfff5986ed14f49199c251bb041a2d2c3e93cacb86c9368dd71f8ed7575620984eeb54e65eaf183f062ffa11dee
7
+ data.tar.gz: 948888c0e83bca6acf73b06a0e5030bab2fde65cafa6366c6a3054cb07eca722d938f28a8b1e1bb226c8f48c9bb483b9d66704d432fdc87e23a4d2461809bb38
data/README.md CHANGED
@@ -1,11 +1,11 @@
1
1
  # RuboCop AST
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/rubocop-ast.svg)](https://badge.fury.io/rb/rubocop-ast)
4
- [![CI](https://github.com/rubocop-hq/rubocop-ast/workflows/CI/badge.svg)](https://github.com/rubocop-hq/rubocop-ast/actions?query=workflow%3ACI)
5
- [![Test Coverage](https://api.codeclimate.com/v1/badges/a29666e6373bc41bc0a9/test_coverage)](https://codeclimate.com/github/rubocop-hq/rubocop-ast/test_coverage)
6
- [![Maintainability](https://api.codeclimate.com/v1/badges/a29666e6373bc41bc0a9/maintainability)](https://codeclimate.com/github/rubocop-hq/rubocop-ast/maintainability)
4
+ [![CI](https://github.com/rubocop/rubocop-ast/workflows/CI/badge.svg)](https://github.com/rubocop/rubocop-ast/actions?query=workflow%3ACI)
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
7
 
8
- Contains the classes needed by [RuboCop](https://github.com/rubocop-hq/rubocop) to deal with Ruby's AST, in particular:
8
+ Contains the classes needed by [RuboCop](https://github.com/rubocop/rubocop) to deal with Ruby's AST, in particular:
9
9
 
10
10
  * `RuboCop::AST::Node` ([doc](docs/modules/ROOT/pages/node_types.adoc))
11
11
  * `RuboCop::AST::NodePattern` ([doc](docs/modules/ROOT/pages/node_pattern.adoc))
@@ -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
@@ -31,9 +31,23 @@ module RuboCop
31
31
  node_parts[1...-1]
32
32
  end
33
33
 
34
+ # Returns an array of all the when branches in the `case` statement.
35
+ #
36
+ # @return [Array<Node, nil>] an array of the bodies of the `in` branches
37
+ # and the `else` (if any). Note that these bodies could be nil.
38
+ def branches
39
+ bodies = in_pattern_branches.map(&:body)
40
+ if else?
41
+ # `empty-else` node sets nil because it has no body.
42
+ else_branch.empty_else_type? ? bodies.push(nil) : bodies.push(else_branch)
43
+ end
44
+ bodies
45
+ end
46
+
34
47
  # Returns the else branch of the `case` statement, if any.
35
48
  #
36
49
  # @return [Node] the else branch node of the `case` statement
50
+ # @return [EmptyElse] the empty else branch node of the `case` statement
37
51
  # @return [nil] if the case statement does not have an else branch.
38
52
  def else_branch
39
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
@@ -28,9 +28,9 @@ module RuboCop
28
28
  node_parts[1]
29
29
  end
30
30
 
31
- # The `block` node associated with this method dispatch, if any.
31
+ # The `block` or `numblock` node associated with this method dispatch, if any.
32
32
  #
33
- # @return [BlockNode, nil] the `block` node associated with this method
33
+ # @return [BlockNode, nil] the `block` or `numblock` node associated with this method
34
34
  # call or `nil`
35
35
  def block_node
36
36
  parent if block_literal?
@@ -154,7 +154,7 @@ module RuboCop
154
154
  #
155
155
  # @return [Boolean] whether the dispatched method has a block
156
156
  def block_literal?
157
- parent&.block_type? && eql?(parent.send_node)
157
+ (parent&.block_type? || parent&.numblock_type?) && eql?(parent.send_node)
158
158
  end
159
159
 
160
160
  # Checks whether this node is an arithmetic operation
@@ -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
@@ -421,7 +425,7 @@ module RuboCop
421
425
  end
422
426
 
423
427
  def keyword?
424
- return true if special_keyword? || send_type? && prefix_not?
428
+ return true if special_keyword? || (send_type? && prefix_not?)
425
429
  return false unless KEYWORDS.include?(type)
426
430
 
427
431
  !OPERATOR_KEYWORDS.include?(type) || loc.operator.is?(type.to_s)
@@ -460,7 +464,7 @@ module RuboCop
460
464
  end
461
465
 
462
466
  def numeric_type?
463
- int_type? || float_type?
467
+ int_type? || float_type? || rational_type? || complex_type?
464
468
  end
465
469
 
466
470
  def range_type?
@@ -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
@@ -347,7 +347,7 @@ module RuboCop
347
347
  def compile_loop_advance(to = '+=1')
348
348
  # The `#{@cur_child_var} ||` is just to avoid unused variable warning
349
349
  "(#{@cur_child_var} = #{@seq_var}.children[#{@cur_index_var} #{to}]; " \
350
- "#{@cur_child_var} || true)"
350
+ "#{@cur_child_var} || true)"
351
351
  end
352
352
 
353
353
  def compile_loop(term)
@@ -2,7 +2,7 @@
2
2
  # encoding: UTF-8
3
3
  #--
4
4
  # This file is automatically generated. Do not modify it.
5
- # Generated by: oedipus_lex version 2.5.2.
5
+ # Generated by: oedipus_lex version 2.6.0.
6
6
  # Source: lib/rubocop/ast/node_pattern/lexer.rex
7
7
  #++
8
8
 
@@ -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
@@ -52,7 +52,7 @@ module RuboCop
52
52
 
53
53
  detail = node.loc&.expression&.source || node.to_s
54
54
  raise NodePattern::Invalid, 'parse error, expected unary node pattern ' \
55
- "but got expression matching multiple elements: #{detail}"
55
+ "but got expression matching multiple elements: #{detail}"
56
56
  end
57
57
 
58
58
  # Overrides Racc::Parser's method:
@@ -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.7.0'
6
+ STRING = '1.13.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.7.0
4
+ version: 1.13.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-05-28 00:00:00.000000000 Z
13
+ date: 2021-11-07 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
@@ -145,15 +150,15 @@ files:
145
150
  - lib/rubocop/ast/token.rb
146
151
  - lib/rubocop/ast/traversal.rb
147
152
  - lib/rubocop/ast/version.rb
148
- homepage: https://github.com/rubocop-hq/rubocop-ast
153
+ homepage: https://github.com/rubocop/rubocop-ast
149
154
  licenses:
150
155
  - MIT
151
156
  metadata:
152
157
  homepage_uri: https://www.rubocop.org/
153
- changelog_uri: https://github.com/rubocop-hq/rubocop-ast/blob/master/CHANGELOG.md
154
- source_code_uri: https://github.com/rubocop-hq/rubocop-ast/
158
+ changelog_uri: https://github.com/rubocop/rubocop-ast/blob/master/CHANGELOG.md
159
+ source_code_uri: https://github.com/rubocop/rubocop-ast/
155
160
  documentation_uri: https://docs.rubocop.org/rubocop-ast/
156
- bug_tracker_uri: https://github.com/rubocop-hq/rubocop-ast/issues
161
+ bug_tracker_uri: https://github.com/rubocop/rubocop-ast/issues
157
162
  post_install_message:
158
163
  rdoc_options: []
159
164
  require_paths: