rubocop-yard 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b62c88ebfb27a008a19460e87ced5cde1352bd778fd7432052606b1220cd82aa
4
- data.tar.gz: 11799a04ee4e9944c1303d5daedab3e09eca7ad01262c155ee9022689acb1d73
3
+ metadata.gz: 35f55b03315d73095819182351a19278528a8d91f5fb71f9644e39734ef0aa89
4
+ data.tar.gz: 731f21ea908307e87b59e36dae70fc9da5ced99157cdc53d0084c077673fd496
5
5
  SHA512:
6
- metadata.gz: 4743f4c79a957b78f28af9639bd78fd06f37c935d52e9123e6a48d2c9280db8132153c2f0f299dba401d0df351858517d5a13b62d4d446042a0ef1419b6aa343
7
- data.tar.gz: 5ea7b97be5355401201a0c50692dd9079211c42b5e99b19c4120d2873708e3a5b464747b75f5fa2e1822779169d3d762cd5c5339bade0bb54b6fef9ec6edb886
6
+ metadata.gz: 378e7d10ff7f50c807a1fe0fe6266e7163ebcece3bc6a4cf340cf98baade320a4785d2520c42aa9319bc20e20642dc46b39311088b58e521f57ee0ce15fab48b
7
+ data.tar.gz: a33bc93f4cb7712b0c73c259d3ecb7f94409973ec73c897494e723c33f5879b7686deba726e7a1f871da20aae3abd46554641c8d1ea29d5c58124cc66646213e
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.4.0] - 2023-09-19
4
+
5
+ - Add new cop `YARD/MeaninglessTag`
6
+
3
7
  ## [0.3.1] - 2023-09-16
4
8
 
5
9
  Fix config/default.yml
data/README.md CHANGED
@@ -12,12 +12,12 @@ Check tag type syntax error.
12
12
 
13
13
  ```
14
14
  # @param [Symbol|String]
15
- # ^^^^^^^^^^^^^ SyntaxError as YARD tag type
15
+ ^^^^^^^^^^^^^ SyntaxError as YARD tag type
16
16
  ```
17
17
 
18
18
  ```
19
19
  # @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}`
21
21
  ```
22
22
 
23
23
  ### `YARD/MismatchName`
@@ -26,12 +26,25 @@ Check `@param` and `@option` name with method definition.
26
26
 
27
27
  ```rb
28
28
  # @param [String] string
29
- # ^^^^^^ `string` is not found in method arguments
29
+ ^^^^^^ `string` is not found in method arguments
30
30
  # @option opt bar [String]
31
- # ^^^ `opt` is not found in method arguments
31
+ ^^^ `opt` is not found in method arguments
32
32
  def foo(strings, opts = {})
33
33
  ```
34
34
 
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
+
35
48
  ## Installation
36
49
 
37
50
  Install the gem and add to the application's Gemfile by executing:
data/config/default.yml CHANGED
@@ -1,3 +1,8 @@
1
+ YARD/MeaninglessTag:
2
+ Description: 'Check meaningless tag'
3
+ Enabled: true
4
+ VersionAdded: '0.4.0'
5
+
1
6
  YARD/TagType:
2
7
  Description: 'Check syntax for yard tag type'
3
8
  Enabled: true
@@ -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
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module YARD
6
- # @example
6
+ # @example mismatch name
7
7
  # # bad
8
8
  # # @param [void] baz
9
9
  # # @option opt aaa [void]
@@ -12,7 +12,7 @@ module RuboCop
12
12
  #
13
13
  # # good
14
14
  # # @param [void] bar
15
- # # @param [Array] argsa
15
+ # # @param [Array] arg
16
16
  # # @option opts aaa [void]
17
17
  # def foo(bar, opts = {}, *arg)
18
18
  # end
@@ -3,37 +3,36 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module YARD
6
- # @example
6
+ # @example tag type
7
7
  # # bad
8
- # @param [Integer String]
8
+ # # @param [Integer String]
9
9
  #
10
10
  # # bad
11
- # @param [Hash<Symbol, String>]
11
+ # # @param [Hash<Symbol, String>]
12
12
  #
13
13
  # # bad
14
- # @param [Hash(String)]
14
+ # # @param [Hash(String)]
15
15
  #
16
16
  # # bad
17
- # @param [Array{Symbol => String}]
17
+ # # @param [Array{Symbol => String}]
18
18
  #
19
19
  # # good
20
- # @param [Integer, String]
20
+ # # @param [Integer, String]
21
21
  #
22
22
  # # good
23
- # @param [<String>]
24
- # @param [Array<String>]
25
- # @param [List<String>]
26
- # @param [Array<(String, Fixnum, Hash)>]
23
+ # # @param [<String>]
24
+ # # @param [Array<String>]
25
+ # # @param [List<String>]
26
+ # # @param [Array<(String, Fixnum, Hash)>]
27
27
  #
28
28
  # # good
29
- # @param [(String)]
30
- # @param [Array(String)]
29
+ # # @param [(String)]
30
+ # # @param [Array(String)]
31
31
  #
32
32
  # # good
33
- # @param [{KeyType => ValueType}]
34
- # @param [Hash{KeyType => ValueType}]
33
+ # # @param [{KeyType => ValueType}]
34
+ # # @param [Hash{KeyType => ValueType}]
35
35
  class TagType < Base
36
- MSG = ''
37
36
  include RangeHelp # @return [void,]
38
37
 
39
38
  def on_new_investigation
@@ -50,13 +49,22 @@ module RuboCop
50
49
  def check(comment)
51
50
  docstring = comment.text.gsub(/\A#\s*/, '')
52
51
  ::YARD::DocstringParser.new.parse(docstring).tags.each do |tag|
53
- next unless tag.types
52
+ types = extract_tag_type(tag)
54
53
 
55
- ::YARD::Tags::TypesExplainer::Parser.parse(tag.types.join(', ')).each do |types_explainer|
56
- check_mismatch_collection_type(comment, types_explainer)
54
+ check_syntax_error(comment) do
55
+ types_explainers = ::YARD::Tags::TypesExplainer::Parser.parse(types.join(', '))
56
+ types_explainers.each do |types_explainer|
57
+ check_mismatch_collection_type(comment, types_explainer)
58
+ end
57
59
  end
58
- rescue SyntaxError
59
- add_offense(tag_range_for_comment(comment), message: 'SyntaxError as YARD tag type')
60
+ end
61
+ end
62
+
63
+ def check_syntax_error(comment)
64
+ begin
65
+ yield
66
+ rescue SyntaxError => e
67
+ add_offense(tag_range_for_comment(comment), message: "(#{e.class}) #{e.message}")
60
68
  end
61
69
  end
62
70
 
@@ -88,12 +96,21 @@ module RuboCop
88
96
  end
89
97
  end
90
98
 
99
+ def extract_tag_type(tag)
100
+ case tag
101
+ when ::YARD::Tags::OptionTag
102
+ tag.pair.types
103
+ else
104
+ tag.types
105
+ end
106
+ end
107
+
91
108
  def inline_comment?(comment)
92
109
  !comment_line?(comment.source_range.source_line)
93
110
  end
94
111
 
95
112
  def include_yard_tag?(comment)
96
- comment.source.match?(/@(?:param|return|option|raise|yieldparam|yieldreturn)\s+\[.*\]/)
113
+ comment.source.match?(/@(?:param|return|option|raise|yieldparam|yieldreturn)\s+.*\[.*\]/)
97
114
  end
98
115
 
99
116
  def tag_range_for_comment(comment)
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'yard'
4
+ require_relative 'yard/meaningless_tag'
4
5
  require_relative 'yard/tag_type'
5
6
  require_relative 'yard/mismatch_name'
@@ -2,6 +2,6 @@
2
2
 
3
3
  module RuboCop
4
4
  module YARD
5
- VERSION = "0.3.1"
5
+ VERSION = "0.4.0"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop-yard
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ksss
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-09-16 00:00:00.000000000 Z
11
+ date: 2023-09-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubocop
@@ -51,6 +51,7 @@ files:
51
51
  - README.md
52
52
  - config/default.yml
53
53
  - lib/rubocop-yard.rb
54
+ - lib/rubocop/cop/yard/meaningless_tag.rb
54
55
  - lib/rubocop/cop/yard/mismatch_name.rb
55
56
  - lib/rubocop/cop/yard/tag_type.rb
56
57
  - lib/rubocop/cop/yard_cops.rb