Sha256: 94b63b92b6b01ea22478d3a399ed575ba301afac2f67e8df70eb58e07aaa25c0

Contents?: true

Size: 1.58 KB

Versions: 5

Compression:

Stored size: 1.58 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Salsify

      # This cop checks if quotes match the configured preference. It is
      # intended to be use specifically for specs and in combination with
      # Salsify/RspecDocString.
      #
      # Used together with Salsify/RspecDocString it allows one quote style to
      # be used for doc strings (`describe "foobar"`) and another style to be
      # used for all other strings in specs.
      class RspecStringLiterals < Cop
        include ConfigurableEnforcedStyle
        include StringLiteralsHelp

        DOCUMENTED_METHODS = RuboCop::Cop::Salsify::RspecDocString::DOCUMENTED_METHODS

        SINGLE_QUOTE_MSG = 'Prefer single-quoted strings unless you need ' \
          'interpolation or special symbols.'
        DOUBLE_QUOTE_MSG = 'Prefer double-quoted strings unless you need ' \
          'single quotes to avoid extra backslashes for escaping.'

        def autocorrect(node)
          StringLiteralCorrector.correct(node, style)
        end

        private

        def message(_node)
          style == :single_quotes ? SINGLE_QUOTE_MSG : DOUBLE_QUOTE_MSG
        end

        # Share implementation with Style/StringLiterals from rubocop
        def offense?(node)
          return false if documented_parent?(node)
          return false if inside_interpolation?(node)

          wrong_quotes?(node)
        end

        def documented_parent?(node)
          parent = node.parent
          parent && parent.send_type? &&
            DOCUMENTED_METHODS.include?(parent.children[1])
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
salsify_rubocop-1.0.2 lib/rubocop/cop/salsify/rspec_string_literals.rb
salsify_rubocop-1.0.1 lib/rubocop/cop/salsify/rspec_string_literals.rb
salsify_rubocop-1.0.0 lib/rubocop/cop/salsify/rspec_string_literals.rb
salsify_rubocop-0.91.0 lib/rubocop/cop/salsify/rspec_string_literals.rb
salsify_rubocop-0.85.0 lib/rubocop/cop/salsify/rspec_string_literals.rb