class RuboCop::Cop::Style::FrozenStringLiteralComment
end
# …
module Bar
# frozen_string_literal: true
# good
end
# …
module Baz
# bad
end
# …
module Baz
# frozen_string_literal: false
# bad
# and forces projects to use frozen string literals.
# comment is set to ‘true`. This is a stricter option than `always`
# The `always_true` style enforces that the frozen string literal
@example EnforcedStyle: always_true
end
# …
module Baz
# good
end
# …
module Baz
# frozen_string_literal: true
# bad
# not exist in a file.
# The `never` will enforce that the frozen string literal comment does
@example EnforcedStyle: never
end
# …
module Bar
# frozen_string_literal: false
# good
end
# …
module Bar
# frozen_string_literal: true
# good
end
# …
module Bar
# bad
# called on a string literal.
# to a file, regardless of the Ruby version or if `freeze` or `<<` are
# The `always` style will always add the frozen string literal comment
@example EnforcedStyle: always (default)
will become frozen by default, and will need to be manually refactored.
change from being accepted to raising `FrozenError`, as all strings
This cop’s autocorrection is unsafe since any strings mutations will
@safety
`Layout/EmptyLineAfterMagicComment` cop.
To require a blank line after this comment, please see
to ‘false` instead of `true`.
Note that the cop will accept files where the comment exists but is set
encoding comment. The frozen string literal comment is only valid in Ruby 2.3+.
default in future Ruby. The comment will be added below a shebang and
of files to enable frozen string literals. Frozen string literals may be
It will add the `# frozen_string_literal: true` magic comment to the top
to frozen string literals.
Helps you transition from mutable string literals
def disabled_offense(processed_source)
def disabled_offense(processed_source) frozen_string_literal_comment = frozen_string_literal_comment(processed_source) add_offense(frozen_string_literal_comment.pos, message: MSG_DISABLED) do |corrector| enable_comment(corrector) end end
def enable_comment(corrector)
def enable_comment(corrector) comment = frozen_string_literal_comment(processed_source) corrector.replace(line_range(comment.line), FROZEN_STRING_LITERAL_ENABLED) end
def ensure_comment(processed_source)
def ensure_comment(processed_source) return if frozen_string_literal_comment_exists? missing_offense(processed_source) end
def ensure_enabled_comment(processed_source)
def ensure_enabled_comment(processed_source) if frozen_string_literal_specified? return if frozen_string_literals_enabled? # The comment exists, but is not enabled. disabled_offense(processed_source) else # The comment doesn't exist at all. missing_true_offense(processed_source) end end
def ensure_no_comment(processed_source)
def ensure_no_comment(processed_source) return unless frozen_string_literal_comment_exists? unnecessary_comment_offense(processed_source) end
def following_comment
def following_comment "\n#{FROZEN_STRING_LITERAL_ENABLED}" end
def frozen_string_literal_comment(processed_source)
def frozen_string_literal_comment(processed_source) processed_source.tokens.find do |token| token.text.start_with?(FROZEN_STRING_LITERAL) end end
def insert_comment(corrector)
def insert_comment(corrector) comment = last_special_comment(processed_source) if comment corrector.insert_after(line_range(comment.line), following_comment) else corrector.insert_before(processed_source.buffer.source_range, preceding_comment) end end
def last_special_comment(processed_source)
def last_special_comment(processed_source) token_number = 0 if processed_source.tokens[token_number].text.start_with?(SHEBANG) token = processed_source.tokens[token_number] token_number += 1 end next_token = processed_source.tokens[token_number] if next_token&.text&.valid_encoding? && Encoding::ENCODING_PATTERN.match?(next_token.text) token = next_token end token end
def line_range(line)
def line_range(line) processed_source.buffer.line_range(line) end
def missing_offense(processed_source)
def missing_offense(processed_source) range = source_range(processed_source.buffer, 0, 0) add_offense(range, message: MSG_MISSING) { |corrector| insert_comment(corrector) } end
def missing_true_offense(processed_source)
def missing_true_offense(processed_source) range = source_range(processed_source.buffer, 0, 0) add_offense(range, message: MSG_MISSING_TRUE) { |corrector| insert_comment(corrector) } end
def on_new_investigation
def on_new_investigation return if processed_source.tokens.empty? case style when :never ensure_no_comment(processed_source) when :always_true ensure_enabled_comment(processed_source) else ensure_comment(processed_source) end end
def preceding_comment
def preceding_comment "#{FROZEN_STRING_LITERAL_ENABLED}\n" end
def remove_comment(corrector, node)
def remove_comment(corrector, node) corrector.remove(range_with_surrounding_space(node.pos, side: :right)) end
def unnecessary_comment_offense(processed_source)
def unnecessary_comment_offense(processed_source) frozen_string_literal_comment = frozen_string_literal_comment(processed_source) add_offense(frozen_string_literal_comment.pos, message: MSG_UNNECESSARY) do |corrector| remove_comment(corrector, frozen_string_literal_comment) end end