Class: Reek::CodeComment

Inherits:
Object
  • Object
show all
Defined in:
lib/reek/code_comment.rb

Overview

A comment header from an abstract syntax tree; found directly above module, class and method definitions.

Defined Under Namespace

Classes: CodeCommentValidator

Constant Summary collapse

CONFIGURATION_REGEX =
/
 :reek:     # prefix
 (\w+)      # smell detector e.g.: UncommunicativeVariableName
 (:?\s*)    # separator
 (\{.*?\})? # details in hash style e.g.: { max_methods: 30 }
/x
SANITIZE_REGEX =

Matches ‘#’, newlines and > 1 whitespaces.

/(#|\n|\s)+/
DISABLE_DETECTOR_CONFIGURATION =
'{ enabled: false }'
MINIMUM_CONTENT_LENGTH =
2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(comment:, line: nil, source: nil) ⇒ CodeComment

Returns a new instance of CodeComment.

Parameters:

  • comment (String)

    the original comment as found in the source code

  • line (Integer) (defaults to: nil)

    start of the expression the comment belongs to

  • source (String) (defaults to: nil)

    Path to source file or “string”



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/reek/code_comment.rb', line 34

def initialize(comment:, line: nil, source: nil)
  @original_comment  = comment
  @line              = line
  @source            = source
  @config            = Hash.new { |hash, key| hash[key] = {} }

  @original_comment.scan(CONFIGURATION_REGEX) do |detector_name, separator, options|
    escalate_legacy_separator separator
    validator = CodeCommentValidator.new(detector_name:    detector_name,
                                         original_comment: original_comment,
                                         line:             line,
                                         source:           source,
                                         options:          options)
    validator.validate
    @config.merge! detector_name => validator.parsed_options
  end
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



27
28
29
# File 'lib/reek/code_comment.rb', line 27

def config
  @config
end

#lineObject (readonly, private)

Returns the value of attribute line.



58
59
60
# File 'lib/reek/code_comment.rb', line 58

def line
  @line
end

#original_commentObject (readonly, private)

Returns the value of attribute original_comment.



58
59
60
# File 'lib/reek/code_comment.rb', line 58

def original_comment
  @original_comment
end

#sourceObject (readonly, private)

Returns the value of attribute source.



58
59
60
# File 'lib/reek/code_comment.rb', line 58

def source
  @source
end

Instance Method Details

#descriptive?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/reek/code_comment.rb', line 52

def descriptive?
  sanitized_comment.split(/\s+/).length >= MINIMUM_CONTENT_LENGTH
end

#escalate_legacy_separator(separator) ⇒ Object (private)



67
68
69
70
71
72
73
# File 'lib/reek/code_comment.rb', line 67

def escalate_legacy_separator(separator)
  return unless separator.start_with? ':'

  raise Errors::LegacyCommentSeparatorError.new(original_comment: original_comment,
                                                source: source,
                                                line: line)
end

#sanitized_commentObject (private)



60
61
62
63
64
65
# File 'lib/reek/code_comment.rb', line 60

def sanitized_comment
  @sanitized_comment ||= original_comment.
    gsub(CONFIGURATION_REGEX, '').
    gsub(SANITIZE_REGEX, ' ').
    strip
end