Skip to content

Commit 8f328fc

Browse files
authored
Add types for parser, rubocop-ast, rubocop (#908)
1 parent 227a6f8 commit 8f328fc

File tree

5 files changed

+111
-3
lines changed

5 files changed

+111
-3
lines changed

gems/parser/3.2/_test/test.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,7 @@
7171
if node.loc.is_a? Parser::Source::Map::Send
7272
node.loc.dot
7373
node.loc.selector
74+
node.loc.operator
75+
node.loc.begin
76+
node.loc.end
7477
end

gems/parser/3.2/parser.rbs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ module Parser
121121
class TreeRewriter
122122
def replace: (Range range, String content) -> self
123123
def remove: (Range range) -> self
124+
def insert_before: (Range range, String content) -> self
124125
def insert_after: (Range range, String content) -> self
125126
end
126127

@@ -149,6 +150,9 @@ module Parser
149150
class Map::Send < Map
150151
attr_reader dot: Range
151152
attr_reader selector: Range
153+
attr_reader operator: Range?
154+
attr_reader begin: Range?
155+
attr_reader end: Range?
152156
end
153157

154158
class Comment

gems/rubocop-ast/1.30/_test/test.rb

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,11 @@ def on_hash(node)
209209
node&.each_ancestor&.each { |node| node.send_type? }&.send_type?
210210
node&.each_ancestor(:send)&.each { |node| node.send_type? }&.send_type?
211211
node&.source_range
212+
node&.sibling_index
213+
node&.right_sibling
214+
node&.left_sibling
215+
node&.left_siblings
216+
node&.right_siblings
212217

213218
node&.each_child_node { |node| node.send_type? }
214219
node&.each_child_node(:send) { |node| node.send_type? }
@@ -225,13 +230,56 @@ def on_hash(node)
225230
node&.each_node(:send)&.each { |node| node.send_type? }
226231

227232
def_node = RuboCop::AST::ProcessedSource.new('def hoge(a, b); end', RUBY_VERSION.to_f).ast
228-
def_node.first_argument if def_node.is_a?(RuboCop::AST::DefNode)
233+
if def_node.is_a?(RuboCop::AST::DefNode)
234+
def_node.first_argument
235+
def_node.parenthesized?
236+
def_node.last_argument
237+
def_node.arguments?
238+
def_node.splat_argument?
239+
def_node.rest_argument?
240+
def_node.block_argument?
241+
end
229242

230243
send_node = RuboCop::AST::ProcessedSource.new('1 + 2', RUBY_VERSION.to_f).ast
231244
if send_node.is_a?(RuboCop::AST::SendNode)
232245
send_node.first_argument
233246
send_node.arguments
247+
send_node.receiver
234248
send_node.method_name
249+
send_node.selector
250+
send_node.block_node
251+
send_node.macro?
252+
send_node.access_modifier?
253+
send_node.bare_access_modifier?
254+
send_node.non_bare_access_modifier?
255+
send_node.special_modifier?
256+
send_node.command?(:a)
257+
send_node.setter_method?
258+
send_node.assignment?
259+
send_node.dot?
260+
send_node.double_colon?
261+
send_node.safe_navigation?
262+
send_node.self_receiver?
263+
send_node.const_receiver?
264+
send_node.implicit_call?
265+
send_node.block_literal?
266+
send_node.arithmetic_operation?
267+
send_node.def_modifier?
268+
send_node.def_modifier
269+
send_node.lambda?
270+
send_node.lambda_literal?
271+
send_node.unary_operation?
272+
send_node.binary_operation?
273+
end
274+
275+
array_node = RuboCop::AST::ProcessedSource.new('[1,2,3]', RUBY_VERSION.to_f).ast
276+
if array_node.is_a?(RuboCop::AST::ArrayNode)
277+
array_node.values
278+
array_node.each_value { |node| node }
279+
array_node.each_value.each {}
280+
array_node.square_brackets?
281+
array_node.percent_literal?
282+
array_node.bracketed?
235283
end
236284

237285
block_node = RuboCop::AST::ProcessedSource.new('1.tap { |n| n }', RUBY_VERSION.to_f).ast

gems/rubocop-ast/1.30/rubocop-ast.rbs

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ module RuboCop
1111
class NodePattern
1212
module Macros
1313
def def_node_matcher: (Symbol, String) -> void
14+
def def_node_search: (Symbol, String) -> void
1415
end
1516

1617
class Compiler
@@ -39,18 +40,55 @@ module RuboCop
3940

4041
module MethodDispatchNode
4142
include MethodIdentifierPredicates
43+
def receiver: () -> Node?
4244
def method_name: () -> Symbol
45+
def selector: () -> Parser::Source::Range
46+
def block_node: () -> BlockNode?
47+
def macro?: () -> bool
48+
def access_modifier?: () -> bool
49+
def bare_access_modifier?: () -> bool
50+
def non_bare_access_modifier?: () -> bool
51+
def special_modifier?: () -> bool
52+
def command?: (Symbol name) -> bool
53+
def setter_method?: () -> bool
54+
alias assignment? setter_method?
55+
def dot?: () -> bool
56+
def double_colon?: () -> bool
57+
def safe_navigation?: () -> bool
58+
def self_receiver?: () -> bool
59+
def const_receiver?: () -> bool
60+
def implicit_call?: () -> bool
61+
def block_literal?: () -> bool
62+
def arithmetic_operation?: () -> bool
63+
def def_modifier?: (?Node node) -> bool
64+
def def_modifier: (?Node node) -> Node?
65+
def lambda?: () -> bool
66+
def lambda_literal?: () -> bool
67+
def unary_operation?: () -> bool
68+
def binary_operation?: () -> bool
4369
end
4470

4571
module MethodIdentifierPredicates
4672
def method?: ((Symbol | String) name) -> bool
4773
end
4874

4975
module ParameterizedNode
76+
def parenthesized?: () -> bool
5077
def first_argument: () -> Node?
51-
def arguments: () -> Array[Node]
78+
def last_argument: () -> Node?
79+
def arguments?: () -> bool
80+
def splat_argument?: () -> bool
81+
alias rest_argument? splat_argument?
82+
def block_argument?: () -> bool
83+
84+
module WrappedArguments
85+
include ParameterizedNode
86+
def arguments: () -> Array[Node]
87+
end
88+
5289
module RestArguments
5390
include ParameterizedNode
91+
def arguments: () -> Array[Node]
5492
end
5593
end
5694

@@ -205,10 +243,20 @@ module RuboCop
205243
def each_ancestor: (*Symbol types) { (Node) -> void } -> self
206244
| (*Symbol types) -> ::Enumerator[Node, self]
207245
def source_range: () -> Parser::Source::Range
246+
def sibling_index: () -> (Integer | nil)
247+
def right_sibling: () -> (Node | nil)
248+
def left_sibling: () -> (Node | nil)
249+
def left_siblings: () -> Array[Node]
250+
def right_siblings: () -> Array[Node]
208251
end
209252

210253
class ArrayNode < Node
211254
alias values children
255+
def each_value: () { (Node) -> void } -> self
256+
| () -> ::Enumerator[Node, self]
257+
def square_brackets?: () -> bool
258+
def percent_literal?: (?Symbol type) -> bool
259+
def bracketed?: () -> bool
212260
end
213261

214262
class BlockNode < Node
@@ -278,7 +326,7 @@ module RuboCop
278326

279327
class StrNode < Node
280328
include BasicLiteralNode
281-
def value: () -> String
329+
def value: () -> (String | StrNode)
282330
end
283331

284332
class SymbolNode < Node

gems/rubocop/1.57/rubocop.rbs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ module RuboCop
2929
class Corrector < Parser::Source::TreeRewriter
3030
def replace: ((Parser::Source::Range | RuboCop::AST::Node) range, String content) -> self
3131
def remove: ((Parser::Source::Range | RuboCop::AST::Node) range) -> self
32+
def insert_before: ((Parser::Source::Range | RuboCop::AST::Node) range, String content) -> self
3233
def insert_after: ((Parser::Source::Range | RuboCop::AST::Node) range, String content) -> self
3334
end
3435

@@ -39,6 +40,10 @@ module RuboCop
3940
module RangeHelp
4041
def source_range: (Parser::Source::Buffer source_buffer, Integer line_number, Integer column, ?Integer length) -> Parser::Source::Range
4142
def range_between: (Integer start_pos, Integer end_pos) -> Parser::Source::Range
43+
def range_with_surrounding_space: (?Parser::Source::Range range_positional,
44+
?range: Parser::Source::Range, ?side: Symbol, ?newlines: bool,
45+
?whitespace: bool, ?continuations: bool,
46+
?buffer: Parser::Source::Buffer) -> Parser::Source::Range
4247
end
4348

4449
module IgnoredNode

0 commit comments

Comments
 (0)