Sha256: 1ac90c424b650f961947dfc7e446e66862a6680099d17d1019a0cb1905417985

Contents?: true

Size: 1.86 KB

Versions: 13

Compression:

Stored size: 1.86 KB

Contents

module SCSSLint
  # Checks for spaces following the colon that separates a variable's name from
  # its value.
  class Linter::SpaceAfterVariableColon < Linter
    include LinterRegistry

    def visit_variable(node)
      whitespace = whitespace_after_colon(node)

      case config['style']
      when 'no_space'
        check_for_no_spaces(node, whitespace)
      when 'one_space'
        check_for_one_space(node, whitespace)
      when 'at_least_one_space'
        check_for_at_least_one_space(node, whitespace)
      when 'one_space_or_newline'
        check_for_one_space_or_newline(node, whitespace)
      end
    end

  private

    def check_for_no_spaces(node, whitespace)
      return if whitespace == []
      add_lint(node, 'Colon after variable should not be followed by any spaces')
    end

    def check_for_one_space(node, whitespace)
      return if whitespace == [' ']
      add_lint(node, 'Colon after variable should be followed by one space')
    end

    def check_for_at_least_one_space(node, whitespace)
      return if whitespace.uniq == [' ']
      add_lint(node, 'Colon after variable should be followed by at least one space')
    end

    def check_for_one_space_or_newline(node, whitespace)
      return if whitespace == [' '] || whitespace == ["\n"]
      return if whitespace[0] == "\n" && whitespace[1..-1].uniq == [' ']
      add_lint(node, 'Colon after variable should be followed by one space or a newline')
    end

    def whitespace_after_colon(node)
      whitespace = []
      offset = 0
      start_pos = node.source_range.start_pos

      # Find the colon after the variable name
      offset = offset_to(start_pos, ':', offset) + 1

      # Count spaces after the colon
      while [' ', "\t", "\n"].include? character_at(start_pos, offset)
        whitespace << character_at(start_pos, offset)
        offset += 1
      end

      whitespace
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
scss_lint-0.53.0 lib/scss_lint/linter/space_after_variable_colon.rb
scss_lint-0.52.0 lib/scss_lint/linter/space_after_variable_colon.rb
scss_lint-0.51.0 lib/scss_lint/linter/space_after_variable_colon.rb
scss_lint-0.50.3 lib/scss_lint/linter/space_after_variable_colon.rb
scss_lint-0.50.2 lib/scss_lint/linter/space_after_variable_colon.rb
scss_lint-0.50.1 lib/scss_lint/linter/space_after_variable_colon.rb
scss_lint-0.50.0 lib/scss_lint/linter/space_after_variable_colon.rb
scss_lint-0.49.0 lib/scss_lint/linter/space_after_variable_colon.rb
scss_lint-0.48.0 lib/scss_lint/linter/space_after_variable_colon.rb
scss_lint-0.47.1 lib/scss_lint/linter/space_after_variable_colon.rb
scss_lint-0.47.0 lib/scss_lint/linter/space_after_variable_colon.rb
scss_lint-0.46.0 lib/scss_lint/linter/space_after_variable_colon.rb
scss_lint-0.45.0 lib/scss_lint/linter/space_after_variable_colon.rb