class RuboCop::MagicComment

Parse different formats of magic comments.

@abstract parent of three different magic comment handlers

Constants

KEYWORDS
TOKEN

IRB’s pattern for matching magic comment tokens. @see github.com/ruby/ruby/blob/b4a55c1/lib/irb/magic-file.rb#L5

Public Class Methods

new(comment) click to toggle source
# File lib/rubocop/magic_comment.rb, line 32
def initialize(comment)
  @comment = comment
end
parse(comment) click to toggle source

Detect magic comment format and pass it to the appropriate wrapper.

@param comment [String]

@return [RuboCop::MagicComment]

# File lib/rubocop/magic_comment.rb, line 23
def self.parse(comment)
  case comment
  when EmacsComment::REGEXP then EmacsComment.new(comment)
  when VimComment::REGEXP   then VimComment.new(comment)
  else
    SimpleComment.new(comment)
  end
end

Public Instance Methods

any?() click to toggle source
# File lib/rubocop/magic_comment.rb, line 36
def any?
  frozen_string_literal_specified? ||
    encoding_specified? ||
    shareable_constant_value_specified? ||
    typed_specified?
end
encoding_specified?() click to toggle source
# File lib/rubocop/magic_comment.rb, line 104
def encoding_specified?
  specified?(encoding)
end
frozen_string_literal() click to toggle source

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/rubocop/magic_comment.rb, line 86
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
frozen_string_literal?() click to toggle source

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/rubocop/magic_comment.rb, line 55
def frozen_string_literal?
  frozen_string_literal == true
end
frozen_string_literal_specified?() click to toggle source

Was a magic comment for the frozen string literal found?

@return [Boolean]

# File lib/rubocop/magic_comment.rb, line 70
def frozen_string_literal_specified?
  specified?(frozen_string_literal)
end
shareable_constant_value() click to toggle source

Expose the ‘shareable_constant_value` value coerced to a boolean if possible.

@return [String] for shareable_constant_value config

# File lib/rubocop/magic_comment.rb, line 100
def shareable_constant_value
  extract_shareable_constant_value
end
shareable_constant_value_specified?() click to toggle source

Was a shareable_constant_value specified?

@return [Boolean]

# File lib/rubocop/magic_comment.rb, line 77
def shareable_constant_value_specified?
  specified?(shareable_constant_value)
end
typed() click to toggle source
# File lib/rubocop/magic_comment.rb, line 115
def typed
  extract_typed
end
typed_specified?() click to toggle source

Was the Sorbet ‘typed` sigil specified?

@return [Boolean]

# File lib/rubocop/magic_comment.rb, line 111
def typed_specified?
  specified?(extract_typed)
end
valid?() click to toggle source
# File lib/rubocop/magic_comment.rb, line 43
def valid?
  @comment.start_with?('#') && any?
end
valid_literal_value?() click to toggle source
# File lib/rubocop/magic_comment.rb, line 59
def valid_literal_value?
  [true, false].include?(frozen_string_literal)
end
valid_shareable_constant_value?() click to toggle source
# File lib/rubocop/magic_comment.rb, line 63
def valid_shareable_constant_value?
  %w[none literal experimental_everything experimental_copy].include?(shareable_constant_value)
end

Private Instance Methods

extract(pattern) click to toggle source

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/rubocop/magic_comment.rb, line 131
def extract(pattern)
  @comment[pattern, :token]
end
specified?(value) click to toggle source
# File lib/rubocop/magic_comment.rb, line 121
def specified?(value)
  !value.nil?
end