rubocop-on-rbs 1.2.0 → 1.4.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 501f65e78ff030f4d111a08793afa70a614c28715e9d9fa4caa95e24bbcc48dd
4
- data.tar.gz: 5600ce340c99239465721d201d75881e3bcd8343787659bbf18faaaf8f51f58f
3
+ metadata.gz: 22a2658fe60905c8ede887f40b3bf114cd42c945194d0a968804b2f30069af9c
4
+ data.tar.gz: 3922f3dd22bf0f1b2afd66c0bbf0b8f5449424c3e64dcaa976def99ecdcd47d9
5
5
  SHA512:
6
- metadata.gz: 9506a4022805eeaabedd6de962bc91812f34176e40251cb902b4abaee2a5b7fc45dd962946032dcf5cedea397f88daec6d46cae9e54ccc8d9c3f35a6b9941b15
7
- data.tar.gz: 3b0176a6d2aa9fc2a85c21445720b9571e8881d6cd742c9b1b534ca76ff5103bc340e38a417e0bf6aec32259b5818aa24872553bdff3e6e47096eba19291cc9f
6
+ metadata.gz: a690a213a8654d2a9dd28822d6b112848348340956fa7e533d727203b28db67ed8233adb997b728e9fca9bd820bc0688ab1f984eb0498b6dfa645a9165b2e575
7
+ data.tar.gz: 1ffdd5f0b4305ff3158187a052cb4cd5fcce2ddf3e0629e3847699e7094741c5db66ee9962f9fc252e34d7a7f6f7a514bc32736cd4003e0377911b725b5a4a1a
data/CHANGELOG.md CHANGED
@@ -1,5 +1,31 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [1.3.0] - 2024-12-24
4
+
5
+ * Add RBS/Lint/AmbiguousKeywordArgumentKey by @ksss in https://github.com/ksss/rubocop-on-rbs/pull/51
6
+ * Fix false positive for TopLevel* by @ksss in https://github.com/ksss/rubocop-on-rbs/pull/52
7
+ * Support `RBS::AST::Members::Var` by @ksss in https://github.com/ksss/rubocop-on-rbs/pull/53
8
+ * [RBS/Layout/SpaceAroundOperators] Support other declarations by @ksss in https://github.com/ksss/rubocop-on-rbs/pull/54
9
+ * Support other declarations by @ksss in https://github.com/ksss/rubocop-on-rbs/pull/55
10
+ * Use 3.4.0-rc1 by @ksss in https://github.com/ksss/rubocop-on-rbs/pull/56
11
+ * Add RBS/Lint/UselessTypeAliasTypeParams by @ksss in https://github.com/ksss/rubocop-on-rbs/pull/57
12
+ * Use "Unused" instead of "Useless" as much as possible. by @ksss in https://github.com/ksss/rubocop-on-rbs/pull/58
13
+ * Check config/default.yml on CI by @ksss in https://github.com/ksss/rubocop-on-rbs/pull/59
14
+ * Use "Unused" instead of "Useless" as much as possible. by @ksss in https://github.com/ksss/rubocop-on-rbs/pull/60
15
+ * Add RBS/Layout/SpaceAfterComma by @ksss in https://github.com/ksss/rubocop-on-rbs/pull/61
16
+ * Add RBS/Style/InstanceWithInstance by @ksss in https://github.com/ksss/rubocop-on-rbs/pull/62
17
+ * Introduce rbs-inline by @ksss in https://github.com/ksss/rubocop-on-rbs/pull/63
18
+ * Reimplement RBS/Style/InstanceWithInstance by @ksss in https://github.com/ksss/rubocop-on-rbs/pull/64
19
+ * Add RBS/Style/ClassWithSingleton by @ksss in https://github.com/ksss/rubocop-on-rbs/pull/65
20
+
21
+ ## [1.2.0] - 2024-11-18
22
+
23
+ * Fix false positive when oneline class by @ksss in https://github.com/ksss/rubocop-on-rbs/pull/41
24
+ * Add RBS/Lint/TopLevelTypeAlias by @ksss in https://github.com/ksss/rubocop-on-rbs/pull/46
25
+ * Add RBS/Lint/TopLevelInterface by @ksss in https://github.com/ksss/rubocop-on-rbs/pull/47
26
+ * [RBS/Lint/TopLevelTypeAlias] Fix false positive by @ksss in https://github.com/ksss/rubocop-on-rbs/pull/48
27
+ * Bundle update by @ksss in https://github.com/ksss/rubocop-on-rbs/pull/49
28
+
3
29
  ## [1.1.0] - 2024-10-01
4
30
 
5
31
  * Add RBS/Layout/EmptyLinesAroundAccessModifier by @ksss in https://github.com/ksss/rubocop-on-rbs/pull/39
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
@@ -113,6 +121,10 @@ RBS/Lint/LiteralIntersection:
113
121
  Description: 'Check literal intersection'
114
122
  Enabled: true
115
123
 
124
+ RBS/Lint/NewReturnsVoid:
125
+ Description: 'sigleton `new` method should not returns `void` type'
126
+ Enabled: true
127
+
116
128
  RBS/Lint/Syntax:
117
129
  Severity: fatal
118
130
  Description: 'Check RBS syntax'
@@ -120,12 +132,12 @@ RBS/Lint/Syntax:
120
132
 
121
133
  RBS/Lint/TopLevelInterface:
122
134
  Description: 'Check top level interface'
123
- Enabled: pending
135
+ Enabled: true
124
136
  VersionAdded: '1.2.0'
125
137
 
126
138
  RBS/Lint/TopLevelTypeAlias:
127
139
  Description: 'Check top level type alias'
128
- Enabled: pending
140
+ Enabled: true
129
141
  VersionAdded: '1.2.0'
130
142
 
131
143
  RBS/Lint/UselessAccessModifier:
@@ -133,10 +145,14 @@ RBS/Lint/UselessAccessModifier:
133
145
  Description: 'Check useless access modifier'
134
146
  Enabled: true
135
147
 
136
- RBS/Lint/UselessOverloadTypeParams:
148
+ RBS/Lint/UnusedOverloadTypeParams:
137
149
  Description: 'Check redundant overload type params'
138
150
  Enabled: true
139
151
 
152
+ RBS/Lint/UnusedTypeAliasTypeParams:
153
+ Description: 'Check redundant type alias type params'
154
+ Enabled: true
155
+
140
156
  RBS/Lint/WillSyntaxError:
141
157
  Description: 'Check RBS will syntax error'
142
158
  Enabled: true
@@ -150,6 +166,10 @@ RBS/Style/BlockReturnBoolish:
150
166
  Description: 'Use `bool` for block return type'
151
167
  Enabled: true
152
168
 
169
+ RBS/Style/ClassWithSingleton:
170
+ Description: 'Check `class` with singleton context'
171
+ Enabled: true
172
+
153
173
  RBS/Style/ClassicType:
154
174
  Description: 'Use simple type'
155
175
  Enabled: true
@@ -166,6 +186,10 @@ RBS/Style/InitializeReturnType:
166
186
  Description: 'Use `void` for initialize method'
167
187
  Enabled: true
168
188
 
189
+ RBS/Style/InstanceWithInstance:
190
+ Description: 'Check `instance` with instance context'
191
+ Enabled: true
192
+
169
193
  RBS/Style/OptionalNil:
170
194
  Description: 'Use nil instead of nil?'
171
195
  Enabled: true
@@ -58,10 +58,6 @@ module RuboCop
58
58
  decl.members.each { |member| walk_decl(member, &block) }
59
59
  end
60
60
  end
61
-
62
- def tokenize(source)
63
- ::RBS::Parser.lex(source).value
64
- end
65
61
  end
66
62
  end
67
63
  end
@@ -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
- on_type(type)
19
+ check_type(type)
20
20
  end
21
21
  end
22
22
  end
23
23
 
24
- def on_type(type)
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
- on_type(t)
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
@@ -32,6 +32,7 @@ module RuboCop
32
32
  alias on_rbs_global on_rbs_constant
33
33
  alias on_rbs_type_alias on_rbs_constant
34
34
  alias on_rbs_attribute on_rbs_constant
35
+ alias on_rbs_var on_rbs_constant
35
36
 
36
37
  def check_type(type)
37
38
  on_type([::RBS::Types::Union], type) do |union|
@@ -36,6 +36,7 @@ module RuboCop
36
36
  alias on_rbs_global on_rbs_constant
37
37
  alias on_rbs_type_alias on_rbs_constant
38
38
  alias on_rbs_attribute on_rbs_constant
39
+ alias on_rbs_var on_rbs_constant
39
40
 
40
41
  def check_intersection(intersection)
41
42
  intersection.types.each do |type|
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ # rbs_inline: enabled
4
+
5
+ module RuboCop
6
+ module Cop
7
+ module RBS
8
+ module Lint
9
+ # Checks that there are void types in the return type of `.new` method
10
+ # `self.new` is a special and fundamental method, and extra care should be taken regarding its return value.
11
+ # In most cases, assigning it `void` is an unintended mistake.
12
+ #
13
+ # @example default
14
+ # # bad
15
+ # def self.new: () -> void
16
+ #
17
+ # # good
18
+ # def self.new: () -> instance
19
+ #
20
+ class NewReturnsVoid < RuboCop::RBS::CopBase
21
+ MSG = "Don't use `void` in self.new method. Did you mean `instance`?"
22
+
23
+ # @rbs decl: RBS::AST::Members::MethodDefinition
24
+ def on_rbs_def(decl)
25
+ return unless decl.kind == :singleton
26
+ return unless decl.name == :new
27
+
28
+ decl.overloads.each do |overload|
29
+ return_type = overload.method_type.type.return_type
30
+ case return_type
31
+ when ::RBS::Types::Bases::Void
32
+ next unless return_type.location
33
+
34
+ range = location_to_range(return_type.location)
35
+ add_offense(range)
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -35,6 +35,7 @@ module RuboCop
35
35
 
36
36
  def on_rbs_interface(decl)
37
37
  return unless @last_end_pos.nil? || (@last_end_pos < decl.location.start_pos)
38
+ return unless decl.name.namespace.path.empty?
38
39
 
39
40
  range = location_to_range(decl.location)
40
41
  add_offense(range)
@@ -32,6 +32,7 @@ module RuboCop
32
32
 
33
33
  def on_rbs_type_alias(decl)
34
34
  return unless @last_end_pos.nil? || (@last_end_pos < decl.location.start_pos)
35
+ return unless decl.name.namespace.path.empty?
35
36
 
36
37
  range = location_to_range(decl.location)
37
38
  add_offense(range)
@@ -4,7 +4,7 @@ module RuboCop
4
4
  module Cop
5
5
  module RBS
6
6
  module Lint
7
- # Notice useless overload type parameters.
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 UselessOverloadTypeParams < RuboCop::RBS::CopBase
16
- MSG = 'Useless overload type variable - `%<variable>s`.'
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
@@ -45,14 +45,26 @@ module RuboCop
45
45
  check_module_or_class(decl)
46
46
  end
47
47
 
48
- def check_module_or_class(decl)
48
+ def check_type_params(decl)
49
49
  decl.type_params.each do |param|
50
50
  if ub = param.upper_bound
51
51
  void_type_context_validator(ub)
52
52
  no_self_type_validator(ub)
53
53
  no_classish_type_validator(ub)
54
54
  end
55
+
56
+ if param.respond_to?(:default_type)
57
+ if dt = param.default_type
58
+ void_type_context_validator(dt)
59
+ no_self_type_validator(dt)
60
+ no_classish_type_validator(dt)
61
+ end
62
+ end
55
63
  end
64
+ end
65
+
66
+ def check_module_or_class(decl)
67
+ check_type_params(decl)
56
68
 
57
69
  decl.each_member do |member|
58
70
  case member
@@ -79,6 +91,8 @@ module RuboCop
79
91
  end
80
92
 
81
93
  def on_rbs_interface(decl)
94
+ check_type_params(decl)
95
+
82
96
  decl.members.each do |member|
83
97
  case member
84
98
  when AST::Members::MethodDefinition
@@ -96,7 +110,14 @@ module RuboCop
96
110
  void_type_context_validator(decl.type)
97
111
  end
98
112
  alias on_rbs_global on_rbs_constant
99
- alias on_rbs_type_alias on_rbs_constant
113
+
114
+ def on_rbs_type_alias(decl)
115
+ no_self_type_validator(decl.type)
116
+ no_classish_type_validator(decl.type)
117
+ void_type_context_validator(decl.type)
118
+
119
+ check_type_params(decl)
120
+ end
100
121
 
101
122
  private
102
123
 
@@ -166,6 +187,7 @@ module RuboCop
166
187
  end
167
188
  when Types::Proc
168
189
  void_type_context_validator(type.type)
190
+ void_type_context_validator(type.self_type) if type.self_type
169
191
  if type.block
170
192
  void_type_context_validator(type.block.type)
171
193
  void_type_context_validator(type.block.self_type) if type.block.self_type
@@ -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|
@@ -163,6 +163,7 @@ module RuboCop
163
163
  alias on_rbs_global on_rbs_constant
164
164
  alias on_rbs_type_alias on_rbs_constant
165
165
  alias on_rbs_attribute on_rbs_constant
166
+ alias on_rbs_var on_rbs_constant
166
167
  end
167
168
  end
168
169
  end
@@ -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
@@ -156,6 +156,7 @@ module RuboCop
156
156
  alias on_rbs_global on_rbs_constant
157
157
  alias on_rbs_type_alias on_rbs_constant
158
158
  alias on_rbs_attribute on_rbs_constant
159
+ alias on_rbs_var on_rbs_constant
159
160
  end
160
161
  end
161
162
  end
@@ -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,21 +22,26 @@ 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'
29
+ require_relative 'rbs/lint/new_returns_void'
27
30
  require_relative 'rbs/lint/syntax'
28
31
  require_relative 'rbs/lint/top_level_interface'
29
32
  require_relative 'rbs/lint/top_level_type_alias'
33
+ require_relative 'rbs/lint/unused_overload_type_params'
34
+ require_relative 'rbs/lint/unused_type_alias_type_params'
30
35
  require_relative 'rbs/lint/useless_access_modifier'
31
- require_relative 'rbs/lint/useless_overload_type_params'
32
36
  require_relative 'rbs/lint/will_syntax_error'
33
37
 
34
38
  require_relative 'rbs/style/block_return_boolish'
39
+ require_relative 'rbs/style/class_with_singleton'
35
40
  require_relative 'rbs/style/classic_type'
36
41
  require_relative 'rbs/style/duplicated_type'
37
42
  require_relative 'rbs/style/empty_argument'
38
43
  require_relative 'rbs/style/initialize_return_type'
44
+ require_relative 'rbs/style/instance_with_instance'
39
45
  require_relative 'rbs/style/optional_nil'
40
46
  require_relative 'rbs/style/redundant_parentheses'
41
47
  require_relative 'rbs/style/true_false'
@@ -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(with_trivia: false)
27
- @tokens ||= begin
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
@@ -2,6 +2,6 @@
2
2
 
3
3
  module RuboCop
4
4
  module RBS
5
- VERSION = '1.2.0'
5
+ VERSION = '1.4.0'
6
6
  end
7
7
  end
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.2.0
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ksss
8
- autorequire:
9
8
  bindir: exe
10
9
  cert_chain: []
11
- date: 2024-11-18 00:00:00.000000000 Z
10
+ date: 2025-02-23 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: rbs
@@ -77,26 +76,32 @@ 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
90
+ - lib/rubocop/cop/rbs/lint/new_returns_void.rb
89
91
  - lib/rubocop/cop/rbs/lint/syntax.rb
90
92
  - lib/rubocop/cop/rbs/lint/top_level_interface.rb
91
93
  - lib/rubocop/cop/rbs/lint/top_level_type_alias.rb
94
+ - lib/rubocop/cop/rbs/lint/unused_overload_type_params.rb
95
+ - lib/rubocop/cop/rbs/lint/unused_type_alias_type_params.rb
92
96
  - lib/rubocop/cop/rbs/lint/useless_access_modifier.rb
93
- - lib/rubocop/cop/rbs/lint/useless_overload_type_params.rb
94
97
  - lib/rubocop/cop/rbs/lint/will_syntax_error.rb
95
98
  - lib/rubocop/cop/rbs/style/block_return_boolish.rb
99
+ - lib/rubocop/cop/rbs/style/class_with_singleton.rb
96
100
  - lib/rubocop/cop/rbs/style/classic_type.rb
97
101
  - lib/rubocop/cop/rbs/style/duplicated_type.rb
98
102
  - lib/rubocop/cop/rbs/style/empty_argument.rb
99
103
  - lib/rubocop/cop/rbs/style/initialize_return_type.rb
104
+ - lib/rubocop/cop/rbs/style/instance_with_instance.rb
100
105
  - lib/rubocop/cop/rbs/style/optional_nil.rb
101
106
  - lib/rubocop/cop/rbs/style/redundant_parentheses.rb
102
107
  - lib/rubocop/cop/rbs/style/true_false.rb
@@ -115,7 +120,6 @@ metadata:
115
120
  source_code_uri: https://github.com/ksss/rubocop-on-rbs
116
121
  changelog_uri: https://github.com/ksss/rubocop-on-rbs
117
122
  rubygems_mfa_required: 'true'
118
- post_install_message:
119
123
  rdoc_options: []
120
124
  require_paths:
121
125
  - lib
@@ -130,8 +134,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
130
134
  - !ruby/object:Gem::Version
131
135
  version: '0'
132
136
  requirements: []
133
- rubygems_version: 3.5.16
134
- signing_key:
137
+ rubygems_version: 3.6.2
135
138
  specification_version: 4
136
139
  summary: RuboCop extension for RBS file.
137
140
  test_files: []