Module: RuboCop::Cop::FrozenStringLiteral

Included in:
Style::EmptyLiteral, Style::FrozenStringLiteralComment, Style::MutableConstant, Style::RedundantFreeze
Defined in:
lib/rubocop/cop/mixin/frozen_string_literal.rb

Overview

Common functionality for dealing with frozen string literals.

Constant Summary collapse

FROZEN_STRING_LITERAL =
'# frozen_string_literal:'.freeze
FROZEN_STRING_LITERAL_ENABLED =
'# frozen_string_literal: true'.freeze
FROZEN_STRING_LITERAL_TYPES =
[:str, :dstr].freeze

Class Method Summary collapse

Class Method Details

.frozen_string_literal_comment_exists?(processed_source, comment = FROZEN_STRING_LITERAL) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
17
18
19
20
21
22
# File 'lib/rubocop/cop/mixin/frozen_string_literal.rb', line 14

def frozen_string_literal_comment_exists?(processed_source,
                                          comment = FROZEN_STRING_LITERAL)
  first_three_lines =
    [processed_source[0], processed_source[1], processed_source[2]]
  first_three_lines.compact!
  first_three_lines.any? do |line|
    line.start_with?(comment)
  end
end

.frozen_string_literals_enabled?(processed_source) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
27
28
29
30
31
32
# File 'lib/rubocop/cop/mixin/frozen_string_literal.rb', line 24

def frozen_string_literals_enabled?(processed_source)
  ruby_version = processed_source.ruby_version
  return false unless ruby_version
  return true if ruby_version >= 3.0
  return false unless ruby_version >= 2.3
  frozen_string_literal_comment_exists?(
    processed_source, FROZEN_STRING_LITERAL_ENABLED
  )
end