Module: RuboCop::Cop::FrozenStringLiteral

Overview

Common functionality for dealing with frozen string literals.

Constant Summary collapse

FROZEN_STRING_LITERAL_ENABLED =
'# frozen_string_literal: true'

Class Method Summary collapse

Class Method Details

.frozen_string_literal?(node) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
23
24
25
26
27
28
# File 'lib/rubocop/cop/mixin/frozen_string_literal.rb', line 20

def frozen_string_literal?(node)
  frozen_string = if target_ruby_version >= 3.0
                    uninterpolated_string?(node) || uninterpolated_heredoc?(node)
                  else
                    FROZEN_STRING_LITERAL_TYPES_RUBY27.include?(node.type)
                  end

  frozen_string && frozen_string_literals_enabled?
end

.frozen_string_literal_comment_exists?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/rubocop/cop/mixin/frozen_string_literal.rb', line 14

def frozen_string_literal_comment_exists?
  leading_comment_lines.any? { |line| MagicComment.parse(line).valid_literal_value? }
end

.frozen_string_literal_specified?Boolean

Returns:

  • (Boolean)


77
78
79
80
81
# File 'lib/rubocop/cop/mixin/frozen_string_literal.rb', line 77

def frozen_string_literal_specified?
  leading_comment_lines.any? do |line|
    MagicComment.parse(line).frozen_string_literal_specified?
  end
end

.frozen_string_literals_disabled?Boolean

Returns:

  • (Boolean)


71
72
73
74
75
# File 'lib/rubocop/cop/mixin/frozen_string_literal.rb', line 71

def frozen_string_literals_disabled?
  leading_comment_lines.any? do |line|
    MagicComment.parse(line).frozen_string_literal == false
  end
end

.frozen_string_literals_enabled?Boolean

Returns:

  • (Boolean)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/rubocop/cop/mixin/frozen_string_literal.rb', line 43

def frozen_string_literals_enabled?
  ruby_version = processed_source.ruby_version
  return false unless ruby_version

  # Check if a magic string literal comment specifies what to do
  magic_comments = leading_comment_lines.filter_map { |line| MagicComment.parse(line) }
  if (literal_magic_comment = magic_comments.find(&:frozen_string_literal_specified?))
    return literal_magic_comment.frozen_string_literal?
  end

  # 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 4.0
  # or not is still unclear as of July 2024.
  # It may be necessary to add this code in the future.
  #
  #   return ruby_version >= 4.0 if string_literals_frozen_by_default?.nil?
  #
  # And the above `ruby_version >= 4.0` is undecided whether it will be
  # Ruby 4.0 or others.
  # See https://bugs.ruby-lang.org/issues/20205 for details.
  # For now, offer a configuration value to override behavior is using RUBYOPT.
  return false if string_literals_frozen_by_default?.nil?

  string_literals_frozen_by_default?
end

.leading_comment_linesObject



87
88
89
90
91
92
93
94
95
96
# File 'lib/rubocop/cop/mixin/frozen_string_literal.rb', line 87

def leading_comment_lines
  first_non_comment_token = processed_source.tokens.find { |token| !token.comment? }

  if first_non_comment_token
    # `line` is 1-indexed so we need to subtract 1 to get the array index
    processed_source.lines[0...(first_non_comment_token.line - 1)]
  else
    processed_source.lines
  end
end

.leading_magic_commentsObject



83
84
85
# File 'lib/rubocop/cop/mixin/frozen_string_literal.rb', line 83

def leading_magic_comments
  leading_comment_lines.map { |line| MagicComment.parse(line) }
end

.uninterpolated_heredoc?(node) ⇒ Boolean Also known as: frozen_heredoc?

Returns:

  • (Boolean)


36
37
38
39
40
# File 'lib/rubocop/cop/mixin/frozen_string_literal.rb', line 36

def uninterpolated_heredoc?(node)
  return false unless node.dstr_type? && node.heredoc?

  node.children.all?(&:str_type?)
end

.uninterpolated_string?(node) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
33
34
# File 'lib/rubocop/cop/mixin/frozen_string_literal.rb', line 30

def uninterpolated_string?(node)
  node.str_type? || (
    node.dstr_type? && node.each_descendant(:begin, :ivar, :cvar, :gvar).none?
  )
end