Sha256: d1ae210bbd93a89f7437be8a41abb5a2d6104ab1ccd513f0f9e6b04750b97523

Contents?: true

Size: 1.3 KB

Versions: 12

Compression:

Stored size: 1.3 KB

Contents

# frozen_string_literal: true

module SCSSLint
  # Checks for the unnecessary inclusion of a zero-value mantissa in numbers.
  # (e.g. `4.0` could be written as just `4`)
  class Linter::UnnecessaryMantissa < Linter
    include LinterRegistry

    def visit_script_string(node)
      return unless node.type == :identifier
      return if node.value =~ /^'|"/
      return if url_literal?(node)

      node.value.scan(REAL_NUMBER_REGEX) do |number, integer, mantissa, units|
        if unnecessary_mantissa?(mantissa)
          add_lint(node, MESSAGE_FORMAT % [number, integer, units])
        end
      end
    end

    def visit_script_number(node)
      return unless match = REAL_NUMBER_REGEX.match(source_from_range(node.source_range))
      return unless unnecessary_mantissa?(match[:mantissa])

      add_lint(node, MESSAGE_FORMAT % [match[:number], match[:integer],
                                       match[:units]])
    end

  private

    REAL_NUMBER_REGEX = /
      \b(?<number>
        (?<integer>\d*)
        \.
        (?<mantissa>\d+)
        (?<units>\w*)
      )\b
    /ix

    MESSAGE_FORMAT = '`%s` should be written without the mantissa as `%s%s`'.freeze

    def unnecessary_mantissa?(mantissa)
      mantissa !~ /[^0]/
    end

    def url_literal?(node)
      node.value.start_with?('url(')
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
scss_lint-0.57.1 lib/scss_lint/linter/unnecessary_mantissa.rb
scss_lint-0.57.0 lib/scss_lint/linter/unnecessary_mantissa.rb
scss_lint-0.56.0 lib/scss_lint/linter/unnecessary_mantissa.rb
scss_lint-0.55.0 lib/scss_lint/linter/unnecessary_mantissa.rb
scss_lint-0.54.0 lib/scss_lint/linter/unnecessary_mantissa.rb
scss_lint-0.53.0 lib/scss_lint/linter/unnecessary_mantissa.rb
scss_lint-0.52.0 lib/scss_lint/linter/unnecessary_mantissa.rb
scss_lint-0.51.0 lib/scss_lint/linter/unnecessary_mantissa.rb
scss_lint-0.50.3 lib/scss_lint/linter/unnecessary_mantissa.rb
scss_lint-0.50.2 lib/scss_lint/linter/unnecessary_mantissa.rb
scss_lint-0.50.1 lib/scss_lint/linter/unnecessary_mantissa.rb
scss_lint-0.50.0 lib/scss_lint/linter/unnecessary_mantissa.rb