rubocop-ast 1.50.0 → 1.53.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 +4 -4
- data/lib/rubocop/ast/builder.rb +2 -113
- data/lib/rubocop/ast/builder_extensions.rb +118 -0
- data/lib/rubocop/ast/ext/range.rb +31 -0
- data/lib/rubocop/ast/node/block_node.rb +1 -1
- data/lib/rubocop/ast/node/if_node.rb +6 -10
- data/lib/rubocop/ast/node/mixin/conditional_node.rb +4 -4
- data/lib/rubocop/ast/node/mixin/descendence.rb +25 -0
- data/lib/rubocop/ast/node/mixin/hash_element_node.rb +1 -2
- data/lib/rubocop/ast/node.rb +23 -2
- data/lib/rubocop/ast/node_layout.rb +152 -0
- data/lib/rubocop/ast/node_pattern/checker/sequence_check.rb +140 -0
- data/lib/rubocop/ast/node_pattern/checker.rb +101 -0
- data/lib/rubocop/ast/node_pattern/compiler/debug.rb +1 -2
- data/lib/rubocop/ast/node_pattern/compiler/node_pattern_subcompiler.rb +1 -2
- data/lib/rubocop/ast/node_pattern/compiler/sequence_subcompiler.rb +25 -26
- data/lib/rubocop/ast/node_pattern/lexer.rex.rb +1 -1
- data/lib/rubocop/ast/node_pattern/method_definer.rb +1 -1
- data/lib/rubocop/ast/node_pattern/parser.racc.rb +1 -1
- data/lib/rubocop/ast/node_pattern.rb +58 -2
- data/lib/rubocop/ast/parser_source.rb +76 -0
- data/lib/rubocop/ast/prism_compiler.rb +2106 -0
- data/lib/rubocop/ast/prism_lexer.rb +779 -0
- data/lib/rubocop/ast/prism_parser.rb +211 -0
- data/lib/rubocop/ast/processed_source.rb +52 -40
- data/lib/rubocop/ast/sexp.rb +1 -1
- data/lib/rubocop/ast/vendor/parser/LICENSE.txt +26 -0
- data/lib/rubocop/ast/vendor/parser/ast/node.rb +40 -0
- data/lib/rubocop/ast/vendor/parser/clobbering_error.rb +13 -0
- data/lib/rubocop/ast/vendor/parser/deprecation.rb +13 -0
- data/lib/rubocop/ast/vendor/parser/diagnostic/engine.rb +104 -0
- data/lib/rubocop/ast/vendor/parser/diagnostic.rb +163 -0
- data/lib/rubocop/ast/vendor/parser/messages.rb +125 -0
- data/lib/rubocop/ast/vendor/parser/meta.rb +48 -0
- data/lib/rubocop/ast/vendor/parser/source/buffer.rb +369 -0
- data/lib/rubocop/ast/vendor/parser/source/comment/associator.rb +233 -0
- data/lib/rubocop/ast/vendor/parser/source/comment.rb +134 -0
- data/lib/rubocop/ast/vendor/parser/source/map/collection.rb +18 -0
- data/lib/rubocop/ast/vendor/parser/source/map/condition.rb +21 -0
- data/lib/rubocop/ast/vendor/parser/source/map/constant.rb +32 -0
- data/lib/rubocop/ast/vendor/parser/source/map/definition.rb +23 -0
- data/lib/rubocop/ast/vendor/parser/source/map/for.rb +19 -0
- data/lib/rubocop/ast/vendor/parser/source/map/heredoc.rb +19 -0
- data/lib/rubocop/ast/vendor/parser/source/map/index.rb +33 -0
- data/lib/rubocop/ast/vendor/parser/source/map/keyword.rb +20 -0
- data/lib/rubocop/ast/vendor/parser/source/map/method_definition.rb +25 -0
- data/lib/rubocop/ast/vendor/parser/source/map/objc_kwarg.rb +19 -0
- data/lib/rubocop/ast/vendor/parser/source/map/operator.rb +17 -0
- data/lib/rubocop/ast/vendor/parser/source/map/rescue_body.rb +21 -0
- data/lib/rubocop/ast/vendor/parser/source/map/send.rb +36 -0
- data/lib/rubocop/ast/vendor/parser/source/map/ternary.rb +18 -0
- data/lib/rubocop/ast/vendor/parser/source/map/variable.rb +31 -0
- data/lib/rubocop/ast/vendor/parser/source/map.rb +186 -0
- data/lib/rubocop/ast/vendor/parser/source/range.rb +326 -0
- data/lib/rubocop/ast/vendor/parser/source/tree_rewriter/action.rb +243 -0
- data/lib/rubocop/ast/vendor/parser/source/tree_rewriter.rb +431 -0
- data/lib/rubocop/ast/vendor/parser/syntax_error.rb +21 -0
- data/lib/rubocop/ast/vendor/parser/unknown_encoding_in_magic_comment_error.rb +15 -0
- data/lib/rubocop/ast/vendor/parser/version.rb +5 -0
- data/lib/rubocop/ast/version.rb +1 -1
- data/lib/rubocop/ast.rb +12 -3
- metadata +73 -4
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 15f3ff66c63d95025b2eca2939e1f8f25fd9ba69955c39fcf1fdd8b3b1c4dcee
|
|
4
|
+
data.tar.gz: 75c883d5821bc6f313f558043250fa3b551870f4e6c07965563e2a2fb288d33f
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 0afeda8567f8184a08b1339e0e95b7d59db0c157c1e3b00b0e48d10389b665635875524fc8907d72f9eae9af4f001471869f155dead1a81238b8410aa2abde56
|
|
7
|
+
data.tar.gz: '00678a61cc42b1e3e822c7e764cf928d937dd6b9625d37d14f63ff78e72c2ffc3b43ff35cbdf95167463d69be54fa5a2ade2b2ea70fcb38e0bcdf79647f62902'
|
data/lib/rubocop/ast/builder.rb
CHANGED
|
@@ -1,120 +1,9 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require 'parser'
|
|
4
|
+
|
|
3
5
|
module RuboCop
|
|
4
6
|
module AST
|
|
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
|
|
12
|
-
|
|
13
|
-
# @api private
|
|
14
|
-
NODE_MAP = {
|
|
15
|
-
and: AndNode,
|
|
16
|
-
and_asgn: AndAsgnNode,
|
|
17
|
-
alias: AliasNode,
|
|
18
|
-
arg: ArgNode,
|
|
19
|
-
blockarg: ArgNode,
|
|
20
|
-
forward_arg: ArgNode,
|
|
21
|
-
kwarg: ArgNode,
|
|
22
|
-
kwoptarg: ArgNode,
|
|
23
|
-
kwrestarg: ArgNode,
|
|
24
|
-
optarg: ArgNode,
|
|
25
|
-
restarg: ArgNode,
|
|
26
|
-
shadowarg: ArgNode,
|
|
27
|
-
args: ArgsNode,
|
|
28
|
-
array: ArrayNode,
|
|
29
|
-
lvasgn: AsgnNode,
|
|
30
|
-
ivasgn: AsgnNode,
|
|
31
|
-
cvasgn: AsgnNode,
|
|
32
|
-
gvasgn: AsgnNode,
|
|
33
|
-
block: BlockNode,
|
|
34
|
-
numblock: BlockNode,
|
|
35
|
-
itblock: BlockNode,
|
|
36
|
-
break: BreakNode,
|
|
37
|
-
case_match: CaseMatchNode,
|
|
38
|
-
casgn: CasgnNode,
|
|
39
|
-
case: CaseNode,
|
|
40
|
-
class: ClassNode,
|
|
41
|
-
complex: ComplexNode,
|
|
42
|
-
const: ConstNode,
|
|
43
|
-
def: DefNode,
|
|
44
|
-
defined?: DefinedNode,
|
|
45
|
-
defs: DefNode,
|
|
46
|
-
dstr: DstrNode,
|
|
47
|
-
ensure: EnsureNode,
|
|
48
|
-
for: ForNode,
|
|
49
|
-
forward_args: ForwardArgsNode,
|
|
50
|
-
forwarded_kwrestarg: KeywordSplatNode,
|
|
51
|
-
float: FloatNode,
|
|
52
|
-
hash: HashNode,
|
|
53
|
-
if: IfNode,
|
|
54
|
-
in_pattern: InPatternNode,
|
|
55
|
-
int: IntNode,
|
|
56
|
-
index: IndexNode,
|
|
57
|
-
indexasgn: IndexasgnNode,
|
|
58
|
-
irange: RangeNode,
|
|
59
|
-
erange: RangeNode,
|
|
60
|
-
kwargs: HashNode,
|
|
61
|
-
kwbegin: KeywordBeginNode,
|
|
62
|
-
kwsplat: KeywordSplatNode,
|
|
63
|
-
lambda: LambdaNode,
|
|
64
|
-
masgn: MasgnNode,
|
|
65
|
-
mlhs: MlhsNode,
|
|
66
|
-
module: ModuleNode,
|
|
67
|
-
next: NextNode,
|
|
68
|
-
op_asgn: OpAsgnNode,
|
|
69
|
-
or_asgn: OrAsgnNode,
|
|
70
|
-
or: OrNode,
|
|
71
|
-
pair: PairNode,
|
|
72
|
-
procarg0: Procarg0Node,
|
|
73
|
-
rational: RationalNode,
|
|
74
|
-
regexp: RegexpNode,
|
|
75
|
-
rescue: RescueNode,
|
|
76
|
-
resbody: ResbodyNode,
|
|
77
|
-
return: ReturnNode,
|
|
78
|
-
csend: CsendNode,
|
|
79
|
-
send: SendNode,
|
|
80
|
-
str: StrNode,
|
|
81
|
-
xstr: StrNode,
|
|
82
|
-
sclass: SelfClassNode,
|
|
83
|
-
super: SuperNode,
|
|
84
|
-
zsuper: SuperNode,
|
|
85
|
-
sym: SymbolNode,
|
|
86
|
-
until: UntilNode,
|
|
87
|
-
until_post: UntilNode,
|
|
88
|
-
lvar: VarNode,
|
|
89
|
-
ivar: VarNode,
|
|
90
|
-
cvar: VarNode,
|
|
91
|
-
gvar: VarNode,
|
|
92
|
-
when: WhenNode,
|
|
93
|
-
while: WhileNode,
|
|
94
|
-
while_post: WhileNode,
|
|
95
|
-
yield: YieldNode
|
|
96
|
-
}.freeze
|
|
97
|
-
|
|
98
|
-
# Generates {Node} from the given information.
|
|
99
|
-
#
|
|
100
|
-
# @return [Node] the generated node
|
|
101
|
-
def n(type, children, source_map)
|
|
102
|
-
node_klass(type).new(type, children, location: source_map)
|
|
103
|
-
end
|
|
104
|
-
|
|
105
|
-
# Overwrite the base method to allow strings with invalid encoding
|
|
106
|
-
# More details here https://github.com/whitequark/parser/issues/283
|
|
107
|
-
def string_value(token)
|
|
108
|
-
value(token)
|
|
109
|
-
end
|
|
110
|
-
|
|
111
|
-
private
|
|
112
|
-
|
|
113
|
-
def node_klass(type)
|
|
114
|
-
NODE_MAP[type] || Node
|
|
115
|
-
end
|
|
116
|
-
end
|
|
117
|
-
|
|
118
7
|
# `RuboCop::AST::Builder` is an AST builder that is utilized to let `Parser`
|
|
119
8
|
# generate ASTs with {RuboCop::AST::Node}.
|
|
120
9
|
#
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RuboCop
|
|
4
|
+
module AST
|
|
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
|
|
12
|
+
|
|
13
|
+
# @api private
|
|
14
|
+
NODE_MAP = {
|
|
15
|
+
and: AndNode,
|
|
16
|
+
and_asgn: AndAsgnNode,
|
|
17
|
+
alias: AliasNode,
|
|
18
|
+
arg: ArgNode,
|
|
19
|
+
blockarg: ArgNode,
|
|
20
|
+
forward_arg: ArgNode,
|
|
21
|
+
kwarg: ArgNode,
|
|
22
|
+
kwoptarg: ArgNode,
|
|
23
|
+
kwrestarg: ArgNode,
|
|
24
|
+
optarg: ArgNode,
|
|
25
|
+
restarg: ArgNode,
|
|
26
|
+
shadowarg: ArgNode,
|
|
27
|
+
args: ArgsNode,
|
|
28
|
+
array: ArrayNode,
|
|
29
|
+
lvasgn: AsgnNode,
|
|
30
|
+
ivasgn: AsgnNode,
|
|
31
|
+
cvasgn: AsgnNode,
|
|
32
|
+
gvasgn: AsgnNode,
|
|
33
|
+
block: BlockNode,
|
|
34
|
+
numblock: BlockNode,
|
|
35
|
+
itblock: BlockNode,
|
|
36
|
+
break: BreakNode,
|
|
37
|
+
case_match: CaseMatchNode,
|
|
38
|
+
casgn: CasgnNode,
|
|
39
|
+
case: CaseNode,
|
|
40
|
+
class: ClassNode,
|
|
41
|
+
complex: ComplexNode,
|
|
42
|
+
const: ConstNode,
|
|
43
|
+
def: DefNode,
|
|
44
|
+
defined?: DefinedNode,
|
|
45
|
+
defs: DefNode,
|
|
46
|
+
dstr: DstrNode,
|
|
47
|
+
ensure: EnsureNode,
|
|
48
|
+
for: ForNode,
|
|
49
|
+
forward_args: ForwardArgsNode,
|
|
50
|
+
forwarded_kwrestarg: KeywordSplatNode,
|
|
51
|
+
float: FloatNode,
|
|
52
|
+
hash: HashNode,
|
|
53
|
+
if: IfNode,
|
|
54
|
+
in_pattern: InPatternNode,
|
|
55
|
+
int: IntNode,
|
|
56
|
+
index: IndexNode,
|
|
57
|
+
indexasgn: IndexasgnNode,
|
|
58
|
+
irange: RangeNode,
|
|
59
|
+
erange: RangeNode,
|
|
60
|
+
kwargs: HashNode,
|
|
61
|
+
kwbegin: KeywordBeginNode,
|
|
62
|
+
kwsplat: KeywordSplatNode,
|
|
63
|
+
lambda: LambdaNode,
|
|
64
|
+
masgn: MasgnNode,
|
|
65
|
+
mlhs: MlhsNode,
|
|
66
|
+
module: ModuleNode,
|
|
67
|
+
next: NextNode,
|
|
68
|
+
op_asgn: OpAsgnNode,
|
|
69
|
+
or_asgn: OrAsgnNode,
|
|
70
|
+
or: OrNode,
|
|
71
|
+
pair: PairNode,
|
|
72
|
+
procarg0: Procarg0Node,
|
|
73
|
+
rational: RationalNode,
|
|
74
|
+
regexp: RegexpNode,
|
|
75
|
+
rescue: RescueNode,
|
|
76
|
+
resbody: ResbodyNode,
|
|
77
|
+
return: ReturnNode,
|
|
78
|
+
csend: CsendNode,
|
|
79
|
+
send: SendNode,
|
|
80
|
+
str: StrNode,
|
|
81
|
+
xstr: StrNode,
|
|
82
|
+
sclass: SelfClassNode,
|
|
83
|
+
super: SuperNode,
|
|
84
|
+
zsuper: SuperNode,
|
|
85
|
+
sym: SymbolNode,
|
|
86
|
+
until: UntilNode,
|
|
87
|
+
until_post: UntilNode,
|
|
88
|
+
lvar: VarNode,
|
|
89
|
+
ivar: VarNode,
|
|
90
|
+
cvar: VarNode,
|
|
91
|
+
gvar: VarNode,
|
|
92
|
+
when: WhenNode,
|
|
93
|
+
while: WhileNode,
|
|
94
|
+
while_post: WhileNode,
|
|
95
|
+
yield: YieldNode
|
|
96
|
+
}.freeze
|
|
97
|
+
|
|
98
|
+
# Generates {Node} from the given information.
|
|
99
|
+
#
|
|
100
|
+
# @return [Node] the generated node
|
|
101
|
+
def n(type, children, source_map)
|
|
102
|
+
node_klass(type).new(type, children, location: source_map)
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
# Overwrite the base method to allow strings with invalid encoding
|
|
106
|
+
# More details here https://github.com/whitequark/parser/issues/283
|
|
107
|
+
def string_value(token)
|
|
108
|
+
value(token)
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
private
|
|
112
|
+
|
|
113
|
+
def node_klass(type)
|
|
114
|
+
NODE_MAP[type] || Node
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
end
|
|
@@ -20,6 +20,37 @@ module RuboCop
|
|
|
20
20
|
def line_span(exclude_end: false)
|
|
21
21
|
::Range.new(first_line, last_line, exclude_end)
|
|
22
22
|
end
|
|
23
|
+
|
|
24
|
+
# @param [Parser::Source::Range, RuboCop::AST::Node, RuboCop::AST::Token,
|
|
25
|
+
# Parser::Source::Comment] other
|
|
26
|
+
# @return [Boolean] whether this range starts on the same line as `other`
|
|
27
|
+
def same_line?(other)
|
|
28
|
+
other_line = if other.respond_to?(:line)
|
|
29
|
+
other.line
|
|
30
|
+
elsif other.respond_to?(:loc)
|
|
31
|
+
other.loc.line
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
!other_line.nil? && line == other_line
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# Arbitrarily chosen value, should be enough to cover
|
|
38
|
+
# the most nested source code in real world projects.
|
|
39
|
+
MAX_LINE_BEGINS_REGEX_INDEX = 50
|
|
40
|
+
LINE_BEGINS_REGEX_CACHE = Hash.new do |hash, index|
|
|
41
|
+
hash[index] = /^\s{#{index}}\S/ if index <= MAX_LINE_BEGINS_REGEX_INDEX
|
|
42
|
+
end
|
|
43
|
+
private_constant :MAX_LINE_BEGINS_REGEX_INDEX, :LINE_BEGINS_REGEX_CACHE
|
|
44
|
+
|
|
45
|
+
# @return [Boolean] whether this range begins its line, i.e. is preceded
|
|
46
|
+
# only by whitespace
|
|
47
|
+
def begins_its_line?
|
|
48
|
+
if (regex = LINE_BEGINS_REGEX_CACHE[column])
|
|
49
|
+
source_line.match?(regex)
|
|
50
|
+
else
|
|
51
|
+
source_line.index(/\S/) == column
|
|
52
|
+
end
|
|
53
|
+
end
|
|
23
54
|
end
|
|
24
55
|
end
|
|
25
56
|
end
|
|
@@ -130,7 +130,7 @@ module RuboCop
|
|
|
130
130
|
#
|
|
131
131
|
# @return [Boolean] whether the `block` literal is on a single line
|
|
132
132
|
def single_line?
|
|
133
|
-
loc.begin.
|
|
133
|
+
loc.begin.same_line?(loc.end)
|
|
134
134
|
end
|
|
135
135
|
|
|
136
136
|
# Checks whether this is a multiline block. This is overridden here
|
|
@@ -22,7 +22,9 @@ module RuboCop
|
|
|
22
22
|
#
|
|
23
23
|
# @return [Boolean] whether the node is an `unless` statement
|
|
24
24
|
def unless?
|
|
25
|
-
keyword
|
|
25
|
+
# Compare the keyword length to avoid allocating the keyword source;
|
|
26
|
+
# a non-ternary `if` node's keyword can only be `if`, `elsif` or `unless`.
|
|
27
|
+
!ternary? && loc.keyword.length == 6
|
|
26
28
|
end
|
|
27
29
|
|
|
28
30
|
# Checks whether the `if` node has an `then` clause.
|
|
@@ -120,7 +122,7 @@ module RuboCop
|
|
|
120
122
|
# @return [Node] the truthy branch node of the `if` node
|
|
121
123
|
# @return [nil] if the truthy branch is empty
|
|
122
124
|
def if_branch
|
|
123
|
-
|
|
125
|
+
unless? ? children[2] : children[1]
|
|
124
126
|
end
|
|
125
127
|
|
|
126
128
|
# Returns the branch of the `if` node that gets evaluated when its
|
|
@@ -131,7 +133,7 @@ module RuboCop
|
|
|
131
133
|
# @return [Node] the falsey branch node of the `if` node
|
|
132
134
|
# @return [nil] when there is no else branch
|
|
133
135
|
def else_branch
|
|
134
|
-
|
|
136
|
+
unless? ? children[1] : children[2]
|
|
135
137
|
end
|
|
136
138
|
|
|
137
139
|
# Custom destructuring method. This is used to normalize the branches
|
|
@@ -139,13 +141,7 @@ module RuboCop
|
|
|
139
141
|
#
|
|
140
142
|
# @return [Array<Node>] the different parts of the `if` statement
|
|
141
143
|
def node_parts
|
|
142
|
-
|
|
143
|
-
condition, false_branch, true_branch = *self
|
|
144
|
-
else
|
|
145
|
-
condition, true_branch, false_branch = *self
|
|
146
|
-
end
|
|
147
|
-
|
|
148
|
-
[condition, true_branch, false_branch]
|
|
144
|
+
[condition, if_branch, else_branch]
|
|
149
145
|
end
|
|
150
146
|
|
|
151
147
|
# Returns an array of all the branches in the conditional statement.
|
|
@@ -11,7 +11,7 @@ module RuboCop
|
|
|
11
11
|
#
|
|
12
12
|
# @return [Boolean] whether the condition is on a single line
|
|
13
13
|
def single_line_condition?
|
|
14
|
-
loc.keyword.
|
|
14
|
+
loc.keyword.same_line?(condition)
|
|
15
15
|
end
|
|
16
16
|
|
|
17
17
|
# Checks whether the condition of the node is written on more than
|
|
@@ -22,12 +22,12 @@ module RuboCop
|
|
|
22
22
|
!single_line_condition?
|
|
23
23
|
end
|
|
24
24
|
|
|
25
|
-
# Returns the condition of the node.
|
|
26
|
-
#
|
|
25
|
+
# Returns the condition of the node. The condition is the first child
|
|
26
|
+
# for all conditional node types, including normalized `unless` nodes.
|
|
27
27
|
#
|
|
28
28
|
# @return [Node, nil] the condition of the node
|
|
29
29
|
def condition
|
|
30
|
-
|
|
30
|
+
children[0]
|
|
31
31
|
end
|
|
32
32
|
|
|
33
33
|
# Returns the body associated with the condition. This works together with
|
|
@@ -73,6 +73,31 @@ module RuboCop
|
|
|
73
73
|
each_descendant.to_a
|
|
74
74
|
end
|
|
75
75
|
|
|
76
|
+
# Checks whether any descendant node matches. The types and the optional
|
|
77
|
+
# block are interpreted the same way as for `each_descendant`.
|
|
78
|
+
#
|
|
79
|
+
# @overload any_descendant?
|
|
80
|
+
# Checks whether there is any descendant node.
|
|
81
|
+
# @overload any_descendant?(type, ...)
|
|
82
|
+
# Checks whether there is any descendant node matching any of the types.
|
|
83
|
+
# @param [Symbol] type a node type
|
|
84
|
+
# @yieldparam [Node] node descendant nodes matching the types, if a block is given
|
|
85
|
+
# @return [Boolean] whether any (matching) descendant node was found
|
|
86
|
+
# (for which the block returned a truthy value, if given)
|
|
87
|
+
def any_descendant?(*types)
|
|
88
|
+
if block_given?
|
|
89
|
+
each_descendant(*types) do |descendant|
|
|
90
|
+
return true if yield(descendant)
|
|
91
|
+
end
|
|
92
|
+
else
|
|
93
|
+
each_descendant(*types) do # rubocop:disable Lint/UnreachableLoop
|
|
94
|
+
return true
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
false
|
|
99
|
+
end
|
|
100
|
+
|
|
76
101
|
# Calls the given block for the receiver and each descendant node in
|
|
77
102
|
# depth-first order.
|
|
78
103
|
# If no block is given, an `Enumerator` is returned.
|
|
@@ -99,9 +99,8 @@ module RuboCop
|
|
|
99
99
|
|
|
100
100
|
def valid_argument_types?
|
|
101
101
|
[first, second].all? do |argument|
|
|
102
|
-
# rubocop:disable InternalAffairs/NodeTypeMultiplePredicates
|
|
102
|
+
# rubocop:disable-next InternalAffairs/NodeTypeMultiplePredicates
|
|
103
103
|
argument.pair_type? || argument.kwsplat_type?
|
|
104
|
-
# rubocop:enable InternalAffairs/NodeTypeMultiplePredicates
|
|
105
104
|
end
|
|
106
105
|
end
|
|
107
106
|
|
data/lib/rubocop/ast/node.rb
CHANGED
|
@@ -135,7 +135,6 @@ module RuboCop
|
|
|
135
135
|
match_pattern: :any_match_pattern,
|
|
136
136
|
match_pattern_p: :any_match_pattern
|
|
137
137
|
}.freeze
|
|
138
|
-
private_constant :GROUP_FOR_TYPE
|
|
139
138
|
|
|
140
139
|
# Define a +recursive_?+ predicate method for the given node kind.
|
|
141
140
|
private_class_method def self.def_recursive_literal_predicate(kind) # rubocop:disable Metrics/MethodLength
|
|
@@ -246,7 +245,7 @@ module RuboCop
|
|
|
246
245
|
# part of it is changed.
|
|
247
246
|
def updated(type = nil, children = nil, properties = {})
|
|
248
247
|
properties[:location] ||= @location
|
|
249
|
-
klass =
|
|
248
|
+
klass = BuilderExtensions::NODE_MAP[type || @type] || Node
|
|
250
249
|
klass.new(type || @type, children || @children, properties)
|
|
251
250
|
end
|
|
252
251
|
|
|
@@ -536,6 +535,28 @@ module RuboCop
|
|
|
536
535
|
parent&.call_type? && equal?(parent.receiver)
|
|
537
536
|
end
|
|
538
537
|
|
|
538
|
+
# Returns the receiver at the base of this node's call chain, e.g. the
|
|
539
|
+
# `if` node for `(cond ? a : b).foo.bar`. Returns `self` for non-call
|
|
540
|
+
# nodes and `nil` when the chain starts with a receiverless call
|
|
541
|
+
# (e.g. for `foo.bar.baz`).
|
|
542
|
+
#
|
|
543
|
+
# @return [Node, nil] the node at the base of this call chain
|
|
544
|
+
def first_part_of_call_chain
|
|
545
|
+
node = self
|
|
546
|
+
|
|
547
|
+
while node
|
|
548
|
+
if node.call_type?
|
|
549
|
+
node = node.receiver
|
|
550
|
+
elsif node.any_block_type?
|
|
551
|
+
node = node.send_node
|
|
552
|
+
else
|
|
553
|
+
break
|
|
554
|
+
end
|
|
555
|
+
end
|
|
556
|
+
|
|
557
|
+
node
|
|
558
|
+
end
|
|
559
|
+
|
|
539
560
|
def argument?
|
|
540
561
|
return false unless parent&.send_type?
|
|
541
562
|
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RuboCop
|
|
4
|
+
module AST
|
|
5
|
+
# What the children of the nodes of one type are: how many there are, and
|
|
6
|
+
# which of them are nodes and which are values like a symbol.
|
|
7
|
+
#
|
|
8
|
+
# A layout is described by one word per child:
|
|
9
|
+
#
|
|
10
|
+
# * `node` is a node, and the name of a class is a value of that class
|
|
11
|
+
# * `node?` is a child that may be `nil`
|
|
12
|
+
# * `[node]` is a child that may be missing
|
|
13
|
+
# * `node*` is any number of children, and `node+` at least one
|
|
14
|
+
#
|
|
15
|
+
# So `send` nodes are `node? Symbol node*`: the receiver, if there is one,
|
|
16
|
+
# the method name and the arguments.
|
|
17
|
+
#
|
|
18
|
+
# Only the node types that the parsers emit for the supported Ruby
|
|
19
|
+
# versions have a layout.
|
|
20
|
+
#
|
|
21
|
+
# @api private
|
|
22
|
+
class NodeLayout
|
|
23
|
+
# One child, or a run of children, of the same kinds.
|
|
24
|
+
Child = Struct.new(:kinds, :least, :most)
|
|
25
|
+
|
|
26
|
+
DESCRIPTIONS = {
|
|
27
|
+
# Literals
|
|
28
|
+
true: '', false: '', nil: '',
|
|
29
|
+
int: 'Integer', float: 'Float', rational: 'Rational', complex: 'Complex',
|
|
30
|
+
str: 'String', sym: 'Symbol',
|
|
31
|
+
dstr: 'node*', dsym: 'node*', xstr: 'node*',
|
|
32
|
+
regexp: 'node+', regopt: 'Symbol*',
|
|
33
|
+
array: 'node*', hash: 'node*', pair: 'node node',
|
|
34
|
+
splat: '[node]', kwsplat: '[node]',
|
|
35
|
+
irange: 'node? node?', erange: 'node? node?',
|
|
36
|
+
|
|
37
|
+
# Variables and constants
|
|
38
|
+
self: '', cbase: '',
|
|
39
|
+
lvar: 'Symbol', ivar: 'Symbol', cvar: 'Symbol', gvar: 'Symbol',
|
|
40
|
+
nth_ref: 'Integer', back_ref: 'Symbol',
|
|
41
|
+
const: 'node? Symbol',
|
|
42
|
+
|
|
43
|
+
# Assignments
|
|
44
|
+
lvasgn: 'Symbol [node]', ivasgn: 'Symbol [node]',
|
|
45
|
+
cvasgn: 'Symbol [node]', gvasgn: 'Symbol [node]',
|
|
46
|
+
casgn: 'node? Symbol [node]',
|
|
47
|
+
masgn: 'node node', mlhs: 'node+',
|
|
48
|
+
op_asgn: 'node Symbol node', or_asgn: 'node node', and_asgn: 'node node',
|
|
49
|
+
match_with_lvasgn: 'node node',
|
|
50
|
+
|
|
51
|
+
# Definitions
|
|
52
|
+
def: 'Symbol node node?', defs: 'node Symbol node node?',
|
|
53
|
+
class: 'node node? node?', module: 'node node?', sclass: 'node node?',
|
|
54
|
+
alias: 'node node', undef: 'node+',
|
|
55
|
+
|
|
56
|
+
# Arguments
|
|
57
|
+
args: 'node*',
|
|
58
|
+
arg: 'Symbol', optarg: 'Symbol node', restarg: '[Symbol]',
|
|
59
|
+
kwarg: 'Symbol', kwoptarg: 'Symbol node', kwrestarg: '[Symbol]', kwnilarg: '',
|
|
60
|
+
blockarg: 'Symbol?', shadowarg: 'Symbol', forward_arg: '',
|
|
61
|
+
forwarded_args: '', forwarded_restarg: '', forwarded_kwrestarg: '',
|
|
62
|
+
|
|
63
|
+
# Calls
|
|
64
|
+
send: 'node? Symbol node*', csend: 'node Symbol node*',
|
|
65
|
+
super: 'node*', zsuper: '', yield: 'node*',
|
|
66
|
+
block: 'node node node?', numblock: 'node Integer node?', itblock: 'node Symbol node?',
|
|
67
|
+
block_pass: 'node?',
|
|
68
|
+
|
|
69
|
+
# Control flow
|
|
70
|
+
and: 'node node', or: 'node node', defined?: 'node',
|
|
71
|
+
if: 'node node? node?',
|
|
72
|
+
case: 'node? node+ node?', when: 'node+ node?',
|
|
73
|
+
while: 'node node?', until: 'node node?',
|
|
74
|
+
while_post: 'node node', until_post: 'node node',
|
|
75
|
+
for: 'node node node?',
|
|
76
|
+
iflipflop: 'node? node?', eflipflop: 'node? node?',
|
|
77
|
+
match_current_line: 'node',
|
|
78
|
+
return: 'node*', break: 'node*', next: 'node*', redo: '', retry: '',
|
|
79
|
+
begin: 'node*', kwbegin: 'node*',
|
|
80
|
+
rescue: 'node? node+ node?', resbody: 'node? node? node?', ensure: 'node? node?',
|
|
81
|
+
preexe: 'node?', postexe: 'node?',
|
|
82
|
+
|
|
83
|
+
# Pattern matching
|
|
84
|
+
case_match: 'node node+ node?', in_pattern: 'node node? node?', empty_else: '',
|
|
85
|
+
match_pattern: 'node node', match_pattern_p: 'node node',
|
|
86
|
+
match_var: 'Symbol', match_rest: '[node]', match_nil_pattern: '',
|
|
87
|
+
match_as: 'node node', match_alt: 'node node', pin: 'node',
|
|
88
|
+
if_guard: 'node', unless_guard: 'node',
|
|
89
|
+
array_pattern: 'node*', array_pattern_with_tail: 'node*', find_pattern: 'node+',
|
|
90
|
+
hash_pattern: 'node*', const_pattern: 'node node'
|
|
91
|
+
}.freeze
|
|
92
|
+
|
|
93
|
+
WORD = /\A(?<optional>\[)?(?<kind>\w+)(?<nilable>\?)?\]?(?<repeated>[*+])?\z/.freeze
|
|
94
|
+
private_constant :WORD
|
|
95
|
+
|
|
96
|
+
class << self
|
|
97
|
+
# @return [NodeLayout, nil] the layout of the nodes of the given type
|
|
98
|
+
def of(type)
|
|
99
|
+
LAYOUTS[type]
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
# @return [Symbol, Class] `:node`, or the class of a child that is a value
|
|
103
|
+
def kind(child)
|
|
104
|
+
child.is_a?(::AST::Node) ? :node : child.class
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
# @return [Range] how many children a node can have
|
|
109
|
+
attr_reader :arity
|
|
110
|
+
|
|
111
|
+
def initialize(description)
|
|
112
|
+
@children = description.split.map { |word| parse(word) }.freeze
|
|
113
|
+
@arity = @children.sum(&:least)..@children.sum(&:most)
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
# @return [Array<Symbol, Class>] what the child at the index can be
|
|
117
|
+
def kinds(index)
|
|
118
|
+
least = most = 0
|
|
119
|
+
@children.each_with_object([]) do |child, kinds|
|
|
120
|
+
kinds.concat(child.kinds) if index >= least && index < most + child.most
|
|
121
|
+
least += child.least
|
|
122
|
+
most += child.most
|
|
123
|
+
end.uniq
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
# @return [Boolean] whether a node can have these children
|
|
127
|
+
def fit?(children, layout = @children)
|
|
128
|
+
child, *rest = layout
|
|
129
|
+
return children.empty? unless child
|
|
130
|
+
|
|
131
|
+
[child.most, children.size].min.downto(child.least).any? do |count|
|
|
132
|
+
children.first(count).all? { |value| child.kinds.include?(self.class.kind(value)) } &&
|
|
133
|
+
fit?(children.drop(count), rest)
|
|
134
|
+
end
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
private
|
|
138
|
+
|
|
139
|
+
def parse(word)
|
|
140
|
+
match = WORD.match(word) || raise(ArgumentError, "invalid layout: #{word}")
|
|
141
|
+
kinds = [match[:kind] == 'node' ? :node : Object.const_get(match[:kind])]
|
|
142
|
+
kinds << NilClass if match[:nilable]
|
|
143
|
+
least = match[:optional] || match[:repeated] == '*' ? 0 : 1
|
|
144
|
+
|
|
145
|
+
Child.new(kinds.freeze, least, match[:repeated] ? Float::INFINITY : 1).freeze
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
LAYOUTS = DESCRIPTIONS.transform_values { |description| new(description) }.freeze
|
|
149
|
+
private_constant :LAYOUTS
|
|
150
|
+
end
|
|
151
|
+
end
|
|
152
|
+
end
|