rubocop-ast 1.51.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/node.rb +1 -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/lexer.rex.rb +1 -1
- data/lib/rubocop/ast/node_pattern/parser.racc.rb +1 -1
- 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 +26 -27
- 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 +71 -2
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
|
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
|
|
|
@@ -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
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RuboCop
|
|
4
|
+
module AST
|
|
5
|
+
class NodePattern
|
|
6
|
+
class Checker
|
|
7
|
+
# Checks a sequence, like `(send _ :foo)`, against the layouts of the
|
|
8
|
+
# node types that it is for.
|
|
9
|
+
#
|
|
10
|
+
# @api private
|
|
11
|
+
class SequenceCheck
|
|
12
|
+
LITERALS = { symbol: [Symbol], number: [Integer, Float, Rational, Complex], string: [String] }.freeze
|
|
13
|
+
GROUPS = AST::Node::GROUP_FOR_TYPE.group_by(&:last)
|
|
14
|
+
.transform_values { |pairs| pairs.map(&:first) }
|
|
15
|
+
.freeze
|
|
16
|
+
private_constant :LITERALS, :GROUPS
|
|
17
|
+
|
|
18
|
+
# @return [Array<(NodePattern::Node, String)>] what is wrong, and with which node
|
|
19
|
+
attr_reader :problems
|
|
20
|
+
|
|
21
|
+
def initialize(node)
|
|
22
|
+
@node = node
|
|
23
|
+
@terms = node.children.drop(1)
|
|
24
|
+
@problems = []
|
|
25
|
+
alternatives = head_types(node.child)
|
|
26
|
+
return unless alternatives
|
|
27
|
+
|
|
28
|
+
fitting = check_arity(alternatives.map { |types| types.select { |type| NodeLayout.of(type) } })
|
|
29
|
+
# The children of a type that has no layout can be anything.
|
|
30
|
+
check_children(fitting) if alternatives.flatten.all? { |type| NodeLayout.of(type) }
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
private
|
|
34
|
+
|
|
35
|
+
def problem(node, message)
|
|
36
|
+
@problems << [node, message]
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# @return [Array<Array<Symbol>>, nil] the node types that each
|
|
40
|
+
# alternative of the head stands for, if the head is nothing but
|
|
41
|
+
# node types
|
|
42
|
+
def head_types(head)
|
|
43
|
+
heads = head.type == :union ? head.children : [head]
|
|
44
|
+
return unless heads.all? { |child| node_type?(child) }
|
|
45
|
+
|
|
46
|
+
heads.map { |child| members(child.child) }
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def node_type?(node)
|
|
50
|
+
node.type == :node_type && Checker.type?(node.child)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# @return [Array<Symbol>] the types that a node type or a group stands for
|
|
54
|
+
def members(type)
|
|
55
|
+
type = type.to_s.tr('-', '_').to_sym
|
|
56
|
+
GROUPS.fetch(type) { [type] }
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# @return [Array<Symbol>] the types that can have as many children as the terms match
|
|
60
|
+
def check_arity(alternatives)
|
|
61
|
+
wanted = arity
|
|
62
|
+
alternatives.flat_map do |types|
|
|
63
|
+
fitting = types.select { |type| overlap?(NodeLayout.of(type).arity, wanted) }
|
|
64
|
+
# A group is fine as long as one of its types fits.
|
|
65
|
+
types.each { |type| arity_problem(type, wanted) } if fitting.empty?
|
|
66
|
+
fitting
|
|
67
|
+
end.uniq
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
# @return [Range] how many children the terms match
|
|
71
|
+
def arity
|
|
72
|
+
ranges = @terms.map(&:arity_range)
|
|
73
|
+
ranges.sum(&:begin)..ranges.sum(&:end)
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def overlap?(range, other)
|
|
77
|
+
range.begin <= other.end && other.begin <= range.end
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def arity_problem(type, wanted)
|
|
81
|
+
has = count(NodeLayout.of(type).arity, 'children')
|
|
82
|
+
problem(@node, "`#{type}` nodes have #{has}, not #{count(wanted)}.")
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def count(range, noun = nil)
|
|
86
|
+
number = if range.end == Float::INFINITY
|
|
87
|
+
"#{range.begin} or more"
|
|
88
|
+
elsif range.begin == range.end
|
|
89
|
+
range.begin.zero? && noun ? 'no' : range.begin.to_s
|
|
90
|
+
else
|
|
91
|
+
"#{range.begin} to #{range.end}"
|
|
92
|
+
end
|
|
93
|
+
noun = 'child' if noun && range == (1..1)
|
|
94
|
+
[number, noun].compact.join(' ')
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def check_children(types)
|
|
98
|
+
@terms.each_with_index do |term, index|
|
|
99
|
+
# From here on, which child a term matches is not known.
|
|
100
|
+
break unless term.arity == 1
|
|
101
|
+
|
|
102
|
+
kinds = types.flat_map { |type| NodeLayout.of(type).kinds(index) }.uniq
|
|
103
|
+
next if kinds.empty? || possible?(term, kinds)
|
|
104
|
+
|
|
105
|
+
problem(term, "This never matches child #{index + 1} of `#{types.join('`, `')}` " \
|
|
106
|
+
"nodes, which is #{kinds.map { |kind| describe(kind) }.join(' or ')}.")
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
# @return [Boolean] whether a term can match a child that is one of `kinds`
|
|
111
|
+
def possible?(term, kinds)
|
|
112
|
+
case term.type
|
|
113
|
+
when :capture then possible?(term.child, kinds)
|
|
114
|
+
when :union, :set then term.children.any? { |child| possible?(child, kinds) }
|
|
115
|
+
when :intersection then term.children.all? { |child| possible?(child, kinds) }
|
|
116
|
+
else kind?(term, kinds)
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def kind?(term, kinds)
|
|
121
|
+
case term.type
|
|
122
|
+
when :symbol, :number, :string then !(kinds & LITERALS.fetch(term.type)).empty?
|
|
123
|
+
when :node_type then !Checker.type?(term.child) || kinds.include?(:node)
|
|
124
|
+
when :sequence then kinds.include?(:node)
|
|
125
|
+
when :predicate then term.children != [:nil?] || kinds.include?(NilClass)
|
|
126
|
+
else true
|
|
127
|
+
end
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
def describe(kind)
|
|
131
|
+
return 'a node' if kind == :node
|
|
132
|
+
return 'nil' if kind == NilClass
|
|
133
|
+
|
|
134
|
+
"#{kind == Integer ? 'an' : 'a'} #{kind.name.downcase}"
|
|
135
|
+
end
|
|
136
|
+
end
|
|
137
|
+
end
|
|
138
|
+
end
|
|
139
|
+
end
|
|
140
|
+
end
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RuboCop
|
|
4
|
+
module AST
|
|
5
|
+
class NodePattern
|
|
6
|
+
# Finds the parts of a pattern that can never match, or that are there
|
|
7
|
+
# twice:
|
|
8
|
+
#
|
|
9
|
+
# * node types that do not exist
|
|
10
|
+
# * sequences that have more or fewer children than a node of that type
|
|
11
|
+
# * children that are a node where a node of that type has a value like a
|
|
12
|
+
# symbol, and the other way around
|
|
13
|
+
# * predicates that nothing responds to
|
|
14
|
+
# * alternatives of a union that are repeated
|
|
15
|
+
#
|
|
16
|
+
# Checker.new.check('(def _ _)').map(&:message)
|
|
17
|
+
# # => ['`def` nodes have 3 children, not 2.']
|
|
18
|
+
#
|
|
19
|
+
# A pattern that has none of these is not necessarily right.
|
|
20
|
+
class Checker
|
|
21
|
+
# What is wrong, and the node of the pattern that it is about. The
|
|
22
|
+
# range is relative to the pattern.
|
|
23
|
+
Problem = Struct.new(:node, :message) do
|
|
24
|
+
def range
|
|
25
|
+
node.source_range
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
VALUES = [Symbol, String, Integer, Float, Rational, Complex, NilClass].freeze
|
|
30
|
+
private_constant :VALUES
|
|
31
|
+
|
|
32
|
+
# @return [Boolean] whether a pattern can have this node type in it
|
|
33
|
+
def self.type?(type)
|
|
34
|
+
AST::Node.method_defined?(:"#{type.to_s.tr('-', '_')}_type?")
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# @param pattern [String]
|
|
38
|
+
# @return [Array<Problem>]
|
|
39
|
+
# @raise [NodePattern::Invalid] if the pattern can't be parsed
|
|
40
|
+
def check(pattern)
|
|
41
|
+
@problems = []
|
|
42
|
+
visit(Parser::WithMeta.new.parse(pattern))
|
|
43
|
+
@problems
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
private
|
|
47
|
+
|
|
48
|
+
def visit(node)
|
|
49
|
+
return unless node.is_a?(Node)
|
|
50
|
+
|
|
51
|
+
case node.type
|
|
52
|
+
when :node_type then check_type(node)
|
|
53
|
+
when :sequence then check_sequence(node)
|
|
54
|
+
when :predicate then check_predicate(node)
|
|
55
|
+
when :union, :set then check_alternatives(node)
|
|
56
|
+
end
|
|
57
|
+
node.children.each { |child| visit(child) }
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def problem(node, message)
|
|
61
|
+
@problems << Problem.new(node, message)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def check_type(node)
|
|
65
|
+
problem(node, "Unknown node type `#{node.child}`.") unless Checker.type?(node.child)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def check_sequence(node)
|
|
69
|
+
SequenceCheck.new(node).problems.each { |term, message| problem(term, message) }
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def check_predicate(node)
|
|
73
|
+
name = node.method_name
|
|
74
|
+
return if receivers.any? { |klass| klass.method_defined?(name) }
|
|
75
|
+
|
|
76
|
+
problem(node, "Neither a node nor a child of one responds to `#{name}`.")
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def receivers
|
|
80
|
+
@receivers ||= VALUES + node_classes
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def node_classes
|
|
84
|
+
AST.constants.reject { |name| AST.autoload?(name) }
|
|
85
|
+
.map { |name| AST.const_get(name) }
|
|
86
|
+
.select { |const| const.is_a?(Class) && const <= AST::Node }
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def check_alternatives(node)
|
|
90
|
+
# What a union in the head of a sequence is rewritten to has no location.
|
|
91
|
+
sources = node.children.grep(Node).filter_map { |child| child.loc&.expression&.source }
|
|
92
|
+
sources.select { |source| sources.count(source) > 1 }.uniq.each do |source|
|
|
93
|
+
problem(node, "`#{source}` is in the union more than once.")
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
require_relative 'checker/sequence_check'
|