rubocop-on-rbs 2.0.0.dev → 2.0.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/CHANGELOG.md +48 -0
- data/README.md +1 -4
- data/config/default.yml +4 -4
- data/lib/rubocop/cop/rbs/layout/annotation_indentation.rb +89 -0
- data/lib/rubocop/cop/rbs/layout/comment_indentation.rb +34 -32
- data/lib/rubocop/cop/rbs/layout/empty_lines_around_access_modifier.rb +1 -1
- data/lib/rubocop/cop/rbs/layout/empty_lines_around_overloads.rb +22 -6
- data/lib/rubocop/cop/rbs/layout/overload_indentation.rb +9 -1
- data/lib/rubocop/cop/rbs/layout/space_around_operators.rb +19 -1
- data/lib/rubocop/cop/rbs/layout/space_before_overload.rb +27 -13
- data/lib/rubocop/cop/rbs/style/block_return_boolish.rb +1 -0
- data/lib/rubocop/cop/rbs/style/class_with_singleton.rb +1 -0
- data/lib/rubocop/cop/rbs/style/initialize_return_type.rb +1 -0
- data/lib/rubocop/cop/rbs/style/instance_with_instance.rb +1 -0
- data/lib/rubocop/cop/rbs/style/redundant_parentheses.rb +1 -0
- data/lib/rubocop/cop/rbs_cops.rb +1 -1
- data/lib/rubocop/rbs/version.rb +1 -1
- metadata +7 -7
- data/lib/rubocop/cop/rbs/lint/will_syntax_error.rb +0 -228
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 52b6d2d8d5f69d9af653de61ac795ed71d7ce63663c65450dd842e4c1816c69f
|
|
4
|
+
data.tar.gz: e81e32efe74897317c3eb4183cfb11e6e65b0bcaa04345261787dfc29eb92741
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 6f7a1fdceb0841f976a3cfe4147e9f17e43e13447702314e03747bbd0565f945615f23e952d7ffd08ba7dd00c46f34b2d5bfcd7d8692d94eaf901d528c776cd1
|
|
7
|
+
data.tar.gz: f0949c448ec168f168b4d4c2b723670bac8e2dade272e8b569533314c366e26b89b805860f455f49f5a3d1f06294a7ad0334baa8c878ab043459b2cbf22c115f
|
data/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,54 @@
|
|
|
1
1
|
## [Unreleased]
|
|
2
2
|
|
|
3
|
+
## [2.0.0] - 2026-03-27
|
|
4
|
+
|
|
5
|
+
* Support rbs v4 by @ksss in https://github.com/ksss/rubocop-on-rbs/pull/145
|
|
6
|
+
* Drop `RBS/Lint/WillSyntaxError` by @ksss in https://github.com/ksss/rubocop-on-rbs/pull/145
|
|
7
|
+
|
|
8
|
+
## [1.9.1] - 2026-02-10
|
|
9
|
+
|
|
10
|
+
* [RBS/Layout/OverloadIndentation] Fix false positive by @ksss in https://github.com/ksss/rubocop-on-rbs/pull/141
|
|
11
|
+
* [RBS/Layout/EmptyLinesAroundOverloads] Fix false positive by @ksss in https://github.com/ksss/rubocop-on-rbs/pull/142
|
|
12
|
+
* [RBS/Layout/SpaceBeforeOverload] Support `...` by @ksss in https://github.com/ksss/rubocop-on-rbs/pull/143
|
|
13
|
+
|
|
14
|
+
## [1.9.0] - 2026-01-14
|
|
15
|
+
|
|
16
|
+
* Add cop `RBS/Layout/AnnotationIndentation` by @ksss in https://github.com/ksss/rubocop-on-rbs/pull/124
|
|
17
|
+
* Fix bug when multi annotation by @ksss in https://github.com/ksss/rubocop-on-rbs/pull/125
|
|
18
|
+
* [RBS/Layout/SpaceAroundOperators] Support type params by @ksss in https://github.com/ksss/rubocop-on-rbs/pull/126
|
|
19
|
+
* Support new Cops by @ksss in https://github.com/ksss/rubocop-on-rbs/pull/129
|
|
20
|
+
* [RBS/Layout/CommentIndentation] Fix false positive by @ksss in https://github.com/ksss/rubocop-on-rbs/pull/136
|
|
21
|
+
* Use ruby v4 by @ksss in https://github.com/ksss/rubocop-on-rbs/pull/137
|
|
22
|
+
|
|
23
|
+
## [1.8.0] - 2025-06-26
|
|
24
|
+
|
|
25
|
+
* Add task for update dependencies by @ksss in https://github.com/ksss/rubocop-on-rbs/pull/110
|
|
26
|
+
* Should melt down Gemfile.lock by @ksss in https://github.com/ksss/rubocop-on-rbs/pull/111
|
|
27
|
+
* Dynamically generate document URLs by version by @ksss in https://github.com/ksss/rubocop-on-rbs/pull/114
|
|
28
|
+
* Add new cop `RBS/Lint/ImplicitlyReturnsNil` by @ksss in https://github.com/ksss/rubocop-on-rbs/pull/115
|
|
29
|
+
* [RBS/Lint/DuplicateOverload] Ignore the difference of return type. by @ksss in https://github.com/ksss/rubocop-on-rbs/pull/116
|
|
30
|
+
* Add `RBS/Lint/DuplicateAnnotation` by @ksss in https://github.com/ksss/rubocop-on-rbs/pull/117
|
|
31
|
+
* Fix message by @ksss in https://github.com/ksss/rubocop-on-rbs/pull/118
|
|
32
|
+
|
|
33
|
+
## [1.7.0] - 2025-05-18
|
|
34
|
+
|
|
35
|
+
* Add `RBS/Lint/RestKeywordHash` by @ksss in https://github.com/ksss/rubocop-on-rbs/pull/102
|
|
36
|
+
* Support documentation URL for department by @ksss in https://github.com/ksss/rubocop-on-rbs/pull/103
|
|
37
|
+
* Refine documentation by @ksss in https://github.com/ksss/rubocop-on-rbs/pull/104
|
|
38
|
+
* Add RBS/Layout/EmptyLineBetweenDeclarations by @ksss in https://github.com/ksss/rubocop-on-rbs/pull/105
|
|
39
|
+
* [RBS/Layout/EmptyLineBetweenDeclarations] Short fix by @ksss in https://github.com/ksss/rubocop-on-rbs/pull/106
|
|
40
|
+
|
|
41
|
+
## [1.6.0] - 2025-04-25
|
|
42
|
+
|
|
43
|
+
* [RBS::Style::TrueFalse] More support decls by @ksss in https://github.com/ksss/rubocop-on-rbs/pull/97
|
|
44
|
+
* [RBS/Style/OptionalNil] More support decls by @ksss in https://github.com/ksss/rubocop-on-rbs/pull/98
|
|
45
|
+
* [RBS/Style/DuplicatedType] More support decls by @ksss in https://github.com/ksss/rubocop-on-rbs/pull/100
|
|
46
|
+
|
|
47
|
+
## [1.5.0] - 2025-03-23
|
|
48
|
+
|
|
3
49
|
* Pluginfy RuboCop on RBS by @ydah in https://github.com/ksss/rubocop-on-rbs/pull/88
|
|
50
|
+
* Drop support for ruby v3.1 by @ksss in https://github.com/ksss/rubocop-on-rbs/pull/90
|
|
51
|
+
* Fix RuboCop RakeTask plugin assignment in README by @ydah in https://github.com/ksss/rubocop-on-rbs/pull/92
|
|
4
52
|
|
|
5
53
|
## [1.4.2] - 2025-02-23
|
|
6
54
|
|
data/README.md
CHANGED
|
@@ -24,11 +24,8 @@ def foo: ( void) -> untyped
|
|
|
24
24
|
sig/foo.rbs:2:11: C: [Correctable] RBS/Layout/ExtraSpacing: Unnecessary spacing detected.
|
|
25
25
|
def foo: ( void) -> untyped
|
|
26
26
|
^^
|
|
27
|
-
sig/foo.rbs:2:14: W: RBS/Lint/WillSyntaxError: void type is only allowed in return type or generics parameter
|
|
28
|
-
def foo: ( void) -> untyped
|
|
29
|
-
^^^^
|
|
30
27
|
|
|
31
|
-
1 file inspected,
|
|
28
|
+
1 file inspected, 2 offenses detected, 2 offenses autocorrectable
|
|
32
29
|
```
|
|
33
30
|
|
|
34
31
|
## Support VSCode
|
data/config/default.yml
CHANGED
|
@@ -27,6 +27,10 @@ RBS:
|
|
|
27
27
|
RBS/Layout:
|
|
28
28
|
Enabled: true
|
|
29
29
|
|
|
30
|
+
RBS/Layout/AnnotationIndentation:
|
|
31
|
+
Description: 'Use 2 spaces for annotation indentation'
|
|
32
|
+
Enabled: true
|
|
33
|
+
|
|
30
34
|
RBS/Layout/CommentIndentation:
|
|
31
35
|
Description: 'Use 2 spaces for comment indentation'
|
|
32
36
|
Enabled: true
|
|
@@ -172,10 +176,6 @@ RBS/Lint/UnusedTypeAliasTypeParams:
|
|
|
172
176
|
Description: 'Check redundant type alias type params'
|
|
173
177
|
Enabled: true
|
|
174
178
|
|
|
175
|
-
RBS/Lint/WillSyntaxError:
|
|
176
|
-
Description: 'Check RBS will syntax error'
|
|
177
|
-
Enabled: true
|
|
178
|
-
|
|
179
179
|
## RBS/Style
|
|
180
180
|
|
|
181
181
|
RBS/Style:
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RuboCop
|
|
4
|
+
module Cop
|
|
5
|
+
module RBS
|
|
6
|
+
module Layout
|
|
7
|
+
# Checks the indentation of annotations in RBS.
|
|
8
|
+
#
|
|
9
|
+
# @example
|
|
10
|
+
# # bad
|
|
11
|
+
# # comment
|
|
12
|
+
# %a{pure}
|
|
13
|
+
# def foo: () -> void
|
|
14
|
+
#
|
|
15
|
+
# # good
|
|
16
|
+
# # comment
|
|
17
|
+
# %a{pure}
|
|
18
|
+
# def foo: () -> void
|
|
19
|
+
#
|
|
20
|
+
class AnnotationIndentation < RuboCop::RBS::CopBase
|
|
21
|
+
extend AutoCorrector
|
|
22
|
+
|
|
23
|
+
MSG = "Incorrect indentation detected (column %<expect>s instead of %<actual>s)."
|
|
24
|
+
|
|
25
|
+
def on_rbs_new_investigation
|
|
26
|
+
indent_start_lines = Set.new
|
|
27
|
+
indent_end_lines = Set.new
|
|
28
|
+
ignore_poses = Set.new
|
|
29
|
+
processed_rbs_source.decls.each do |decl|
|
|
30
|
+
walk_decl(decl) do |d|
|
|
31
|
+
indent_start_lines << d.location.start_line
|
|
32
|
+
indent_end_lines << d.location.end_line
|
|
33
|
+
d.members.each do |member|
|
|
34
|
+
case member
|
|
35
|
+
when ::RBS::AST::Members::MethodDefinition
|
|
36
|
+
member.overloads.each do |overload|
|
|
37
|
+
overload.annotations.each do |annotation|
|
|
38
|
+
ignore_poses << annotation.location.start_pos
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
expected_width = 0
|
|
47
|
+
last_annotation_line = -1
|
|
48
|
+
processed_rbs_source.tokens.each do |token|
|
|
49
|
+
case token.type
|
|
50
|
+
when :kMODULE, :kCLASS, :kINTERFACE
|
|
51
|
+
next unless indent_start_lines.include?(token.location.start_line)
|
|
52
|
+
|
|
53
|
+
expected_width += 2
|
|
54
|
+
when :kEND
|
|
55
|
+
next unless indent_end_lines.include?(token.location.start_line)
|
|
56
|
+
|
|
57
|
+
expected_width -= 2
|
|
58
|
+
when :tANNOTATION
|
|
59
|
+
next if ignore_poses.include?(token.location.start_pos)
|
|
60
|
+
|
|
61
|
+
if token.location.start_column == expected_width
|
|
62
|
+
last_annotation_line = token.location.start_line
|
|
63
|
+
next
|
|
64
|
+
end
|
|
65
|
+
next if last_annotation_line == token.location.start_line
|
|
66
|
+
|
|
67
|
+
token_range = location_to_range(token.location)
|
|
68
|
+
message = format(MSG, expect: expected_width, actual: token.location.start_column)
|
|
69
|
+
last_annotation_line = token.location.start_line
|
|
70
|
+
add_offense(token_range, message: message) do |corrector|
|
|
71
|
+
line_start_pos = processed_source.buffer.line_range(token.location.start_line).begin_pos
|
|
72
|
+
indent = range_between(line_start_pos, token.location.start_pos)
|
|
73
|
+
corrector.replace(indent, ' ' * expected_width)
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def walk_decl(decl, &block)
|
|
80
|
+
if decl.respond_to?(:members)
|
|
81
|
+
yield decl
|
|
82
|
+
decl.members.each { |member| walk_decl(member, &block) }
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
end
|
|
@@ -21,45 +21,47 @@ module RuboCop
|
|
|
21
21
|
MSG = "Incorrect indentation detected (column %<expect>s instead of %<actual>s)."
|
|
22
22
|
|
|
23
23
|
def on_rbs_new_investigation
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
walk_decl(decl) do |d|
|
|
28
|
-
indent_start_lines << d.location.start_line
|
|
29
|
-
indent_end_lines << d.location.end_line
|
|
30
|
-
end
|
|
24
|
+
comments = processed_rbs_source.tokens.select { |token| token.type == :tLINECOMMENT }
|
|
25
|
+
comments.each_with_index do |token, comment_index|
|
|
26
|
+
check(token, comment_index)
|
|
31
27
|
end
|
|
28
|
+
end
|
|
32
29
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
next unless indent_start_lines.include?(token.location.start_line)
|
|
30
|
+
def check(comment_token, _comment_index)
|
|
31
|
+
next_line = line_after_comment(comment_token)
|
|
32
|
+
correct_comment_indentation = correct_indentation(next_line)
|
|
33
|
+
column = comment_token.location.start_column
|
|
38
34
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
next unless indent_end_lines.include?(token.location.start_line)
|
|
35
|
+
column_delta = correct_comment_indentation - column
|
|
36
|
+
return if column_delta.zero?
|
|
42
37
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
line_start_pos = processed_source.buffer.line_range(token.location.start_line).begin_pos
|
|
50
|
-
indent = range_between(line_start_pos, token.location.start_pos)
|
|
51
|
-
corrector.replace(indent, ' ' * expected_width)
|
|
52
|
-
end
|
|
53
|
-
end
|
|
54
|
-
end
|
|
38
|
+
token_range = location_to_range(comment_token.location)
|
|
39
|
+
message = format(MSG, expect: correct_comment_indentation, actual: column)
|
|
40
|
+
add_offense(token_range, message: message) do |corrector|
|
|
41
|
+
line_start_pos = processed_source.buffer.line_range(comment_token.location.start_line).begin_pos
|
|
42
|
+
indent = range_between(line_start_pos, comment_token.location.start_pos)
|
|
43
|
+
corrector.replace(indent, ' ' * correct_comment_indentation)
|
|
55
44
|
end
|
|
56
45
|
end
|
|
57
46
|
|
|
58
|
-
def
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
47
|
+
def line_after_comment(comment)
|
|
48
|
+
lines = processed_source.lines
|
|
49
|
+
lines[comment.location.start_line..].find { |line| !line.blank? }
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def correct_indentation(next_line)
|
|
53
|
+
return 0 unless next_line
|
|
54
|
+
|
|
55
|
+
indentation_of_next_line = next_line =~ /\S/
|
|
56
|
+
indentation_of_next_line + if less_indented?(next_line)
|
|
57
|
+
2
|
|
58
|
+
else
|
|
59
|
+
0
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def less_indented?(line)
|
|
64
|
+
/\A\s*end\b/.match?(line)
|
|
63
65
|
end
|
|
64
66
|
end
|
|
65
67
|
end
|
|
@@ -68,7 +68,7 @@ module RuboCop
|
|
|
68
68
|
end
|
|
69
69
|
|
|
70
70
|
def previous_line_ignoring_comments(processed_source, send_line)
|
|
71
|
-
processed_source[0..send_line - 2].reverse.find { |line| !comment_line?(line) }
|
|
71
|
+
processed_source[0..(send_line - 2)].reverse.find { |line| !comment_line?(line) }
|
|
72
72
|
end
|
|
73
73
|
|
|
74
74
|
def previous_line_empty?(send_line)
|
|
@@ -21,19 +21,35 @@ module RuboCop
|
|
|
21
21
|
|
|
22
22
|
MSG = 'Empty line detected around overloads.'
|
|
23
23
|
|
|
24
|
+
# @rbs decl: ::RBS::AST::Members::MethodDefinition
|
|
25
|
+
# @rbs return: void
|
|
24
26
|
def on_rbs_def(decl)
|
|
25
|
-
return unless 1 < decl.overloads.length
|
|
26
|
-
|
|
27
27
|
decl.overloads.each_cons(2) do |overload, next_overload|
|
|
28
|
-
|
|
28
|
+
end_line = overload&.method_type&.location&.end_line || 0
|
|
29
|
+
next_start_line = next_overload&.method_type&.location&.start_line || 0
|
|
30
|
+
check_empty_lines(end_line, next_start_line)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
if decl.overloading? && decl.overloads.length.positive?
|
|
34
|
+
# : () -> void
|
|
35
|
+
#
|
|
36
|
+
# | ...
|
|
37
|
+
end_location = decl.overloads.last.method_type.location or return
|
|
38
|
+
slice = processed_source.raw_source[end_location.end_pos..] or return
|
|
39
|
+
bar_index = slice.index('|') or return
|
|
40
|
+
bar_line = slice[0..bar_index]&.count("\n") or return
|
|
41
|
+
check_empty_lines(end_location.end_line, end_location.end_line + bar_line)
|
|
29
42
|
end
|
|
30
43
|
end
|
|
31
44
|
|
|
32
|
-
|
|
33
|
-
|
|
45
|
+
# @rbs end_line: Integer
|
|
46
|
+
# @rbs next_start_line: Integer
|
|
47
|
+
# @rbs return: void
|
|
48
|
+
def check_empty_lines(end_line, next_start_line)
|
|
49
|
+
return if end_line + 1 == next_start_line
|
|
34
50
|
|
|
35
51
|
total = 0
|
|
36
|
-
range =
|
|
52
|
+
range = end_line...(next_start_line - 1)
|
|
37
53
|
processed_source.raw_source.each_line.each_with_index do |line, lineno|
|
|
38
54
|
if range.cover?(lineno) && line == "\n"
|
|
39
55
|
empty_line = range_between(total, total + 1)
|
|
@@ -39,8 +39,11 @@ module RuboCop
|
|
|
39
39
|
|
|
40
40
|
loc = bar&.location
|
|
41
41
|
next unless loc
|
|
42
|
-
next unless overload_starts.include?(base_pos + after.location.start_pos)
|
|
43
42
|
|
|
43
|
+
# pDOT3 is not included in decl.overloads
|
|
44
|
+
next unless overload_starts.include?(base_pos + after.location.start_pos) || after.type == :pDOT3
|
|
45
|
+
|
|
46
|
+
# () -> void | (Integer) -> void
|
|
44
47
|
if before.location.end_line == bar.location.start_line
|
|
45
48
|
range = range_between(base_pos + bar.location.start_pos, base_pos + bar.location.end_pos)
|
|
46
49
|
add_offense(range, message: "Insert newline before `|`") do |corrector|
|
|
@@ -49,12 +52,17 @@ module RuboCop
|
|
|
49
52
|
end
|
|
50
53
|
end
|
|
51
54
|
|
|
55
|
+
# () -> void
|
|
56
|
+
# |
|
|
57
|
+
# (Integer) -> void
|
|
52
58
|
if bar.location.end_line != after.location.start_line
|
|
53
59
|
range = range_between(base_pos + bar.location.start_pos, base_pos + bar.location.end_pos)
|
|
54
60
|
add_offense(range, message: "Remove newline after `|`") do |corrector|
|
|
55
61
|
space = range_between(base_pos + bar.location.end_pos, base_pos + after.location.start_pos)
|
|
56
62
|
corrector.replace(space, ' ')
|
|
57
63
|
end
|
|
64
|
+
# : () -> void
|
|
65
|
+
# | (Integer) -> void
|
|
58
66
|
elsif bar.location.start_column != first_colon_column
|
|
59
67
|
range = range_between(base_pos + bar.location.start_pos, base_pos + bar.location.end_pos)
|
|
60
68
|
add_offense(range, message: 'Indent the `|` to the first `:`') do |corrector|
|
|
@@ -14,8 +14,15 @@ module RuboCop
|
|
|
14
14
|
class SpaceAroundOperators < RuboCop::RBS::CopBase
|
|
15
15
|
extend AutoCorrector
|
|
16
16
|
|
|
17
|
+
def on_rbs_class(decl)
|
|
18
|
+
check_type_params(decl)
|
|
19
|
+
end
|
|
20
|
+
alias on_rbs_module on_rbs_class
|
|
21
|
+
alias on_rbs_interface on_rbs_class
|
|
22
|
+
|
|
17
23
|
def on_rbs_def(decl)
|
|
18
24
|
decl.overloads.each do |overload|
|
|
25
|
+
check_type_params(overload.method_type)
|
|
19
26
|
overload.method_type.each_type do |type|
|
|
20
27
|
check_type(type)
|
|
21
28
|
end
|
|
@@ -26,10 +33,14 @@ module RuboCop
|
|
|
26
33
|
check_type(decl.type)
|
|
27
34
|
end
|
|
28
35
|
alias on_rbs_global on_rbs_constant
|
|
29
|
-
alias on_rbs_type_alias on_rbs_constant
|
|
30
36
|
alias on_rbs_attribute on_rbs_constant
|
|
31
37
|
alias on_rbs_var on_rbs_constant
|
|
32
38
|
|
|
39
|
+
def on_rbs_type_alias(decl)
|
|
40
|
+
check_type_params(decl)
|
|
41
|
+
check_type(decl.type)
|
|
42
|
+
end
|
|
43
|
+
|
|
33
44
|
def check_type(type)
|
|
34
45
|
case type
|
|
35
46
|
when ::RBS::Types::Union
|
|
@@ -42,6 +53,13 @@ module RuboCop
|
|
|
42
53
|
end
|
|
43
54
|
end
|
|
44
55
|
|
|
56
|
+
def check_type_params(decl)
|
|
57
|
+
decl.type_params.each do |type_param|
|
|
58
|
+
check_type(type_param.default_type) if type_param.default_type
|
|
59
|
+
check_type(type_param.upper_bound_type) if type_param.upper_bound_type
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
45
63
|
def check_operator(type, operator)
|
|
46
64
|
type.types.each_cons(2) do |before, after|
|
|
47
65
|
next unless before.location.end_line == after.location.start_line
|
|
@@ -17,25 +17,39 @@ module RuboCop
|
|
|
17
17
|
|
|
18
18
|
MSG = 'Use one space before overload.'
|
|
19
19
|
|
|
20
|
-
# @
|
|
20
|
+
# @rbs decl: ::RBS::AST::Members::MethodDefinition
|
|
21
21
|
def on_rbs_def(decl)
|
|
22
|
-
source = processed_source.raw_source
|
|
23
22
|
decl.overloads.each_with_index do |overload, i|
|
|
24
|
-
loc = overload.method_type.location
|
|
23
|
+
loc = overload.method_type.location or next
|
|
25
24
|
overload_char = i == 0 ? ':' : '|'
|
|
25
|
+
check(loc, overload_char)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
if decl.overloading?
|
|
29
|
+
loc = decl.location or return
|
|
30
|
+
overloading_loc = loc[:overloading] or return
|
|
31
|
+
overload_char = decl.overloads.length == 0 ? ':' : '|'
|
|
32
|
+
check(overloading_loc, overload_char)
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
private
|
|
37
|
+
|
|
38
|
+
# @rbs loc: ::RBS::Location[bot, bot]
|
|
39
|
+
def check(loc, overload_char)
|
|
40
|
+
source = processed_source.raw_source
|
|
26
41
|
|
|
27
|
-
|
|
28
|
-
|
|
42
|
+
char_start_pos = source.rindex(overload_char, loc.start_pos)
|
|
43
|
+
return unless char_start_pos
|
|
29
44
|
|
|
30
|
-
|
|
31
|
-
|
|
45
|
+
word_after_char_pos = source.index(/[^\s]/, char_start_pos + 1)
|
|
46
|
+
return unless word_after_char_pos
|
|
32
47
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
end
|
|
48
|
+
if char_start_pos + 2 != word_after_char_pos
|
|
49
|
+
char = range_between(char_start_pos, char_start_pos + 1)
|
|
50
|
+
add_offense(char) do |corrector|
|
|
51
|
+
range = range_between(char_start_pos + 1, word_after_char_pos)
|
|
52
|
+
corrector.replace(range, ' ')
|
|
39
53
|
end
|
|
40
54
|
end
|
|
41
55
|
end
|
data/lib/rubocop/cop/rbs_cops.rb
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
require 'rbs'
|
|
4
4
|
|
|
5
|
+
require_relative 'rbs/layout/annotation_indentation'
|
|
5
6
|
require_relative 'rbs/layout/comment_indentation'
|
|
6
7
|
require_relative 'rbs/layout/empty_line_between_declarations'
|
|
7
8
|
require_relative 'rbs/layout/empty_lines_around_access_modifier'
|
|
@@ -37,7 +38,6 @@ require_relative 'rbs/lint/top_level_type_alias'
|
|
|
37
38
|
require_relative 'rbs/lint/unused_overload_type_params'
|
|
38
39
|
require_relative 'rbs/lint/unused_type_alias_type_params'
|
|
39
40
|
require_relative 'rbs/lint/useless_access_modifier'
|
|
40
|
-
require_relative 'rbs/lint/will_syntax_error'
|
|
41
41
|
|
|
42
42
|
require_relative 'rbs/style/block_return_boolish'
|
|
43
43
|
require_relative 'rbs/style/class_with_singleton'
|
data/lib/rubocop/rbs/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: rubocop-on-rbs
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 2.0.0
|
|
4
|
+
version: 2.0.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- ksss
|
|
@@ -27,16 +27,16 @@ dependencies:
|
|
|
27
27
|
name: rbs
|
|
28
28
|
requirement: !ruby/object:Gem::Requirement
|
|
29
29
|
requirements:
|
|
30
|
-
- -
|
|
30
|
+
- - "~>"
|
|
31
31
|
- !ruby/object:Gem::Version
|
|
32
|
-
version: 4.0
|
|
32
|
+
version: '4.0'
|
|
33
33
|
type: :runtime
|
|
34
34
|
prerelease: false
|
|
35
35
|
version_requirements: !ruby/object:Gem::Requirement
|
|
36
36
|
requirements:
|
|
37
|
-
- -
|
|
37
|
+
- - "~>"
|
|
38
38
|
- !ruby/object:Gem::Version
|
|
39
|
-
version: 4.0
|
|
39
|
+
version: '4.0'
|
|
40
40
|
- !ruby/object:Gem::Dependency
|
|
41
41
|
name: rubocop
|
|
42
42
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -84,6 +84,7 @@ files:
|
|
|
84
84
|
- README.md
|
|
85
85
|
- config/default.yml
|
|
86
86
|
- lib/rubocop-on-rbs.rb
|
|
87
|
+
- lib/rubocop/cop/rbs/layout/annotation_indentation.rb
|
|
87
88
|
- lib/rubocop/cop/rbs/layout/comment_indentation.rb
|
|
88
89
|
- lib/rubocop/cop/rbs/layout/empty_line_between_declarations.rb
|
|
89
90
|
- lib/rubocop/cop/rbs/layout/empty_lines.rb
|
|
@@ -118,7 +119,6 @@ files:
|
|
|
118
119
|
- lib/rubocop/cop/rbs/lint/unused_overload_type_params.rb
|
|
119
120
|
- lib/rubocop/cop/rbs/lint/unused_type_alias_type_params.rb
|
|
120
121
|
- lib/rubocop/cop/rbs/lint/useless_access_modifier.rb
|
|
121
|
-
- lib/rubocop/cop/rbs/lint/will_syntax_error.rb
|
|
122
122
|
- lib/rubocop/cop/rbs/style/block_return_boolish.rb
|
|
123
123
|
- lib/rubocop/cop/rbs/style/class_with_singleton.rb
|
|
124
124
|
- lib/rubocop/cop/rbs/style/classic_type.rb
|
|
@@ -159,7 +159,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
159
159
|
- !ruby/object:Gem::Version
|
|
160
160
|
version: '0'
|
|
161
161
|
requirements: []
|
|
162
|
-
rubygems_version:
|
|
162
|
+
rubygems_version: 4.0.3
|
|
163
163
|
specification_version: 4
|
|
164
164
|
summary: RuboCop extension for RBS file.
|
|
165
165
|
test_files: []
|
|
@@ -1,228 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module RuboCop
|
|
4
|
-
module Cop
|
|
5
|
-
module RBS
|
|
6
|
-
module Lint
|
|
7
|
-
# This cop checks the WillSyntaxError in RBS.
|
|
8
|
-
# RBS with this diagnostics will fail in `rbs validate` command.
|
|
9
|
-
#
|
|
10
|
-
# @example
|
|
11
|
-
# # bad
|
|
12
|
-
# def foo: (void) -> void
|
|
13
|
-
#
|
|
14
|
-
# # bad
|
|
15
|
-
# CONST: self
|
|
16
|
-
#
|
|
17
|
-
class WillSyntaxError < RuboCop::RBS::CopBase
|
|
18
|
-
# @rbs!
|
|
19
|
-
# module Types = ::RBS::Types
|
|
20
|
-
# module AST = ::RBS::AST
|
|
21
|
-
|
|
22
|
-
# @rbs skip
|
|
23
|
-
AST = ::RBS::AST
|
|
24
|
-
# @rbs skip
|
|
25
|
-
Types = ::RBS::Types
|
|
26
|
-
|
|
27
|
-
def on_rbs_class(decl)
|
|
28
|
-
if super_class = decl.super_class
|
|
29
|
-
super_class.args.each do |arg|
|
|
30
|
-
void_type_context_validator(arg, true)
|
|
31
|
-
no_self_type_validator(arg)
|
|
32
|
-
no_classish_type_validator(arg)
|
|
33
|
-
end
|
|
34
|
-
end
|
|
35
|
-
check_module_or_class(decl)
|
|
36
|
-
end
|
|
37
|
-
|
|
38
|
-
def on_rbs_module(decl)
|
|
39
|
-
decl.self_types.each do |self_type|
|
|
40
|
-
self_type.args.each do |arg|
|
|
41
|
-
void_type_context_validator(arg, true)
|
|
42
|
-
no_self_type_validator(arg)
|
|
43
|
-
no_classish_type_validator(arg)
|
|
44
|
-
end
|
|
45
|
-
end
|
|
46
|
-
check_module_or_class(decl)
|
|
47
|
-
end
|
|
48
|
-
|
|
49
|
-
def check_type_params(decl)
|
|
50
|
-
decl.type_params.each do |param|
|
|
51
|
-
if ub = param.upper_bound
|
|
52
|
-
void_type_context_validator(ub)
|
|
53
|
-
no_self_type_validator(ub)
|
|
54
|
-
no_classish_type_validator(ub)
|
|
55
|
-
end
|
|
56
|
-
|
|
57
|
-
if param.respond_to?(:default_type)
|
|
58
|
-
if dt = param.default_type
|
|
59
|
-
void_type_context_validator(dt, true)
|
|
60
|
-
no_self_type_validator(dt)
|
|
61
|
-
no_classish_type_validator(dt)
|
|
62
|
-
end
|
|
63
|
-
end
|
|
64
|
-
end
|
|
65
|
-
end
|
|
66
|
-
|
|
67
|
-
def check_module_or_class(decl)
|
|
68
|
-
check_type_params(decl)
|
|
69
|
-
|
|
70
|
-
decl.each_member do |member|
|
|
71
|
-
case member
|
|
72
|
-
when AST::Members::MethodDefinition
|
|
73
|
-
member.overloads.each do |ov|
|
|
74
|
-
void_type_context_validator(ov.method_type)
|
|
75
|
-
end
|
|
76
|
-
when AST::Members::Attribute
|
|
77
|
-
void_type_context_validator(member.type)
|
|
78
|
-
when AST::Members::Mixin
|
|
79
|
-
member.args.each do |arg|
|
|
80
|
-
no_self_type_validator(arg)
|
|
81
|
-
unless arg.is_a?(Types::Bases::Void)
|
|
82
|
-
void_type_context_validator(arg, true)
|
|
83
|
-
end
|
|
84
|
-
end
|
|
85
|
-
when AST::Members::Var
|
|
86
|
-
void_type_context_validator(member.type)
|
|
87
|
-
if member.is_a?(AST::Members::ClassVariable)
|
|
88
|
-
no_self_type_validator(member.type)
|
|
89
|
-
end
|
|
90
|
-
end
|
|
91
|
-
end
|
|
92
|
-
end
|
|
93
|
-
|
|
94
|
-
def on_rbs_interface(decl)
|
|
95
|
-
check_type_params(decl)
|
|
96
|
-
|
|
97
|
-
decl.members.each do |member|
|
|
98
|
-
case member
|
|
99
|
-
when AST::Members::MethodDefinition
|
|
100
|
-
member.overloads.each do |ov|
|
|
101
|
-
void_type_context_validator(ov.method_type)
|
|
102
|
-
no_classish_type_validator(ov.method_type)
|
|
103
|
-
end
|
|
104
|
-
end
|
|
105
|
-
end
|
|
106
|
-
end
|
|
107
|
-
|
|
108
|
-
def on_rbs_constant(decl)
|
|
109
|
-
no_self_type_validator(decl.type)
|
|
110
|
-
no_classish_type_validator(decl.type)
|
|
111
|
-
void_type_context_validator(decl.type)
|
|
112
|
-
end
|
|
113
|
-
alias on_rbs_global on_rbs_constant
|
|
114
|
-
|
|
115
|
-
def on_rbs_type_alias(decl)
|
|
116
|
-
no_self_type_validator(decl.type)
|
|
117
|
-
no_classish_type_validator(decl.type)
|
|
118
|
-
void_type_context_validator(decl.type)
|
|
119
|
-
|
|
120
|
-
check_type_params(decl)
|
|
121
|
-
end
|
|
122
|
-
|
|
123
|
-
private
|
|
124
|
-
|
|
125
|
-
# @rbs type: ::RBS::Types::t
|
|
126
|
-
def no_self_type_validator(type)
|
|
127
|
-
case type
|
|
128
|
-
when ::RBS::MethodType,
|
|
129
|
-
Types::Record,
|
|
130
|
-
Types::Tuple,
|
|
131
|
-
Types::Union,
|
|
132
|
-
Types::Intersection,
|
|
133
|
-
Types::Optional,
|
|
134
|
-
Types::ClassInstance,
|
|
135
|
-
Types::Proc
|
|
136
|
-
type.each_type do |t|
|
|
137
|
-
no_self_type_validator(t)
|
|
138
|
-
end
|
|
139
|
-
else
|
|
140
|
-
if type.has_self_type?
|
|
141
|
-
offence(type, "`self` type is not allowed in this context")
|
|
142
|
-
end
|
|
143
|
-
end
|
|
144
|
-
end
|
|
145
|
-
|
|
146
|
-
# @rbs type: ::RBS::MethodType | ::RBS::Types::t
|
|
147
|
-
def no_classish_type_validator(type)
|
|
148
|
-
case type
|
|
149
|
-
when ::RBS::MethodType,
|
|
150
|
-
Types::Record,
|
|
151
|
-
Types::Tuple,
|
|
152
|
-
Types::Union,
|
|
153
|
-
Types::Intersection,
|
|
154
|
-
Types::Optional,
|
|
155
|
-
Types::ClassInstance,
|
|
156
|
-
Types::Proc
|
|
157
|
-
type.each_type do |t|
|
|
158
|
-
no_classish_type_validator(t)
|
|
159
|
-
end
|
|
160
|
-
when Types::Bases::Instance
|
|
161
|
-
offence(type, "`instance` type is not allowed in this context")
|
|
162
|
-
when Types::Bases::Class
|
|
163
|
-
offence(type, "`class` type is not allowed in this context")
|
|
164
|
-
else
|
|
165
|
-
if type.has_classish_type?
|
|
166
|
-
offence(type, "`instance` or `class` type is not allowed in this context")
|
|
167
|
-
end
|
|
168
|
-
end
|
|
169
|
-
end
|
|
170
|
-
|
|
171
|
-
# @rbs type: ::RBS::MethodType | ::RBS::Types::Function | ::RBS::Types::t
|
|
172
|
-
# @rbs allowed_here: bool
|
|
173
|
-
def void_type_context_validator(type, allowed_here = false)
|
|
174
|
-
return if type.is_a?(Types::UntypedFunction)
|
|
175
|
-
|
|
176
|
-
case type
|
|
177
|
-
when ::RBS::MethodType
|
|
178
|
-
void_type_context_validator(type.type)
|
|
179
|
-
when Types::Function
|
|
180
|
-
type.each_param do |param|
|
|
181
|
-
void_type_context_validator(param.type)
|
|
182
|
-
end
|
|
183
|
-
case type.return_type
|
|
184
|
-
when Types::Bases::Void
|
|
185
|
-
# `() -> void` is allowed
|
|
186
|
-
else
|
|
187
|
-
void_type_context_validator(type.return_type, true)
|
|
188
|
-
end
|
|
189
|
-
when Types::Proc
|
|
190
|
-
void_type_context_validator(type.type)
|
|
191
|
-
void_type_context_validator(type.self_type) if type.self_type
|
|
192
|
-
if type.block
|
|
193
|
-
void_type_context_validator(type.block.type)
|
|
194
|
-
void_type_context_validator(type.block.self_type) if type.block.self_type
|
|
195
|
-
end
|
|
196
|
-
when Types::ClassInstance
|
|
197
|
-
type.args.each do |arg|
|
|
198
|
-
next if arg.is_a?(Types::Bases::Void)
|
|
199
|
-
|
|
200
|
-
void_type_context_validator(arg)
|
|
201
|
-
end
|
|
202
|
-
when Types::Record,
|
|
203
|
-
Types::Tuple,
|
|
204
|
-
Types::Union,
|
|
205
|
-
Types::Intersection,
|
|
206
|
-
Types::Optional
|
|
207
|
-
type.each_type do |t|
|
|
208
|
-
void_type_context_validator(t)
|
|
209
|
-
end
|
|
210
|
-
else
|
|
211
|
-
if allowed_here
|
|
212
|
-
return if type.is_a?(Types::Bases::Void)
|
|
213
|
-
end
|
|
214
|
-
if type.with_nonreturn_void?
|
|
215
|
-
offence(type, "`void` type is only allowed in return type or generics parameter")
|
|
216
|
-
end
|
|
217
|
-
end
|
|
218
|
-
end
|
|
219
|
-
|
|
220
|
-
def offence(decl, message)
|
|
221
|
-
range = location_to_range(decl.location)
|
|
222
|
-
add_offense(range, message: message)
|
|
223
|
-
end
|
|
224
|
-
end
|
|
225
|
-
end
|
|
226
|
-
end
|
|
227
|
-
end
|
|
228
|
-
end
|