rubocop-rbs_inline 1.5.0.pre.2 → 1.5.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: 813f81c0980296d50e92c6a928f3ac51e3954748095a1e0dd6117cdcf77a8166
4
- data.tar.gz: '029b0e05c922841def3501e57587045660c4d0f05752dba974797f7215661b62'
3
+ metadata.gz: 1d23ee9bd49cabe4e0b153c7d1423768c42172f800fecd4c8a1786dc9c110078
4
+ data.tar.gz: 7605f5de5e26c0ea0134f5a842503beaa49b5099ec4bf9c149a149342be15b84
5
5
  SHA512:
6
- metadata.gz: 2768b2232009f4c70366981b7ffc47ef55edef17f6938f31d0740a645189a71fea2666e64edaf005cb27489ea18f2038f80c917240bc861fa8b60672e2286dbc
7
- data.tar.gz: 42191af687cb627862f2ee617579df7932c65b656d35c7c4651453dc2c58106f28f73b2794107c3eb99a4d66bab141b43015072493bd19b60e3a8bc27f7d4f76
6
+ metadata.gz: 2cc7f50364b7bdb9db4258aadb53cc9b88e4f5c1e4de691ab0e9630df5821e91ce07d4935ef5784a09a9667d9e3042566293921f710c9312a3c0231775e7a735
7
+ data.tar.gz: c396f462d1d3ab5e8342595e392e65f9105e44b8c8457ad477a15415122a364969e04324c597b5d68b25c58fd43fb8bdb1fdade219951e8dce888df89d676138
data/CHANGELOG.md CHANGED
@@ -1,12 +1,12 @@
1
1
  # Changelog
2
2
 
3
- ## 1.5.0 (unreleased)
3
+ ## 1.5.0 (2026-02-24)
4
4
 
5
5
  ### New Cops
6
6
 
7
+ - **Style/RbsInline/UntypedInstanceVariable**: Warns when an instance variable used in a class or module does not have an RBS type annotation. An instance variable is considered typed when a `# @rbs @ivar: Type` annotation exists in the class body, or when it is covered by an `attr_reader`, `attr_writer`, or `attr_accessor` declaration with an inline `#: Type` comment.
7
8
  - **Style/RbsInline/RedundantAnnotationWithSkip**: Warns when type annotations (`#:` method type signatures, `# @rbs` method types, parameter annotations, return type annotations, or trailing inline types) are present alongside `# @rbs skip` or `# @rbs override`. These directives skip RBS generation, making any additional type annotations redundant. Supports unsafe autocorrect.
8
- - **Style/RbsInline/RedundantReturnType**: Warns when both a `# @rbs return` comment and a `#:` annotation comment specify the return type for the same method. The `EnforcedStyle` option accepts `inline_comment` (prefer `#:`) or `rbs_return_comment` (prefer `# @rbs return`). Supports safe autocorrect.
9
- - **Style/RbsInline/RedundantArgumentType**: Warns when both a `# @rbs param` comment and a `#:` annotation comment specify the argument type for the same method. The `EnforcedStyle` option accepts `annotation_comment` or `rbs_param_comment` (default). Supports autocorrect when `annotation_comment` style is preferred.
9
+ - **Style/RbsInline/RedundantTypeAnnotation**: Warns when redundant type annotations exist for the same method definition. Detects conflicts between `#:` method type signatures, `# @rbs param:` annotations, `# @rbs return:` annotations, and trailing inline `#:` return types. The `EnforcedStyle` option accepts `method_type_signature` (prefer `#:` signatures), `doc_style` (prefer `# @rbs` annotations), or `doc_style_and_return_annotation` (prefer `# @rbs` params with trailing `#:` return type, default: `doc_style`). Supports unsafe autocorrect.
10
10
  - **Style/RbsInline/MissingTypeAnnotation**: Warns when a method definition has no type annotation. The `Visibility` option narrows the target methods by visibility (default: `all`).
11
11
  - **Style/RbsInline/RequireRbsInlineComment**: Enforces the presence or absence of a `# rbs_inline:` magic comment. The `EnforcedStyle` option accepts `always` (default, requires the comment) or `never` (forbids the comment).
12
12
  - **Style/RbsInline/EmbeddedRbsSpacing**: Warns when a `@rbs!` embedded RBS comment block is not followed by a blank line. Supports autocorrect.
@@ -14,6 +14,8 @@
14
14
  - **Style/RbsInline/MethodCommentSpacing**: Warns when a method-related `@rbs` annotation (`param`, `return`, `&block`, `override`, `skip`, etc.) is not placed immediately before the method definition it describes.
15
15
  - **Style/RbsInline/MissingDataClassAnnotation**: Warns when an attribute passed to `Data.define` does not have a trailing `#:` inline type annotation. Supports autocorrect.
16
16
  - **Style/RbsInline/DataClassCommentAlignment**: Warns when the `#:` inline type annotation comments in a multiline `Data.define` block are not aligned to the same column. Supports autocorrect.
17
+ - **Style/RbsInline/DataDefineWithBlock**: Warns when `Data.define` is called with a block. RBS::Inline does not parse block contents, so methods defined inside will not be recognized. Users should call `Data.define` without a block and reopen the class separately to add methods.
18
+ - **Style/RbsInline/RedundantInstanceVariableAnnotation**: Warns when a `# @rbs @ivar: Type` instance variable type annotation is redundant because an `attr_*` with an inline type annotation already exists for the same attribute. Supports autocorrect.
17
19
 
18
20
  ### Enhancements
19
21
 
data/README.md CHANGED
@@ -27,6 +27,52 @@ plugins:
27
27
 
28
28
  rubocop-rbs_inline provides the following cops to validate [RBS::Inline](https://github.com/soutaro/rbs-inline) annotations:
29
29
 
30
+ ### Style/RbsInline/DataClassCommentAlignment
31
+
32
+ Checks that `#:` inline type annotations in a multiline `Data.define` call are aligned to the same column. The expected column is determined by the longest attribute name (plus its trailing comma). Folded `Data.define` calls (where multiple attributes share a line) are excluded.
33
+
34
+ Supports autocorrect.
35
+
36
+ **Examples:**
37
+ ```ruby
38
+ # bad
39
+ MethodEntry = Data.define(
40
+ :name, #: Symbol
41
+ :node, #: Parser::AST::Node
42
+ :visibility #: Symbol
43
+ )
44
+
45
+ # good
46
+ MethodEntry = Data.define(
47
+ :name, #: Symbol
48
+ :node, #: Parser::AST::Node
49
+ :visibility #: Symbol
50
+ )
51
+ ```
52
+
53
+ ### Style/RbsInline/DataDefineWithBlock
54
+
55
+ Checks for `Data.define` calls with a block. RBS::Inline does not parse block contents, so any methods defined inside the block will not be recognized for type checking. Instead, call `Data.define` without a block and reopen the class separately to add methods.
56
+
57
+ **Examples:**
58
+ ```ruby
59
+ # bad
60
+ User = Data.define(:name, :role) do
61
+ def admin?
62
+ role == :admin
63
+ end
64
+ end
65
+
66
+ # good
67
+ User = Data.define(:name, :role)
68
+
69
+ class User
70
+ def admin? #: bool
71
+ role == :admin
72
+ end
73
+ end
74
+ ```
75
+
30
76
  ### Style/RbsInline/EmbeddedRbsSpacing
31
77
 
32
78
  Checks that `@rbs!` comments (embedded RBS) are followed by a blank line.
@@ -91,6 +137,67 @@ Ensures RBS keywords (`module-self`, `inherits`, `override`, etc.) are not follo
91
137
  # @rbs module-self String
92
138
  ```
93
139
 
140
+ ### Style/RbsInline/MethodCommentSpacing
141
+
142
+ Checks that method-related `@rbs` annotations are placed immediately before their method definition, with no blank lines in between. Also flags method-related annotations that are not followed by a method definition at all.
143
+
144
+ Method-related annotations include `# @rbs param:`, `# @rbs return:`, `# @rbs &block:`, `# @rbs override`, `# @rbs skip`, `# @rbs %a{...}`, `# @rbs (...) -> Type`, and `#: (...) -> Type`.
145
+
146
+ Supports autocorrect (removes blank lines between the annotation and the method definition).
147
+
148
+ **Examples:**
149
+ ```ruby
150
+ # bad - blank line between annotation and method
151
+ # @rbs x: Integer
152
+ # @rbs return: String
153
+
154
+ def method(x)
155
+ end
156
+
157
+ # bad - annotation comment not followed by a method definition
158
+ # @rbs x: Integer
159
+ puts "something"
160
+
161
+ # good
162
+ # @rbs x: Integer
163
+ # @rbs return: String
164
+ def method(x)
165
+ end
166
+
167
+ # good
168
+ #: (Integer) -> String
169
+ def method(x)
170
+ end
171
+ ```
172
+
173
+ ### Style/RbsInline/MissingDataClassAnnotation
174
+
175
+ Checks that each attribute passed to `Data.define` has a trailing `#:` inline type annotation on the same line.
176
+
177
+ For folded `Data.define` calls (where multiple attributes share a line), the cop will suggest rewriting as a multiline call so each attribute can be annotated individually.
178
+
179
+ Supports autocorrect.
180
+
181
+ **Examples:**
182
+ ```ruby
183
+ # bad
184
+ MethodEntry = Data.define(:name, :node, :visibility)
185
+
186
+ # bad - missing annotation for :node
187
+ MethodEntry = Data.define(
188
+ :name, #: Symbol
189
+ :node,
190
+ :visibility #: Symbol
191
+ )
192
+
193
+ # good
194
+ MethodEntry = Data.define(
195
+ :name, #: Symbol
196
+ :node, #: Parser::AST::Node
197
+ :visibility #: Symbol
198
+ )
199
+ ```
200
+
94
201
  ### Style/RbsInline/MissingTypeAnnotation
95
202
 
96
203
  Enforces that method definitions and `attr_*` declarations have RBS inline type annotations.
@@ -208,86 +315,95 @@ def method(a)
208
315
  end
209
316
  ```
210
317
 
211
- ### Style/RbsInline/RedundantArgumentType
318
+ ### Style/RbsInline/RedundantInstanceVariableAnnotation
212
319
 
213
- Detects redundant argument type specifications when both `#:` annotation comments and `# @rbs` parameter comments exist.
320
+ Warns when a `# @rbs @ivar: Type` instance variable type annotation is redundant because an `attr_*` with an inline type annotation already exists for the same attribute.
214
321
 
215
- **Configuration:** `EnforcedStyle` (default: `doc_style`)
216
- - `method_type_signature`: Prefers `#:` annotation comments with parameter types
217
- - `doc_style`: Prefers `# @rbs param:` annotations
322
+ Supports autocorrect (removes the redundant instance variable type annotation).
218
323
 
219
- **Examples (EnforcedStyle: doc_style):**
324
+ **Examples:**
220
325
  ```ruby
221
- # bad - both annotation comment and @rbs param
222
- # @rbs a: Integer
223
- #: (Integer) -> void
224
- def method(a)
225
- end
326
+ # bad
327
+ # @rbs @foo: Integer
328
+
329
+ attr_reader :foo #: Integer
226
330
 
227
331
  # good
228
- # @rbs a: Integer
229
- def method(a) #: void
230
- end
332
+ attr_reader :foo #: Integer
333
+
334
+ # good - no inline annotation, so ivar annotation is not redundant
335
+ # @rbs @foo: Integer
336
+
337
+ attr_reader :foo
231
338
  ```
232
339
 
340
+ ### Style/RbsInline/RedundantTypeAnnotation
341
+
342
+ Detects redundant type annotations when multiple type specifications exist for the same method. This covers both redundant argument type annotations (when both `#:` and `# @rbs param` specify the same parameter) and redundant return type annotations (when multiple of `#:`, trailing `#:`, and `# @rbs return` specify the return type).
343
+
344
+ Supports unsafe autocorrect.
345
+
346
+ **Configuration:** `EnforcedStyle` (default: `doc_style`)
347
+ - `method_type_signature`: Prefers `#:` annotation comments with the full method signature; `# @rbs param:` and `# @rbs return:` annotations alongside a `#:` signature are redundant
348
+ - `doc_style`: Prefers `# @rbs` annotations; `#:` method type signatures alongside `# @rbs` annotations are redundant
349
+ - `doc_style_and_return_annotation`: Prefers `# @rbs param:` annotations with a trailing inline `#:` return type; full `#:` signatures and `# @rbs return:` annotations are redundant
350
+
233
351
  **Examples (EnforcedStyle: method_type_signature):**
234
352
  ```ruby
235
- # bad - both annotation comment and @rbs param
353
+ # bad - redundant @rbs parameter annotation
236
354
  # @rbs a: Integer
237
355
  #: (Integer) -> void
238
356
  def method(a)
239
357
  end
240
358
 
241
- # good
242
- #: (Integer) -> void
243
- def method(a)
244
- end
245
- ```
246
-
247
- ### Style/RbsInline/RedundantReturnType
248
-
249
- Detects redundant return type specifications when multiple return type annotations exist.
250
-
251
- **Configuration:** `EnforcedStyle` (default: `return_type_annotation`)
252
- - `method_type_signature`: Prefers `#:` annotation comments before the method
253
- - `return_type_annotation`: Prefers inline `#:` return type on the def line
254
- - `doc_style`: Prefers `# @rbs return:` annotations
255
-
256
- **Examples (EnforcedStyle: return_type_annotation):**
257
- ```ruby
258
- # bad - multiple return type specifications
359
+ # bad - redundant trailing return type
259
360
  #: () -> String
260
361
  def method(arg) #: String
261
362
  end
262
363
 
263
- # good - single inline return type
264
- def method(arg) #: String
364
+ # good
365
+ #: (Integer) -> String
366
+ def method(a)
265
367
  end
266
368
  ```
267
369
 
268
- **Examples (EnforcedStyle: method_type_signature):**
370
+ **Examples (EnforcedStyle: doc_style):**
269
371
  ```ruby
270
- # bad - multiple return type specifications
271
- #: () -> String
372
+ # bad - redundant #: method type signature
373
+ # @rbs a: Integer
374
+ #: (Integer) -> void
375
+ def method(a)
376
+ end
377
+
378
+ # bad - redundant trailing return type
379
+ # @rbs return: String
272
380
  def method(arg) #: String
273
381
  end
274
382
 
275
- # good - annotation comment with return type
276
- #: () -> String
277
- def method(arg)
383
+ # good
384
+ # @rbs a: Integer
385
+ # @rbs return: String
386
+ def method(a)
278
387
  end
279
388
  ```
280
389
 
281
- **Examples (EnforcedStyle: doc_style):**
390
+ **Examples (EnforcedStyle: doc_style_and_return_annotation):**
282
391
  ```ruby
283
- # bad - multiple return type specifications
284
- # @rbs return: String
285
- def method(arg) #: String
392
+ # bad - redundant #: method type signature
393
+ # @rbs a: Integer
394
+ #: (Integer) -> String
395
+ def method(a)
286
396
  end
287
397
 
288
- # good - @rbs return annotation
398
+ # bad - redundant @rbs return annotation
399
+ # @rbs a: Integer
289
400
  # @rbs return: String
290
- def method(arg)
401
+ def method(a) #: String
402
+ end
403
+
404
+ # good
405
+ # @rbs a: Integer
406
+ def method(a) #: String
291
407
  end
292
408
  ```
293
409
 
@@ -326,16 +442,69 @@ def method(arg); end
326
442
  def method(arg); end
327
443
  ```
328
444
 
445
+ ### Style/RbsInline/UntypedInstanceVariable
446
+
447
+ Warns when an instance variable used in a class or module does not have an RBS type annotation. An instance variable is considered typed when a `# @rbs @ivar: Type` annotation exists in the class body, or when it is covered by an `attr_reader`, `attr_writer`, or `attr_accessor` declaration with an inline `#: Type` comment.
448
+
449
+ **Examples:**
450
+ ```ruby
451
+ # bad
452
+ class Foo
453
+ def bar
454
+ @baz
455
+ end
456
+ end
457
+
458
+ # good
459
+ class Foo
460
+ # @rbs @baz: Integer
461
+
462
+ def bar
463
+ @baz
464
+ end
465
+ end
466
+
467
+ # good
468
+ class Foo
469
+ attr_reader :baz #: Integer
470
+
471
+ def bar
472
+ @baz
473
+ end
474
+ end
475
+ ```
476
+
477
+ ### Style/RbsInline/VariableCommentSpacing
478
+
479
+ Checks that `@rbs` variable comments for instance variables (`@ivar`), class variables (`@@cvar`), and class instance variables (`self.@civar`) are followed by a blank line. RBS::Inline requires these comments to be standalone, so code must not immediately follow them.
480
+
481
+ Supports autocorrect.
482
+
483
+ **Examples:**
484
+ ```ruby
485
+ # bad
486
+ # @rbs @ivar: Integer
487
+ # @rbs @@cvar: Float
488
+ # @rbs self.@civar: String
489
+ def method
490
+ end
491
+
492
+ # good
493
+ # @rbs @ivar: Integer
494
+ # @rbs @@cvar: Float
495
+ # @rbs self.@civar: String
496
+
497
+ def method
498
+ end
499
+ ```
500
+
329
501
  ## Configuration
330
502
 
331
503
  You can customize cop behavior in your `.rubocop.yml`. For example:
332
504
 
333
505
  ```yaml
334
506
  # Prefer method type signatures over doc-style annotations
335
- Style/RbsInline/RedundantArgumentType:
336
- EnforcedStyle: method_type_signature
337
-
338
- Style/RbsInline/RedundantReturnType:
507
+ Style/RbsInline/RedundantTypeAnnotation:
339
508
  EnforcedStyle: method_type_signature
340
509
 
341
510
  # Only require annotations on public methods
data/config/default.yml CHANGED
@@ -3,8 +3,8 @@ Style/RbsInline/DataClassCommentAlignment:
3
3
  Enabled: true
4
4
  VersionAdded: '1.5.0'
5
5
 
6
- Style/RbsInline/MissingDataClassAnnotation:
7
- Description: "Checks that `Data.define` attributes have inline type annotations."
6
+ Style/RbsInline/DataDefineWithBlock:
7
+ Description: "Checks for `Data.define` calls with a block, which RBS::Inline does not parse."
8
8
  Enabled: true
9
9
  VersionAdded: '1.5.0'
10
10
 
@@ -23,11 +23,21 @@ Style/RbsInline/InvalidTypes:
23
23
  Enabled: true
24
24
  VersionAdded: "1.0.0"
25
25
 
26
+ Style/RbsInline/KeywordSeparator:
27
+ Description: "Checks the rbs-inline annotation comment does not use a separator after the keywords."
28
+ Enabled: true
29
+ VersionAdded: "1.0.0"
30
+
26
31
  Style/RbsInline/MethodCommentSpacing:
27
32
  Description: 'Checks that method-related `@rbs` annotations are placed immediately before method definitions.'
28
33
  Enabled: true
29
34
  VersionAdded: '1.5.0'
30
35
 
36
+ Style/RbsInline/MissingDataClassAnnotation:
37
+ Description: "Checks that `Data.define` attributes have inline type annotations."
38
+ Enabled: true
39
+ VersionAdded: '1.5.0'
40
+
31
41
  Style/RbsInline/MissingTypeAnnotation:
32
42
  Description: "Checks that method definitions and attr_* declarations have RBS inline type annotations."
33
43
  Enabled: true
@@ -39,11 +49,6 @@ Style/RbsInline/MissingTypeAnnotation:
39
49
  - doc_style_and_return_annotation
40
50
  Visibility: all
41
51
 
42
- Style/RbsInline/KeywordSeparator:
43
- Description: "Checks the rbs-inline annotation comment does not use a separator after the keywords."
44
- Enabled: true
45
- VersionAdded: "1.0.0"
46
-
47
52
  Style/RbsInline/ParametersSeparator:
48
53
  Description: "Checks the rbs-inline annotation comment uses a separator to parameters"
49
54
  Enabled: true
@@ -55,6 +60,11 @@ Style/RbsInline/RedundantAnnotationWithSkip:
55
60
  SafeAutoCorrect: false
56
61
  VersionAdded: '1.5.0'
57
62
 
63
+ Style/RbsInline/RedundantInstanceVariableAnnotation:
64
+ Description: "Checks for redundant `@rbs` instance variable type annotation when `attr_*` with an inline type annotation is already defined."
65
+ Enabled: true
66
+ VersionAdded: '1.5.0'
67
+
58
68
  Style/RbsInline/RedundantTypeAnnotation:
59
69
  Description: "Checks for redundant type annotations when multiple type specifications exist for the same method definition."
60
70
  Enabled: true
@@ -80,6 +90,11 @@ Style/RbsInline/UnmatchedAnnotations:
80
90
  Enabled: true
81
91
  VersionAdded: "1.0.0"
82
92
 
93
+ Style/RbsInline/UntypedInstanceVariable:
94
+ Description: "Checks that instance variables in classes/modules have RBS type annotations."
95
+ Enabled: true
96
+ VersionAdded: '1.5.0'
97
+
83
98
  Style/RbsInline/VariableCommentSpacing:
84
99
  Description: 'Checks that `@rbs` variable comments are followed by a blank line.'
85
100
  Enabled: true
@@ -2,17 +2,20 @@
2
2
 
3
3
  require_relative 'style/rbs_inline/source_code_helper'
4
4
  require_relative 'style/rbs_inline/comment_parser'
5
+ require_relative 'style/rbs_inline/data_class_comment_alignment'
6
+ require_relative 'style/rbs_inline/data_define_with_block'
5
7
  require_relative 'style/rbs_inline/embedded_rbs_spacing'
6
8
  require_relative 'style/rbs_inline/invalid_comment'
7
9
  require_relative 'style/rbs_inline/invalid_types'
8
10
  require_relative 'style/rbs_inline/keyword_separator'
9
11
  require_relative 'style/rbs_inline/method_comment_spacing'
10
- require_relative 'style/rbs_inline/data_class_comment_alignment'
11
12
  require_relative 'style/rbs_inline/missing_data_class_annotation'
12
13
  require_relative 'style/rbs_inline/missing_type_annotation'
13
14
  require_relative 'style/rbs_inline/parameters_separator'
14
15
  require_relative 'style/rbs_inline/redundant_annotation_with_skip'
16
+ require_relative 'style/rbs_inline/redundant_instance_variable_annotation'
15
17
  require_relative 'style/rbs_inline/redundant_type_annotation'
16
18
  require_relative 'style/rbs_inline/require_rbs_inline_comment'
17
19
  require_relative 'style/rbs_inline/unmatched_annotations'
20
+ require_relative 'style/rbs_inline/untyped_instance_variable'
18
21
  require_relative 'style/rbs_inline/variable_comment_spacing'
@@ -80,14 +80,17 @@ module RuboCop
80
80
 
81
81
  # Find @rbs parameter annotations before a method definition
82
82
  # @rbs def_line: Integer
83
- def find_doc_style_param_annotations(def_line) #: Array[RBS::Inline::AST::Annotations::VarType | RBS::Inline::AST::Annotations::BlockType]?
83
+ # rubocop:disable Layout/LineLength
84
+ def find_doc_style_param_annotations(def_line) #: Array[RBS::Inline::AST::Annotations::VarType | RBS::Inline::AST::Annotations::BlockType | RBS::Inline::AST::Annotations::SplatParamType | RBS::Inline::AST::Annotations::DoubleSplatParamType]?
85
+ # rubocop:enable Layout/LineLength
84
86
  leading_annotation = find_leading_annotation(def_line)
85
87
  return unless leading_annotation
86
88
 
87
- annotations = [] #: Array[RBS::Inline::AST::Annotations::VarType | RBS::Inline::AST::Annotations::BlockType]
89
+ annotations = [] #: Array[RBS::Inline::AST::Annotations::VarType | RBS::Inline::AST::Annotations::BlockType | RBS::Inline::AST::Annotations::SplatParamType | RBS::Inline::AST::Annotations::DoubleSplatParamType] # rubocop:disable Layout/LineLength
88
90
  leading_annotation.each_annotation do |annotation|
89
91
  case annotation
90
- when RBS::Inline::AST::Annotations::VarType, RBS::Inline::AST::Annotations::BlockType
92
+ when RBS::Inline::AST::Annotations::VarType, RBS::Inline::AST::Annotations::BlockType,
93
+ RBS::Inline::AST::Annotations::SplatParamType, RBS::Inline::AST::Annotations::DoubleSplatParamType
91
94
  annotations << annotation
92
95
  end
93
96
  end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Style
6
+ module RbsInline
7
+ # Checks for `Data.define` calls with a block.
8
+ #
9
+ # RBS::Inline does not parse the contents of `Data.define` blocks, so any
10
+ # methods defined inside will not be recognized for type checking. Instead,
11
+ # call `Data.define` without a block and define additional methods by
12
+ # reopening the class separately.
13
+ #
14
+ # NOTE: This is a known limitation of RBS::Inline. See
15
+ # https://github.com/soutaro/rbs-inline/pull/183 for the upstream fix.
16
+ #
17
+ # @example
18
+ # # bad
19
+ # User = Data.define(:name, :role) do
20
+ # def admin? = role == :admin #: bool
21
+ # end
22
+ #
23
+ # # good
24
+ # User = Data.define(:name, :role)
25
+ #
26
+ # class User
27
+ # def admin? = role == :admin #: bool
28
+ # end
29
+ #
30
+ class DataDefineWithBlock < Base
31
+ MSG = 'Do not use `Data.define` with a block. RBS::Inline does not parse block contents, ' \
32
+ 'so methods defined in the block will not be recognized. ' \
33
+ 'Use a separate class definition instead.'
34
+
35
+ # @rbs node: RuboCop::AST::SendNode
36
+ def on_send(node) #: void
37
+ return unless data_define?(node)
38
+
39
+ block_node = node.parent
40
+ return unless block_node&.block_type?
41
+
42
+ add_offense(node)
43
+ end
44
+
45
+ private
46
+
47
+ # @rbs node: RuboCop::AST::SendNode
48
+ def data_define?(node) #: bool
49
+ return false unless node.method_name == :define
50
+
51
+ receiver = node.receiver
52
+ receiver.is_a?(RuboCop::AST::ConstNode) && receiver.short_name == :Data
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
@@ -29,6 +29,8 @@ module RuboCop
29
29
  # Only `# @rbs override: SomeType` (with a type) is valid as a parameter annotation.
30
30
  NO_ARGUMENT_KEYWORDS = %w[override skip].freeze #: Array[String]
31
31
 
32
+ # @rbs @method_annotation_lines: Set[Integer]
33
+
32
34
  def on_new_investigation #: void
33
35
  super
34
36
  parse_comments