Sha256: ec2419e9b03d5569a1e106007fb37b3ffcf3757c52b52637ff6c6a8e619a1454

Contents?: true

Size: 1.07 KB

Versions: 32

Compression:

Stored size: 1.07 KB

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|
        next unless zero_with_length_units?(match.first)
        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]
      return unless zero_with_length_units?(length)

      add_lint(node, MESSAGE_FORMAT % length)
    end

  private

    ZERO_UNIT_REGEX = /
      \b
      (?<!\.|\#)    # Ignore zeroes following `#` (colors) or `.` (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`'

    LENGTH_UNITS = %w[em ex ch rem vw vh vmin vmax cm mm in pt pc px].to_set

    def zero_with_length_units?(string)
      string =~ /^0([a-z]+)/ && LENGTH_UNITS.include?(Regexp.last_match(1))
    end
  end
end

Version data entries

32 entries across 32 versions & 3 rubygems

Version Path
scss_lint-0.43.2 lib/scss_lint/linter/zero_unit.rb
scss_lint-0.43.1 lib/scss_lint/linter/zero_unit.rb
scss_lint-0.43.0 lib/scss_lint/linter/zero_unit.rb
scss_lint-0.42.2 lib/scss_lint/linter/zero_unit.rb
scss_lint-0.42.1 lib/scss_lint/linter/zero_unit.rb
scss_lint-0.42.0 lib/scss_lint/linter/zero_unit.rb
scss_lint-0.41.0 lib/scss_lint/linter/zero_unit.rb
scss_lint-0.40.1 lib/scss_lint/linter/zero_unit.rb
scss_lint-0.40.0 lib/scss_lint/linter/zero_unit.rb
scss_lint-0.39.0 lib/scss_lint/linter/zero_unit.rb
scss_lint-0.38.0 lib/scss_lint/linter/zero_unit.rb
scss-lint-0.38.0 lib/scss_lint/linter/zero_unit.rb
scss-lint-bliss-1.0.0 lib/scss_lint/linter/zero_unit.rb
scss-lint-0.37.0 lib/scss_lint/linter/zero_unit.rb
scss-lint-0.36.1 lib/scss_lint/linter/zero_unit.rb
scss-lint-0.36.0 lib/scss_lint/linter/zero_unit.rb
scss-lint-0.35.0 lib/scss_lint/linter/zero_unit.rb
scss-lint-0.34.0 lib/scss_lint/linter/zero_unit.rb
scss-lint-0.33.0 lib/scss_lint/linter/zero_unit.rb
scss-lint-0.32.0 lib/scss_lint/linter/zero_unit.rb