Sha256: 7b650b17b80ac1e67e1ea70d332a9104633c1a1c9ff3bbeff94c9e4b7f81bf5e

Contents?: true

Size: 921 Bytes

Versions: 2

Compression:

Stored size: 921 Bytes

Contents

module SCSSLint
  # Checks for unnecessary units on zero values.
  class Linter::ZeroUnit < Linter
    include LinterRegistry

    def visit_script_string(node)
      return unless node.type == :identifier

      node.value.scan(ZERO_UNIT_REGEX) do |match|
        add_lint(node, MESSAGE_FORMAT % match.first)
      end
    end

    def visit_script_number(node)
      length = source_from_range(node.source_range)[ZERO_UNIT_REGEX, 1]

      if node.value == 0 && zero_with_units?(length)
        add_lint(node, MESSAGE_FORMAT % length)
      end
    end

  private

    ZERO_UNIT_REGEX = %r{
      \b
      (?<!\.|\#)    # Ignore zeroes following `#` or `.` (colors / decimals)
      (0[a-z]+)     # Zero followed by letters (indicating some sort of unit)
      \b
    }ix

    MESSAGE_FORMAT = '`%s` should be written without units as `0`'

    def zero_with_units?(string)
      string =~ /^0[a-z]+/
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
scss-lint-0.23.1 lib/scss_lint/linter/zero_unit.rb
scss-lint-0.23.0 lib/scss_lint/linter/zero_unit.rb