Sha256: fae6b11e7c346aff908abb5a2e9e567b040b11ff0b478743b237cc7bba443ab4

Contents?: true

Size: 1.18 KB

Versions: 16

Compression:

Stored size: 1.18 KB

Contents

module SCSSLint
  # Reports the use of literals for properties where variables are prefered.
  class Linter::VariableForProperty < Linter
    include LinterRegistry

    IGNORED_VALUES = %w[currentColor inherit initial transparent].freeze

    def visit_root(_node)
      @properties = Set.new(config['properties'])
      yield if @properties.any?
    end

    def visit_prop(node)
      property_name = node.name.join
      return unless @properties.include?(property_name)
      return if ignored_value?(node.value)
      return if node.children.first.is_a?(Sass::Script::Tree::Variable)
      return if variable_property_with_important?(node.value)

      add_lint(node, "Property #{property_name} should use " \
                     'a variable rather than a literal value')
    end

  private

    def variable_property_with_important?(value)
      value.is_a?(Sass::Script::Tree::ListLiteral) &&
        value.children.length == 2 &&
        value.children.first.is_a?(Sass::Script::Tree::Variable) &&
        value.children.last.value.value == '!important'
    end

    def ignored_value?(value)
      value.respond_to?(:value) &&
        IGNORED_VALUES.include?(value.value.to_s)
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
scss_lint-0.55.0 lib/scss_lint/linter/variable_for_property.rb
scss_lint-0.54.0 lib/scss_lint/linter/variable_for_property.rb
scss_lint-0.53.0 lib/scss_lint/linter/variable_for_property.rb
scss_lint-0.52.0 lib/scss_lint/linter/variable_for_property.rb
scss_lint-0.51.0 lib/scss_lint/linter/variable_for_property.rb
scss_lint-0.50.3 lib/scss_lint/linter/variable_for_property.rb
scss_lint-0.50.2 lib/scss_lint/linter/variable_for_property.rb
scss_lint-0.50.1 lib/scss_lint/linter/variable_for_property.rb
scss_lint-0.50.0 lib/scss_lint/linter/variable_for_property.rb
scss_lint-0.49.0 lib/scss_lint/linter/variable_for_property.rb
scss_lint-0.48.0 lib/scss_lint/linter/variable_for_property.rb
scss_lint-0.47.1 lib/scss_lint/linter/variable_for_property.rb
scss_lint-0.47.0 lib/scss_lint/linter/variable_for_property.rb
scss_lint-0.46.0 lib/scss_lint/linter/variable_for_property.rb
scss_lint-0.45.0 lib/scss_lint/linter/variable_for_property.rb
scss_lint-0.44.0 lib/scss_lint/linter/variable_for_property.rb