Module: SCSSLint::Utils
- Included in:
- Linter
- Defined in:
- lib/scss_lint/utils.rb
Overview
Collection of helpers used across a variety of linters.
Constant Summary collapse
- COLOR_REGEX =
/^#[a-f0-9]{3,6}$/i.freeze
Instance Method Summary collapse
-
#color?(string) ⇒ true, false
Returns whether the given string is a color literal (keyword or hex code).
-
#color_hex?(string) ⇒ true, false
Returns whether the given string is a color hexadecimal code.
-
#color_keyword?(string) ⇒ true, false
Returns whether the given string is a valid color keyword.
-
#color_keyword_to_code(string) ⇒ String
Returns the hexadecimal code for the given color keyword.
-
#else_node?(node) ⇒ true, false
Returns whether a node is an IfNode corresponding to an @else/@else if statement.
-
#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).
-
#node_ancestor(node, levels) ⇒ Sass::Tree::Node, ...
Return nth-ancestor of a node, where 1 is the parent, 2 is grandparent, etc.
- #node_siblings(node) ⇒ Object
- #pluralize(value, word) ⇒ Object
- #previous_node(node) ⇒ Object
-
#remove_quoted_strings(string) ⇒ Object
Takes a string like ‘hello “world” ’how are’ you` and turns it into: ‘hello you`.
-
#same_position?(pos1, pos2) ⇒ Boolean
Sass doesn’t define an equality operator for Sass::Source::Position objects, so we define a helper for our own use.
Instance Method Details
#color?(string) ⇒ true, false
Returns whether the given string is a color literal (keyword or hex code).
10 11 12 |
# File 'lib/scss_lint/utils.rb', line 10 def color?(string) color_keyword?(string) || color_hex?(string) end |
#color_hex?(string) ⇒ true, false
Returns whether the given string is a color hexadecimal code.
18 19 20 |
# File 'lib/scss_lint/utils.rb', line 18 def color_hex?(string) string =~ COLOR_REGEX end |
#color_keyword?(string) ⇒ true, false
Returns whether the given string is a valid color keyword.
26 27 28 |
# File 'lib/scss_lint/utils.rb', line 26 def color_keyword?(string) color_keyword_to_code(string) && string != 'transparent' end |
#color_keyword_to_code(string) ⇒ String
Returns the hexadecimal code for the given color keyword.
34 35 36 |
# File 'lib/scss_lint/utils.rb', line 34 def color_keyword_to_code(string) Sass::Script::Value::Color::COLOR_NAMES[string] end |
#else_node?(node) ⇒ true, false
Returns whether a node is an IfNode corresponding to an @else/@else if statement.
43 44 45 |
# File 'lib/scss_lint/utils.rb', line 43 def else_node?(node) source_from_range(node.source_range).strip.start_with?('@else') 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.
60 61 62 63 64 |
# File 'lib/scss_lint/utils.rb', line 60 def extract_string_selectors(selector_array) selector_array.reject { |item| item.is_a? Sass::Script::Node } .join .split end |
#node_ancestor(node, levels) ⇒ Sass::Tree::Node, ...
Return nth-ancestor of a node, where 1 is the parent, 2 is grandparent, etc.
100 101 102 103 104 105 106 107 108 |
# File 'lib/scss_lint/utils.rb', line 100 def node_ancestor(node, levels) while levels > 0 node = node.node_parent return unless node levels -= 1 end node end |
#node_siblings(node) ⇒ Object
87 88 89 90 91 92 |
# File 'lib/scss_lint/utils.rb', line 87 def node_siblings(node) return unless node && node.node_parent node.node_parent .children .select { |child| child.is_a?(Sass::Tree::Node) } end |
#pluralize(value, word) ⇒ Object
110 111 112 |
# File 'lib/scss_lint/utils.rb', line 110 def pluralize(value, word) value == 1 ? "#{value} #{word}" : "#{value} #{word}s" end |
#previous_node(node) ⇒ Object
76 77 78 79 80 81 82 83 84 85 |
# File 'lib/scss_lint/utils.rb', line 76 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”).
72 73 74 |
# File 'lib/scss_lint/utils.rb', line 72 def remove_quoted_strings(string) string.gsub(/"[^"]*"|'[^']*'/, '') end |
#same_position?(pos1, pos2) ⇒ Boolean
Sass doesn’t define an equality operator for Sass::Source::Position objects, so we define a helper for our own use.
116 117 118 119 |
# File 'lib/scss_lint/utils.rb', line 116 def same_position?(pos1, pos2) return unless pos1 && pos2 pos1.line == pos2.line && pos1.offset == pos2.offset end |