Sha256: 424da4616f2ea448b7dddeafa8f63cde33d58e807f36e84bc4b3daab676254b4

Contents?: true

Size: 1.78 KB

Versions: 70

Compression:

Stored size: 1.78 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Layout
      # This cop checks for whitespace within string interpolations.
      #
      # @example EnforcedStyle: no_space (default)
      #   # bad
      #      var = "This is the #{ space } example"
      #
      #   # good
      #      var = "This is the #{no_space} example"
      #
      # @example EnforcedStyle: space
      #   # bad
      #      var = "This is the #{no_space} example"
      #
      #   # good
      #      var = "This is the #{ space } example"
      class SpaceInsideStringInterpolation < Base
        include Interpolation
        include SurroundingSpace
        include ConfigurableEnforcedStyle
        include RangeHelp
        extend AutoCorrector

        NO_SPACE_MSG = 'Space inside string interpolation detected.'
        SPACE_MSG = 'Missing space inside string interpolation detected.'

        def on_interpolation(begin_node)
          return if begin_node.multiline?

          delims = delimiters(begin_node)
          return if empty_brackets?(*delims)

          if style == :no_space
            no_space_offenses(begin_node, *delims, NO_SPACE_MSG)
          else
            space_offenses(begin_node, *delims, SPACE_MSG)
          end
        end

        private

        def autocorrect(corrector, begin_node)
          delims = delimiters(begin_node)

          if style == :no_space
            SpaceCorrector.remove_space(processed_source, corrector, *delims)
          else
            SpaceCorrector.add_space(processed_source, corrector, *delims)
          end
        end

        def delimiters(begin_node)
          left = processed_source.first_token_of(begin_node)
          right = processed_source.last_token_of(begin_node)
          [left, right]
        end
      end
    end
  end
end

Version data entries

70 entries across 70 versions & 7 rubygems

Version Path
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/layout/space_inside_string_interpolation.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/layout/space_inside_string_interpolation.rb
rubocop-1.29.1 lib/rubocop/cop/layout/space_inside_string_interpolation.rb
rubocop-1.29.0 lib/rubocop/cop/layout/space_inside_string_interpolation.rb
rubocop-1.28.2 lib/rubocop/cop/layout/space_inside_string_interpolation.rb
rubocop-1.28.1 lib/rubocop/cop/layout/space_inside_string_interpolation.rb
rubocop-1.28.0 lib/rubocop/cop/layout/space_inside_string_interpolation.rb
rubocop-1.27.0 lib/rubocop/cop/layout/space_inside_string_interpolation.rb
rubocop-1.26.1 lib/rubocop/cop/layout/space_inside_string_interpolation.rb
op_connect-0.1.2 vendor/bundle/ruby/3.1.0/gems/rubocop-1.26.0/lib/rubocop/cop/layout/space_inside_string_interpolation.rb
rubocop-1.26.0 lib/rubocop/cop/layout/space_inside_string_interpolation.rb
plaid-14.13.0 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/layout/space_inside_string_interpolation.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/layout/space_inside_string_interpolation.rb
rubocop-1.25.1 lib/rubocop/cop/layout/space_inside_string_interpolation.rb
rubocop-1.25.0 lib/rubocop/cop/layout/space_inside_string_interpolation.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/layout/space_inside_string_interpolation.rb
plaid-14.11.1 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/layout/space_inside_string_interpolation.rb
phillipug-foodie-0.1.0 .vendor/ruby/3.0.0/gems/rubocop-1.24.0/lib/rubocop/cop/layout/space_inside_string_interpolation.rb
rubocop-1.24.1 lib/rubocop/cop/layout/space_inside_string_interpolation.rb
rubocop-1.24.0 lib/rubocop/cop/layout/space_inside_string_interpolation.rb