Class: SCSSLint::Linter::DuplicateProperty

Inherits:
SCSSLint::Linter
  • Object
show all
Includes:
SCSSLint::LinterRegistry
Defined in:
lib/scss_lint/linter/duplicate_property.rb

Overview

Checks for a property declared twice in a rule set.

Instance Attribute Summary

Attributes inherited from SCSSLint::Linter

#config, #engine, #lints

Instance Method Summary collapse

Methods included from SCSSLint::LinterRegistry

extract_linters_from, included

Methods inherited from SCSSLint::Linter

#add_lint, #character_at, #description, #initialize, #run, #source_from_range, #visit, #visit_children

Methods included from Utils

#can_be_condensed?, #extract_string_selectors, #pluralize, #previous_node, #remove_quoted_strings, #shortest_hex_form

Methods included from SelectorVisitor

#visit_selector

Constructor Details

This class inherits a constructor from SCSSLint::Linter

Instance Method Details

#visit_rule(node) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/scss_lint/linter/duplicate_property.rb', line 6

def visit_rule(node)
  properties = node.children
                   .select { |child| child.is_a?(Sass::Tree::PropNode) }
                   .reject { |prop| prop.name.any? { |item| item.is_a?(Sass::Script::Node) } }

  prop_names = {}

  properties.each do |prop|
    name = prop.name.join

    prop_hash = name
    prop_value =
      case prop.value
      when Sass::Script::Funcall
        prop.value.name
      when Sass::Script::String
        prop.value.value
      else
        prop.value.to_s
      end

    prop_value.to_s.scan(/^(-[^-]+-.+)/) do |vendor_keyword|
      prop_hash << vendor_keyword.first
    end

    if existing_prop = prop_names[prop_hash]
      add_lint(prop, "Property `#{name}` already defined on line #{existing_prop.line}")
    else
      prop_names[prop_hash] = prop
    end
  end
end