rubocop-on-rbs 1.1.0 → 1.3.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 +13 -0
- data/config/default.yml +31 -1
- data/lib/rubocop/cop/rbs/layout/comment_indentation.rb +0 -4
- data/lib/rubocop/cop/rbs/layout/empty_lines_around_body.rb +1 -1
- data/lib/rubocop/cop/rbs/layout/space_after_comma.rb +35 -0
- data/lib/rubocop/cop/rbs/layout/space_around_arrow.rb +9 -0
- data/lib/rubocop/cop/rbs/layout/space_around_operators.rb +11 -3
- data/lib/rubocop/cop/rbs/lint/ambiguous_keyword_argument_key.rb +62 -0
- data/lib/rubocop/cop/rbs/lint/ambiguous_operator_precedence.rb +1 -0
- data/lib/rubocop/cop/rbs/lint/literal_intersection.rb +1 -0
- data/lib/rubocop/cop/rbs/lint/top_level_interface.rb +47 -0
- data/lib/rubocop/cop/rbs/lint/top_level_type_alias.rb +44 -0
- data/lib/rubocop/cop/rbs/lint/{useless_overload_type_params.rb → unused_overload_type_params.rb} +3 -3
- data/lib/rubocop/cop/rbs/lint/unused_type_alias_type_params.rb +59 -0
- data/lib/rubocop/cop/rbs/style/class_with_singleton.rb +48 -0
- data/lib/rubocop/cop/rbs/style/classic_type.rb +8 -0
- data/lib/rubocop/cop/rbs/style/empty_argument.rb +1 -0
- data/lib/rubocop/cop/rbs/style/instance_with_instance.rb +57 -0
- data/lib/rubocop/cop/rbs/style/redundant_parentheses.rb +1 -0
- data/lib/rubocop/cop/rbs_cops.rb +8 -1
- data/lib/rubocop/rbs/cop_base.rb +3 -0
- data/lib/rubocop/rbs/processed_rbs_source.rb +2 -6
- data/lib/rubocop/rbs/version.rb +1 -1
- metadata +11 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 79ae2335c1cd8ea94cf1e5003b058c1c7d00966f3cc790a0410b3494e7c94ee4
|
4
|
+
data.tar.gz: 4c6697e0dc3ee2b0724bfe34505539608adfb76ae4733781a3d5195dfbc0917e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0530b33a2394b966e88784add6936f79631ee8b9f9d81019c265273c73d33719d1e65553e75b2c320f45a8a8c40d8d01b225cbe0f94096bdd0700ae3a281dfc3
|
7
|
+
data.tar.gz: f20537912b6b4fcd8fff9d24da715c6abfae186957c3464fa7989bbc9ae2565d9478cb410436e59e1da3ee1c9a1b50b7070602b82f9dda88abb0f1b99b01731c
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,18 @@
|
|
1
1
|
## [Unreleased]
|
2
2
|
|
3
|
+
## [1.2.0] - 2024-11-18
|
4
|
+
|
5
|
+
* Fix false positive when oneline class by @ksss in https://github.com/ksss/rubocop-on-rbs/pull/41
|
6
|
+
* Add RBS/Lint/TopLevelTypeAlias by @ksss in https://github.com/ksss/rubocop-on-rbs/pull/46
|
7
|
+
* Add RBS/Lint/TopLevelInterface by @ksss in https://github.com/ksss/rubocop-on-rbs/pull/47
|
8
|
+
* [RBS/Lint/TopLevelTypeAlias] Fix false positive by @ksss in https://github.com/ksss/rubocop-on-rbs/pull/48
|
9
|
+
* Bundle update by @ksss in https://github.com/ksss/rubocop-on-rbs/pull/49
|
10
|
+
|
11
|
+
## [1.1.0] - 2024-10-01
|
12
|
+
|
13
|
+
* Add RBS/Layout/EmptyLinesAroundAccessModifier by @ksss in https://github.com/ksss/rubocop-on-rbs/pull/39
|
14
|
+
* Fix error when same line with `RBS/Layout/EndAlignment` by @ksss in https://github.com/ksss/rubocop-on-rbs/pull/40
|
15
|
+
|
3
16
|
## [1.0.0] - 2024-09-17
|
4
17
|
|
5
18
|
* Add RBS/Layout/EmptyLines by @ksss in https://github.com/ksss/rubocop-on-rbs/pull/35
|
data/config/default.yml
CHANGED
@@ -71,6 +71,10 @@ RBS/Layout/OverloadIndentation:
|
|
71
71
|
Description: 'Indent overload method'
|
72
72
|
Enabled: true
|
73
73
|
|
74
|
+
RBS/Layout/SpaceAfterComma:
|
75
|
+
Description: 'Use space after `,`'
|
76
|
+
Enabled: true
|
77
|
+
|
74
78
|
RBS/Layout/SpaceAroundArrow:
|
75
79
|
Description: 'Use space around `->`'
|
76
80
|
Enabled: true
|
@@ -101,6 +105,10 @@ RBS/Lint:
|
|
101
105
|
Severity: warning
|
102
106
|
Enabled: true
|
103
107
|
|
108
|
+
RBS/Lint/AmbiguousKeywordArgumentKey:
|
109
|
+
Description: 'Check ambiguous keyword argument key'
|
110
|
+
Enabled: true
|
111
|
+
|
104
112
|
RBS/Lint/AmbiguousOperatorPrecedence:
|
105
113
|
Description: 'Check ambiguous operator precedence'
|
106
114
|
Enabled: true
|
@@ -118,15 +126,29 @@ RBS/Lint/Syntax:
|
|
118
126
|
Description: 'Check RBS syntax'
|
119
127
|
Enabled: true
|
120
128
|
|
129
|
+
RBS/Lint/TopLevelInterface:
|
130
|
+
Description: 'Check top level interface'
|
131
|
+
Enabled: pending
|
132
|
+
VersionAdded: '1.2.0'
|
133
|
+
|
134
|
+
RBS/Lint/TopLevelTypeAlias:
|
135
|
+
Description: 'Check top level type alias'
|
136
|
+
Enabled: pending
|
137
|
+
VersionAdded: '1.2.0'
|
138
|
+
|
121
139
|
RBS/Lint/UselessAccessModifier:
|
122
140
|
AutoCorrect: contextual
|
123
141
|
Description: 'Check useless access modifier'
|
124
142
|
Enabled: true
|
125
143
|
|
126
|
-
RBS/Lint/
|
144
|
+
RBS/Lint/UnusedOverloadTypeParams:
|
127
145
|
Description: 'Check redundant overload type params'
|
128
146
|
Enabled: true
|
129
147
|
|
148
|
+
RBS/Lint/UnusedTypeAliasTypeParams:
|
149
|
+
Description: 'Check redundant type alias type params'
|
150
|
+
Enabled: true
|
151
|
+
|
130
152
|
RBS/Lint/WillSyntaxError:
|
131
153
|
Description: 'Check RBS will syntax error'
|
132
154
|
Enabled: true
|
@@ -140,6 +162,10 @@ RBS/Style/BlockReturnBoolish:
|
|
140
162
|
Description: 'Use `bool` for block return type'
|
141
163
|
Enabled: true
|
142
164
|
|
165
|
+
RBS/Style/ClassWithSingleton:
|
166
|
+
Description: 'Check `class` with singleton context'
|
167
|
+
Enabled: pending
|
168
|
+
|
143
169
|
RBS/Style/ClassicType:
|
144
170
|
Description: 'Use simple type'
|
145
171
|
Enabled: true
|
@@ -156,6 +182,10 @@ RBS/Style/InitializeReturnType:
|
|
156
182
|
Description: 'Use `void` for initialize method'
|
157
183
|
Enabled: true
|
158
184
|
|
185
|
+
RBS/Style/InstanceWithInstance:
|
186
|
+
Description: 'Check `instance` with instance context'
|
187
|
+
Enabled: pending
|
188
|
+
|
159
189
|
RBS/Style/OptionalNil:
|
160
190
|
Description: 'Use nil instead of nil?'
|
161
191
|
Enabled: true
|
@@ -12,6 +12,7 @@ module RuboCop
|
|
12
12
|
def check(decl)
|
13
13
|
first_line = decl.location.start_line
|
14
14
|
last_line = decl.location.end_line
|
15
|
+
return if last_line == first_line
|
15
16
|
|
16
17
|
check_beginning(first_line)
|
17
18
|
check_ending(last_line)
|
@@ -29,7 +30,6 @@ module RuboCop
|
|
29
30
|
return unless line_no.positive?
|
30
31
|
return unless processed_source.lines[line_no]
|
31
32
|
return unless processed_source.lines[line_no].empty?
|
32
|
-
return unless processed_source.lines[line_no + 1]
|
33
33
|
|
34
34
|
range = source_range(processed_source.buffer, line_no + 1, 0)
|
35
35
|
message = message(MSG_EXTRA, desc)
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module RBS
|
6
|
+
module Layout
|
7
|
+
# @example default
|
8
|
+
# # bad
|
9
|
+
# def foo: (Integer,String) -> void
|
10
|
+
#
|
11
|
+
# # good
|
12
|
+
# def foo: (Integer, String) -> void
|
13
|
+
class SpaceAfterComma < RuboCop::RBS::CopBase
|
14
|
+
extend AutoCorrector
|
15
|
+
|
16
|
+
MSG = 'Space missing after comma.'
|
17
|
+
|
18
|
+
def on_rbs_new_investigation
|
19
|
+
processed_rbs_source.tokens.each_cons(2) do |comma, after|
|
20
|
+
next unless comma.type == :pCOMMA
|
21
|
+
next unless comma.location
|
22
|
+
next unless comma.location.end_line == after.location.start_line
|
23
|
+
next unless after.type != :tTRIVIA
|
24
|
+
|
25
|
+
range = location_to_range(comma.location)
|
26
|
+
add_offense(range) do |corrector|
|
27
|
+
corrector.insert_after(range, ' ')
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -33,6 +33,8 @@ module RuboCop
|
|
33
33
|
next unless loc
|
34
34
|
|
35
35
|
if before && (before.location.end_pos + 1 != loc.start_pos)
|
36
|
+
next unless before.location.end_line == loc.start_line
|
37
|
+
|
36
38
|
arrow = location_to_range(loc).adjust(begin_pos: base, end_pos: base)
|
37
39
|
add_offense(arrow, message: MSG_BEFORE) do |corrector|
|
38
40
|
range = range_between(before.location.end_pos, loc.start_pos)
|
@@ -41,6 +43,8 @@ module RuboCop
|
|
41
43
|
end
|
42
44
|
|
43
45
|
if loc.end_pos + 1 != after.location.start_pos
|
46
|
+
next unless loc.end_line == after.location.start_line
|
47
|
+
|
44
48
|
arrow = location_to_range(loc).adjust(begin_pos: base, end_pos: base)
|
45
49
|
add_offense(arrow, message: MSG_AFTER) do |corrector|
|
46
50
|
range = range_between(loc.end_pos, after.location.start_pos)
|
@@ -49,6 +53,11 @@ module RuboCop
|
|
49
53
|
end
|
50
54
|
end
|
51
55
|
end
|
56
|
+
alias on_rbs_constant on_rbs_def
|
57
|
+
alias on_rbs_global on_rbs_def
|
58
|
+
alias on_rbs_type_alias on_rbs_def
|
59
|
+
alias on_rbs_attribute on_rbs_def
|
60
|
+
alias on_rbs_var on_rbs_def
|
52
61
|
end
|
53
62
|
end
|
54
63
|
end
|
@@ -16,12 +16,20 @@ module RuboCop
|
|
16
16
|
def on_rbs_def(decl)
|
17
17
|
decl.overloads.each do |overload|
|
18
18
|
overload.method_type.each_type do |type|
|
19
|
-
|
19
|
+
check_type(type)
|
20
20
|
end
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
24
|
-
def
|
24
|
+
def on_rbs_constant(decl)
|
25
|
+
check_type(decl.type)
|
26
|
+
end
|
27
|
+
alias on_rbs_global on_rbs_constant
|
28
|
+
alias on_rbs_type_alias on_rbs_constant
|
29
|
+
alias on_rbs_attribute on_rbs_constant
|
30
|
+
alias on_rbs_var on_rbs_constant
|
31
|
+
|
32
|
+
def check_type(type)
|
25
33
|
case type
|
26
34
|
when ::RBS::Types::Union
|
27
35
|
check_operator(type, '|')
|
@@ -29,7 +37,7 @@ module RuboCop
|
|
29
37
|
check_operator(type, '&')
|
30
38
|
end
|
31
39
|
type.each_type do |t|
|
32
|
-
|
40
|
+
check_type(t)
|
33
41
|
end
|
34
42
|
end
|
35
43
|
|
@@ -0,0 +1,62 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module RBS
|
6
|
+
module Lint
|
7
|
+
# Checks keyword argument keys that are not local variable names.
|
8
|
+
#
|
9
|
+
# @example default
|
10
|
+
# # bad
|
11
|
+
# def foo: (option?: bool, option!: bool, Option: bool) -> void
|
12
|
+
#
|
13
|
+
class AmbiguousKeywordArgumentKey < RuboCop::RBS::CopBase
|
14
|
+
extend AutoCorrector
|
15
|
+
|
16
|
+
# @rbs decl: ::RBS::AST::Members::MethodDefinition
|
17
|
+
def on_rbs_def(decl)
|
18
|
+
decl.overloads.each do |overload|
|
19
|
+
func = overload.method_type.type
|
20
|
+
next unless func.kind_of?(::RBS::Types::Function)
|
21
|
+
next unless !func.required_keywords.empty? || !func.optional_keywords.empty?
|
22
|
+
|
23
|
+
base_pos = overload.method_type.location.start_pos
|
24
|
+
lex_result = ::RBS::Parser.lex(overload.method_type.location.source)
|
25
|
+
buf = []
|
26
|
+
lex_result.value.each do |token|
|
27
|
+
case token.type
|
28
|
+
when :pLPAREN, :pRPAREN, :tTRIVIA
|
29
|
+
# skip
|
30
|
+
when :pCOLON
|
31
|
+
next if buf.empty?
|
32
|
+
|
33
|
+
case buf.last.type
|
34
|
+
when :pQUESTION
|
35
|
+
buf.shift if buf.first.type == :pQUESTION
|
36
|
+
actual = buf.map { |t| t.location.source }.join
|
37
|
+
did_you_mean = buf.reject { |t| t.location.source == '?' }.map { |t| t.location.source }.join
|
38
|
+
message = +"`#{actual}` is not local variable name."
|
39
|
+
if did_you_mean.length > 0
|
40
|
+
message << " Did you mean `?#{did_you_mean}` for optional keyword argument?"
|
41
|
+
end
|
42
|
+
range = range_between(buf.first.location.start_pos + base_pos, buf.last.location.end_pos + base_pos)
|
43
|
+
add_offense(range, message: message)
|
44
|
+
when :tBANGIDENT, :tUIDENT
|
45
|
+
buf.shift if buf.first.type == :pQUESTION
|
46
|
+
actual = buf.map { |t| t.location.source }.join
|
47
|
+
range = range_between(buf.first.location.start_pos + base_pos, buf.last.location.end_pos + base_pos)
|
48
|
+
add_offense(range, message: "`#{actual}` is not local variable name.")
|
49
|
+
end
|
50
|
+
when :pCOMMA
|
51
|
+
buf.clear
|
52
|
+
else
|
53
|
+
buf << token
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module RBS
|
6
|
+
module Lint
|
7
|
+
# Top-level namespaces are likely to conflict and should be avoided.
|
8
|
+
#
|
9
|
+
# @example
|
10
|
+
# # bad
|
11
|
+
# interface _Option
|
12
|
+
# def option: () -> untyped
|
13
|
+
# end
|
14
|
+
#
|
15
|
+
# # good
|
16
|
+
# class Foo
|
17
|
+
# interface _Option
|
18
|
+
# def option: () -> untyped
|
19
|
+
# end
|
20
|
+
# end
|
21
|
+
#
|
22
|
+
class TopLevelInterface < RuboCop::RBS::CopBase
|
23
|
+
MSG = 'Top level interface detected.'
|
24
|
+
|
25
|
+
def on_rbs_new_investigation
|
26
|
+
@last_end = nil
|
27
|
+
end
|
28
|
+
|
29
|
+
def on_rbs_class(decl)
|
30
|
+
return unless @last_end_pos.nil? || (@last_end_pos < decl.location.end_pos)
|
31
|
+
|
32
|
+
@last_end_pos = decl.location.end_pos
|
33
|
+
end
|
34
|
+
alias on_rbs_module on_rbs_class
|
35
|
+
|
36
|
+
def on_rbs_interface(decl)
|
37
|
+
return unless @last_end_pos.nil? || (@last_end_pos < decl.location.start_pos)
|
38
|
+
return unless decl.name.namespace.path.empty?
|
39
|
+
|
40
|
+
range = location_to_range(decl.location)
|
41
|
+
add_offense(range)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module RBS
|
6
|
+
module Lint
|
7
|
+
# Top-level namespaces are likely to conflict and should be avoided.
|
8
|
+
#
|
9
|
+
# @example
|
10
|
+
# # bad
|
11
|
+
# type foo = String
|
12
|
+
#
|
13
|
+
# # good
|
14
|
+
# class Foo
|
15
|
+
# type bar = Integer
|
16
|
+
# end
|
17
|
+
#
|
18
|
+
class TopLevelTypeAlias < RuboCop::RBS::CopBase
|
19
|
+
MSG = 'Top level type alias detected.'
|
20
|
+
|
21
|
+
def on_rbs_new_investigation
|
22
|
+
@last_end = nil
|
23
|
+
end
|
24
|
+
|
25
|
+
def on_rbs_class(decl)
|
26
|
+
return unless @last_end_pos.nil? || (@last_end_pos < decl.location.end_pos)
|
27
|
+
|
28
|
+
@last_end_pos = decl.location.end_pos
|
29
|
+
end
|
30
|
+
alias on_rbs_module on_rbs_class
|
31
|
+
alias on_rbs_interface on_rbs_class
|
32
|
+
|
33
|
+
def on_rbs_type_alias(decl)
|
34
|
+
return unless @last_end_pos.nil? || (@last_end_pos < decl.location.start_pos)
|
35
|
+
return unless decl.name.namespace.path.empty?
|
36
|
+
|
37
|
+
range = location_to_range(decl.location)
|
38
|
+
add_offense(range)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
data/lib/rubocop/cop/rbs/lint/{useless_overload_type_params.rb → unused_overload_type_params.rb}
RENAMED
@@ -4,7 +4,7 @@ module RuboCop
|
|
4
4
|
module Cop
|
5
5
|
module RBS
|
6
6
|
module Lint
|
7
|
-
# Notice
|
7
|
+
# Notice unused overload type parameters.
|
8
8
|
#
|
9
9
|
# @example default
|
10
10
|
# # bad
|
@@ -12,8 +12,8 @@ module RuboCop
|
|
12
12
|
#
|
13
13
|
# # good
|
14
14
|
# def foo: [T] (T) -> T
|
15
|
-
class
|
16
|
-
MSG = '
|
15
|
+
class UnusedOverloadTypeParams < RuboCop::RBS::CopBase
|
16
|
+
MSG = 'Unused overload type variable - `%<variable>s`.'
|
17
17
|
|
18
18
|
def on_rbs_def(decl)
|
19
19
|
decl.overloads.each do |overload|
|
@@ -0,0 +1,59 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module RBS
|
6
|
+
module Lint
|
7
|
+
# Notice unused type parameters.
|
8
|
+
#
|
9
|
+
# @example default
|
10
|
+
# # bad
|
11
|
+
# type ary[T] = Array[Integer]
|
12
|
+
#
|
13
|
+
# # good
|
14
|
+
# type ary[T] = Array[T]
|
15
|
+
class UnusedTypeAliasTypeParams < RuboCop::RBS::CopBase
|
16
|
+
MSG = 'Unused type variable - `%<variable>s`.'
|
17
|
+
|
18
|
+
def on_rbs_type_alias(decl)
|
19
|
+
return if decl.type_params.empty?
|
20
|
+
|
21
|
+
type_params = decl.type_params.dup
|
22
|
+
|
23
|
+
map = type_params.to_h { |param| [param.name, param] }
|
24
|
+
type_params.each do |type_param|
|
25
|
+
if type_param.upper_bound
|
26
|
+
used_variable_in_type(type_param.upper_bound) do |var|
|
27
|
+
map.delete(var.name)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
used_variable_in_type(decl.type) do |var|
|
33
|
+
map.delete(var.name)
|
34
|
+
end
|
35
|
+
return if map.empty?
|
36
|
+
|
37
|
+
map.each do |name, type_param|
|
38
|
+
next unless type_param.location
|
39
|
+
|
40
|
+
t = location_to_range(type_param.location[:name])
|
41
|
+
add_offense(t, message: format(MSG, variable: name))
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def used_variable_in_type(type, &block)
|
46
|
+
case type
|
47
|
+
when ::RBS::Types::Variable
|
48
|
+
yield type
|
49
|
+
else
|
50
|
+
type.each_type do |t|
|
51
|
+
used_variable_in_type(t, &block)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
# rbs_inline: enabled
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
module RuboCop
|
5
|
+
module Cop
|
6
|
+
module RBS
|
7
|
+
module Style
|
8
|
+
# Checks that `class` in singleton context.
|
9
|
+
#
|
10
|
+
# @example (default)
|
11
|
+
# # bad
|
12
|
+
# def self.foo: (class) -> class
|
13
|
+
#
|
14
|
+
# # good
|
15
|
+
# def self.foo: (self) -> self
|
16
|
+
#
|
17
|
+
class ClassWithSingleton < RuboCop::RBS::CopBase
|
18
|
+
extend AutoCorrector
|
19
|
+
MSG = 'Use `self` instead of `class`.'
|
20
|
+
|
21
|
+
# @rbs decl: RBS::AST::Members::MethodDefinition
|
22
|
+
def on_rbs_def(decl)
|
23
|
+
return unless decl.kind == :singleton
|
24
|
+
|
25
|
+
decl.overloads.each do |overload|
|
26
|
+
overload.method_type.each_type do |type|
|
27
|
+
check_type(type)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
# @rbs type: RBS::Types::t
|
33
|
+
def check_type(type)
|
34
|
+
case type
|
35
|
+
when ::RBS::Types::Bases::Class
|
36
|
+
return unless type.location
|
37
|
+
|
38
|
+
range = location_to_range(type.location)
|
39
|
+
add_offense(range) do |corrector|
|
40
|
+
corrector.replace(range, 'self')
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -29,6 +29,14 @@ module RuboCop
|
|
29
29
|
end
|
30
30
|
end
|
31
31
|
|
32
|
+
def on_rbs_constant(decl)
|
33
|
+
check_type(decl.type)
|
34
|
+
end
|
35
|
+
alias on_rbs_global on_rbs_constant
|
36
|
+
alias on_rbs_type_alias on_rbs_constant
|
37
|
+
alias on_rbs_attribute on_rbs_constant
|
38
|
+
alias on_rbs_var on_rbs_constant
|
39
|
+
|
32
40
|
# @rbs type: ::RBS::Types::t
|
33
41
|
def check_type(type)
|
34
42
|
find_replacement(type) do |t, replaced|
|
@@ -0,0 +1,57 @@
|
|
1
|
+
# rbs_inline: enabled
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
module RuboCop
|
5
|
+
module Cop
|
6
|
+
module RBS
|
7
|
+
module Style
|
8
|
+
# Checks that `instance` in instance context.
|
9
|
+
#
|
10
|
+
# @example (default)
|
11
|
+
# # bad
|
12
|
+
# def foo: (instance) -> instance
|
13
|
+
#
|
14
|
+
# # good
|
15
|
+
# def foo: (self) -> self
|
16
|
+
#
|
17
|
+
class InstanceWithInstance < RuboCop::RBS::CopBase
|
18
|
+
extend AutoCorrector
|
19
|
+
MSG = 'Use `self` instead of `instance`.'
|
20
|
+
|
21
|
+
# @rbs decl: RBS::AST::Declarations::Class
|
22
|
+
def on_rbs_class(decl)
|
23
|
+
# The meaning of `self` and `instance` changes in generic class.
|
24
|
+
return unless decl.type_params.empty?
|
25
|
+
|
26
|
+
decl.members.each do |member|
|
27
|
+
case member
|
28
|
+
when ::RBS::AST::Members::MethodDefinition
|
29
|
+
next unless member.kind == :instance
|
30
|
+
|
31
|
+
member.overloads.each do |overload|
|
32
|
+
overload.method_type.each_type do |type|
|
33
|
+
check_type(type)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
when ::RBS::AST::Members::InstanceVariable
|
37
|
+
check_type(member.type)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def check_type(type)
|
43
|
+
case type
|
44
|
+
when ::RBS::Types::Bases::Instance
|
45
|
+
return unless type.location
|
46
|
+
|
47
|
+
range = location_to_range(type.location)
|
48
|
+
add_offense(range) do |corrector|
|
49
|
+
corrector.replace(range, 'self')
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
data/lib/rubocop/cop/rbs_cops.rb
CHANGED
@@ -14,6 +14,7 @@ require_relative 'rbs/layout/end_alignment'
|
|
14
14
|
require_relative 'rbs/layout/extra_spacing'
|
15
15
|
require_relative 'rbs/layout/indentation_width'
|
16
16
|
require_relative 'rbs/layout/overload_indentation'
|
17
|
+
require_relative 'rbs/layout/space_after_comma'
|
17
18
|
require_relative 'rbs/layout/space_around_arrow'
|
18
19
|
require_relative 'rbs/layout/space_around_braces'
|
19
20
|
require_relative 'rbs/layout/space_around_operators'
|
@@ -21,19 +22,25 @@ require_relative 'rbs/layout/space_before_colon'
|
|
21
22
|
require_relative 'rbs/layout/space_before_overload'
|
22
23
|
require_relative 'rbs/layout/trailing_whitespace'
|
23
24
|
|
25
|
+
require_relative 'rbs/lint/ambiguous_keyword_argument_key'
|
24
26
|
require_relative 'rbs/lint/ambiguous_operator_precedence'
|
25
27
|
require_relative 'rbs/lint/duplicate_overload'
|
26
28
|
require_relative 'rbs/lint/literal_intersection'
|
27
29
|
require_relative 'rbs/lint/syntax'
|
30
|
+
require_relative 'rbs/lint/top_level_interface'
|
31
|
+
require_relative 'rbs/lint/top_level_type_alias'
|
32
|
+
require_relative 'rbs/lint/unused_overload_type_params'
|
33
|
+
require_relative 'rbs/lint/unused_type_alias_type_params'
|
28
34
|
require_relative 'rbs/lint/useless_access_modifier'
|
29
|
-
require_relative 'rbs/lint/useless_overload_type_params'
|
30
35
|
require_relative 'rbs/lint/will_syntax_error'
|
31
36
|
|
32
37
|
require_relative 'rbs/style/block_return_boolish'
|
38
|
+
require_relative 'rbs/style/class_with_singleton'
|
33
39
|
require_relative 'rbs/style/classic_type'
|
34
40
|
require_relative 'rbs/style/duplicated_type'
|
35
41
|
require_relative 'rbs/style/empty_argument'
|
36
42
|
require_relative 'rbs/style/initialize_return_type'
|
43
|
+
require_relative 'rbs/style/instance_with_instance'
|
37
44
|
require_relative 'rbs/style/optional_nil'
|
38
45
|
require_relative 'rbs/style/redundant_parentheses'
|
39
46
|
require_relative 'rbs/style/true_false'
|
data/lib/rubocop/rbs/cop_base.rb
CHANGED
@@ -73,6 +73,7 @@ module RuboCop
|
|
73
73
|
def on_rbs_attribute(member); end
|
74
74
|
def on_rbs_public(member); end
|
75
75
|
def on_rbs_private(member); end
|
76
|
+
def on_rbs_var(member); end
|
76
77
|
|
77
78
|
def walk(decl)
|
78
79
|
case decl
|
@@ -99,6 +100,8 @@ module RuboCop
|
|
99
100
|
on_rbs_public(decl)
|
100
101
|
when ::RBS::AST::Members::Private
|
101
102
|
on_rbs_private(decl)
|
103
|
+
when ::RBS::AST::Members::Var
|
104
|
+
on_rbs_var(decl)
|
102
105
|
end
|
103
106
|
end
|
104
107
|
|
@@ -23,12 +23,8 @@ module RuboCop
|
|
23
23
|
@error.nil?
|
24
24
|
end
|
25
25
|
|
26
|
-
def tokens
|
27
|
-
@tokens ||=
|
28
|
-
tokens = ::RBS::Parser.lex(buffer).value
|
29
|
-
tokens.reject! { |token| token.type == :tTRIVIA } unless with_trivia
|
30
|
-
tokens
|
31
|
-
end
|
26
|
+
def tokens
|
27
|
+
@tokens ||= ::RBS::Parser.lex(buffer).value
|
32
28
|
end
|
33
29
|
end
|
34
30
|
end
|
data/lib/rubocop/rbs/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rubocop-on-rbs
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ksss
|
8
|
-
autorequire:
|
9
8
|
bindir: exe
|
10
9
|
cert_chain: []
|
11
|
-
date: 2024-
|
10
|
+
date: 2024-12-24 00:00:00.000000000 Z
|
12
11
|
dependencies:
|
13
12
|
- !ruby/object:Gem::Dependency
|
14
13
|
name: rbs
|
@@ -77,24 +76,31 @@ files:
|
|
77
76
|
- lib/rubocop/cop/rbs/layout/extra_spacing.rb
|
78
77
|
- lib/rubocop/cop/rbs/layout/indentation_width.rb
|
79
78
|
- lib/rubocop/cop/rbs/layout/overload_indentation.rb
|
79
|
+
- lib/rubocop/cop/rbs/layout/space_after_comma.rb
|
80
80
|
- lib/rubocop/cop/rbs/layout/space_around_arrow.rb
|
81
81
|
- lib/rubocop/cop/rbs/layout/space_around_braces.rb
|
82
82
|
- lib/rubocop/cop/rbs/layout/space_around_operators.rb
|
83
83
|
- lib/rubocop/cop/rbs/layout/space_before_colon.rb
|
84
84
|
- lib/rubocop/cop/rbs/layout/space_before_overload.rb
|
85
85
|
- lib/rubocop/cop/rbs/layout/trailing_whitespace.rb
|
86
|
+
- lib/rubocop/cop/rbs/lint/ambiguous_keyword_argument_key.rb
|
86
87
|
- lib/rubocop/cop/rbs/lint/ambiguous_operator_precedence.rb
|
87
88
|
- lib/rubocop/cop/rbs/lint/duplicate_overload.rb
|
88
89
|
- lib/rubocop/cop/rbs/lint/literal_intersection.rb
|
89
90
|
- lib/rubocop/cop/rbs/lint/syntax.rb
|
91
|
+
- lib/rubocop/cop/rbs/lint/top_level_interface.rb
|
92
|
+
- lib/rubocop/cop/rbs/lint/top_level_type_alias.rb
|
93
|
+
- lib/rubocop/cop/rbs/lint/unused_overload_type_params.rb
|
94
|
+
- lib/rubocop/cop/rbs/lint/unused_type_alias_type_params.rb
|
90
95
|
- lib/rubocop/cop/rbs/lint/useless_access_modifier.rb
|
91
|
-
- lib/rubocop/cop/rbs/lint/useless_overload_type_params.rb
|
92
96
|
- lib/rubocop/cop/rbs/lint/will_syntax_error.rb
|
93
97
|
- lib/rubocop/cop/rbs/style/block_return_boolish.rb
|
98
|
+
- lib/rubocop/cop/rbs/style/class_with_singleton.rb
|
94
99
|
- lib/rubocop/cop/rbs/style/classic_type.rb
|
95
100
|
- lib/rubocop/cop/rbs/style/duplicated_type.rb
|
96
101
|
- lib/rubocop/cop/rbs/style/empty_argument.rb
|
97
102
|
- lib/rubocop/cop/rbs/style/initialize_return_type.rb
|
103
|
+
- lib/rubocop/cop/rbs/style/instance_with_instance.rb
|
98
104
|
- lib/rubocop/cop/rbs/style/optional_nil.rb
|
99
105
|
- lib/rubocop/cop/rbs/style/redundant_parentheses.rb
|
100
106
|
- lib/rubocop/cop/rbs/style/true_false.rb
|
@@ -113,7 +119,6 @@ metadata:
|
|
113
119
|
source_code_uri: https://github.com/ksss/rubocop-on-rbs
|
114
120
|
changelog_uri: https://github.com/ksss/rubocop-on-rbs
|
115
121
|
rubygems_mfa_required: 'true'
|
116
|
-
post_install_message:
|
117
122
|
rdoc_options: []
|
118
123
|
require_paths:
|
119
124
|
- lib
|
@@ -128,8 +133,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
128
133
|
- !ruby/object:Gem::Version
|
129
134
|
version: '0'
|
130
135
|
requirements: []
|
131
|
-
rubygems_version: 3.
|
132
|
-
signing_key:
|
136
|
+
rubygems_version: 3.6.2
|
133
137
|
specification_version: 4
|
134
138
|
summary: RuboCop extension for RBS file.
|
135
139
|
test_files: []
|