yard-lint 1.13.1 → 1.15.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.
Files changed (27) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +19 -0
  3. data/README.md +15 -8
  4. data/bin/yard-lint +24 -34
  5. data/lib/yard/lint/contracts/base.rb +20 -6
  6. data/lib/yard/lint/contracts/cli_options.rb +62 -0
  7. data/lib/yard/lint/contracts/config.rb +1 -1
  8. data/lib/yard/lint/executor/in_process_registry.rb +79 -9
  9. data/lib/yard/lint/formatters/relative_path.rb +32 -0
  10. data/lib/yard/lint/formatters/sarif.rb +2 -12
  11. data/lib/yard/lint/result_builder.rb +44 -15
  12. data/lib/yard/lint/results/aggregate.rb +4 -2
  13. data/lib/yard/lint/runner.rb +16 -14
  14. data/lib/yard/lint/stats_calculator.rb +59 -37
  15. data/lib/yard/lint/todo_generator.rb +33 -21
  16. data/lib/yard/lint/validators/documentation/undocumented_boolean_methods/messages_builder.rb +23 -0
  17. data/lib/yard/lint/validators/documentation/undocumented_boolean_methods/result.rb +1 -2
  18. data/lib/yard/lint/validators/documentation/undocumented_method_arguments/messages_builder.rb +10 -3
  19. data/lib/yard/lint/validators/documentation/undocumented_method_arguments/parser.rb +22 -3
  20. data/lib/yard/lint/validators/documentation/undocumented_method_arguments/validator.rb +46 -8
  21. data/lib/yard/lint/validators/documentation/undocumented_objects/parser.rb +15 -13
  22. data/lib/yard/lint/validators/documentation/undocumented_objects/result.rb +6 -1
  23. data/lib/yard/lint/validators/documentation/undocumented_objects/validator.rb +25 -2
  24. data/lib/yard/lint/validators/tags/example_style/validator.rb +7 -2
  25. data/lib/yard/lint/version.rb +1 -1
  26. data/lib/yard/lint.rb +15 -14
  27. metadata +4 -1
@@ -32,10 +32,9 @@ module Yard
32
32
  build_result(parsed_results, @selection)
33
33
  end
34
34
 
35
- private
36
-
37
35
  # Run all validators using in-process YARD execution.
38
36
  # Parses files once and shares the registry across all validators.
37
+ # @api private
39
38
  # @return [Hash] hash with raw results from all validators
40
39
  def run_validators
41
40
  results = {}
@@ -94,21 +93,11 @@ module Yard
94
93
  results
95
94
  end
96
95
 
97
- # Filter files for a specific validator based on per-validator exclusions
98
- # @param validator_name [String] full validator name
99
- # @param files [Array<String>] array of file paths
100
- # @return [Array<String>] filtered array of file paths
101
- def filter_files_for_validator(validator_name, files)
102
- validator_excludes = config.validator_exclude(validator_name)
103
- return files if validator_excludes.empty?
104
-
105
- files.reject { |file| config.excluded_path?(file, validator_excludes) }
106
- end
107
-
108
96
  # Filter result offenses based on per-validator exclusions
109
97
  # Also always removes offenses with no file location (e.g. YARD warnings
110
98
  # emitted without file context such as from macro expansion with nil object)
111
99
  # since they carry no actionable information and would display as `:0`.
100
+ # @api private
112
101
  # @param validator_name [String] full validator name
113
102
  # @param result [Results::Base] result object with offenses
114
103
  # @return [Results::Base, nil] result with filtered offenses, or nil if no offenses remain
@@ -134,6 +123,19 @@ module Yard
134
123
  result
135
124
  end
136
125
 
126
+ private
127
+
128
+ # Filter files for a specific validator based on per-validator exclusions
129
+ # @param validator_name [String] full validator name
130
+ # @param files [Array<String>] array of file paths
131
+ # @return [Array<String>] filtered array of file paths
132
+ def filter_files_for_validator(validator_name, files)
133
+ validator_excludes = config.validator_exclude(validator_name)
134
+ return files if validator_excludes.empty?
135
+
136
+ files.reject { |file| config.excluded_path?(file, validator_excludes) }
137
+ end
138
+
137
139
  # Parse raw results from validators and create Result objects
138
140
  # Delegates result building to ResultBuilder
139
141
  # @param raw [Hash] hash with raw results from all validators
@@ -161,7 +163,7 @@ module Yard
161
163
  # @param files [Array<String>] array of files that were analyzed
162
164
  # @return [Results::Aggregate] aggregate result object
163
165
  def build_result(results, files)
164
- Results::Aggregate.new(results, config, files)
166
+ Results::Aggregate.new(results, config, files, source: @source)
165
167
  end
166
168
  end
167
169
  end
@@ -5,13 +5,18 @@ module Yard
5
5
  # Calculates documentation coverage statistics
6
6
  # Runs YARD queries to count documented vs undocumented objects
7
7
  class StatsCalculator
8
- attr_reader :config, :files
8
+ # Method part of a stats line: title and arity (a title may itself contain "|")
9
+ METHOD_REGEX = /\A(.+)\|(\d+)\z/
10
+
11
+ attr_reader :config, :files, :source
9
12
 
10
13
  # @param config [Yard::Lint::Config] configuration object
11
14
  # @param files [Array<String>] Ruby source files whose YARD documentation will be checked for coverage statistics
12
- def initialize(config, files)
15
+ # @param source [String, nil] in-memory content of the single file in files (--stdin)
16
+ def initialize(config, files, source: nil)
13
17
  @config = config
14
18
  @files = Array(files).compact
19
+ @source = source
15
20
  end
16
21
 
17
22
  # Calculate documentation coverage statistics
@@ -21,15 +26,12 @@ module Yard
21
26
 
22
27
  raw_stats = run_yard_stats_query
23
28
  # nil means the YARD subprocess failed - return nil (coverage unknown)
24
- # rather than default_stats, which would falsely report 100% coverage
25
- # and let a MinCoverage gate pass.
29
+ # rather than default_stats, which would report the files as having
30
+ # nothing to document.
26
31
  return nil if raw_stats.nil?
27
32
  return default_stats if raw_stats.empty?
28
33
 
29
- parsed_stats = parse_stats_output(raw_stats)
30
- filtered_stats = apply_exclusions(parsed_stats)
31
-
32
- calculate_coverage_percentage(filtered_stats)
34
+ calculate_coverage_percentage(parse_stats_output(raw_stats))
33
35
  end
34
36
 
35
37
  private
@@ -37,28 +39,27 @@ module Yard
37
39
  # Default stats for empty file lists
38
40
  # @return [Hash]
39
41
  def default_stats
40
- { total: 0, documented: 0, coverage: 100.0 }
42
+ { total: 0, documented: 0, coverage: 0.0 }
41
43
  end
42
44
 
43
45
  # Run YARD query to get object documentation status
44
46
  # @return [String] YARD query output
45
47
  def run_yard_stats_query
46
- # Create temp file with file list
47
- Tempfile.create(['yard_stats', '.txt']) do |f|
48
- files.each { |file| f.puts(Shellwords.escape(file)) }
49
- f.flush
50
-
51
- query = build_stats_query
52
-
53
- # Use temp directory for YARD database (auto-cleanup)
54
- Dir.mktmpdir("yard_stats_#{Process.pid}_") do |temp_dir|
48
+ # Use temp directory for YARD database (auto-cleanup)
49
+ Dir.mktmpdir("yard_stats_#{Process.pid}_") do |temp_dir|
50
+ # Create temp file with file list
51
+ Tempfile.create(['yard_stats', '.txt']) do |f|
52
+ stats_files(temp_dir).each { |file| f.puts(Shellwords.escape(file)) }
53
+ f.flush
54
+
55
+ query = build_stats_query
55
56
  cmd = build_yard_command(f.path, query, temp_dir)
56
57
 
57
58
  stdout, _stderr, status = Open3.capture3(cmd)
58
59
 
59
60
  # Return nil if the YARD command fails (non-zero exit, or killed by
60
61
  # a signal so exitstatus is nil) - distinct from genuinely empty
61
- # output, so coverage is reported as unknown rather than 100%.
62
+ # output, so coverage is reported as unknown rather than 0%.
62
63
  return nil unless status.exitstatus&.zero?
63
64
 
64
65
  stdout
@@ -66,11 +67,26 @@ module Yard
66
67
  end
67
68
  end
68
69
 
70
+ # Files for YARD to read. The in-memory source is written to a temp copy,
71
+ # as the file on disk may differ or not exist
72
+ # @param temp_dir [String] temporary directory for the copy
73
+ # @return [Array<String>] file paths
74
+ def stats_files(temp_dir)
75
+ return files unless source
76
+
77
+ source_dir = File.join(temp_dir, 'source')
78
+ Dir.mkdir(source_dir)
79
+ path = File.join(source_dir, File.basename(files.first))
80
+ File.write(path, source)
81
+ [path]
82
+ end
83
+
69
84
  # Build the YARD query for stats collection
85
+ # Methods also print their title and arity, so ExcludedMethods can be applied
70
86
  # @return [String] YARD query string
71
87
  def build_stats_query
72
88
  <<~QUERY.chomp
73
- type = object.type.to_s; state = object.docstring.all.empty? ? "undoc" : "doc"; puts "\#{type}:\#{state}"
89
+ type = object.type.to_s; state = object.docstring.all.empty? ? "undoc" : "doc"; if object.type == :method; arity = object.parameters.reject { |p| p[0].to_s.start_with?("*", "&") }.size; puts "\#{type}:\#{state}:\#{object.title}|\#{arity}"; else; puts "\#{type}:\#{state}"; end
74
90
  QUERY
75
91
  end
76
92
 
@@ -91,20 +107,29 @@ module Yard
91
107
  CMD
92
108
  end
93
109
 
94
- # Parse YARD stats output
95
- # Format: "type:state" (e.g., "method:doc", "class:undoc")
110
+ # Parse YARD stats output, skipping methods that match ExcludedMethods
111
+ # Format: "type:state" (e.g., "class:undoc") or "method:state:Title|arity"
112
+ # (e.g., "method:doc:Foo#bar|1")
96
113
  # @param output [String] YARD command output
97
114
  # @return [Hash] counts by type and state
98
115
  def parse_stats_output(output)
99
116
  stats = Hash.new { |h, k| h[k] = { documented: 0, undocumented: 0 } }
117
+ excluded_methods = excluded_method_patterns
100
118
 
101
119
  output.each_line do |line|
102
120
  line.strip!
103
121
  next if line.empty?
104
122
 
105
- type, state = line.split(':', 2)
123
+ type, state, method = line.split(':', 3)
106
124
  next unless type && state
107
125
 
126
+ if method
127
+ match = method.match(METHOD_REGEX)
128
+ next unless match
129
+ next if excluded_methods.any? &&
130
+ exclusion_parser.method_excluded?(match[1], match[2].to_i, excluded_methods)
131
+ end
132
+
108
133
  if state == 'doc'
109
134
  stats[type][:documented] += 1
110
135
  elsif state == 'undoc'
@@ -115,21 +140,18 @@ module Yard
115
140
  stats
116
141
  end
117
142
 
118
- # Apply validator exclusions to stats
119
- # Respects ExcludedMethods and other validator-specific exclusions
120
- # @param stats [Hash] parsed stats
121
- # @return [Hash] filtered stats
122
- def apply_exclusions(stats)
123
- # Get excluded methods from UndocumentedObjects validator config
124
- excluded_methods = config.validator_config('Documentation/UndocumentedObjects', 'ExcludedMethods') || []
125
-
126
- return stats if excluded_methods.empty?
143
+ # ExcludedMethods patterns of the UndocumentedObjects validator
144
+ # @return [Array<String>] sanitized patterns
145
+ def excluded_method_patterns
146
+ patterns = config.validator_config('Documentation/UndocumentedObjects', 'ExcludedMethods') || []
127
147
 
128
- # For now, we can't easily filter out specific methods without re-parsing
129
- # This would require running YARD query with method names
130
- # TODO: Implement precise method-level filtering if needed
148
+ exclusion_parser.sanitize_patterns(Array(patterns))
149
+ end
131
150
 
132
- stats
151
+ # Parser whose ExcludedMethods matching the validator itself uses
152
+ # @return [Validators::Documentation::UndocumentedObjects::Parser]
153
+ def exclusion_parser
154
+ @exclusion_parser ||= Validators::Documentation::UndocumentedObjects::Parser.new
133
155
  end
134
156
 
135
157
  # Calculate coverage percentage from stats
@@ -147,7 +169,7 @@ module Yard
147
169
  total_objects = total_documented + total_undocumented
148
170
 
149
171
  coverage = if total_objects.zero?
150
- 100.0
172
+ 0.0
151
173
  else
152
174
  (total_documented.to_f / total_objects * 100)
153
175
  end
@@ -83,8 +83,8 @@ module Yard
83
83
  def check
84
84
  todo_entries = load_todo_entries
85
85
  audit_config = build_audit_config(todo_entries)
86
- path_files = Yard::Lint.send(:expand_path, @path, audit_config)
87
- files = Yard::Lint.send(:expand_path, '.', audit_config) | path_files
86
+ path_files = Yard::Lint.expand_path(@path, audit_config)
87
+ files = Yard::Lint.expand_path('.', audit_config) | path_files
88
88
 
89
89
  violations_by_validator = audit_violations([files, path_files], audit_config)
90
90
  files = files.map { |file| make_relative_path(file) }
@@ -93,7 +93,7 @@ module Yard
93
93
  unused_entries = todo_entries.each_with_object({}) do |(validator_name, patterns), unused|
94
94
  next unless judged?(validator_name, audit_config)
95
95
 
96
- violating_files = violations_by_validator.fetch(validator_name, [])
96
+ violating_files = violating_files(validator_name, violations_by_validator)
97
97
  stale = patterns.select do |pattern|
98
98
  in_scope?(pattern, files, path_files) && violating_files.none? { |file| todo_match?(pattern, file) }
99
99
  end
@@ -153,6 +153,18 @@ module Yard
153
153
  end
154
154
  end
155
155
 
156
+ # Files whose offenses a validator's todo entries can silence
157
+ # A composite parent's Exclude also filters the offenses of its children.
158
+ # @param validator_name [String] validator name from the todo file
159
+ # @param violations_by_validator [Hash{String => Array<String>}] validator names to files with offenses
160
+ # @return [Array<String>] files with offenses of the validator or of its composite children
161
+ def violating_files(validator_name, violations_by_validator)
162
+ validator_cfg = ConfigLoader.validator_config(validator_name)
163
+ children = validator_cfg ? validator_cfg.combines_with : []
164
+
165
+ ([validator_name] + children).flat_map { |name| violations_by_validator.fetch(name, []) }.uniq
166
+ end
167
+
156
168
  # Whether the audit can judge a validator's todo entries
157
169
  # Entries of validators left out by only_validators are not judged, as they were not run.
158
170
  # @param validator_name [String] validator name from the todo file
@@ -214,11 +226,11 @@ module Yard
214
226
  def run_linting(files: nil, config: @config)
215
227
  # Run each validator individually to track violations per validator
216
228
  # This is more reliable than trying to infer validator from offense name
217
- files ||= Yard::Lint.send(:expand_path, @path, config)
229
+ files ||= Yard::Lint.expand_path(@path, config)
218
230
  runner = Runner.new(files, config)
219
231
 
220
232
  # Run validators and collect raw results
221
- raw_results = runner.send(:run_validators)
233
+ raw_results = runner.run_validators
222
234
  result_builder = ResultBuilder.new(config)
223
235
 
224
236
  violations_by_validator = {}
@@ -228,22 +240,22 @@ module Yard
228
240
  ConfigLoader::ALL_VALIDATORS.each do |validator_name|
229
241
  next unless config.validator_enabled?(validator_name)
230
242
 
231
- validator_result = result_builder.build(validator_name, raw_results)
232
- next unless validator_result
233
-
234
- # Apply per-validator Exclude patterns (and drop no-location offenses)
235
- # exactly as a normal run does, so the baseline does not silence files
236
- # that are already excluded.
237
- validator_result = runner.send(:filter_result_offenses, validator_name, validator_result)
238
- next unless validator_result && validator_result.offenses.any?
239
-
240
- # Extract file paths from offenses, skipping those without a location
241
- file_paths = validator_result.offenses.filter_map do |offense|
242
- make_relative_path(offense[:location])
243
- end.uniq.sort
244
-
245
- violations_by_validator[validator_name] = file_paths
246
- total_offenses += validator_result.count
243
+ # A composite child's offenses are keyed by the child, whose todo entry silences them
244
+ result_builder.build_by_validator(validator_name, raw_results).each do |name, validator_result|
245
+ # Apply per-validator Exclude patterns (and drop no-location offenses)
246
+ # exactly as a normal run does, so the baseline does not silence files
247
+ # that are already excluded.
248
+ validator_result = runner.filter_result_offenses(validator_name, validator_result)
249
+ next unless validator_result && validator_result.offenses.any?
250
+
251
+ # Extract file paths from offenses, skipping those without a location
252
+ file_paths = validator_result.offenses.filter_map do |offense|
253
+ make_relative_path(offense[:location])
254
+ end.uniq.sort
255
+
256
+ violations_by_validator[name] = file_paths
257
+ total_offenses += validator_result.count
258
+ end
247
259
  end
248
260
 
249
261
  { violations_by_validator: violations_by_validator, total_offenses: total_offenses }
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Yard
4
+ module Lint
5
+ module Validators
6
+ module Documentation
7
+ module UndocumentedBooleanMethods
8
+ # Builds messages for undocumented boolean methods offenses
9
+ class MessagesBuilder
10
+ class << self
11
+ # Build message for a documented boolean method without a @return tag
12
+ # @param offense [Hash] offense data with :element key
13
+ # @return [String] formatted message
14
+ def call(offense)
15
+ "The `#{offense[:element]}` method has no @return tag"
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -6,7 +6,6 @@ module Yard
6
6
  module Documentation
7
7
  module UndocumentedBooleanMethods
8
8
  # Result object for undocumented boolean methods validation
9
- # Reuses UndocumentedObjects::MessagesBuilder
10
9
  class Result < Results::Base
11
10
  self.default_severity = 'warning'
12
11
  self.offense_type = 'line'
@@ -16,7 +15,7 @@ module Yard
16
15
  # @param offense [Hash] offense data
17
16
  # @return [String] formatted message
18
17
  def build_message(offense)
19
- UndocumentedObjects::MessagesBuilder.call(offense)
18
+ MessagesBuilder.call(offense)
20
19
  end
21
20
  end
22
21
  end
@@ -9,11 +9,18 @@ module Yard
9
9
  class MessagesBuilder
10
10
  class << self
11
11
  # Build message for undocumented method arguments
12
- # @param offense [Hash] offense data with :method_name key
12
+ # @param offense [Hash] offense data with :method_name and
13
+ # :missing_params keys
13
14
  # @return [String] formatted message
14
15
  def call(offense)
15
- "The `#{offense[:method_name]}` method is missing documentation " \
16
- 'for some of the arguments.'
16
+ missing = Array(offense[:missing_params])
17
+
18
+ if missing.empty?
19
+ "The `#{offense[:method_name]}` method is missing documentation " \
20
+ 'for some of the arguments.'
21
+ else
22
+ "The `#{offense[:method_name]}` method has no @param for: #{missing.join(', ')}"
23
+ end
17
24
  end
18
25
  end
19
26
  end
@@ -6,8 +6,11 @@ module Yard
6
6
  module Documentation
7
7
  module UndocumentedMethodArguments
8
8
  # Class used to extract details about methods with undocumented arguments
9
+ # Tags/Order and the separator validators reuse it for plain location
10
+ # lines, which carry no tab-separated list of missing parameter names.
11
+ # Names in that list are escaped as %25, %2C, %09 and %0A.
9
12
  # @example Output format (skip-lint)
10
- # /path/to/file.rb:10: Platform::Analysis::Authors#initialize
13
+ # /path/to/file.rb:10: Platform::Analysis::Authors#initialize\tname,options
11
14
  class Parser < Parsers::Base
12
15
  # Regex to extract file, line, and object title from yard list output
13
16
  # Format: /path/to/file.rb:10: ClassName#method_name
@@ -30,19 +33,35 @@ module Yard
30
33
  # Extract: file path, line number, class name, method name
31
34
  file_path = match_data[1]
32
35
  line_number = match_data[2].to_i
33
- class_name, method_name = split_title(match_data[3])
36
+ title = match_data[3]
37
+ title, _, missing = title.partition("\t") if title.include?("\t")
38
+ class_name, method_name = split_title(title)
34
39
 
35
- {
40
+ offense = {
36
41
  location: file_path,
37
42
  method_name: method_name,
38
43
  line: line_number,
39
44
  class_name: class_name
40
45
  }
46
+ next offense unless missing
47
+
48
+ offense[:element] = title
49
+ offense[:missing_params] = missing.split(',').map! { |name| unescape_field(name) }
50
+ offense
41
51
  end
42
52
  end
43
53
 
44
54
  private
45
55
 
56
+ # Reverses the validator's escaping of '%', ',', tabs and newlines
57
+ # @param text [String] escaped field text
58
+ # @return [String] original text
59
+ def unescape_field(text)
60
+ return text unless text.include?('%')
61
+
62
+ text.gsub(/%(?:25|2C|09|0A)/, '%25' => '%', '%2C' => ',', '%09' => "\t", '%0A' => "\n")
63
+ end
64
+
46
65
  # Splits a YARD object title into namespace and method name parts
47
66
  # @param title [String] object title (e.g. "Foo#bar", "#bar", "CONST")
48
67
  # @return [Array(String, String)] namespace and method name; for titles
@@ -37,27 +37,32 @@ module Yard
37
37
  # Documentation/UndocumentedObjects instead of reporting them here too.
38
38
  return if config_or_default('SkipFullyUndocumented') && object.docstring.blank?
39
39
 
40
- return unless arguments_missing_docs?(object, params)
40
+ missing = missing_param_names(object, params)
41
+ return unless missing
41
42
 
42
- collector.puts "#{object.file}:#{object.line}: #{object.title}"
43
+ names = missing.map! { |name| escape_field(display_name(name)) }.join(',')
44
+ collector.puts "#{object.file}:#{object.line}: #{object.title}\t#{names}"
43
45
  end
44
46
 
45
47
  private
46
48
 
47
- # Decide whether a method's parameters are under-documented.
49
+ # Find the parameters of a method that have no @param tag.
48
50
  # @param object [YARD::CodeObjects::MethodObject] the method to check
49
51
  # @param params [Array<Array>] non-splat/block parameters
50
- # @return [Boolean] true if documentation is missing
51
- def arguments_missing_docs?(object, params)
52
+ # @return [Array<String>, nil] names of the undocumented parameters
53
+ # (empty in count-only mode, which does not know them), or nil when
54
+ # documentation is complete
55
+ def missing_param_names(object, params)
52
56
  # Tags nested inside @overload blocks live on the overload's own
53
57
  # docstring, so all_typed_tags collects those @param tags too.
54
58
  param_tags = all_typed_tags(object.docstring, %w[param])
55
59
 
56
60
  if config_or_default('CheckParameterNames')
57
61
  documented = param_tags.map { |tag| normalize_param_name(tag.name) }
58
- params.any? { |param| !documented.include?(normalize_param_name(param[0])) }
59
- else
60
- params.size > param_tags.size
62
+ missing = params.map { |param| normalize_param_name(param[0]) }.uniq - documented
63
+ missing unless missing.empty?
64
+ elsif params.size > param_tags.size
65
+ []
61
66
  end
62
67
  end
63
68
 
@@ -69,6 +74,39 @@ module Yard
69
74
  def normalize_param_name(name)
70
75
  name.to_s.sub(/:\z/, '')
71
76
  end
77
+
78
+ # YARD names a destructured parameter `(a, b)` as `a, b`, keeping the
79
+ # source whitespace, and drops only the outermost pair, so a nested one
80
+ # can come back unbalanced (`((a, b), c)` as `a, b), c`). Rebalance,
81
+ # put the outer pair back and collapse whitespace so the message
82
+ # shows it as one parameter.
83
+ # @param name [String] normalized parameter name
84
+ # @return [String] name as shown in the message
85
+ def display_name(name)
86
+ return name unless name.include?(',')
87
+
88
+ depth = 0
89
+ unopened = 0
90
+ name.each_char do |char|
91
+ if char == '('
92
+ depth += 1
93
+ elsif char == ')'
94
+ depth.zero? ? unopened += 1 : depth -= 1
95
+ end
96
+ end
97
+
98
+ "(#{'(' * unopened}#{name.gsub(/\s+/, ' ')}#{')' * depth})"
99
+ end
100
+
101
+ # Escapes ',' (the name separator), tabs and newlines (the line and
102
+ # field separators) and '%' (the escape character)
103
+ # @param text [String] raw field text
104
+ # @return [String] escaped field text
105
+ def escape_field(text)
106
+ return text unless text.match?(/[%,\t\n]/)
107
+
108
+ text.gsub(/[%,\t\n]/, '%' => '%25', ',' => '%2C', "\t" => '%09', "\n" => '%0A')
109
+ end
72
110
  end
73
111
  end
74
112
  end
@@ -64,8 +64,6 @@ module Yard
64
64
  end
65
65
  end
66
66
 
67
- private
68
-
69
67
  # Checks if a method should be excluded based on ExcludedMethods config
70
68
  # Supports: simple names, arity notation, and regex patterns
71
69
  # @param element [String] the element name (e.g., "Class#method")
@@ -73,6 +71,7 @@ module Yard
73
71
  # excluding splat and block)
74
72
  # @param excluded_methods [Array<String>] list of exclusion patterns
75
73
  # @return [Boolean] true if method should be excluded
74
+ # @api private
76
75
  def method_excluded?(element, arity, excluded_methods)
77
76
  # ExcludedMethods only applies to methods. A class, module, or
78
77
  # constant element has no #/. separator, so never derive a
@@ -100,6 +99,20 @@ module Yard
100
99
  end
101
100
  end
102
101
 
102
+ # Sanitize exclusion patterns
103
+ # @param patterns [Array] raw patterns from config
104
+ # @return [Array<String>] cleaned and validated patterns
105
+ # @api private
106
+ def sanitize_patterns(patterns)
107
+ patterns
108
+ .compact # Remove nil values
109
+ .map { |p| p.to_s.strip } # Convert to strings and trim whitespace
110
+ .reject(&:empty?) # Remove empty strings
111
+ .reject { |p| p == '//' } # Reject empty regex (matches everything)
112
+ end
113
+
114
+ private
115
+
103
116
  # Checks if an object should be excluded based on ExcludedObjects config.
104
117
  # Matches against the fully-qualified object name (the whole element),
105
118
  # so it applies to every object type - classes, modules, methods, and
@@ -132,17 +145,6 @@ module Yard
132
145
  end
133
146
  end
134
147
 
135
- # Sanitize exclusion patterns
136
- # @param patterns [Array] raw patterns from config
137
- # @return [Array<String>] cleaned and validated patterns
138
- def sanitize_patterns(patterns)
139
- patterns
140
- .compact # Remove nil values
141
- .map { |p| p.to_s.strip } # Convert to strings and trim whitespace
142
- .reject(&:empty?) # Remove empty strings
143
- .reject { |p| p == '//' } # Reject empty regex (matches everything)
144
- end
145
-
146
148
  # Match a regex pattern against method name with error handling
147
149
  # @param method_name [String] the method name to match
148
150
  # @param regex_pattern [String] the regex pattern (without delimiters)
@@ -15,7 +15,12 @@ module Yard
15
15
  # @param offense [Hash] offense data
16
16
  # @return [String] formatted message
17
17
  def build_message(offense)
18
- MessagesBuilder.call(offense)
18
+ # Offenses of the combined UndocumentedBooleanMethods carry :method_name
19
+ if offense.key?(:method_name)
20
+ UndocumentedBooleanMethods::MessagesBuilder.call(offense)
21
+ else
22
+ MessagesBuilder.call(offense)
23
+ end
19
24
  end
20
25
  end
21
26
  end
@@ -17,17 +17,40 @@ module Yard
17
17
  # @return [void]
18
18
  def in_process_query(object, collector)
19
19
  return if parent_class_allowed?(object)
20
- # Check if docstring is empty
21
- return unless object.docstring.all.empty?
22
20
 
23
21
  if object.is_a?(YARD::CodeObjects::MethodObject)
22
+ return unless undocumented_method?(object)
23
+
24
24
  # For methods, include arity (excluding splat and block params)
25
25
  arity = object.parameters.reject { |p| p[0].to_s.start_with?('*', '&') }.size
26
26
  collector.puts "#{object.file}:#{object.line}: #{object.title}|#{arity}"
27
27
  else
28
+ return unless object.docstring.all.empty?
29
+
28
30
  collector.puts "#{object.file}:#{object.line}: #{object.title}"
29
31
  end
30
32
  end
33
+
34
+ private
35
+
36
+ # A module_function pair is reported once, on its public module method.
37
+ # YARD gives one half of an undocumented `?` pair an inferred `@return`.
38
+ # @param object [YARD::CodeObjects::MethodObject] the method to check
39
+ # @return [Boolean] true when the method should be reported
40
+ def undocumented_method?(object)
41
+ if object.docstring.all.empty?
42
+ # A class cannot define module functions; skipping it saves the twin lookup.
43
+ # Visibility cannot skip it: the copy is public when `module_function :name`
44
+ # is parsed before the def
45
+ object.scope == :class ||
46
+ object.namespace.is_a?(YARD::CodeObjects::ClassObject) ||
47
+ module_function_twin(object).nil?
48
+ else
49
+ object.module_function? &&
50
+ object.name.end_with?('?') &&
51
+ module_function_twin(object)&.docstring&.all&.empty? == true
52
+ end
53
+ end
31
54
  end
32
55
  end
33
56
  end