Skip to content

Commit

Permalink
Extract frozen string literal logic into a helper
Browse files Browse the repository at this point in the history
Use this helper method instead repeating the logic everywhere
to ease future changes.
  • Loading branch information
splattael committed Aug 23, 2021
1 parent f145884 commit 5a12fad
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 10 deletions.
6 changes: 6 additions & 0 deletions lib/rubocop/cop/mixin/frozen_string_literal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,18 @@ module FrozenStringLiteral
FROZEN_STRING_LITERAL_ENABLED = '# frozen_string_literal: true'
FROZEN_STRING_LITERAL_TYPES = %i[str dstr].freeze

private_constant :FROZEN_STRING_LITERAL_TYPES

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

private

def frozen_string_literal?(node)
FROZEN_STRING_LITERAL_TYPES.include?(node.type) && frozen_string_literals_enabled?
end

def frozen_string_literals_enabled?
ruby_version = processed_source.ruby_version
return false unless ruby_version
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/style/frozen_string_literal_comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def last_special_comment(processed_source)

def frozen_string_literal_comment(processed_source)
processed_source.find_token do |token|
token.text.start_with?(FrozenStringLiteral::FROZEN_STRING_LITERAL)
token.text.start_with?(FROZEN_STRING_LITERAL)
end
end

Expand Down
8 changes: 2 additions & 6 deletions lib/rubocop/cop/style/mutable_constant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ def check(value)
range_enclosed_in_parentheses = range_enclosed_in_parentheses?(value)
return unless mutable_literal?(value) ||
target_ruby_version <= 2.7 && range_enclosed_in_parentheses
return if FROZEN_STRING_LITERAL_TYPES.include?(value.type) &&
frozen_string_literals_enabled?

return if frozen_string_literal?(value)
return if shareable_constant_value?(value)

add_offense(value) { |corrector| autocorrect(corrector, value) }
Expand Down Expand Up @@ -183,10 +183,6 @@ def immutable_literal?(node)
frozen_regexp_or_range_literals?(node) || node.immutable_literal?
end

def frozen_string_literal?(node)
FROZEN_STRING_LITERAL_TYPES.include?(node.type) && frozen_string_literals_enabled?
end

def shareable_constant_value?(node)
return false if target_ruby_version < 3.0

Expand Down
4 changes: 1 addition & 3 deletions lib/rubocop/cop/style/redundant_freeze.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ def immutable_literal?(node)
node = strip_parenthesis(node)

return true if node.immutable_literal?

return true if FROZEN_STRING_LITERAL_TYPES.include?(node.type) &&
frozen_string_literals_enabled?
return true if frozen_string_literal?(node)

target_ruby_version >= 3.0 && (node.regexp_type? || node.range_type?)
end
Expand Down

0 comments on commit 5a12fad

Please sign in to comment.