rubocop-yard 0.5.0 → 0.6.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: d4dd27dab805f9450c5043686ad6a530235d31e2a148e1913db76288fa51a39e
4
- data.tar.gz: 2820aa0509d5ff6ef0adecbc772168cd8f2b64650109feb4bfb38e810625812a
3
+ metadata.gz: e5f138fd3b5b0c5eb5550b368164986ba104280ebb40af21d7b8fe843105dad3
4
+ data.tar.gz: 2a237052e98046b9ac7ffcd72100fd1d96ad90164d8075e8f997981c73a78f98
5
5
  SHA512:
6
- metadata.gz: be2c582e52297d345a8d09f413d98e15c7724b4adbd288fa5bfa38b280e9b5c752dd549885e6ec521bcd4c9dd335a71a9210475ef585d472332c2dd30066350c
7
- data.tar.gz: c239f115e44518f79edb4da03cbf54c4c3e6d66659e1ff51ede40047fb5d7af6944dd4a0e08cc8e9bad53c88ae0d7869a4071655105c40704cab44a7eda10620
6
+ metadata.gz: fe3818daae4c2d4597a86ad8f1e1e6785a56e5c34e5e96b0b78be0b52b7cb219ce4d9f390ebac30af9ec844d5f2add8f4c7dfa7d5d4e6496543a0ab6a880980d
7
+ data.tar.gz: fd20c16153f50987d92fe2c5a6af00ecf1fd274d29205d57e63c8c176a314555de9f9e926ae1bad5b7c5b18bf3d1e7adcca26e8ead35b0f8eacc55744fb4cabb
data/CHANGELOG.md CHANGED
@@ -1,8 +1,6 @@
1
1
  ## [Unreleased]
2
2
 
3
- ## [0.5.0] - 2023-10-9
4
-
5
- - Add new cop `YARD/CollectionStyle`
3
+ ## [0.6.0] - 2023-10-10
6
4
 
7
5
  - Split cop from `YARD/TagType` to
8
6
  - `YARD/TagTypeSyntax`
@@ -42,7 +42,7 @@ module RuboCop
42
42
  # # good
43
43
  # # @param [Array<String>]
44
44
  class CollectionStyle < Base
45
- include YARD::CollectionHelper
45
+ include YARD::Helper
46
46
  include RangeHelp
47
47
  include ConfigurableEnforcedStyle
48
48
  extend AutoCorrector
@@ -62,7 +62,7 @@ module RuboCop
62
62
  docstring = ::YARD::DocstringParser.new.parse(comment.text.gsub(/\A#\s*/, ''))
63
63
  each_types_explainer(docstring) do |type, types_explainer|
64
64
  correct_type = styled_string(types_explainer)
65
- unless type == correct_type
65
+ unless ignore_whitespace(type) == ignore_whitespace(correct_type)
66
66
  add_offense(comment, message: "`#{type}` is using #{bad_style} style syntax") do |corrector|
67
67
  corrector.replace(comment, comment.source.sub(/\[(.*)\]/) { "[#{correct_type}]" })
68
68
  end
@@ -70,6 +70,10 @@ module RuboCop
70
70
  end
71
71
  end
72
72
 
73
+ def ignore_whitespace(str)
74
+ str.tr(' ', '')
75
+ end
76
+
73
77
  def bad_style
74
78
  if style == :long
75
79
  :short
@@ -22,7 +22,7 @@ module RuboCop
22
22
  # # good
23
23
  # # @param [Hash{Symbol => String}]
24
24
  class CollectionType < Base
25
- include YARD::CollectionHelper
25
+ include YARD::Helper
26
26
  include RangeHelp
27
27
  include ConfigurableEnforcedStyle
28
28
  extend AutoCorrector
@@ -86,8 +86,7 @@ module RuboCop
86
86
  case types_explainer.name
87
87
  when 'Hash'
88
88
  if types_explainer.types.length == 2
89
- # `Hash<Key, Value>` pattern is the documented hash specific syntax.
90
- message = "`Hash<Key, Value>` is the documented hash specific syntax"
89
+ message = "`Hash<Key, Value>` is ambiguous syntax"
91
90
  add_offense(tag_range_for_comment(comment), message: message) do |corrector|
92
91
  hash_type = ::YARD::Tags::TypesExplainer::HashCollectionType.new(
93
92
  'Hash',
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module YARD
6
- module CollectionHelper
6
+ module Helper
7
7
  def extract_tag_types(tag)
8
8
  case tag
9
9
  when ::YARD::Tags::OptionTag
@@ -13,12 +13,16 @@ module RuboCop
13
13
  end
14
14
  end
15
15
 
16
+ def parse_type(type)
17
+ ::YARD::Tags::TypesExplainer::Parser.parse(type)
18
+ end
19
+
16
20
  def each_types_explainer(docstring, &block)
17
21
  docstring.tags.each do |tag|
18
22
  types = extract_tag_types(tag)
19
23
 
20
24
  begin
21
- types_explainers = ::YARD::Tags::TypesExplainer::Parser.parse(types.join(', '))
25
+ types_explainers = parse_type(types.join(', '))
22
26
  types.zip(types_explainers).each do |type, types_explainer|
23
27
  block.call(type, types_explainer)
24
28
  end
@@ -17,6 +17,7 @@ module RuboCop
17
17
  # def foo(bar, opts = {}, *arg)
18
18
  # end
19
19
  class MismatchName < Base
20
+ include YARD::Helper
20
21
  include RangeHelp
21
22
  include DocumentationComment
22
23
 
@@ -28,31 +29,49 @@ module RuboCop
28
29
 
29
30
  yard_docstring = preceding_lines.map { |line| line.text.gsub(/\A#\s*/, '') }.join("\n")
30
31
  docstring = ::YARD::DocstringParser.new.parse(yard_docstring)
31
- docstring.tags.each_with_index do |tag, i|
32
- next unless tag.tag_name == 'param' || tag.tag_name == 'option'
32
+ return false if include_overload_tag?(docstring)
33
33
 
34
- comment = find_by_tag(preceding_lines, tag, i)
35
- next unless comment
34
+ each_tags_by_docstring(['param', 'option'], docstring) do |tags|
35
+ tags.each_with_index do |tag, i|
36
+ comment = find_by_tag(preceding_lines, tag, i)
37
+ next unless comment
36
38
 
37
- unless tag.name && tag.types
38
- if tag.name.nil?
39
- add_offense(comment, message: "No tag name is supplied in `@#{tag.tag_name}`")
40
- elsif tag.types.nil?
41
- add_offense(comment, message: "No types are associated with the tag in `@#{tag.tag_name}`")
39
+ # YARD::Tags::RefTagList is not has name and types
40
+ next if tag.instance_of?(::YARD::Tags::RefTagList)
41
+
42
+ types = extract_tag_types(tag)
43
+ unless tag.name && types
44
+ if tag.name.nil?
45
+ add_offense(comment, message: "No tag name is supplied in `@#{tag.tag_name}`")
46
+ elsif types.nil?
47
+ add_offense(comment, message: "No types are associated with the tag in `@#{tag.tag_name}`")
48
+ end
49
+
50
+ next
42
51
  end
43
52
 
44
- next
45
- end
53
+ next unless node.arguments.none? { |arg_node| tag.name.to_sym == arg_node.name }
46
54
 
47
- next unless node.arguments.none? { |arg_node| tag.name.to_sym == arg_node.name }
55
+ begin
56
+ parse_type(types.join(', '))
57
+ rescue SyntaxError
58
+ next
59
+ end
48
60
 
49
- add_offense_to_tag(comment, tag)
61
+ add_offense_to_tag(comment, tag)
62
+ end
50
63
  end
51
64
  end
52
65
  alias on_defs on_def
53
66
 
54
67
  private
55
68
 
69
+ def each_tags_by_docstring(tag_names, docstring)
70
+ tag_names.each do |tag_name|
71
+ yield docstring.tags.select { |tag| tag.tag_name == tag_name }
72
+ end
73
+ end
74
+
56
75
  def find_by_tag(preceding_lines, tag, i)
57
76
  count = -1
58
77
  preceding_lines.find do |line|
@@ -69,6 +88,10 @@ module RuboCop
69
88
  range = source_range(processed_source.buffer, comment.location.line, offense_start..offense_end)
70
89
  add_offense(range, message: "`#{tag.name}` is not found in method arguments")
71
90
  end
91
+
92
+ def include_overload_tag?(docstring)
93
+ docstring.tags.any? { |tag| tag.tag_name == "overload" }
94
+ end
72
95
  end
73
96
  end
74
97
  end
@@ -10,6 +10,7 @@ module RuboCop
10
10
  # # good
11
11
  # # @param [Integer, String]
12
12
  class TagTypeSyntax < Base
13
+ include YARD::Helper
13
14
  include RangeHelp
14
15
 
15
16
  def on_new_investigation
@@ -26,10 +27,10 @@ module RuboCop
26
27
  def check(comment)
27
28
  docstring = comment.text.gsub(/\A#\s*/, '')
28
29
  ::YARD::DocstringParser.new.parse(docstring).tags.each do |tag|
29
- types = extract_tag_type(tag)
30
+ types = extract_tag_types(tag)
30
31
 
31
32
  check_syntax_error(comment) do
32
- ::YARD::Tags::TypesExplainer::Parser.parse(types.join(', '))
33
+ parse_type(types.join(', '))
33
34
  end
34
35
  end
35
36
  end
@@ -42,15 +43,6 @@ module RuboCop
42
43
  end
43
44
  end
44
45
 
45
- def extract_tag_type(tag)
46
- case tag
47
- when ::YARD::Tags::OptionTag
48
- tag.pair.types
49
- else
50
- tag.types
51
- end
52
- end
53
-
54
46
  def inline_comment?(comment)
55
47
  !comment_line?(comment.source_range.source_line)
56
48
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'yard'
4
- require_relative 'yard/collection_helper'
4
+ require_relative 'yard/helper'
5
5
  require_relative 'yard/collection_style'
6
6
  require_relative 'yard/collection_type'
7
7
  require_relative 'yard/meaningless_tag'
@@ -2,6 +2,6 @@
2
2
 
3
3
  module RuboCop
4
4
  module YARD
5
- VERSION = "0.5.0"
5
+ VERSION = "0.6.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.5.0
4
+ version: 0.6.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-10-08 00:00:00.000000000 Z
11
+ date: 2023-10-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubocop
@@ -51,9 +51,9 @@ files:
51
51
  - README.md
52
52
  - config/default.yml
53
53
  - lib/rubocop-yard.rb
54
- - lib/rubocop/cop/yard/collection_helper.rb
55
54
  - lib/rubocop/cop/yard/collection_style.rb
56
55
  - lib/rubocop/cop/yard/collection_type.rb
56
+ - lib/rubocop/cop/yard/helper.rb
57
57
  - lib/rubocop/cop/yard/meaningless_tag.rb
58
58
  - lib/rubocop/cop/yard/mismatch_name.rb
59
59
  - lib/rubocop/cop/yard/tag_type_syntax.rb