rubocop-ast 1.50.0 → 1.52.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.
Files changed (59) hide show
  1. checksums.yaml +4 -4
  2. data/lib/rubocop/ast/builder.rb +2 -113
  3. data/lib/rubocop/ast/builder_extensions.rb +118 -0
  4. data/lib/rubocop/ast/ext/range.rb +31 -0
  5. data/lib/rubocop/ast/node/block_node.rb +1 -1
  6. data/lib/rubocop/ast/node/if_node.rb +6 -10
  7. data/lib/rubocop/ast/node/mixin/conditional_node.rb +4 -4
  8. data/lib/rubocop/ast/node/mixin/descendence.rb +25 -0
  9. data/lib/rubocop/ast/node/mixin/hash_element_node.rb +1 -2
  10. data/lib/rubocop/ast/node.rb +23 -1
  11. data/lib/rubocop/ast/node_pattern/compiler/debug.rb +1 -2
  12. data/lib/rubocop/ast/node_pattern/compiler/node_pattern_subcompiler.rb +1 -2
  13. data/lib/rubocop/ast/node_pattern/compiler/sequence_subcompiler.rb +25 -26
  14. data/lib/rubocop/ast/node_pattern/lexer.rex.rb +1 -1
  15. data/lib/rubocop/ast/node_pattern/method_definer.rb +1 -1
  16. data/lib/rubocop/ast/node_pattern/parser.racc.rb +1 -1
  17. data/lib/rubocop/ast/node_pattern.rb +58 -2
  18. data/lib/rubocop/ast/parser_source.rb +76 -0
  19. data/lib/rubocop/ast/prism_compiler.rb +2106 -0
  20. data/lib/rubocop/ast/prism_lexer.rb +779 -0
  21. data/lib/rubocop/ast/prism_parser.rb +211 -0
  22. data/lib/rubocop/ast/processed_source.rb +52 -40
  23. data/lib/rubocop/ast/sexp.rb +1 -1
  24. data/lib/rubocop/ast/vendor/parser/LICENSE.txt +26 -0
  25. data/lib/rubocop/ast/vendor/parser/ast/node.rb +40 -0
  26. data/lib/rubocop/ast/vendor/parser/clobbering_error.rb +13 -0
  27. data/lib/rubocop/ast/vendor/parser/deprecation.rb +13 -0
  28. data/lib/rubocop/ast/vendor/parser/diagnostic/engine.rb +104 -0
  29. data/lib/rubocop/ast/vendor/parser/diagnostic.rb +163 -0
  30. data/lib/rubocop/ast/vendor/parser/messages.rb +125 -0
  31. data/lib/rubocop/ast/vendor/parser/meta.rb +48 -0
  32. data/lib/rubocop/ast/vendor/parser/source/buffer.rb +369 -0
  33. data/lib/rubocop/ast/vendor/parser/source/comment/associator.rb +233 -0
  34. data/lib/rubocop/ast/vendor/parser/source/comment.rb +134 -0
  35. data/lib/rubocop/ast/vendor/parser/source/map/collection.rb +18 -0
  36. data/lib/rubocop/ast/vendor/parser/source/map/condition.rb +21 -0
  37. data/lib/rubocop/ast/vendor/parser/source/map/constant.rb +32 -0
  38. data/lib/rubocop/ast/vendor/parser/source/map/definition.rb +23 -0
  39. data/lib/rubocop/ast/vendor/parser/source/map/for.rb +19 -0
  40. data/lib/rubocop/ast/vendor/parser/source/map/heredoc.rb +19 -0
  41. data/lib/rubocop/ast/vendor/parser/source/map/index.rb +33 -0
  42. data/lib/rubocop/ast/vendor/parser/source/map/keyword.rb +20 -0
  43. data/lib/rubocop/ast/vendor/parser/source/map/method_definition.rb +25 -0
  44. data/lib/rubocop/ast/vendor/parser/source/map/objc_kwarg.rb +19 -0
  45. data/lib/rubocop/ast/vendor/parser/source/map/operator.rb +17 -0
  46. data/lib/rubocop/ast/vendor/parser/source/map/rescue_body.rb +21 -0
  47. data/lib/rubocop/ast/vendor/parser/source/map/send.rb +36 -0
  48. data/lib/rubocop/ast/vendor/parser/source/map/ternary.rb +18 -0
  49. data/lib/rubocop/ast/vendor/parser/source/map/variable.rb +31 -0
  50. data/lib/rubocop/ast/vendor/parser/source/map.rb +186 -0
  51. data/lib/rubocop/ast/vendor/parser/source/range.rb +326 -0
  52. data/lib/rubocop/ast/vendor/parser/source/tree_rewriter/action.rb +243 -0
  53. data/lib/rubocop/ast/vendor/parser/source/tree_rewriter.rb +431 -0
  54. data/lib/rubocop/ast/vendor/parser/syntax_error.rb +21 -0
  55. data/lib/rubocop/ast/vendor/parser/unknown_encoding_in_magic_comment_error.rb +15 -0
  56. data/lib/rubocop/ast/vendor/parser/version.rb +5 -0
  57. data/lib/rubocop/ast/version.rb +1 -1
  58. data/lib/rubocop/ast.rb +10 -3
  59. metadata +70 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 21fa08d16f9a915d62568fbf85b25efeefbf23fba1bf710b772130bcdcced47d
4
- data.tar.gz: 2789ea768313b62f16dc6a6f77e3f4b02e5d1af01a7d765483284fb3cd65c44c
3
+ metadata.gz: 10a0427201ac1703c7cd4860649d1021132e5829adb6b8bb56a42b5d618567c5
4
+ data.tar.gz: 1aaf7f176cc37142421011a587ad5030b410441758b9af4ae5fd86ca3c8fad2f
5
5
  SHA512:
6
- metadata.gz: 53cd0e762e23e4394cbd01ed3d2ae6a86983e22f0a18d7782207874d691140a6eca839e67f2ee746ffb942863c65c3278cde553b408cf8c1fdfb0da736e393e4
7
- data.tar.gz: d8657ab6d0d89f0d9d963e13379283c7aba9245fb9fa3227316f4b7955d6faab37a2da0539814461c5b39e5d8e5c7c12d715c8ebcdd2406e606e7e6f350e0160
6
+ metadata.gz: 2dc127a53b8f8f4a1d72ab4a39e6e7e4697f02f142ea94e3b5d8d5e8fb3dfc2ef375b18e8f0628da81e2a46c48813cd1f0d3e113fc920afce5be344996b99284
7
+ data.tar.gz: a429f0e43ae4c558aa166bacca175b78174cf330beee74a231f994b70ef88663194c05f77f4731fbd962a117b500ebfb70caa5d301c3eceffe44676d769cda67
@@ -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.line == loc.end.line
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 == 'unless'
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
- node_parts[1]
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
- node_parts[2]
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
- if unless?
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.line == condition.source_range.line
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. This works together with each node's
26
- # custom destructuring method to select the correct part of the node.
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
- node_parts[0]
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
 
@@ -246,7 +246,7 @@ module RuboCop
246
246
  # part of it is changed.
247
247
  def updated(type = nil, children = nil, properties = {})
248
248
  properties[:location] ||= @location
249
- klass = RuboCop::AST::Builder::NODE_MAP[type || @type] || Node
249
+ klass = BuilderExtensions::NODE_MAP[type || @type] || Node
250
250
  klass.new(type || @type, children || @children, properties)
251
251
  end
252
252
 
@@ -536,6 +536,28 @@ module RuboCop
536
536
  parent&.call_type? && equal?(parent.receiver)
537
537
  end
538
538
 
539
+ # Returns the receiver at the base of this node's call chain, e.g. the
540
+ # `if` node for `(cond ? a : b).foo.bar`. Returns `self` for non-call
541
+ # nodes and `nil` when the chain starts with a receiverless call
542
+ # (e.g. for `foo.bar.baz`).
543
+ #
544
+ # @return [Node, nil] the node at the base of this call chain
545
+ def first_part_of_call_chain
546
+ node = self
547
+
548
+ while node
549
+ if node.call_type?
550
+ node = node.receiver
551
+ elsif node.any_block_type?
552
+ node = node.send_node
553
+ else
554
+ break
555
+ end
556
+ end
557
+
558
+ node
559
+ end
560
+
539
561
  def argument?
540
562
  return false unless parent&.send_type?
541
563
 
@@ -15,12 +15,11 @@ module RuboCop
15
15
  @visit = {}
16
16
  end
17
17
 
18
- # rubocop:disable Naming/PredicateMethod
18
+ # rubocop:disable-next Naming/PredicateMethod
19
19
  def enter(node_id)
20
20
  @visit[node_id] = false
21
21
  true
22
22
  end
23
- # rubocop:enable Naming/PredicateMethod
24
23
 
25
24
  def success(node_id)
26
25
  @visit[node_id] = true
@@ -72,8 +72,7 @@ module RuboCop
72
72
 
73
73
  def visit_intersection
74
74
  multiple_access(:intersection) do
75
- node.children.map { |child| compile(child) }
76
- .join(' && ')
75
+ node.children.map { |child| compile(child) }.join(' && ')
77
76
  end
78
77
  end
79
78
 
@@ -29,7 +29,7 @@ module RuboCop
29
29
  end
30
30
 
31
31
  def compile_sequence
32
- # rubocop:disable Layout/CommentIndentation
32
+ # rubocop:disable-next Layout/CommentIndentation
33
33
  compiler.with_temp_variables do |cur_child, cur_index, previous_index|
34
34
  @cur_child_var = cur_child # To hold the current child node
35
35
  @cur_index_var = cur_index # To hold the current child index (always >= 0)
@@ -51,13 +51,36 @@ module RuboCop
51
51
  # Must be true if `@cur_index` is `:variadic_mode`
52
52
  compile_terms
53
53
  end
54
- # rubocop:enable Layout/CommentIndentation
55
54
  end
56
55
 
57
56
  private
58
57
 
59
58
  private :compile # Not meant to be called from outside
60
59
 
60
+ def compile_terms(children = @seq.children, last_arity = 0..0)
61
+ arities = remaining_arities(children, last_arity)
62
+ total_arity = arities.shift
63
+ guard = compile_child_nb_guard(total_arity)
64
+ return guard if children.empty?
65
+
66
+ @remaining_arity = total_arity
67
+ terms = children.map do |child|
68
+ use_index_from_end
69
+ @remaining_arity = arities.shift
70
+ handle_prev { compile(child) }
71
+ end
72
+ [guard, terms].join(" &&\n")
73
+ end
74
+
75
+ # yield `sync_code` iff not already in sync
76
+ def sync
77
+ return if @in_sync
78
+
79
+ code = compile_loop_advance("= #{compile_cur_index}")
80
+ @in_sync = true
81
+ yield code
82
+ end
83
+
61
84
  # Single node patterns are all handled here
62
85
  def visit_other_type
63
86
  access = case @cur_index
@@ -228,30 +251,6 @@ module RuboCop
228
251
  code
229
252
  end
230
253
 
231
- def compile_terms(children = @seq.children, last_arity = 0..0)
232
- arities = remaining_arities(children, last_arity)
233
- total_arity = arities.shift
234
- guard = compile_child_nb_guard(total_arity)
235
- return guard if children.empty?
236
-
237
- @remaining_arity = total_arity
238
- terms = children.map do |child|
239
- use_index_from_end
240
- @remaining_arity = arities.shift
241
- handle_prev { compile(child) }
242
- end
243
- [guard, terms].join(" &&\n")
244
- end
245
-
246
- # yield `sync_code` iff not already in sync
247
- def sync
248
- return if @in_sync
249
-
250
- code = compile_loop_advance("= #{compile_cur_index}")
251
- @in_sync = true
252
- yield code
253
- end
254
-
255
254
  # @api private
256
255
  attr_reader :in_sync, :cur_index
257
256
 
@@ -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.3.
5
+ # Generated by: oedipus_lex version 2.6.2.
6
6
  # Source: lib/rubocop/ast/node_pattern/lexer.rex
7
7
  #++
8
8
 
@@ -48,7 +48,7 @@ module RuboCop
48
48
  end
49
49
 
50
50
  def def_helper(base, method_name, **defaults)
51
- location = caller_locations(3, 1).first
51
+ location = definition_location || caller_locations(3, 1).first
52
52
  unless defaults.empty?
53
53
  call = :"without_defaults_#{method_name}"
54
54
  base.send :define_method, method_name, &wrapping_block(call, **defaults)
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
  #
3
3
  # DO NOT MODIFY!!!!
4
- # This file is automatically generated by Racc 1.8.1
4
+ # This file is automatically generated by Racc 1.7.3
5
5
  # from Racc grammar file "parser.y".
6
6
  #
7
7
 
@@ -26,6 +26,18 @@ module RuboCop
26
26
  class NodePattern
27
27
  class Invalid < StandardError; end
28
28
 
29
+ # When changed to `true`, methods defined with the `Macros` compile
30
+ # their pattern on first invocation instead of at definition time.
31
+ # Compiling the patterns is a significant part of loading a large body
32
+ # of cops, and most of them are never invoked in a given run.
33
+ # Note that with lazy compilation an invalid pattern raises
34
+ # `NodePattern::Invalid` when the method is first called, not when it
35
+ # is defined; set to `false` (default) to get errors at definition-time.
36
+ class << self
37
+ attr_accessor :lazy_compilation
38
+ end
39
+ self.lazy_compilation = false
40
+
29
41
  # Helpers for defining methods based on a pattern string
30
42
  module Macros
31
43
  # Define a method which applies a pattern to an AST node
@@ -36,7 +48,12 @@ module RuboCop
36
48
  # If the node matches, and no block is provided, the new method will
37
49
  # return the captures, or `true` if there were none.
38
50
  def def_node_matcher(method_name, pattern_str, **keyword_defaults)
39
- NodePattern.new(pattern_str).def_node_matcher(self, method_name, **keyword_defaults)
51
+ if NodePattern.lazy_compilation
52
+ def_node_pattern_method_lazily(:def_node_matcher, method_name, pattern_str,
53
+ keyword_defaults, caller_locations(1, 1).first)
54
+ else
55
+ NodePattern.new(pattern_str).def_node_matcher(self, method_name, **keyword_defaults)
56
+ end
40
57
  end
41
58
 
42
59
  # Define a method which recurses over the descendants of an AST node,
@@ -46,7 +63,41 @@ module RuboCop
46
63
  # as soon as it finds a descendant which matches. Otherwise, it will
47
64
  # yield all descendants which match.
48
65
  def def_node_search(method_name, pattern_str, **keyword_defaults)
49
- NodePattern.new(pattern_str).def_node_search(self, method_name, **keyword_defaults)
66
+ if NodePattern.lazy_compilation
67
+ def_node_pattern_method_lazily(:def_node_search, method_name, pattern_str,
68
+ keyword_defaults, caller_locations(1, 1).first)
69
+ else
70
+ NodePattern.new(pattern_str).def_node_search(self, method_name, **keyword_defaults)
71
+ end
72
+ end
73
+
74
+ private
75
+
76
+ # Defines a stub that compiles the pattern and replaces itself on
77
+ # first invocation. The compiled method is invoked through the owner's
78
+ # instance method rather than a regular dispatch, so that overrides
79
+ # calling `super` keep working.
80
+ def def_node_pattern_method_lazily(definer, method_name, pattern_str, keyword_defaults,
81
+ location)
82
+ base = self
83
+
84
+ define_method(method_name) do |*args, **kwargs, &block|
85
+ # Remove the stub before defining the compiled method in its
86
+ # place, so that no method redefinition warning is emitted.
87
+ begin
88
+ base.send(:remove_method, method_name)
89
+ rescue NameError
90
+ nil # the method was already compiled concurrently
91
+ end
92
+
93
+ pattern = NodePattern.new(pattern_str)
94
+ pattern.definition_location = location
95
+ pattern.public_send(definer, base, method_name, **keyword_defaults)
96
+
97
+ base.instance_method(method_name).bind_call(self, *args, **kwargs, &block)
98
+ end
99
+
100
+ method_name
50
101
  end
51
102
  end
52
103
 
@@ -73,6 +124,11 @@ module RuboCop
73
124
 
74
125
  attr_reader :pattern, :ast, :match_code
75
126
 
127
+ # The location to attribute methods defined from this pattern to, when
128
+ # it can't be determined from the call stack (e.g. for lazily compiled
129
+ # patterns). Used by `MethodDefiner`.
130
+ attr_accessor :definition_location
131
+
76
132
  def_delegators :@compiler, :captures, :named_parameters, :positional_parameters
77
133
 
78
134
  def initialize(str, compiler: Compiler.new)