Sha256: fab14106e85bc408daefc4b104525cdfde0728cca03d001f5beeabff77830703

Contents?: true

Size: 1.49 KB

Versions: 7

Compression:

Stored size: 1.49 KB

Contents

# encoding: utf-8

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.'.freeze
        DOUBLE_QUOTE_MSG = 'Prefer double-quoted strings unless you need ' \
          'single quotes to avoid extra backslashes for escaping.'.freeze

        private

        def message(*)
          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

7 entries across 7 versions & 1 rubygems

Version Path
salsify_rubocop-0.48.1 lib/rubocop/cop/salsify/rspec_string_literals.rb
salsify_rubocop-0.48.0 lib/rubocop/cop/salsify/rspec_string_literals.rb
salsify_rubocop-0.47.2 lib/rubocop/cop/salsify/rspec_string_literals.rb
salsify_rubocop-0.47.1 lib/rubocop/cop/salsify/rspec_string_literals.rb
salsify_rubocop-0.47.1.rc0 lib/rubocop/cop/salsify/rspec_string_literals.rb
salsify_rubocop-0.47.0 lib/rubocop/cop/salsify/rspec_string_literals.rb
salsify_rubocop-0.46.0 lib/rubocop/cop/salsify/rspec_string_literals.rb