rubocop-ast 1.3.0 → 1.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +3 -1
- data/lib/rubocop/ast.rb +2 -1
- data/lib/rubocop/ast/builder.rb +5 -2
- data/lib/rubocop/ast/node.rb +15 -0
- data/lib/rubocop/ast/node/case_match_node.rb +2 -2
- data/lib/rubocop/ast/node/dstr_node.rb +16 -0
- data/lib/rubocop/ast/node/in_pattern_node.rb +31 -0
- data/lib/rubocop/ast/node/mixin/descendence.rb +2 -6
- data/lib/rubocop/ast/node/mixin/method_dispatch_node.rb +4 -0
- data/lib/rubocop/ast/node/send_node.rb +1 -0
- data/lib/rubocop/ast/node_pattern.rb +1 -1
- data/lib/rubocop/ast/node_pattern/parser.racc.rb +1 -1
- data/lib/rubocop/ast/processed_source.rb +4 -1
- data/lib/rubocop/ast/traversal.rb +3 -2
- data/lib/rubocop/ast/version.rb +1 -1
- metadata +11 -10
- data/lib/rubocop/ast/ext/set.rb +0 -12
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c1f86e6a6bcb200cfd73b5bf9d959584812252f6cc365b115e1448daae6f1430
|
4
|
+
data.tar.gz: 5c40556b0692122859f39f0dbe2d9dbf5fea1aeaed67b654828b815dd3ea85f1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 40ced8eab14849512c8d5c4096716a27786d95a5e1d14b300c5f1b52fd009e685e93fa472b842beaa67e9141e21465ef155ae2876d7020566a61da23855a57af
|
7
|
+
data.tar.gz: 259e988322fa736908819654e29ecdafe791db5336b06a790ca268b8a73f7031fd262560508743a137dd3ba996405c92ad7d97354c630182e82f9c3d1e0baf43
|
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
|
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'
|
@@ -54,6 +53,7 @@ require_relative 'ast/node/forward_args_node'
|
|
54
53
|
require_relative 'ast/node/float_node'
|
55
54
|
require_relative 'ast/node/hash_node'
|
56
55
|
require_relative 'ast/node/if_node'
|
56
|
+
require_relative 'ast/node/in_pattern_node'
|
57
57
|
require_relative 'ast/node/index_node'
|
58
58
|
require_relative 'ast/node/indexasgn_node'
|
59
59
|
require_relative 'ast/node/int_node'
|
@@ -72,6 +72,7 @@ require_relative 'ast/node/return_node'
|
|
72
72
|
require_relative 'ast/node/self_class_node'
|
73
73
|
require_relative 'ast/node/send_node'
|
74
74
|
require_relative 'ast/node/str_node'
|
75
|
+
require_relative 'ast/node/dstr_node'
|
75
76
|
require_relative 'ast/node/super_node'
|
76
77
|
require_relative 'ast/node/symbol_node'
|
77
78
|
require_relative 'ast/node/until_node'
|
data/lib/rubocop/ast/builder.rb
CHANGED
@@ -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,17 +42,20 @@ 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,
|
47
49
|
float: FloatNode,
|
48
50
|
hash: HashNode,
|
49
51
|
if: IfNode,
|
52
|
+
in_pattern: InPatternNode,
|
50
53
|
int: IntNode,
|
51
54
|
index: IndexNode,
|
52
55
|
indexasgn: IndexasgnNode,
|
53
56
|
irange: RangeNode,
|
54
57
|
erange: RangeNode,
|
58
|
+
kwargs: HashNode,
|
55
59
|
kwsplat: KeywordSplatNode,
|
56
60
|
lambda: LambdaNode,
|
57
61
|
module: ModuleNode,
|
@@ -66,7 +70,6 @@ module RuboCop
|
|
66
70
|
csend: SendNode,
|
67
71
|
send: SendNode,
|
68
72
|
str: StrNode,
|
69
|
-
dstr: StrNode,
|
70
73
|
xstr: StrNode,
|
71
74
|
sclass: SelfClassNode,
|
72
75
|
super: SuperNode,
|
data/lib/rubocop/ast/node.rb
CHANGED
@@ -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
|
@@ -24,9 +24,9 @@ module RuboCop
|
|
24
24
|
self
|
25
25
|
end
|
26
26
|
|
27
|
-
# Returns an array of all the
|
27
|
+
# Returns an array of all the `in` pattern branches in the `case` statement.
|
28
28
|
#
|
29
|
-
# @return [Array<
|
29
|
+
# @return [Array<InPatternNode>] an array of `in_pattern` nodes
|
30
30
|
def in_pattern_branches
|
31
31
|
node_parts[1...-1]
|
32
32
|
end
|
@@ -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
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module AST
|
5
|
+
# A node extension for `in` 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 `in` nodes within RuboCop.
|
8
|
+
class InPatternNode < Node
|
9
|
+
# Returns the index of the `in` branch within the `case` statement.
|
10
|
+
#
|
11
|
+
# @return [Integer] the index of the `in` branch
|
12
|
+
def branch_index
|
13
|
+
parent.in_pattern_branches.index(self)
|
14
|
+
end
|
15
|
+
|
16
|
+
# Checks whether the `in` node has a `then` keyword.
|
17
|
+
#
|
18
|
+
# @return [Boolean] whether the `in` node has a `then` keyword
|
19
|
+
def then?
|
20
|
+
loc.begin&.is?('then')
|
21
|
+
end
|
22
|
+
|
23
|
+
# Returns the body of the `in` node.
|
24
|
+
#
|
25
|
+
# @return [Node, nil] the body of the `in` node
|
26
|
+
def body
|
27
|
+
node_parts[-1]
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
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]
|
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
|
@@ -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.
|
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
|
data/lib/rubocop/ast/version.rb
CHANGED
metadata
CHANGED
@@ -1,16 +1,16 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rubocop-ast
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bozhidar Batsov
|
8
8
|
- Jonas Arvidsson
|
9
9
|
- Yuji Nakayama
|
10
|
-
autorequire:
|
10
|
+
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date:
|
13
|
+
date: 2021-05-26 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:
|
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:
|
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,12 +75,14 @@ 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
|
82
82
|
- lib/rubocop/ast/node/forward_args_node.rb
|
83
83
|
- lib/rubocop/ast/node/hash_node.rb
|
84
84
|
- lib/rubocop/ast/node/if_node.rb
|
85
|
+
- lib/rubocop/ast/node/in_pattern_node.rb
|
85
86
|
- lib/rubocop/ast/node/index_node.rb
|
86
87
|
- lib/rubocop/ast/node/indexasgn_node.rb
|
87
88
|
- lib/rubocop/ast/node/int_node.rb
|
@@ -153,7 +154,7 @@ metadata:
|
|
153
154
|
source_code_uri: https://github.com/rubocop-hq/rubocop-ast/
|
154
155
|
documentation_uri: https://docs.rubocop.org/rubocop-ast/
|
155
156
|
bug_tracker_uri: https://github.com/rubocop-hq/rubocop-ast/issues
|
156
|
-
post_install_message:
|
157
|
+
post_install_message:
|
157
158
|
rdoc_options: []
|
158
159
|
require_paths:
|
159
160
|
- lib
|
@@ -161,15 +162,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
161
162
|
requirements:
|
162
163
|
- - ">="
|
163
164
|
- !ruby/object:Gem::Version
|
164
|
-
version: 2.
|
165
|
+
version: 2.5.0
|
165
166
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
166
167
|
requirements:
|
167
168
|
- - ">="
|
168
169
|
- !ruby/object:Gem::Version
|
169
170
|
version: '0'
|
170
171
|
requirements: []
|
171
|
-
rubygems_version: 3.1.
|
172
|
-
signing_key:
|
172
|
+
rubygems_version: 3.1.2
|
173
|
+
signing_key:
|
173
174
|
specification_version: 4
|
174
175
|
summary: RuboCop tools to deal with Ruby code AST.
|
175
176
|
test_files: []
|