Class: SCSSLint::Linter::PropertyUnits

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

Overview

Check for allowed units

Constant Summary collapse

NUMBER_WITH_UNITS_REGEX =
/
  (?:
   (["']).+?\1 # [0: quote mark] quoted string, e.g. "hi there"
   |           # or
   (?:^|\s)    # beginning of value or whitespace
   (?:
    \d+        # any number of digits, e.g. 123
    |          # or
    \d*\.?\d+  # any number of digits with decimal, e.g. 1.23 or .123
   )
   ([a-z%]+)   # [1: units] letters or percent sign, e.g. px or %
  )
/ix

Constants included from Utils

Utils::COLOR_REGEX

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

inherited, #initialize, #name, #run

Methods included from Utils

#color?, #color_hex?, #color_keyword?, #color_keyword_to_code, #else_node?, #extract_string_selectors, #node_ancestor, #node_siblings, #pluralize, #previous_node, #remove_quoted_strings, #same_position?

Methods included from SelectorVisitor

#visit_selector

Constructor Details

This class inherits a constructor from SCSSLint::Linter

Instance Method Details

#visit_prop(node) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/scss_lint/linter/property_units.rb', line 27

def visit_prop(node)
  property = node.name.join

  # Handle nested properties by ensuring the full name is extracted
  if @nested_under
    property = "#{@nested_under}-#{property}"
  end

  if node.value.respond_to?(:value)
    node.value.value.to_s.scan(NUMBER_WITH_UNITS_REGEX).each do |matches|
      is_quoted_value = !matches[0].nil?
      next if is_quoted_value
      units = matches[1]
      check_units(node, property, units)
    end
  end

  @nested_under = property
  yield # Continue linting nested properties
  @nested_under = nil
end

#visit_root(_node) ⇒ Object



20
21
22
23
24
25
# File 'lib/scss_lint/linter/property_units.rb', line 20

def visit_root(_node)
  @globally_allowed_units = config['global'].to_set
  @allowed_units_for_property = config['properties']

  yield # Continue linting children
end