Module: SCSSLint::Utils

Included in:
Linter
Defined in:
lib/scss_lint/utils.rb

Overview

Collection of helpers used across a variety of linters.

Instance Method Summary collapse

Instance Method Details

#can_be_condensed?(hex) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
34
35
36
# File 'lib/scss_lint/utils.rb', line 31

def can_be_condensed?(hex)
  hex.length == 7 &&
    hex[1] == hex[2] &&
    hex[3] == hex[4] &&
    hex[5] == hex[6]
end

#extract_string_selectors(selector_array) ⇒ Object

Given a selector array which is a list of strings with Sass::Script::Nodes interspersed within them, return an array of strings representing those selectors with the Sass::Script::Nodes removed (i.e., ignoring interpolation). For example:

.selector-one, .selector-#$var-two

becomes:

.selector-one, .selector–two

This is useful for lints that wish to ignore interpolation, since interpolation can’t be resolved at this step.



17
18
19
20
21
# File 'lib/scss_lint/utils.rb', line 17

def extract_string_selectors(selector_array)
  selector_array.reject { |item| item.is_a? Sass::Script::Node }.
                 join.
                 split
end

#node_has_bad_name?(node) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/scss_lint/utils.rb', line 23

def node_has_bad_name?(node)
  node.name =~ /#{INVALID_NAME_CHARS}/
end

#pluralize(value, word) ⇒ Object



59
60
61
# File 'lib/scss_lint/utils.rb', line 59

def pluralize(value, word)
  value == 1 ? "#{value} #{word}" : "#{value} #{word}s"
end

#previous_node(node) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/scss_lint/utils.rb', line 48

def previous_node(node)
  return unless node && parent = node.node_parent
  index = parent.children.index(node)

  if index == 0
    parent
  else
    parent.children[index - 1]
  end
end

#remove_quoted_strings(string) ⇒ Object

Takes a string like ‘hello “world” ’how are’ you` and turns it into: ‘hello you`. This is useful for scanning for keywords in shorthand properties or lists which can contain quoted strings but for which you don’t want to inspect quoted strings (e.g. you care about the actual color keyword ‘red`, not the string “red”).



44
45
46
# File 'lib/scss_lint/utils.rb', line 44

def remove_quoted_strings(string)
  string.gsub(/"[^"]*"|'[^']*'/, '')
end

#shortest_hex_form(hex) ⇒ Object



27
28
29
# File 'lib/scss_lint/utils.rb', line 27

def shortest_hex_form(hex)
  (can_be_condensed?(hex) ? (hex[0..1] + hex[3] + hex[5]) : hex).downcase
end