Class: Reek::CodeComment::CodeCommentValidator
- Inherits:
-
Object
- Object
- Reek::CodeComment::CodeCommentValidator
- 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
-
#detector_name ⇒ Object
readonly
private
Returns the value of attribute detector_name.
-
#line ⇒ Object
readonly
private
Returns the value of attribute line.
-
#options ⇒ Object
readonly
private
Returns the value of attribute options.
-
#original_comment ⇒ Object
readonly
private
Returns the value of attribute original_comment.
-
#separator ⇒ Object
readonly
private
Returns the value of attribute separator.
-
#source ⇒ Object
readonly
private
Returns the value of attribute source.
Instance Method Summary collapse
-
#configuration_keys_difference ⇒ String
private
All keys from the code comment that look bad.
- #detector_class ⇒ Object private
- #escalate_unknown_configuration_key ⇒ Object private
-
#given_configuration_keys ⇒ Set
private
The configuration keys that are found in the code comment.
-
#given_keys_legit? ⇒ Boolean
private
All keys in code comment are applicable to the detector in question.
-
#initialize(detector_name:, original_comment:, line:, source:, options:) ⇒ CodeCommentValidator
constructor
A new instance of CodeCommentValidator.
-
#legacy_format? ⇒ Boolean
private
Comment uses legacy three-colon format.
- #parsed_options ⇒ Object
-
#valid_detector_keys ⇒ Set
private
All keys that are legit for the given detector.
-
#validate ⇒ undefined
Method can raise the following errors: * Errors::LegacyCommentSeparatorError * Errors::BadDetectorInCommentError * Errors::GarbageDetectorConfigurationInCommentError * Errors::BadDetectorConfigurationKeyInCommentError.
Constructor Details
#initialize(detector_name:, original_comment:, line:, source:, options:) ⇒ CodeCommentValidator
Returns a new instance of CodeCommentValidator.
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 = end |
Instance Attribute Details
#detector_name ⇒ Object (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 |
#line ⇒ Object (readonly, private)
Returns the value of attribute line.
135 136 137 |
# File 'lib/reek/code_comment.rb', line 135 def line @line end |
#options ⇒ Object (readonly, private)
Returns the value of attribute options.
135 136 137 |
# File 'lib/reek/code_comment.rb', line 135 def @options end |
#original_comment ⇒ Object (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 |
#separator ⇒ Object (readonly, private)
Returns the value of attribute separator.
135 136 137 |
# File 'lib/reek/code_comment.rb', line 135 def separator @separator end |
#source ⇒ Object (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_difference ⇒ String (private)
Returns 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_class ⇒ Object (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_key ⇒ Object (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_keys ⇒ Set (private)
Returns 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 .keys.to_set(&:to_sym) end |
#given_keys_legit? ⇒ Boolean (private)
Returns 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.
162 163 164 |
# File 'lib/reek/code_comment.rb', line 162 def legacy_format? separator.start_with? ':' end |
#parsed_options ⇒ Object
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 ||= if Psych::VERSION < '3.1.0' YAML.safe_load( || CodeComment::DISABLE_DETECTOR_CONFIGURATION, [Regexp]) else YAML.safe_load( || 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_keys ⇒ Set (private)
Returns 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 |
#validate ⇒ undefined
Method can raise the following errors:
* Errors::LegacyCommentSeparatorError
* Errors::BadDetectorInCommentError
* Errors::GarbageDetectorConfigurationInCommentError
* Errors::BadDetectorConfigurationKeyInCommentError
114 115 116 |
# File 'lib/reek/code_comment.rb', line 114 def validate escalate_unknown_configuration_key end |