Class: RuboCop::Cop::Style::MethodDocRange

Inherits:
Object
  • Object
show all
Defined in:
lib/rubocop/cop/style/public_method_documentation.rb

Overview

method doc range checks parts of the comment for correctness

Constant Summary collapse

DOC_PARM_REGEXP =
%r{^# \* <tt>:(\w+)</tt>}.freeze
PARM_START =
'# * <tt>:'
PARM_END =
'</tt>'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(comments, type) ⇒ MethodDocRange

Returns a new instance of MethodDocRange.



301
302
303
304
# File 'lib/rubocop/cop/style/public_method_documentation.rb', line 301

def initialize(comments, type)
  @comments = comments
  @type = type
end

Instance Attribute Details

#endObject

Returns the value of attribute end.



295
296
297
# File 'lib/rubocop/cop/style/public_method_documentation.rb', line 295

def end
  @end
end

#startObject

Returns the value of attribute start.



295
296
297
# File 'lib/rubocop/cop/style/public_method_documentation.rb', line 295

def start
  @start
end

#typeObject

Returns the value of attribute type.



295
296
297
# File 'lib/rubocop/cop/style/public_method_documentation.rb', line 295

def type
  @type
end

Instance Method Details

#before?(method_doc_range) ⇒ Boolean

Returns:

  • (Boolean)


306
307
308
309
310
# File 'lib/rubocop/cop/style/public_method_documentation.rb', line 306

def before?(method_doc_range)
  return true if missing? || method_doc_range.missing?

  @start < method_doc_range.start
end

#empty_comm?(comment) ⇒ Boolean

Returns:

  • (Boolean)


312
313
314
315
# File 'lib/rubocop/cop/style/public_method_documentation.rb', line 312

def empty_comm?(comment)
  txt = comment.text
  txt.size <= 2
end

#end_commentObject



317
318
319
# File 'lib/rubocop/cop/style/public_method_documentation.rb', line 317

def end_comment
  @comments[@end]
end

#ends_with_empty_comment?Boolean

Returns:

  • (Boolean)


321
322
323
# File 'lib/rubocop/cop/style/public_method_documentation.rb', line 321

def ends_with_empty_comment?
  missing? || empty_comm?(end_comment)
end

#first_comment?Boolean

Returns:

  • (Boolean)


325
326
327
# File 'lib/rubocop/cop/style/public_method_documentation.rb', line 325

def first_comment?
  @start == 0
end

#first_comment_equal?(text) ⇒ Boolean

Returns:

  • (Boolean)


333
334
335
# File 'lib/rubocop/cop/style/public_method_documentation.rb', line 333

def first_comment_equal?(text)
  !missing? && start_comment.text == text
end

#first_empty_comment?Boolean

Returns:

  • (Boolean)


329
330
331
# File 'lib/rubocop/cop/style/public_method_documentation.rb', line 329

def first_empty_comment?
  missing? || empty_comm?(@comments[@start + 1])
end

#missing?Boolean

Returns:

  • (Boolean)


337
338
339
# File 'lib/rubocop/cop/style/public_method_documentation.rb', line 337

def missing?
  @start.nil?
end

#parm_namesObject



341
342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'lib/rubocop/cop/style/public_method_documentation.rb', line 341

def parm_names
  names = []
  range_body.each do |parm_line|
    # puts "parm_line.text=#{parm_line}"
    # puts "match=#{DOC_PARM_REGEXP.match(parm_line.text).to_s}"
    DOC_PARM_REGEXP.match(parm_line.text) do |m|
      parm_name = m.to_s[PARM_START.size...m.to_s.index(PARM_END)]
      names.push([parm_line, parm_name])
      # puts "parm_name=#{parm_name}"
    end
  end
  # puts "names=#{names}"
  names
end

#range_bodyObject



356
357
358
# File 'lib/rubocop/cop/style/public_method_documentation.rb', line 356

def range_body
  @comments[@start + (first_empty_comment? ? 2 : 1)...@end + (ends_with_empty_comment? ? 0 : 1)]
end

#returns?Boolean

Returns:

  • (Boolean)


360
361
362
# File 'lib/rubocop/cop/style/public_method_documentation.rb', line 360

def returns?
  @type == 'Return'
end

#start_commentObject



364
365
366
# File 'lib/rubocop/cop/style/public_method_documentation.rb', line 364

def start_comment
  @comments[@start]
end

#starts_with_empty_comment?Boolean

Returns:

  • (Boolean)


368
369
370
# File 'lib/rubocop/cop/style/public_method_documentation.rb', line 368

def starts_with_empty_comment?
  empty_comm?(@comments[@start])
end

#to_sObject



372
373
374
# File 'lib/rubocop/cop/style/public_method_documentation.rb', line 372

def to_s
  [missing? ? nil : start_comment, @start, @end]
end