class RubbyCop::MagicComment
Parse different formats of magic comments.
@abstract parent of three different magic comment handlers
Constants
- TOKEN
@see git.io/vMC1C IRB's pattern for matching magic comment tokens
Public Class Methods
# File lib/rubbycop/magic_comment.rb, line 25 def initialize(comment) @comment = comment end
Detect magic comment format and pass it to the appropriate wrapper.
@param comment [String]
@return [RubbyCop::MagicComment]
# File lib/rubbycop/magic_comment.rb, line 16 def self.parse(comment) case comment when EmacsComment::FORMAT then EmacsComment.new(comment) when VimComment::FORMAT then VimComment.new(comment) else SimpleComment.new(comment) end end
Public Instance Methods
# File lib/rubbycop/magic_comment.rb, line 29 def any? frozen_string_literal_specified? || encoding_specified? end
# File lib/rubbycop/magic_comment.rb, line 68 def encoding_specified? specified?(encoding) end
Expose the `frozen_string_literal` value coerced to a boolean if possible.
@return [Boolean] if value is `true` or `false` @return [nil] if frozen_string_literal
comment isn't found @return [String] if comment is found but isn't true or false
# File lib/rubbycop/magic_comment.rb, line 57 def frozen_string_literal return unless (setting = extract_frozen_string_literal) case setting when 'true' then true when 'false' then false else setting end end
Does the magic comment enable the frozen string literal feature.
Test whether the frozen string literal value is `true`. Cannot just return `frozen_string_literal` since an invalid magic comment like `# frozen_string_literal
: yes` is possible and the truthy value `'yes'` does not actually enable the feature
@return [Boolean]
# File lib/rubbycop/magic_comment.rb, line 41 def frozen_string_literal? frozen_string_literal == true end
Was a magic comment for the frozen string literal found?
@return [Boolean]
# File lib/rubbycop/magic_comment.rb, line 48 def frozen_string_literal_specified? specified?(frozen_string_literal) end
Private Instance Methods
Match the entire comment string with a pattern and take the first capture.
@param pattern [Regexp]
@return [String] if pattern matched @return [nil] otherwise
# File lib/rubbycop/magic_comment.rb, line 84 def extract(pattern) @comment[pattern, 1] end
# File lib/rubbycop/magic_comment.rb, line 74 def specified?(value) !value.nil? end