module RuboCop::Cop::FrozenStringLiteral

Common functionality for dealing with frozen string literals.

Constants

FROZEN_STRING_LITERAL
FROZEN_STRING_LITERAL_ENABLED
FROZEN_STRING_LITERAL_TYPES

Public Instance Methods

frozen_string_literal_comment_exists?() click to toggle source
# File lib/rubocop/cop/mixin/frozen_string_literal.rb, line 13
def frozen_string_literal_comment_exists?
  leading_comment_lines.any? do |line|
    MagicComment.parse(line).frozen_string_literal_specified?
  end
end

Private Instance Methods

frozen_string_literals_enabled?() click to toggle source
# File lib/rubocop/cop/mixin/frozen_string_literal.rb, line 21
def frozen_string_literals_enabled?
  ruby_version = processed_source.ruby_version
  return false unless ruby_version

  # TODO: Ruby officially abandon making frozen string literals default
  # for Ruby 3.0.
  # https://bugs.ruby-lang.org/issues/11473#note-53
  # Whether frozen string literals will be the default after Ruby 3.0
  # or not is still unclear as of January 2019.
  # It may be necessary to add this code in the future.
  #
  #   return true if ruby_version >= 3.1
  #
  # And the above `ruby_version >= 3.1` is undecided whether it will be
  # Ruby 3.1, 3.2, 4.0 or others.
  # See https://bugs.ruby-lang.org/issues/8976#note-41 for details.
  leading_comment_lines.any? do |line|
    MagicComment.parse(line).frozen_string_literal?
  end
end
leading_comment_lines() click to toggle source
# File lib/rubocop/cop/mixin/frozen_string_literal.rb, line 42
def leading_comment_lines
  comments = processed_source.comments

  comments.each_with_object([]) do |comment, leading_comments|
    next if comment.loc.line > 3

    leading_comments << comment.text
  end
end