Class: Reek::CodeComment::CodeCommentValidator

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

Overview

A typical configuration via code comment looks like this:

:reek:DuplicateMethodCall { enabled: false }

There are a lot of ways a user can introduce some errors here:

1.) Unknown smell detector 2.) Garbage in the detector configuration like { thats: a: bad: config } 3.) Unknown configuration keys (e.g. by doing a simple typo: “exclude” vs. “exlude” ) 4.) Bad data types given as values for those keys This class validates [1], [2] and [3] at the moment but will also validate

4

in the future.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(detector_name:, original_comment:, line:, source:, options:) ⇒ CodeCommentValidator

Returns a new instance of CodeCommentValidator.

Parameters:

  • detector_name (String)

    the detector class that was parsed out of the original comment, e.g. “DuplicateMethodCall” or “UnknownSmellDetector”

  • original_comment (String)

    the original comment as found in the source code

  • line (Integer)

    start of the expression the comment belongs to

  • source (String)

    path to source file or “string”

  • options (String)

    the configuration options as String for the detector that were extracted from the original comment



99
100
101
102
103
104
105
# File 'lib/reek/code_comment.rb', line 99

def initialize(detector_name:, original_comment:, line:, source:, options:)
  @detector_name    = detector_name
  @original_comment = original_comment
  @line             = line
  @source           = source
  @options          = options
end

Instance Attribute Details

#detector_nameObject (readonly, private)

Returns the value of attribute detector_name.



135
136
137
# File 'lib/reek/code_comment.rb', line 135

def detector_name
  @detector_name
end

#lineObject (readonly, private)

Returns the value of attribute line.



135
136
137
# File 'lib/reek/code_comment.rb', line 135

def line
  @line
end

#optionsObject (readonly, private)

Returns the value of attribute options.



135
136
137
# File 'lib/reek/code_comment.rb', line 135

def options
  @options
end

#original_commentObject (readonly, private)

Returns the value of attribute original_comment.



135
136
137
# File 'lib/reek/code_comment.rb', line 135

def original_comment
  @original_comment
end

#separatorObject (readonly, private)

Returns the value of attribute separator.



135
136
137
# File 'lib/reek/code_comment.rb', line 135

def separator
  @separator
end

#sourceObject (readonly, private)

Returns the value of attribute source.



135
136
137
# File 'lib/reek/code_comment.rb', line 135

def source
  @source
end

Instance Method Details

#configuration_keys_differenceString (private)

Returns all keys from the code comment that look bad.

Returns:

  • (String)

    all keys from the code comment that look bad



177
178
179
180
181
# File 'lib/reek/code_comment.rb', line 177

def configuration_keys_difference
  given_configuration_keys.difference(valid_detector_keys).
    to_a.map { |key| "'#{key}'" }.
    join(', ')
end

#detector_classObject (private)



152
153
154
155
156
157
158
159
# File 'lib/reek/code_comment.rb', line 152

def detector_class
  @detector_class ||= SmellDetectors::BaseDetector.to_detector(detector_name)
rescue NameError
  raise Errors::BadDetectorInCommentError.new(detector_name: detector_name,
                                              original_comment: original_comment,
                                              source: source,
                                              line: line)
end

#escalate_unknown_configuration_keyObject (private)



142
143
144
145
146
147
148
149
150
# File 'lib/reek/code_comment.rb', line 142

def escalate_unknown_configuration_key
  return if given_keys_legit?

  raise Errors::BadDetectorConfigurationKeyInCommentError.new(detector_name: detector_name,
                                                              offensive_keys: configuration_keys_difference,
                                                              original_comment: original_comment,
                                                              source: source,
                                                              line: line)
end

#given_configuration_keysSet (private)

Returns the configuration keys that are found in the code comment.

Returns:

  • (Set)

    the configuration keys that are found in the code comment



172
173
174
# File 'lib/reek/code_comment.rb', line 172

def given_configuration_keys
  parsed_options.keys.to_set(&:to_sym)
end

#given_keys_legit?Boolean (private)

Returns all keys in code comment are applicable to the detector in question.

Returns:

  • (Boolean)

    all keys in code comment are applicable to the detector in question



167
168
169
# File 'lib/reek/code_comment.rb', line 167

def given_keys_legit?
  given_configuration_keys.subset? valid_detector_keys
end

#legacy_format?Boolean (private)

Returns comment uses legacy three-colon format.

Returns:

  • (Boolean)

    comment uses legacy three-colon format



162
163
164
# File 'lib/reek/code_comment.rb', line 162

def legacy_format?
  separator.start_with? ':'
end

#parsed_optionsObject



118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/reek/code_comment.rb', line 118

def parsed_options
  @parsed_options ||=
    if Psych::VERSION < '3.1.0'
      YAML.safe_load(options || CodeComment::DISABLE_DETECTOR_CONFIGURATION, [Regexp])
    else
      YAML.safe_load(options || CodeComment::DISABLE_DETECTOR_CONFIGURATION,
                     permitted_classes: [Regexp])
    end
rescue Psych::SyntaxError
  raise Errors::GarbageDetectorConfigurationInCommentError.new(detector_name: detector_name,
                                                               original_comment: original_comment,
                                                               source: source,
                                                               line: line)
end

#valid_detector_keysSet (private)

Returns all keys that are legit for the given detector.

Returns:

  • (Set)

    all keys that are legit for the given detector



184
185
186
# File 'lib/reek/code_comment.rb', line 184

def valid_detector_keys
  detector_class.configuration_keys
end

#validateundefined

Method can raise the following errors:

* Errors::LegacyCommentSeparatorError
* Errors::BadDetectorInCommentError
* Errors::GarbageDetectorConfigurationInCommentError
* Errors::BadDetectorConfigurationKeyInCommentError

Returns:

  • (undefined)


114
115
116
# File 'lib/reek/code_comment.rb', line 114

def validate
  escalate_unknown_configuration_key
end