rubocop-ast 1.30.0 → 1.37.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 +1 -1
- data/lib/rubocop/ast/builder.rb +8 -0
- data/lib/rubocop/ast/node/array_node.rb +6 -2
- data/lib/rubocop/ast/node/asgn_node.rb +2 -0
- data/lib/rubocop/ast/node/block_node.rb +2 -2
- data/lib/rubocop/ast/node/casgn_node.rb +4 -12
- data/lib/rubocop/ast/node/const_node.rb +1 -52
- data/lib/rubocop/ast/node/def_node.rb +1 -1
- data/lib/rubocop/ast/node/ensure_node.rb +23 -0
- data/lib/rubocop/ast/node/for_node.rb +1 -1
- data/lib/rubocop/ast/node/hash_node.rb +1 -1
- data/lib/rubocop/ast/node/if_node.rb +8 -1
- data/lib/rubocop/ast/node/in_pattern_node.rb +1 -1
- data/lib/rubocop/ast/node/keyword_begin_node.rb +44 -0
- data/lib/rubocop/ast/node/masgn_node.rb +63 -0
- data/lib/rubocop/ast/node/mixin/basic_literal_node.rb +1 -1
- data/lib/rubocop/ast/node/mixin/collection_node.rb +1 -1
- data/lib/rubocop/ast/node/mixin/constant_node.rb +62 -0
- data/lib/rubocop/ast/node/mixin/method_dispatch_node.rb +4 -4
- data/lib/rubocop/ast/node/mixin/numeric_node.rb +1 -1
- data/lib/rubocop/ast/node/mixin/parameterized_node.rb +1 -1
- data/lib/rubocop/ast/node/mlhs_node.rb +29 -0
- data/lib/rubocop/ast/node/op_asgn_node.rb +3 -1
- data/lib/rubocop/ast/node/rational_node.rb +13 -0
- data/lib/rubocop/ast/node/str_node.rb +37 -1
- data/lib/rubocop/ast/node/until_node.rb +1 -1
- data/lib/rubocop/ast/node/var_node.rb +15 -0
- data/lib/rubocop/ast/node/when_node.rb +1 -1
- data/lib/rubocop/ast/node/while_node.rb +1 -1
- data/lib/rubocop/ast/node.rb +88 -32
- data/lib/rubocop/ast/node_pattern/compiler/binding.rb +3 -3
- data/lib/rubocop/ast/node_pattern/compiler/sequence_subcompiler.rb +2 -3
- data/lib/rubocop/ast/node_pattern/compiler.rb +1 -1
- data/lib/rubocop/ast/node_pattern/lexer.rex.rb +1 -2
- data/lib/rubocop/ast/node_pattern/node.rb +1 -2
- data/lib/rubocop/ast/node_pattern/parser.racc.rb +2 -2
- data/lib/rubocop/ast/node_pattern/parser.rb +1 -1
- data/lib/rubocop/ast/node_pattern.rb +1 -1
- data/lib/rubocop/ast/processed_source.rb +89 -59
- data/lib/rubocop/ast/token.rb +1 -1
- data/lib/rubocop/ast/traversal.rb +2 -2
- data/lib/rubocop/ast/utilities/simple_forwardable.rb +16 -0
- data/lib/rubocop/ast/version.rb +1 -1
- data/lib/rubocop/ast.rb +7 -1
- metadata +13 -7
- data/lib/rubocop/ast/ext/range_min_max.rb +0 -18
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module AST
|
5
|
+
# A node extension for `lvar`, `ivar`, `cvar` and `gvar` 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 VarNode < Node
|
9
|
+
# @return [Symbol] The name of the variable.
|
10
|
+
def name
|
11
|
+
node_parts[0]
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
data/lib/rubocop/ast/node.rb
CHANGED
@@ -88,6 +88,55 @@ module RuboCop
|
|
88
88
|
EMPTY_PROPERTIES = {}.freeze
|
89
89
|
private_constant :EMPTY_CHILDREN, :EMPTY_PROPERTIES
|
90
90
|
|
91
|
+
# @api private
|
92
|
+
GROUP_FOR_TYPE = {
|
93
|
+
arg: :argument,
|
94
|
+
optarg: :argument,
|
95
|
+
restarg: :argument,
|
96
|
+
kwarg: :argument,
|
97
|
+
kwoptarg: :argument,
|
98
|
+
kwrestarg: :argument,
|
99
|
+
blockarg: :argument,
|
100
|
+
forward_arg: :argument,
|
101
|
+
shardowarg: :argument,
|
102
|
+
|
103
|
+
true: :boolean,
|
104
|
+
false: :boolean,
|
105
|
+
|
106
|
+
int: :numeric,
|
107
|
+
float: :numeric,
|
108
|
+
rational: :numeric,
|
109
|
+
complex: :numeric,
|
110
|
+
|
111
|
+
irange: :range,
|
112
|
+
erange: :range,
|
113
|
+
|
114
|
+
send: :call,
|
115
|
+
csend: :call
|
116
|
+
}.freeze
|
117
|
+
private_constant :GROUP_FOR_TYPE
|
118
|
+
|
119
|
+
# Define a +recursive_?+ predicate method for the given node kind.
|
120
|
+
private_class_method def self.def_recursive_literal_predicate(kind) # rubocop:disable Metrics/MethodLength
|
121
|
+
recursive_kind = "recursive_#{kind}?"
|
122
|
+
kind_filter = "#{kind}?"
|
123
|
+
|
124
|
+
class_eval <<~RUBY, __FILE__, __LINE__ + 1
|
125
|
+
def #{recursive_kind} # def recursive_literal?
|
126
|
+
case type # case type
|
127
|
+
when :send # when :send
|
128
|
+
LITERAL_RECURSIVE_METHODS.include?(method_name) && # LITERAL_RECURSIVE_METHODS.include?(method_name) &&
|
129
|
+
receiver.send(:#{recursive_kind}) && # receiver.send(:recursive_literal?) &&
|
130
|
+
arguments.all?(&:#{recursive_kind}) # arguments.all?(&:recursive_literal?)
|
131
|
+
when LITERAL_RECURSIVE_TYPES # when LITERAL_RECURSIVE_TYPES
|
132
|
+
children.compact.all?(&:#{recursive_kind}) # children.compact.all?(&:recursive_literal?)
|
133
|
+
else # else
|
134
|
+
send(:#{kind_filter}) # send(:literal?)
|
135
|
+
end # end
|
136
|
+
end # end
|
137
|
+
RUBY
|
138
|
+
end
|
139
|
+
|
91
140
|
# @see https://www.rubydoc.info/gems/ast/AST/Node:initialize
|
92
141
|
def initialize(type, children = EMPTY_CHILDREN, properties = EMPTY_PROPERTIES)
|
93
142
|
@mutable_attributes = {}
|
@@ -105,6 +154,16 @@ module RuboCop
|
|
105
154
|
end
|
106
155
|
end
|
107
156
|
|
157
|
+
# Determine if the node is one of several node types in a single query
|
158
|
+
# Allows specific single node types, as well as "grouped" types
|
159
|
+
# (e.g. `:boolean` for `:true` or `:false`)
|
160
|
+
def type?(*types)
|
161
|
+
return true if types.include?(type)
|
162
|
+
|
163
|
+
group_type = GROUP_FOR_TYPE[type]
|
164
|
+
!group_type.nil? && types.include?(group_type)
|
165
|
+
end
|
166
|
+
|
108
167
|
(Parser::Meta::NODE_TYPES - [:send]).each do |node_type|
|
109
168
|
method_name = "#{node_type.to_s.gsub(/\W/, '')}_type?"
|
110
169
|
class_eval <<~RUBY, __FILE__, __LINE__ + 1
|
@@ -291,13 +350,12 @@ module RuboCop
|
|
291
350
|
def_node_matcher :str_content, '(str $_)'
|
292
351
|
|
293
352
|
def const_name
|
294
|
-
return unless const_type?
|
353
|
+
return unless const_type? || casgn_type?
|
295
354
|
|
296
|
-
namespace, name = *self
|
297
355
|
if namespace && !namespace.cbase_type?
|
298
|
-
"#{namespace.const_name}::#{
|
356
|
+
"#{namespace.const_name}::#{short_name}"
|
299
357
|
else
|
300
|
-
|
358
|
+
short_name.to_s
|
301
359
|
end
|
302
360
|
end
|
303
361
|
|
@@ -326,13 +384,13 @@ module RuboCop
|
|
326
384
|
# what class or module is this method/constant/etc definition in?
|
327
385
|
# returns nil if answer cannot be determined
|
328
386
|
ancestors = each_ancestor(:class, :module, :sclass, :casgn, :block)
|
329
|
-
result = ancestors.
|
387
|
+
result = ancestors.filter_map do |ancestor|
|
330
388
|
parent_module_name_part(ancestor) do |full_name|
|
331
389
|
return nil unless full_name
|
332
390
|
|
333
391
|
full_name
|
334
392
|
end
|
335
|
-
end.
|
393
|
+
end.reverse.join('::')
|
336
394
|
result.empty? ? 'Object' : result
|
337
395
|
end
|
338
396
|
|
@@ -380,22 +438,11 @@ module RuboCop
|
|
380
438
|
IMMUTABLE_LITERALS.include?(type)
|
381
439
|
end
|
382
440
|
|
383
|
-
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
388
|
-
when :send
|
389
|
-
LITERAL_RECURSIVE_METHODS.include?(method_name) &&
|
390
|
-
receiver.send(recursive_kind) &&
|
391
|
-
arguments.all?(&recursive_kind)
|
392
|
-
when LITERAL_RECURSIVE_TYPES
|
393
|
-
children.compact.all?(&recursive_kind)
|
394
|
-
else
|
395
|
-
send(kind_filter)
|
396
|
-
end
|
397
|
-
end
|
398
|
-
end
|
441
|
+
# @!macro [attach] def_recursive_literal_predicate
|
442
|
+
# @!method recursive_$1?
|
443
|
+
# @return [Boolean]
|
444
|
+
def_recursive_literal_predicate :literal
|
445
|
+
def_recursive_literal_predicate :basic_literal
|
399
446
|
|
400
447
|
def variable?
|
401
448
|
VARIABLES.include?(type)
|
@@ -450,11 +497,11 @@ module RuboCop
|
|
450
497
|
end
|
451
498
|
|
452
499
|
def parenthesized_call?
|
453
|
-
|
500
|
+
loc_is?(:begin, '(')
|
454
501
|
end
|
455
502
|
|
456
503
|
def call_type?
|
457
|
-
|
504
|
+
GROUP_FOR_TYPE[type] == :call
|
458
505
|
end
|
459
506
|
|
460
507
|
def chained?
|
@@ -466,27 +513,38 @@ module RuboCop
|
|
466
513
|
end
|
467
514
|
|
468
515
|
def argument_type?
|
469
|
-
|
516
|
+
GROUP_FOR_TYPE[type] == :argument
|
470
517
|
end
|
471
518
|
|
472
519
|
def boolean_type?
|
473
|
-
|
520
|
+
GROUP_FOR_TYPE[type] == :boolean
|
474
521
|
end
|
475
522
|
|
476
523
|
def numeric_type?
|
477
|
-
|
524
|
+
GROUP_FOR_TYPE[type] == :numeric
|
478
525
|
end
|
479
526
|
|
480
527
|
def range_type?
|
481
|
-
|
528
|
+
GROUP_FOR_TYPE[type] == :range
|
482
529
|
end
|
483
530
|
|
484
531
|
def guard_clause?
|
485
|
-
node =
|
532
|
+
node = operator_keyword? ? rhs : self
|
486
533
|
|
487
534
|
node.match_guard_clause?
|
488
535
|
end
|
489
536
|
|
537
|
+
# Shortcut to safely test a particular location, even if
|
538
|
+
# this location does not exist or is `nil`
|
539
|
+
def loc_is?(which_loc, str)
|
540
|
+
return false unless loc.respond_to?(which_loc)
|
541
|
+
|
542
|
+
location = loc.public_send(which_loc)
|
543
|
+
return false unless location
|
544
|
+
|
545
|
+
location.is?(str)
|
546
|
+
end
|
547
|
+
|
490
548
|
# @!method match_guard_clause?(node = self)
|
491
549
|
def_node_matcher :match_guard_clause?, <<~PATTERN
|
492
550
|
[${(send nil? {:raise :fail} ...) return break next} single_line?]
|
@@ -547,8 +605,7 @@ module RuboCop
|
|
547
605
|
# So, does the return value of this node matter? If we changed it to
|
548
606
|
# `(...; nil)`, might that affect anything?
|
549
607
|
#
|
550
|
-
# rubocop:disable Metrics/MethodLength
|
551
|
-
def value_used?
|
608
|
+
def value_used? # rubocop:disable Metrics/MethodLength
|
552
609
|
# Be conservative and return true if we're not sure.
|
553
610
|
return false if parent.nil?
|
554
611
|
|
@@ -569,7 +626,6 @@ module RuboCop
|
|
569
626
|
true
|
570
627
|
end
|
571
628
|
end
|
572
|
-
# rubocop:enable Metrics/MethodLength
|
573
629
|
|
574
630
|
# Some expressions are evaluated for their value, some for their side
|
575
631
|
# effects, and some for both.
|
@@ -26,8 +26,9 @@ module RuboCop
|
|
26
26
|
var
|
27
27
|
end
|
28
28
|
|
29
|
-
#
|
30
|
-
|
29
|
+
# Yields for each branch of the given union, forbidding unification of
|
30
|
+
# bindings which only appear in a subset of the union.
|
31
|
+
def union_bind(enum) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
|
31
32
|
# We need to reset @bound before each branch is processed.
|
32
33
|
# Moreover we need to keep track of newly encountered wildcards.
|
33
34
|
# Var `newly_bound_intersection` will hold those that are encountered
|
@@ -62,7 +63,6 @@ module RuboCop
|
|
62
63
|
|
63
64
|
result
|
64
65
|
end
|
65
|
-
# rubocop:enable Metrics/MethodLength, Metrics/AbcSize
|
66
66
|
|
67
67
|
private
|
68
68
|
|
@@ -13,8 +13,8 @@ module RuboCop
|
|
13
13
|
# Doc on how this fits in the compiling process:
|
14
14
|
# /docs/modules/ROOT/pages/node_pattern.adoc
|
15
15
|
#
|
16
|
-
# rubocop:disable Metrics/ClassLength
|
17
|
-
|
16
|
+
class SequenceSubcompiler < Subcompiler # rubocop:disable Metrics/ClassLength
|
17
|
+
# Shift of 1 from standard Ruby indices
|
18
18
|
DELTA = 1
|
19
19
|
POSITIVE = :positive?.to_proc
|
20
20
|
private_constant :POSITIVE
|
@@ -413,7 +413,6 @@ module RuboCop
|
|
413
413
|
@in_sync = sub_compilers.all?(&:in_sync)
|
414
414
|
end
|
415
415
|
end
|
416
|
-
# rubocop:enable Metrics/ClassLength
|
417
416
|
end
|
418
417
|
end
|
419
418
|
end
|
@@ -9,7 +9,7 @@ module RuboCop
|
|
9
9
|
# Doc on how this fits in the compiling process:
|
10
10
|
# /docs/modules/ROOT/pages/node_pattern.adoc
|
11
11
|
class Compiler
|
12
|
-
extend
|
12
|
+
extend SimpleForwardable
|
13
13
|
attr_reader :captures, :named_parameters, :positional_parameters, :binding
|
14
14
|
|
15
15
|
def initialize
|
@@ -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.6.
|
5
|
+
# Generated by: oedipus_lex version 2.6.2.
|
6
6
|
# Source: lib/rubocop/ast/node_pattern/lexer.rex
|
7
7
|
#++
|
8
8
|
|
@@ -70,7 +70,6 @@ class RuboCop::AST::NodePattern::LexerRex
|
|
70
70
|
yield
|
71
71
|
end
|
72
72
|
|
73
|
-
|
74
73
|
##
|
75
74
|
# The current scanner class. Must be overridden in subclasses.
|
76
75
|
|
@@ -5,9 +5,8 @@ module RuboCop
|
|
5
5
|
class NodePattern
|
6
6
|
# Base class for AST Nodes of a `NodePattern`
|
7
7
|
class Node < ::Parser::AST::Node
|
8
|
-
extend
|
8
|
+
extend SimpleForwardable
|
9
9
|
include ::RuboCop::AST::Descendence
|
10
|
-
using Ext::RangeMinMax
|
11
10
|
|
12
11
|
MATCHES_WITHIN_SET = %i[symbol number string].to_set.freeze
|
13
12
|
private_constant :MATCHES_WITHIN_SET
|
@@ -1,8 +1,8 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
#
|
3
3
|
# DO NOT MODIFY!!!!
|
4
|
-
# This file is automatically generated by Racc 1.
|
5
|
-
# from Racc grammar file "".
|
4
|
+
# This file is automatically generated by Racc 1.8.1
|
5
|
+
# from Racc grammar file "parser.y".
|
6
6
|
#
|
7
7
|
|
8
8
|
require 'racc/parser.rb'
|
@@ -11,7 +11,7 @@ module RuboCop
|
|
11
11
|
# Doc on how this fits in the compiling process:
|
12
12
|
# /docs/modules/ROOT/pages/node_pattern.adoc
|
13
13
|
class Parser < Racc::Parser
|
14
|
-
extend
|
14
|
+
extend SimpleForwardable
|
15
15
|
|
16
16
|
Builder = NodePattern::Builder
|
17
17
|
Lexer = NodePattern::Lexer
|
@@ -2,28 +2,36 @@
|
|
2
2
|
|
3
3
|
require 'digest/sha1'
|
4
4
|
|
5
|
-
# rubocop:disable Metrics/ClassLength
|
6
5
|
module RuboCop
|
7
6
|
module AST
|
8
7
|
# ProcessedSource contains objects which are generated by Parser
|
9
8
|
# and other information such as disabled lines for cops.
|
10
9
|
# It also provides a convenient way to access source lines.
|
11
|
-
class ProcessedSource
|
10
|
+
class ProcessedSource # rubocop:disable Metrics/ClassLength
|
12
11
|
# @api private
|
13
12
|
STRING_SOURCE_NAME = '(string)'
|
14
13
|
|
15
14
|
INVALID_LEVELS = %i[error fatal].freeze
|
16
15
|
private_constant :INVALID_LEVELS
|
17
16
|
|
17
|
+
PARSER_ENGINES = %i[parser_whitequark parser_prism].freeze
|
18
|
+
private_constant :PARSER_ENGINES
|
19
|
+
|
18
20
|
attr_reader :path, :buffer, :ast, :comments, :tokens, :diagnostics,
|
19
|
-
:parser_error, :raw_source, :ruby_version
|
21
|
+
:parser_error, :raw_source, :ruby_version, :parser_engine
|
20
22
|
|
21
|
-
def self.from_file(path, ruby_version)
|
23
|
+
def self.from_file(path, ruby_version, parser_engine: :parser_whitequark)
|
22
24
|
file = File.read(path, mode: 'rb')
|
23
|
-
new(file, ruby_version, path)
|
25
|
+
new(file, ruby_version, path, parser_engine: parser_engine)
|
24
26
|
end
|
25
27
|
|
26
|
-
def initialize(source, ruby_version, path = nil)
|
28
|
+
def initialize(source, ruby_version, path = nil, parser_engine: :parser_whitequark)
|
29
|
+
parser_engine = parser_engine.to_sym
|
30
|
+
unless PARSER_ENGINES.include?(parser_engine)
|
31
|
+
raise ArgumentError, 'The keyword argument `parser_engine` accepts `parser_whitequark` ' \
|
32
|
+
"or `parser_prism`, but `#{parser_engine}` was passed."
|
33
|
+
end
|
34
|
+
|
27
35
|
# Defaults source encoding to UTF-8, regardless of the encoding it has
|
28
36
|
# been read with, which could be non-utf8 depending on the default
|
29
37
|
# external encoding.
|
@@ -33,9 +41,10 @@ module RuboCop
|
|
33
41
|
@path = path
|
34
42
|
@diagnostics = []
|
35
43
|
@ruby_version = ruby_version
|
44
|
+
@parser_engine = parser_engine
|
36
45
|
@parser_error = nil
|
37
46
|
|
38
|
-
parse(source, ruby_version)
|
47
|
+
parse(source, ruby_version, parser_engine)
|
39
48
|
end
|
40
49
|
|
41
50
|
def ast_with_comments
|
@@ -193,13 +202,13 @@ module RuboCop
|
|
193
202
|
end
|
194
203
|
end
|
195
204
|
|
196
|
-
def parse(source, ruby_version)
|
205
|
+
def parse(source, ruby_version, parser_engine)
|
197
206
|
buffer_name = @path || STRING_SOURCE_NAME
|
198
207
|
@buffer = Parser::Source::Buffer.new(buffer_name, 1)
|
199
208
|
|
200
209
|
begin
|
201
210
|
@buffer.source = source
|
202
|
-
rescue EncodingError => e
|
211
|
+
rescue EncodingError, Parser::UnknownEncodingInMagicComment => e
|
203
212
|
@parser_error = e
|
204
213
|
@ast = nil
|
205
214
|
@comments = []
|
@@ -207,7 +216,7 @@ module RuboCop
|
|
207
216
|
return
|
208
217
|
end
|
209
218
|
|
210
|
-
@ast, @comments, @tokens = tokenize(create_parser(ruby_version))
|
219
|
+
@ast, @comments, @tokens = tokenize(create_parser(ruby_version, parser_engine))
|
211
220
|
end
|
212
221
|
|
213
222
|
def tokenize(parser)
|
@@ -226,59 +235,81 @@ module RuboCop
|
|
226
235
|
[ast, comments, tokens]
|
227
236
|
end
|
228
237
|
|
229
|
-
# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
238
|
+
def parser_class(ruby_version, parser_engine) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
|
239
|
+
case parser_engine
|
240
|
+
when :parser_whitequark
|
241
|
+
case ruby_version
|
242
|
+
when 1.9
|
243
|
+
require 'parser/ruby19'
|
244
|
+
Parser::Ruby19
|
245
|
+
when 2.0
|
246
|
+
require 'parser/ruby20'
|
247
|
+
Parser::Ruby20
|
248
|
+
when 2.1
|
249
|
+
require 'parser/ruby21'
|
250
|
+
Parser::Ruby21
|
251
|
+
when 2.2
|
252
|
+
require 'parser/ruby22'
|
253
|
+
Parser::Ruby22
|
254
|
+
when 2.3
|
255
|
+
require 'parser/ruby23'
|
256
|
+
Parser::Ruby23
|
257
|
+
when 2.4
|
258
|
+
require 'parser/ruby24'
|
259
|
+
Parser::Ruby24
|
260
|
+
when 2.5
|
261
|
+
require 'parser/ruby25'
|
262
|
+
Parser::Ruby25
|
263
|
+
when 2.6
|
264
|
+
require 'parser/ruby26'
|
265
|
+
Parser::Ruby26
|
266
|
+
when 2.7
|
267
|
+
require 'parser/ruby27'
|
268
|
+
Parser::Ruby27
|
269
|
+
when 2.8, 3.0
|
270
|
+
require 'parser/ruby30'
|
271
|
+
Parser::Ruby30
|
272
|
+
when 3.1
|
273
|
+
require 'parser/ruby31'
|
274
|
+
Parser::Ruby31
|
275
|
+
when 3.2
|
276
|
+
require 'parser/ruby32'
|
277
|
+
Parser::Ruby32
|
278
|
+
when 3.3
|
279
|
+
require 'parser/ruby33'
|
280
|
+
Parser::Ruby33
|
281
|
+
when 3.4
|
282
|
+
require 'parser/ruby34'
|
283
|
+
Parser::Ruby34
|
284
|
+
else
|
285
|
+
raise ArgumentError, "RuboCop found unknown Ruby version: #{ruby_version.inspect}"
|
286
|
+
end
|
287
|
+
when :parser_prism
|
288
|
+
begin
|
289
|
+
require 'prism'
|
290
|
+
rescue LoadError
|
291
|
+
warn "Error: Unable to load Prism. Add `gem 'prism'` to your Gemfile."
|
292
|
+
exit!
|
293
|
+
end
|
294
|
+
|
295
|
+
case ruby_version
|
296
|
+
when 3.3
|
297
|
+
require 'prism/translation/parser33'
|
298
|
+
Prism::Translation::Parser33
|
299
|
+
when 3.4
|
300
|
+
require 'prism/translation/parser34'
|
301
|
+
Prism::Translation::Parser34
|
302
|
+
else
|
303
|
+
raise ArgumentError, 'RuboCop supports target Ruby versions 3.3 and above with Prism. ' \
|
304
|
+
"Specified target Ruby version: #{ruby_version.inspect}"
|
305
|
+
end
|
274
306
|
end
|
275
307
|
end
|
276
|
-
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
|
277
308
|
|
278
|
-
def create_parser(ruby_version)
|
309
|
+
def create_parser(ruby_version, parser_engine)
|
279
310
|
builder = RuboCop::AST::Builder.new
|
280
311
|
|
281
|
-
parser_class(ruby_version).new(builder).tap do |parser|
|
312
|
+
parser_class(ruby_version, parser_engine).new(builder).tap do |parser|
|
282
313
|
# On JRuby there's a risk that we hang in tokenize() if we
|
283
314
|
# don't set the all errors as fatal flag. The problem is caused by a bug
|
284
315
|
# in Racc that is discussed in issue #93 of the whitequark/parser
|
@@ -311,4 +342,3 @@ module RuboCop
|
|
311
342
|
end
|
312
343
|
end
|
313
344
|
end
|
314
|
-
# rubocop:enable Metrics/ClassLength
|
data/lib/rubocop/ast/token.rb
CHANGED
@@ -38,14 +38,14 @@ module RuboCop
|
|
38
38
|
body: self.body(signature, arity_check))
|
39
39
|
type, *aliases = type
|
40
40
|
lineno = caller_locations(1, 1).first.lineno
|
41
|
-
module_eval(<<~RUBY, __FILE__, lineno)
|
41
|
+
module_eval(<<~RUBY, __FILE__, lineno)
|
42
42
|
def on_#{type}(node) # def on_send(node)
|
43
43
|
#{body} # # body ...
|
44
44
|
nil # nil
|
45
45
|
end # end
|
46
46
|
RUBY
|
47
47
|
aliases.each do |m|
|
48
|
-
alias_method "on_#{m}", "on_#{type}"
|
48
|
+
alias_method :"on_#{m}", :"on_#{type}"
|
49
49
|
end
|
50
50
|
end
|
51
51
|
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
# Similar to `Forwardable#def_delegators`, but simpler & faster
|
5
|
+
module SimpleForwardable
|
6
|
+
def def_delegators(accessor, *methods)
|
7
|
+
methods.each do |method|
|
8
|
+
class_eval(<<~RUBY, __FILE__, __LINE__ + 1)
|
9
|
+
def #{method}(...) # def example(...)
|
10
|
+
#{accessor}.#{method}(...) # foo.example(...)
|
11
|
+
end # end
|
12
|
+
RUBY
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
data/lib/rubocop/ast/version.rb
CHANGED
data/lib/rubocop/ast.rb
CHANGED
@@ -5,7 +5,7 @@ require 'forwardable'
|
|
5
5
|
require 'set'
|
6
6
|
|
7
7
|
require_relative 'ast/ext/range'
|
8
|
-
require_relative 'ast/
|
8
|
+
require_relative 'ast/utilities/simple_forwardable'
|
9
9
|
require_relative 'ast/node_pattern/method_definer'
|
10
10
|
require_relative 'ast/node_pattern'
|
11
11
|
require_relative 'ast/node/mixin/descendence'
|
@@ -27,6 +27,7 @@ require_relative 'ast/node/mixin/method_identifier_predicates'
|
|
27
27
|
require_relative 'ast/node/mixin/binary_operator_node'
|
28
28
|
require_relative 'ast/node/mixin/collection_node'
|
29
29
|
require_relative 'ast/node/mixin/conditional_node'
|
30
|
+
require_relative 'ast/node/mixin/constant_node'
|
30
31
|
require_relative 'ast/node/mixin/hash_element_node'
|
31
32
|
require_relative 'ast/node/mixin/method_dispatch_node'
|
32
33
|
require_relative 'ast/node/mixin/modifier_node'
|
@@ -59,8 +60,11 @@ require_relative 'ast/node/in_pattern_node'
|
|
59
60
|
require_relative 'ast/node/index_node'
|
60
61
|
require_relative 'ast/node/indexasgn_node'
|
61
62
|
require_relative 'ast/node/int_node'
|
63
|
+
require_relative 'ast/node/keyword_begin_node'
|
62
64
|
require_relative 'ast/node/keyword_splat_node'
|
63
65
|
require_relative 'ast/node/lambda_node'
|
66
|
+
require_relative 'ast/node/masgn_node'
|
67
|
+
require_relative 'ast/node/mlhs_node'
|
64
68
|
require_relative 'ast/node/module_node'
|
65
69
|
require_relative 'ast/node/next_node'
|
66
70
|
require_relative 'ast/node/op_asgn_node'
|
@@ -70,6 +74,7 @@ require_relative 'ast/node/or_node'
|
|
70
74
|
require_relative 'ast/node/pair_node'
|
71
75
|
require_relative 'ast/node/procarg0_node'
|
72
76
|
require_relative 'ast/node/range_node'
|
77
|
+
require_relative 'ast/node/rational_node'
|
73
78
|
require_relative 'ast/node/regexp_node'
|
74
79
|
require_relative 'ast/node/rescue_node'
|
75
80
|
require_relative 'ast/node/resbody_node'
|
@@ -82,6 +87,7 @@ require_relative 'ast/node/dstr_node'
|
|
82
87
|
require_relative 'ast/node/super_node'
|
83
88
|
require_relative 'ast/node/symbol_node'
|
84
89
|
require_relative 'ast/node/until_node'
|
90
|
+
require_relative 'ast/node/var_node'
|
85
91
|
require_relative 'ast/node/when_node'
|
86
92
|
require_relative 'ast/node/while_node'
|
87
93
|
require_relative 'ast/node/yield_node'
|