Sha256: c1bc7271624a84d81566711833f2f3c23188a43d221bf8d876ebd3f957c0eb6c

Contents?: true

Size: 1.54 KB

Versions: 21

Compression:

Stored size: 1.54 KB

Contents

module SCSSLint
  # Checks for unnecessary leading zeros in numeric values with decimal points.
  class Linter::LeadingZero < 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[NUMBER_WITH_LEADING_ZERO_REGEX, 1]
        check_for_leading_zeros(node, number)
      end
    end

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

      check_for_leading_zeros(node, number)
    end

  private

    NUMBER_WITH_LEADING_ZERO_REGEX = /^-?(0?\.\d+)/

    CONVENTIONS = {
      'exclude_zero' => {
        explanation: '`%s` should be written without a leading zero as `%s`',
        validator: ->(original) { original =~ /^\.\d+$/ },
        converter: ->(original) { original[1..-1] },
      },
      'include_zero' => {
        explanation: '`%s` should be written with a leading zero as `%s`',
        validator: ->(original) { original =~ /^0\.\d+$/ },
        converter: ->(original) { "0#{original}" }
      },
    }

    def check_for_leading_zeros(node, original_number)
      style = config.fetch('style', 'exclude_zero')
      convention = CONVENTIONS[style]
      return if convention[:validator].call(original_number)

      corrected = convention[:converter].call(original_number)
      add_lint(node, convention[:explanation] % [original_number, corrected])
    end
  end
end

Version data entries

21 entries across 21 versions & 3 rubygems

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