Skip to content

Commit 398f6d4

Browse files
committed
rubocop: Add types for extractors
Add types to RuboCop::Runner.ruby_extractors and related modules.
1 parent ea8449c commit 398f6d4

File tree

4 files changed

+82
-3
lines changed

4 files changed

+82
-3
lines changed

gems/rubocop-ast/1.46/rubocop-ast.rbs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
module RuboCop
22
module AST
33
class ProcessedSource
4-
def initialize: (String source, Float ruby_version, ?path?) -> untyped
4+
attr_reader path: String?
5+
attr_reader ast: Node?
6+
attr_reader ruby_version: Float
7+
attr_reader parser_engine: Symbol
8+
9+
def initialize: (String source, Float ruby_version, ?path?, ?parser_engine: Symbol, ?prism_result: untyped) -> untyped
510
def raw_source: () -> String
611
def buffer: () -> Parser::Source::Buffer
712
def comments: () -> Array[Parser::Source::Comment]
8-
attr_reader ast: Node | nil
913
end
1014

1115
class NodePattern

gems/rubocop/1.57/_test/test.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,12 @@ def source_location(node)
5656
end
5757
end
5858
end
59+
60+
class MyExtractor
61+
def self.call(processed_source)
62+
[{
63+
offset: 0,
64+
processed_source: processed_source
65+
}]
66+
end
67+
end

gems/rubocop/1.57/_test/test.rbs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,7 @@ module RuboCop
1111
end
1212
end
1313
end
14+
15+
class MyExtractor
16+
def self.call: (RuboCop::ProcessedSource processed_source) -> RuboCop::Runner::extractorResult
17+
end

gems/rubocop/1.57/rubocop.rbs

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
11
module RuboCop
2+
class CommentConfig
3+
attr_reader processed_source: ProcessedSource
4+
5+
def initialize: (ProcessedSource processed_source) -> void
6+
end
7+
28
class ConfigLoader
39
def self.debug?: () -> boolish
410
def self.merge_with_default: (Config, String) -> Config
511
end
612

713
class Config
8-
def initialize: (Hash[untyped, untyped], String) -> void
14+
def initialize: (?Hash[untyped, untyped] hash, ?String loaded_path) -> void
915
def for_cop: (String cop) -> Hash[String, untyped]
1016
def for_enabled_cop: (String cop) -> Hash[String, untyped]
1117
end
1218

19+
class ConfigStore
20+
def options_config=: (String options_config) -> void
21+
end
22+
1323
module Cop
1424
class Base
1525
extend AST::NodePattern::Macros
@@ -72,6 +82,16 @@ module RuboCop
7282
def comments_contain_disables?: (RuboCop::AST::Node node, String cop_name) -> bool
7383
end
7484

85+
class Offense
86+
attr_reader severity: Severity
87+
attr_reader location: Parser::Source::Range
88+
attr_reader message: String
89+
attr_reader cop_name: String
90+
91+
def line: () -> Integer
92+
def column: () -> Integer
93+
end
94+
7595
module RangeHelp
7696
def source_range: (Parser::Source::Buffer source_buffer, Integer line_number, Integer column, ?Integer length) -> Parser::Source::Range
7797
def range_between: (Integer start_pos, Integer end_pos) -> Parser::Source::Range
@@ -84,12 +104,24 @@ module RuboCop
84104
?buffer: Parser::Source::Buffer) -> Parser::Source::Range
85105
end
86106

107+
class Registry
108+
end
109+
87110
module IgnoredNode
88111
def ignore_node: (RuboCop::AST::Node node) -> void
89112
def part_of_ignored_node?: (RuboCop::AST::Node node) -> bool
90113
def ignored_node?: (RuboCop::AST::Node node) -> bool
91114
end
92115

116+
class Severity
117+
type t = :info | :refactor | :convention | :warning | :error | :fatal
118+
119+
attr_reader name: t
120+
121+
def code: () -> String
122+
def level: () -> Integer
123+
end
124+
93125
module TargetRubyVersion
94126
def minimum_target_ruby_version: (Float) -> void
95127
def maximum_target_ruby_version: (Float) -> void
@@ -117,8 +149,28 @@ module RuboCop
117149
end
118150
end
119151

152+
module Ext
153+
module ProcessedSource
154+
attr_accessor registry: Cop::Registry
155+
attr_accessor config: Config
156+
157+
def comment_config: () -> CommentConfig
158+
def disabled_line_ranges: () -> Hash[untyped, untyped]
159+
end
160+
end
161+
120162
class ProcessedSource = AST::ProcessedSource
121163

164+
class Runner
165+
type extractorResult = Array[{ offset: Integer, processed_source: ProcessedSource}]?
166+
167+
interface _Extractor
168+
def call: (ProcessedSource) -> extractorResult
169+
end
170+
171+
def self.ruby_extractors: () -> Array[_Extractor]
172+
end
173+
122174
module AutoCorrector
123175
def support_autocorrect?: () -> true
124176
end
@@ -127,3 +179,13 @@ module RuboCop
127179
def smart_path: (String path) -> String
128180
end
129181
end
182+
183+
# Patch to RuboCop::AST::ProcessedSource
184+
185+
module RuboCop
186+
module AST
187+
class ProcessedSource
188+
include Ext::ProcessedSource
189+
end
190+
end
191+
end

0 commit comments

Comments
 (0)