Sha256: a144196d1d4cfec7a86610138380031cf1e624ec286d159093653164bb6cad28

Contents?: true

Size: 1.72 KB

Versions: 12

Compression:

Stored size: 1.72 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Sorbet
      # Checks that there is only one Sorbet sigil in a given file
      #
      # For example, the following class with two sigils
      #
      # ```ruby
      # # typed: true
      # # typed: true
      # # frozen_string_literal: true
      # class Foo; end
      # ```
      #
      # Will be corrected as:
      #
      # ```ruby
      # # typed: true
      # # frozen_string_literal: true
      # class Foo; end
      # ```
      #
      # Other comments or magic comments are left in place.
      class EnforceSingleSigil < ValidSigil
        include RangeHelp

        def investigate(processed_source)
          return if processed_source.tokens.empty?

          sigils = extract_all_sigils(processed_source)
          return if sigils.empty?

          sigils[1..sigils.size].each do |token|
            add_offense(token, location: token.pos, message: "Files must only contain one sigil")
          end
        end

        def autocorrect(_node)
          ->(corrector) do
            sigils = extract_all_sigils(processed_source)
            return if sigils.empty?

            # The first sigil encountered represents the "real" strictness so remove any below
            sigils[1..sigils.size].each do |token|
              corrector.remove(
                source_range(processed_source.buffer, token.line, (0..token.pos.last_column)),
              )
            end
          end
        end

        protected

        def extract_all_sigils(processed_source)
          processed_source.tokens
            .take_while { |token| token.type == :tCOMMENT }
            .find_all { |token| SIGIL_REGEX.match?(token.text) }
        end
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
rubocop-sorbet-0.8.3 lib/rubocop/cop/sorbet/sigils/enforce_single_sigil.rb
rubocop-sorbet-0.8.2 lib/rubocop/cop/sorbet/sigils/enforce_single_sigil.rb
rubocop-sorbet-0.8.1 lib/rubocop/cop/sorbet/sigils/enforce_single_sigil.rb
rubocop-sorbet-0.8.0 lib/rubocop/cop/sorbet/sigils/enforce_single_sigil.rb
rubocop-sorbet-0.7.8 lib/rubocop/cop/sorbet/sigils/enforce_single_sigil.rb
rubocop-sorbet-0.7.7 lib/rubocop/cop/sorbet/sigils/enforce_single_sigil.rb
rubocop-sorbet-0.7.6 lib/rubocop/cop/sorbet/sigils/enforce_single_sigil.rb
rubocop-sorbet-0.7.5 lib/rubocop/cop/sorbet/sigils/enforce_single_sigil.rb
rubocop-sorbet-0.7.4 lib/rubocop/cop/sorbet/sigils/enforce_single_sigil.rb
rubocop-sorbet-0.7.3 lib/rubocop/cop/sorbet/sigils/enforce_single_sigil.rb
rubocop-sorbet-0.7.2 lib/rubocop/cop/sorbet/sigils/enforce_single_sigil.rb
rubocop-sorbet-0.7.1 lib/rubocop/cop/sorbet/sigils/enforce_single_sigil.rb