rubocop-ast 1.2.0 → 1.5.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: d42f2a7ab5fe1a168c8307ffad746e21eaa3811cdb5f9236ce423f2fb829b9ae
4
- data.tar.gz: 74ca8b1fbbbb9f0a89ae61136235f9066579da11b4a056c0161a7a7a34092e19
3
+ metadata.gz: a4e1919e281d2cc09eca1c8f18effe89cd4c818f7981070e5cb222bde40d6be5
4
+ data.tar.gz: e7368a014cc2c9881552f935b6a841357275940e63ee567655f3bb151b118977
5
5
  SHA512:
6
- metadata.gz: ffeca2200e1825749aa09f72e90b98b643617b55f4300ad9de8f34bedb59534278ebd4490d50d6e6866b055a5b5d9e28707e69c221c8da16e6c6691b83e7b162
7
- data.tar.gz: 4e7b93a612f88394024fd26d025cbca1cf3b1926ea27650a115efd89b9f8bda2aab52d55968d427d1e63a295d588407046841d00ef5823e80443bb4d73b3acd2
6
+ metadata.gz: b1c16200ecf6bc69027525ac88c1214d9fb69ce8840307422560ac9c6e314c6d720e278100640da86a406e0d4ae7d229d3f943b4dd5a3446739584baa205336a
7
+ data.tar.gz: eafb7d829dd9c17182b43bc9ed091933bcb4ce7904b499a12a79054f76ecc25896917ae4f043f84790c2b2754f6563ab1457ce86fd58b8ddf490f8188263479a
data/README.md CHANGED
@@ -35,7 +35,9 @@ See the [docs site](https://docs.rubocop.org/rubocop-ast) for more details.
35
35
 
36
36
  ### Parser compatibility switches
37
37
 
38
- This gem, by default, uses most [legacy AST output from parser](https://github.com/whitequark/parser/#usage), except for `emit_forward_arg` which is set to `true`.
38
+ This gem, by default, uses most [legacy AST output from parser](https://github.com/whitequark/parser/#usage), except for the following which are set to `true`:
39
+ * `emit_forward_arg`
40
+ * `emit_match_pattern`
39
41
 
40
42
  The main `RuboCop` gem uses these defaults (and is currently only compatible with these), but this gem can be used separately from `RuboCop` and is meant to be compatible with all settings. For example, to have `-> { ... }` emitted
41
43
  as `LambdaNode` instead of `SendNode`:
data/lib/rubocop/ast.rb CHANGED
@@ -6,7 +6,6 @@ require 'set'
6
6
 
7
7
  require_relative 'ast/ext/range'
8
8
  require_relative 'ast/ext/range_min_max'
9
- require_relative 'ast/ext/set'
10
9
  require_relative 'ast/node_pattern/method_definer'
11
10
  require_relative 'ast/node_pattern'
12
11
  require_relative 'ast/node/mixin/descendence'
@@ -72,6 +71,7 @@ require_relative 'ast/node/return_node'
72
71
  require_relative 'ast/node/self_class_node'
73
72
  require_relative 'ast/node/send_node'
74
73
  require_relative 'ast/node/str_node'
74
+ require_relative 'ast/node/dstr_node'
75
75
  require_relative 'ast/node/super_node'
76
76
  require_relative 'ast/node/symbol_node'
77
77
  require_relative 'ast/node/until_node'
@@ -14,7 +14,8 @@ module RuboCop
14
14
  # parser = Parser::Ruby25.new(builder)
15
15
  # root_node = parser.parse(buffer)
16
16
  class Builder < Parser::Builders::Default
17
- self.emit_forward_arg = true
17
+ self.emit_forward_arg = true if respond_to?(:emit_forward_arg=)
18
+ self.emit_match_pattern = true if respond_to?(:emit_match_pattern=)
18
19
 
19
20
  # @api private
20
21
  NODE_MAP = {
@@ -41,6 +42,7 @@ module RuboCop
41
42
  def: DefNode,
42
43
  defined?: DefinedNode,
43
44
  defs: DefNode,
45
+ dstr: DstrNode,
44
46
  ensure: EnsureNode,
45
47
  for: ForNode,
46
48
  forward_args: ForwardArgsNode,
@@ -52,6 +54,7 @@ module RuboCop
52
54
  indexasgn: IndexasgnNode,
53
55
  irange: RangeNode,
54
56
  erange: RangeNode,
57
+ kwargs: HashNode,
55
58
  kwsplat: KeywordSplatNode,
56
59
  lambda: LambdaNode,
57
60
  module: ModuleNode,
@@ -66,7 +69,6 @@ module RuboCop
66
69
  csend: SendNode,
67
70
  send: SendNode,
68
71
  str: StrNode,
69
- dstr: StrNode,
70
72
  xstr: StrNode,
71
73
  sclass: SelfClassNode,
72
74
  super: SuperNode,
@@ -272,10 +272,12 @@ module RuboCop
272
272
 
273
273
  ## Destructuring
274
274
 
275
+ # @!method receiver(node = self)
275
276
  def_node_matcher :receiver, <<~PATTERN
276
277
  {(send $_ ...) ({block numblock} (send $_ ...) ...)}
277
278
  PATTERN
278
279
 
280
+ # @!method str_content(node = self)
279
281
  def_node_matcher :str_content, '(str $_)'
280
282
 
281
283
  def const_name
@@ -289,6 +291,7 @@ module RuboCop
289
291
  end
290
292
  end
291
293
 
294
+ # @!method defined_module0(node = self)
292
295
  def_node_matcher :defined_module0, <<~PATTERN
293
296
  {(class (const $_ $_) ...)
294
297
  (module (const $_ $_) ...)
@@ -334,6 +337,7 @@ module RuboCop
334
337
  end
335
338
 
336
339
  # Some cops treat the shovel operator as a kind of assignment.
340
+ # @!method assignment_or_similar?(node = self)
337
341
  def_node_matcher :assignment_or_similar?, <<~PATTERN
338
342
  {assignment? (send _recv :<< ...)}
339
343
  PATTERN
@@ -469,37 +473,47 @@ module RuboCop
469
473
  node.match_guard_clause?
470
474
  end
471
475
 
476
+ # @!method match_guard_clause?(node = self)
472
477
  def_node_matcher :match_guard_clause?, <<~PATTERN
473
478
  [${(send nil? {:raise :fail} ...) return break next} single_line?]
474
479
  PATTERN
475
480
 
481
+ # @!method proc?(node = self)
476
482
  def_node_matcher :proc?, <<~PATTERN
477
483
  {(block (send nil? :proc) ...)
478
484
  (block (send #global_const?(:Proc) :new) ...)
479
485
  (send #global_const?(:Proc) :new)}
480
486
  PATTERN
481
487
 
488
+ # @!method lambda?(node = self)
482
489
  def_node_matcher :lambda?, '({block numblock} (send nil? :lambda) ...)'
490
+
491
+ # @!method lambda_or_proc?(node = self)
483
492
  def_node_matcher :lambda_or_proc?, '{lambda? proc?}'
484
493
 
494
+ # @!method global_const?(node = self, name)
485
495
  def_node_matcher :global_const?, '(const {nil? cbase} %1)'
486
496
 
497
+ # @!method class_constructor?(node = self)
487
498
  def_node_matcher :class_constructor?, <<~PATTERN
488
499
  { (send #global_const?({:Class :Module :Struct}) :new ...)
489
500
  (block (send #global_const?({:Class :Module :Struct}) :new ...) ...)}
490
501
  PATTERN
491
502
 
492
503
  # @deprecated Use `:class_constructor?`
504
+ # @!method struct_constructor?(node = self)
493
505
  def_node_matcher :struct_constructor?, <<~PATTERN
494
506
  (block (send #global_const?(:Struct) :new ...) _ $_)
495
507
  PATTERN
496
508
 
509
+ # @!method class_definition?(node = self)
497
510
  def_node_matcher :class_definition?, <<~PATTERN
498
511
  {(class _ _ $_)
499
512
  (sclass _ $_)
500
513
  (block (send #global_const?({:Struct :Class}) :new ...) _ $_)}
501
514
  PATTERN
502
515
 
516
+ # @!method module_definition?(node = self)
503
517
  def_node_matcher :module_definition?, <<~PATTERN
504
518
  {(module _ $_)
505
519
  (block (send #global_const?(:Module) :new ...) _ $_)}
@@ -633,6 +647,7 @@ module RuboCop
633
647
  end
634
648
  end
635
649
 
650
+ # @!method new_class_or_module_block?(node = self)
636
651
  def_node_matcher :new_class_or_module_block?, <<~PATTERN
637
652
  ^(casgn _ _ (block (send (const _ {:Class :Module}) :new) ...))
638
653
  PATTERN
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module AST
5
+ # A node extension for `dstr` nodes. This will be used
6
+ # in place of a plain node when the builder constructs the AST, making
7
+ # its methods available to all `dstr` nodes within RuboCop.
8
+ class DstrNode < StrNode
9
+ def value
10
+ child_nodes.map do |child|
11
+ child.respond_to?(:value) ? child.value : child.source
12
+ end.join
13
+ end
14
+ end
15
+ end
16
+ end
@@ -13,13 +13,9 @@ module RuboCop
13
13
  #
14
14
  # @overload each_child_node
15
15
  # Yield all nodes.
16
- # @overload each_child_node(type)
17
- # Yield only nodes matching the type.
18
- # @param [Symbol] type a node type
19
- # @overload each_child_node(type_a, type_b, ...)
16
+ # @overload each_child_node(type, ...)
20
17
  # Yield only nodes matching any of the types.
21
- # @param [Symbol] type_a a node type
22
- # @param [Symbol] type_b a node type
18
+ # @param [Symbol] type a node type
23
19
  # @yieldparam [Node] node each child node
24
20
  # @return [self] if a block is given
25
21
  # @return [Enumerator] if no block is given
@@ -224,6 +224,7 @@ module RuboCop
224
224
 
225
225
  private
226
226
 
227
+ # @!method in_macro_scope?(node = self)
227
228
  def_node_matcher :in_macro_scope?, <<~PATTERN
228
229
  {
229
230
  root? # Either a root node,
@@ -239,14 +240,17 @@ module RuboCop
239
240
  }
240
241
  PATTERN
241
242
 
243
+ # @!method adjacent_def_modifier?(node = self)
242
244
  def_node_matcher :adjacent_def_modifier?, <<~PATTERN
243
245
  (send nil? _ ({def defs} ...))
244
246
  PATTERN
245
247
 
248
+ # @!method bare_access_modifier_declaration?(node = self)
246
249
  def_node_matcher :bare_access_modifier_declaration?, <<~PATTERN
247
250
  (send nil? {:public :protected :private :module_function})
248
251
  PATTERN
249
252
 
253
+ # @!method non_bare_access_modifier_declaration?(node = self)
250
254
  def_node_matcher :non_bare_access_modifier_declaration?, <<~PATTERN
251
255
  (send nil? {:public :protected :private :module_function} _)
252
256
  PATTERN
@@ -9,6 +9,7 @@ module RuboCop
9
9
  include ParameterizedNode::RestArguments
10
10
  include MethodDispatchNode
11
11
 
12
+ # @!method attribute_accessor?(node = self)
12
13
  def_node_matcher :attribute_accessor?, <<~PATTERN
13
14
  [(send nil? ${:attr_reader :attr_writer :attr_accessor :attr} $...)
14
15
  (_ _ _ _ ...)]
@@ -28,7 +28,7 @@ module RuboCop
28
28
  module Macros
29
29
  # Define a method which applies a pattern to an AST node
30
30
  #
31
- # The new method will return nil if the node does not match
31
+ # The new method will return nil if the node does not match.
32
32
  # If the node matches, and a block is provided, the new method will
33
33
  # yield to the block (passing any captures as block arguments).
34
34
  # If the node matches, and no block is provided, the new method will
@@ -13,7 +13,8 @@ class RuboCop::AST::NodePattern::LexerRex
13
13
  macros
14
14
  CONST_NAME /[A-Z:][a-zA-Z_:]+/
15
15
  SYMBOL_NAME /[\w+@*\/?!<>=~|%^-]+|\[\]=?/
16
- IDENTIFIER /[a-zA-Z_][a-zA-Z0-9_-]*/
16
+ IDENTIFIER /[a-z][a-zA-Z0-9_]*/
17
+ NODE_TYPE /[a-z][a-zA-Z0-9_-]*/ # Same as identifier but allows '-'
17
18
  CALL /(?:#{CONST_NAME}\.)?#{IDENTIFIER}[!?]?/
18
19
  REGEXP_BODY /(?:[^\/]|\\\/)*/
19
20
  REGEXP /\/(#{REGEXP_BODY})(?<!\\)\/([imxo]*)/
@@ -27,14 +28,14 @@ rules
27
28
  %w"( ) { | } [ ] < > $ ! ^ ` ... + * ? ,"
28
29
  )}/o { emit ss.matched, &:to_sym }
29
30
  /#{REGEXP}/o { emit_regexp }
30
- /%(#{CONST_NAME})/o { emit :tPARAM_CONST }
31
+ /%?(#{CONST_NAME})/o { emit :tPARAM_CONST }
31
32
  /%([a-z_]+)/ { emit :tPARAM_NAMED }
32
33
  /%(\d*)/ { emit(:tPARAM_NUMBER) { |s| s.empty? ? 1 : s.to_i } } # Map `%` to `%1`
33
34
  /_(#{IDENTIFIER})/o { emit :tUNIFY }
34
35
  /_/o { emit :tWILDCARD }
35
36
  /\#(#{CALL})/o { @state = :ARG; emit :tFUNCTION_CALL, &:to_sym }
36
37
  /#{IDENTIFIER}\?/o { @state = :ARG; emit :tPREDICATE, &:to_sym }
37
- /#{IDENTIFIER}/o { emit :tNODE_TYPE, &:to_sym }
38
+ /#{NODE_TYPE}/o { emit :tNODE_TYPE, &:to_sym }
38
39
  :ARG /\(/ { @state = nil; emit :tARG_LIST }
39
40
  :ARG // { @state = nil }
40
41
  /\#.*/ { emit_comment }
@@ -26,7 +26,8 @@ class RuboCop::AST::NodePattern::LexerRex
26
26
  # :stopdoc:
27
27
  CONST_NAME = /[A-Z:][a-zA-Z_:]+/
28
28
  SYMBOL_NAME = /[\w+@*\/?!<>=~|%^-]+|\[\]=?/
29
- IDENTIFIER = /[a-zA-Z_][a-zA-Z0-9_-]*/
29
+ IDENTIFIER = /[a-z][a-zA-Z0-9_]*/
30
+ NODE_TYPE = /[a-z][a-zA-Z0-9_-]*/
30
31
  CALL = /(?:#{CONST_NAME}\.)?#{IDENTIFIER}[!?]?/
31
32
  REGEXP_BODY = /(?:[^\/]|\\\/)*/
32
33
  REGEXP = /\/(#{REGEXP_BODY})(?<!\\)\/([imxo]*)/
@@ -134,7 +135,7 @@ class RuboCop::AST::NodePattern::LexerRex
134
135
  action { emit ss.matched, &:to_sym }
135
136
  when ss.skip(/#{REGEXP}/o) then
136
137
  action { emit_regexp }
137
- when ss.skip(/%(#{CONST_NAME})/o) then
138
+ when ss.skip(/%?(#{CONST_NAME})/o) then
138
139
  action { emit :tPARAM_CONST }
139
140
  when ss.skip(/%([a-z_]+)/) then
140
141
  action { emit :tPARAM_NAMED }
@@ -148,7 +149,7 @@ class RuboCop::AST::NodePattern::LexerRex
148
149
  action { @state = :ARG; emit :tFUNCTION_CALL, &:to_sym }
149
150
  when ss.skip(/#{IDENTIFIER}\?/o) then
150
151
  action { @state = :ARG; emit :tPREDICATE, &:to_sym }
151
- when ss.skip(/#{IDENTIFIER}/o) then
152
+ when ss.skip(/#{NODE_TYPE}/o) then
152
153
  action { emit :tNODE_TYPE, &:to_sym }
153
154
  when ss.skip(/\#.*/) then
154
155
  action { emit_comment }
@@ -41,7 +41,7 @@ module RuboCop
41
41
  def ast_with_comments
42
42
  return if !ast || !comments
43
43
 
44
- @ast_with_comments ||= Parser::Source::Comment.associate(ast, comments)
44
+ @ast_with_comments ||= Parser::Source::Comment.associate_by_identity(ast, comments)
45
45
  end
46
46
 
47
47
  # Returns the source lines, line break characters removed, excluding a
@@ -236,6 +236,9 @@ module RuboCop
236
236
  when 2.8, 3.0
237
237
  require 'parser/ruby30'
238
238
  Parser::Ruby30
239
+ when 3.1
240
+ require 'parser/ruby31'
241
+ Parser::Ruby31
239
242
  else
240
243
  raise ArgumentError,
241
244
  "RuboCop found unknown Ruby version: #{ruby_version.inspect}"
@@ -114,9 +114,10 @@ module RuboCop
114
114
  in_match match_alt break next
115
115
  match_as array_pattern array_pattern_with_tail
116
116
  hash_pattern const_pattern find_pattern
117
- index indexasgn procarg0]
117
+ index indexasgn procarg0 kwargs]
118
118
  many_opt_node_children = %i[case rescue resbody ensure for when
119
- case_match in_pattern irange erange]
119
+ case_match in_pattern irange erange
120
+ match_pattern match_pattern_p]
120
121
 
121
122
  ### Callbacks for above
122
123
  def_callback no_children
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module AST
5
5
  module Version
6
- STRING = '1.2.0'
6
+ STRING = '1.5.0'
7
7
  end
8
8
  end
9
9
  end
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.2.0
4
+ version: 1.5.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: 2020-11-24 00:00:00.000000000 Z
13
+ date: 2021-05-02 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: 2.7.1.5
21
+ version: 3.0.1.1
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: 2.7.1.5
28
+ version: 3.0.1.1
29
29
  - !ruby/object:Gem::Dependency
30
30
  name: bundler
31
31
  requirement: !ruby/object:Gem::Requirement
@@ -61,7 +61,6 @@ files:
61
61
  - lib/rubocop/ast/builder.rb
62
62
  - lib/rubocop/ast/ext/range.rb
63
63
  - lib/rubocop/ast/ext/range_min_max.rb
64
- - lib/rubocop/ast/ext/set.rb
65
64
  - lib/rubocop/ast/node.rb
66
65
  - lib/rubocop/ast/node/alias_node.rb
67
66
  - lib/rubocop/ast/node/and_node.rb
@@ -76,6 +75,7 @@ files:
76
75
  - lib/rubocop/ast/node/const_node.rb
77
76
  - lib/rubocop/ast/node/def_node.rb
78
77
  - lib/rubocop/ast/node/defined_node.rb
78
+ - lib/rubocop/ast/node/dstr_node.rb
79
79
  - lib/rubocop/ast/node/ensure_node.rb
80
80
  - lib/rubocop/ast/node/float_node.rb
81
81
  - lib/rubocop/ast/node/for_node.rb
@@ -161,14 +161,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
161
161
  requirements:
162
162
  - - ">="
163
163
  - !ruby/object:Gem::Version
164
- version: 2.4.0
164
+ version: 2.5.0
165
165
  required_rubygems_version: !ruby/object:Gem::Requirement
166
166
  requirements:
167
167
  - - ">="
168
168
  - !ruby/object:Gem::Version
169
169
  version: '0'
170
170
  requirements: []
171
- rubygems_version: 3.1.4
171
+ rubygems_version: 3.2.3
172
172
  signing_key:
173
173
  specification_version: 4
174
174
  summary: RuboCop tools to deal with Ruby code AST.
@@ -1,12 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- test = :foo
4
- case test
5
- when Set[:foo]
6
- # ok, RUBY_VERSION > 2.4
7
- else
8
- # Harmonize `Set#===`
9
- class Set
10
- alias === include?
11
- end
12
- end