class SCSSLint::Linter::LengthVariable

Ensures length literals are used only in variable declarations.

Constants

LENGTH_RE
LENGTH_UNITS

Public Instance Methods

visit_media(node) click to toggle source
# File lib/scss_lint/linter/length_variable.rb, line 37
def visit_media(node)
  lint_lengths(node)
end
visit_mixin(node) click to toggle source
# File lib/scss_lint/linter/length_variable.rb, line 41
def visit_mixin(node)
  lint_lengths(node)
end
visit_mixindef(node) click to toggle source
# File lib/scss_lint/linter/length_variable.rb, line 33
def visit_mixindef(node)
  lint_lengths(node)
end
visit_prop(node) click to toggle source
# File lib/scss_lint/linter/length_variable.rb, line 28
def visit_prop(node)
  return if allowed_prop?(node)
  lint_lengths(node)
end

Private Instance Methods

allowed_prop?(node) click to toggle source
# File lib/scss_lint/linter/length_variable.rb, line 62
def allowed_prop?(node)
  config['allowed_properties'] && config['allowed_properties'].include?(node.name.first.to_s)
end
extract_lengths(node) click to toggle source

Though long, This method is clear enough in a boring, dispatch kind of way. rubocop:disable Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/AbcSize

# File lib/scss_lint/linter/length_variable.rb, line 68
def extract_lengths(node)
  case node
  when Sass::Tree::PropNode
    extract_lengths(node.value)
  when Sass::Script::Tree::Literal
    extract_lengths_from_string(node.value)
  when String
    extract_lengths_from_string(node)
  when Sass::Script::Tree::Funcall,
       Sass::Tree::MixinNode,
       Sass::Tree::MixinDefNode
    extract_lengths_from_list(*node.args)
  when Sass::Script::Tree::ListLiteral
    extract_lengths_from_list(*node.elements)
  when Sass::Tree::MediaNode
    extract_lengths_from_list(*node.query)
  when Array
    extract_lengths_from_list(*node)
  when Sass::Script::Tree::Interpolation
    extract_lengths_from_list(node.before, node.mid, node.after)
  when Sass::Script::Tree::Operation
    extract_lengths_from_list(node.operand1, node.operand2)
  when Sass::Script::Tree::UnaryOperation
    extract_lengths(node.operand)
  when Sass::Script::Tree::Variable
    nil
  end
end
extract_lengths_from_list(*values) click to toggle source
# File lib/scss_lint/linter/length_variable.rb, line 103
def extract_lengths_from_list(*values)
  values.map { |v| extract_lengths(v) }
end
extract_lengths_from_string(string) click to toggle source

rubocop:enable Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/AbcSize

# File lib/scss_lint/linter/length_variable.rb, line 98
def extract_lengths_from_string(string)
  matchdata = string.to_s.match(LENGTH_RE)
  matchdata && matchdata.captures
end
lint_lengths(node) click to toggle source
# File lib/scss_lint/linter/length_variable.rb, line 47
def lint_lengths(node)
  lengths = extract_lengths(node)
  lengths = [lengths].flatten.compact.uniq
  lengths -= config['allowed_lengths'] if config['allowed_lengths']
  lengths.each do |length|
    record_lint(node, length) unless lengths.empty?
  end
end
record_lint(node, length) click to toggle source
# File lib/scss_lint/linter/length_variable.rb, line 56
def record_lint(node, length)
  add_lint node, "Length literals like `#{length}` should only be used in " \
                 'variable declarations; they should be referred to via ' \
                 'variables everywhere else.'
end