Sha256: ec80192211477ffe0ee7f0551fa3da5263526e644b1b8c77a46eb33529004b3a

Contents?: true

Size: 1.21 KB

Versions: 21

Compression:

Stored size: 1.21 KB

Contents

module SCSSLint
  # Checks for unnecessary trailing zeros in numeric values with decimal points.
  class Linter::TrailingZero < Linter
    include LinterRegistry

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

      non_string_values = remove_quoted_strings(node.value).split
      non_string_values.each do |value|
        next unless number = value[FRACTIONAL_DIGIT_REGEX, 1]
        check_for_trailing_zeros(node, number)
      end
    end

    def visit_script_number(node)
      return unless number =
        source_from_range(node.source_range)[FRACTIONAL_DIGIT_REGEX, 1]

      check_for_trailing_zeros(node, number)
    end

  private

    FRACTIONAL_DIGIT_REGEX = /^-?(\d*\.\d+)/

    def check_for_trailing_zeros(node, original_number)
      return unless match = /^(\d*\.\d*)0+$/.match(original_number)

      fixed_number = match[1]

      # Handle special case of 0 being the only trailing digit
      fixed_number = fixed_number[0..-2] if fixed_number.end_with?('.')
      fixed_number = 0 if fixed_number.empty? # Handle ".0" -> "0"

      add_lint(node,
               "`#{original_number}` should be written without a trailing " \
               "zero as `#{fixed_number}`")
    end
  end
end

Version data entries

21 entries across 21 versions & 3 rubygems

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