class Reek::CodeComment::CodeCommentValidator
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.
@quality :reek:TooManyInstanceVariables { max_instance_variables: 7 }
Attributes
Public Class Methods
Source
# 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
@param detector_name
[String] the detector class that was parsed out of the original
comment, e.g. "DuplicateMethodCall" or "UnknownSmellDetector"
@param original_comment
[String] the original comment as found in the source code @param line [Integer] start of the expression the comment belongs to @param source [String] path to source file or “string” @param options [String] the configuration options as String for the detector that were
extracted from the original comment
Public Instance Methods
Source
# 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
Source
# File lib/reek/code_comment.rb, line 114 def validate escalate_unknown_configuration_key end
Method can raise the following errors:
* Errors::LegacyCommentSeparatorError * Errors::BadDetectorInCommentError * Errors::GarbageDetectorConfigurationInCommentError * Errors::BadDetectorConfigurationKeyInCommentError
@return [undefined]
Private Instance Methods
Source
# 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
@return [String] all keys from the code comment that look bad
Source
# 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
Source
# 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
Source
# File lib/reek/code_comment.rb, line 172 def given_configuration_keys parsed_options.keys.to_set(&:to_sym) end
@return [Set] the configuration keys that are found in the code comment
Source
# File lib/reek/code_comment.rb, line 167 def given_keys_legit? given_configuration_keys.subset? valid_detector_keys end
@return [Boolean] all keys in code comment are applicable to the detector in question
Source
# File lib/reek/code_comment.rb, line 162 def legacy_format? separator.start_with? ':' end
@return [Boolean] comment uses legacy three-colon format
Source
# File lib/reek/code_comment.rb, line 184 def valid_detector_keys detector_class.configuration_keys end
@return [Set] all keys that are legit for the given detector