Sha256: 43b3655a69b83f63853a95dac17af8f24b28b689c204fff3aea1cb11adae1828

Contents?: true

Size: 1.13 KB

Versions: 25

Compression:

Stored size: 1.13 KB

Contents

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

      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`'

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

Version data entries

25 entries across 25 versions & 3 rubygems

Version Path
scss_lint-0.40.1 lib/scss_lint/linter/unnecessary_mantissa.rb
scss_lint-0.40.0 lib/scss_lint/linter/unnecessary_mantissa.rb
scss_lint-0.39.0 lib/scss_lint/linter/unnecessary_mantissa.rb
scss_lint-0.38.0 lib/scss_lint/linter/unnecessary_mantissa.rb
scss-lint-0.38.0 lib/scss_lint/linter/unnecessary_mantissa.rb
scss-lint-bliss-1.0.0 lib/scss_lint/linter/unnecessary_mantissa.rb
scss-lint-0.37.0 lib/scss_lint/linter/unnecessary_mantissa.rb
scss-lint-0.36.1 lib/scss_lint/linter/unnecessary_mantissa.rb
scss-lint-0.36.0 lib/scss_lint/linter/unnecessary_mantissa.rb
scss-lint-0.35.0 lib/scss_lint/linter/unnecessary_mantissa.rb
scss-lint-0.34.0 lib/scss_lint/linter/unnecessary_mantissa.rb
scss-lint-0.33.0 lib/scss_lint/linter/unnecessary_mantissa.rb
scss-lint-0.32.0 lib/scss_lint/linter/unnecessary_mantissa.rb
scss-lint-0.31.0 lib/scss_lint/linter/unnecessary_mantissa.rb
scss-lint-0.30.0 lib/scss_lint/linter/unnecessary_mantissa.rb
scss-lint-0.29.0 lib/scss_lint/linter/unnecessary_mantissa.rb
scss-lint-0.28.0 lib/scss_lint/linter/unnecessary_mantissa.rb
scss-lint-0.27.0 lib/scss_lint/linter/unnecessary_mantissa.rb
scss-lint-0.26.2 lib/scss_lint/linter/unnecessary_mantissa.rb
scss-lint-0.26.1 lib/scss_lint/linter/unnecessary_mantissa.rb