Sha256: f907c4da6dbf7b3d3d40fd596127bb4652959aa046bb8df56ce9f77711e69b40

Contents?: true

Size: 1.44 KB

Versions: 12

Compression:

Stored size: 1.44 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    # Common functionality for dealing with frozen string literals.
    module FrozenStringLiteral
      module_function

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

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

      private

      def frozen_string_literals_enabled?
        ruby_version = processed_source.ruby_version
        return false unless ruby_version
        # TODO: Whether frozen string literals will be the default in Ruby 3.0
        # or not is still unclear as of February 2018.
        # It may be necessary to change this code in the future.
        # See https://bugs.ruby-lang.org/issues/8976#note-41 for details.
        return true if ruby_version >= 3.0
        return false unless ruby_version >= 2.3

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

      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
    end
  end
end

Version data entries

12 entries across 10 versions & 2 rubygems

Version Path
rubocop-0.62.0 lib/rubocop/cop/mixin/frozen_string_literal.rb
rubocop-0.61.1 lib/rubocop/cop/mixin/frozen_string_literal.rb
rubocop-0.61.0 lib/rubocop/cop/mixin/frozen_string_literal.rb
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/rubocop-0.60.0/lib/rubocop/cop/mixin/frozen_string_literal.rb
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/rubocop-0.60.0/lib/rubocop/cop/mixin/frozen_string_literal.rb
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/rubocop-0.60.0/lib/rubocop/cop/mixin/frozen_string_literal.rb
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/rubocop-0.60.0/lib/rubocop/cop/mixin/frozen_string_literal.rb
rubocop-0.60.0 lib/rubocop/cop/mixin/frozen_string_literal.rb
rubocop-0.59.2 lib/rubocop/cop/mixin/frozen_string_literal.rb
rubocop-0.59.1 lib/rubocop/cop/mixin/frozen_string_literal.rb
rubocop-0.59.0 lib/rubocop/cop/mixin/frozen_string_literal.rb
rubocop-0.58.2 lib/rubocop/cop/mixin/frozen_string_literal.rb