Skip to content

Commit 7dfcc6b

Browse files
authored
Merge pull request #4 from ksss/meaningless-tag
Implement YARD/MeaninglessTag
2 parents 0e3addd + cf6e896 commit 7dfcc6b

File tree

8 files changed

+152
-6
lines changed

8 files changed

+152
-6
lines changed

README.md

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ Check tag type syntax error.
1212

1313
```
1414
# @param [Symbol|String]
15-
# ^^^^^^^^^^^^^ SyntaxError as YARD tag type
15+
^^^^^^^^^^^^^ SyntaxError as YARD tag type
1616
```
1717

1818
```
1919
# @param [Hash<Symbol, String>]
20-
# ^^^^^^^^^^^^^^^^^^^^ `<Type>` is the collection type syntax. Did you mean `{KeyType => ValueType}` or `Hash{KeyType => ValueType}`
20+
^^^^^^^^^^^^^^^^^^^^ `<Type>` is the collection type syntax. Did you mean `{KeyType => ValueType}` or `Hash{KeyType => ValueType}`
2121
```
2222

2323
### `YARD/MismatchName`
@@ -26,12 +26,25 @@ Check `@param` and `@option` name with method definition.
2626

2727
```rb
2828
# @param [String] string
29-
# ^^^^^^ `string` is not found in method arguments
29+
^^^^^^ `string` is not found in method arguments
3030
# @option opt bar [String]
31-
# ^^^ `opt` is not found in method arguments
31+
^^^ `opt` is not found in method arguments
3232
def foo(strings, opts = {})
3333
```
3434

35+
### `YARD/MeaninglessTag`
36+
37+
Check `@param` and `@option` with class/module or casgn
38+
39+
```rb
40+
# @param [String] foo
41+
^^^^^^^^^^^^^^^^^^^^^ `@param` is meaningless tag on module
42+
module Foo
43+
# @option foo bar [String]
44+
^^^^^^^^^^^^^^^^^^^^^^^^^^ `@option` is meaningless tag on casgn
45+
CONST = 1
46+
```
47+
3548
## Installation
3649

3750
Install the gem and add to the application's Gemfile by executing:

config/default.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
YARD/MeaninglessTag:
2+
Description: 'Check meaningless tag'
3+
Enabled: true
4+
VersionAdded: '0.4.0'
5+
16
YARD/TagType:
27
Description: 'Check syntax for yard tag type'
38
Enabled: true
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# frozen_string_literal: true
2+
3+
module RuboCop
4+
module Cop
5+
module YARD
6+
# @example meaningless tag
7+
# # bad
8+
# # @param [String] foo
9+
# # @option bar baz [String]
10+
# class Foo
11+
#
12+
# # bad
13+
# # @param [String] foo
14+
# # @option bar baz [String]
15+
# CONST = 1
16+
#
17+
# # good
18+
# class Foo
19+
#
20+
# # good
21+
# CONST = 1
22+
class MeaninglessTag < Base
23+
include RangeHelp
24+
include DocumentationComment
25+
26+
def on_class(node)
27+
check(node)
28+
end
29+
alias on_module on_class
30+
alias on_casgn on_class
31+
32+
def check(node)
33+
preceding_lines = preceding_lines(node)
34+
return false unless preceding_comment?(node, preceding_lines.last)
35+
36+
yard_docstring = preceding_lines.map { |line| line.text.gsub(/\A#\s*/, '') }.join("\n")
37+
docstring = ::YARD::DocstringParser.new.parse(yard_docstring)
38+
docstring.tags.each do |tag|
39+
next unless tag.tag_name == 'param' || tag.tag_name == 'option'
40+
41+
comment = preceding_lines.find { |line| line.text.include?("@#{tag.tag_name}") }
42+
next unless comment
43+
44+
add_offense(comment, message: "`@#{tag.tag_name}` is meaningless tag on #{node.type}")
45+
end
46+
end
47+
end
48+
end
49+
end
50+
end

lib/rubocop/cop/yard/mismatch_name.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
module RuboCop
44
module Cop
55
module YARD
6-
# @example
6+
# @example mismatch name
77
# # bad
88
# # @param [void] baz
99
# # @option opt aaa [void]

lib/rubocop/cop/yard/tag_type.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
module RuboCop
44
module Cop
55
module YARD
6-
# @example
6+
# @example tag type
77
# # bad
88
# # @param [Integer String]
99
#

lib/rubocop/cop/yard_cops.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# frozen_string_literal: true
22

33
require 'yard'
4+
require_relative 'yard/meaningless_tag'
45
require_relative 'yard/tag_type'
56
require_relative 'yard/mismatch_name'

smoke/meaningless_tag.json

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
{
2+
"metadata": {
3+
"rubocop_version": "1.56.2",
4+
"ruby_engine": "ruby",
5+
"ruby_version": "3.2.2",
6+
"ruby_patchlevel": "53",
7+
"ruby_platform": "arm64-darwin22"
8+
},
9+
"files": [
10+
{
11+
"path": "smoke/meaningless_tag.rb",
12+
"offenses": [
13+
{
14+
"severity": "convention",
15+
"message": "`@param` is meaningless tag on module",
16+
"cop_name": "YARD/MeaninglessTag",
17+
"corrected": false,
18+
"correctable": false,
19+
"location": {
20+
"start_line": 1,
21+
"start_column": 1,
22+
"last_line": 1,
23+
"last_column": 19,
24+
"length": 19,
25+
"line": 1,
26+
"column": 1
27+
}
28+
},
29+
{
30+
"severity": "convention",
31+
"message": "`@option` is meaningless tag on class",
32+
"cop_name": "YARD/MeaninglessTag",
33+
"corrected": false,
34+
"correctable": false,
35+
"location": {
36+
"start_line": 3,
37+
"start_column": 3,
38+
"last_line": 3,
39+
"last_column": 19,
40+
"length": 17,
41+
"line": 3,
42+
"column": 3
43+
}
44+
},
45+
{
46+
"severity": "convention",
47+
"message": "`@option` is meaningless tag on casgn",
48+
"cop_name": "YARD/MeaninglessTag",
49+
"corrected": false,
50+
"correctable": false,
51+
"location": {
52+
"start_line": 5,
53+
"start_column": 5,
54+
"last_line": 5,
55+
"last_column": 21,
56+
"length": 17,
57+
"line": 5,
58+
"column": 5
59+
}
60+
}
61+
]
62+
}
63+
],
64+
"summary": {
65+
"offense_count": 3,
66+
"target_file_count": 1,
67+
"inspected_file_count": 1
68+
}
69+
}

smoke/meaningless_tag.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# @param [void] foo
2+
module Foo
3+
# @option aaa bbb
4+
class Bar
5+
# @option ccc ddd
6+
CONST = 1
7+
end
8+
end

0 commit comments

Comments
 (0)