Sha256: eef570fa18bee547442e61ba9a9aa74b0706295f48b88cb0ba23c93341412d7b

Contents?: true

Size: 1.44 KB

Versions: 3

Compression:

Stored size: 1.44 KB

Contents

module SCSSLint
  # Checks that selector sequences are split over multiple lines by comma.
  class Linter::SingleLinePerSelector < Linter
    include LinterRegistry

    def visit_rule(node)
      add_lint(node, MESSAGE) if invalid_comma_placement?(node)
      yield # Continue linting children
    end

  private

    MESSAGE = 'Each selector in a comma sequence should be on its own line'

    # A comma is invalid if it starts the line or is not the end of the line
    def invalid_comma_placement?(node)
      # We must ignore selectors with interpolation, since there's no way to
      # tell if the overall selector is valid since the interpolation could
      # insert commas incorrectly. Thus we simply ignore.
      return unless node.rule.all? { |item| item.is_a?(String) }

      normalize_spacing(condense_to_string(node.rule)) =~ /\n,|,[^\n]/
    end

    # Since RuleNode.rule returns an array containing both String and
    # Sass::Script::Nodes, we need to condense it into a single string that we
    # can run a regex against.
    def condense_to_string(sequence_list)
      sequence_list.select { |item| item.is_a?(String) }.inject(:+)
    end

    # Removes extra spacing between lines in a comma-separated sequence due to
    # comments being removed in the parse phase. This makes it easy to check if
    # a comma is where it belongs.
    def normalize_spacing(string_sequence)
      string_sequence.gsub(/,[^\S\n]*\n\s*/, ",\n")
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
scss-lint-0.27.0 lib/scss_lint/linter/single_line_per_selector.rb
scss-lint-0.26.2 lib/scss_lint/linter/single_line_per_selector.rb
scss-lint-0.26.1 lib/scss_lint/linter/single_line_per_selector.rb