Skip to content

Commit

Permalink
Improve ColorVariable to flag uses of rgb/rgba/hsl/hsla
Browse files Browse the repository at this point in the history
  • Loading branch information
cih committed Aug 28, 2015
1 parent bbee72d commit 1759e74
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

* Fix `SpaceAroundOperator` linter to not report false positives operators in
interpolation
* Improve `ColorVariable` to flag uses of rgb/rgba/hsl/hsla

## 0.41.0

Expand Down
20 changes: 20 additions & 0 deletions lib/scss_lint/linter/color_variable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ module SCSSLint
class Linter::ColorVariable < Linter
include LinterRegistry

COLOR_FUNCTIONS = %w[rgb rgba hsl hsla]

def visit_script_color(node)
return if in_variable_declaration?(node) ||
in_map_declaration?(node) ||
Expand Down Expand Up @@ -30,6 +32,14 @@ def visit_comment(_node)
# comments, so it's easiest to just ignore them.
end

def visit_script_funcall(node)
if color_function?(node) && all_arguments_are_literals?(node)
record_lint node, node.to_sass
else
yield
end
end

private

def record_lint(node, color)
Expand Down Expand Up @@ -63,5 +73,15 @@ def in_rgba_function_call?(node)
def in_map_declaration?(node)
node_ancestor(node, 2).is_a?(Sass::Script::Tree::MapLiteral)
end

def all_arguments_are_literals?(node)
node.args.all? do |arg|
arg.is_a?(Sass::Script::Tree::Literal)
end
end

def color_function?(node)
COLOR_FUNCTIONS.include?(node.name)
end
end
end
42 changes: 41 additions & 1 deletion spec/scss_lint/linter/color_variable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@
}
SCSS

it { should_not report_lint }
it { should report_lint line: 2 }
end

context 'when a color literal is used in a map declaration' do
Expand Down Expand Up @@ -164,4 +164,44 @@

it { should_not report_lint }
end

context 'when a color function containing literals is used in a property' do
let(:scss) { <<-SCSS }
p {
color: rgb(0, 100, 200);
}
a {
color: rgb(0%, 50%, 80%);
}
i {
color: rgba(0, 0, 0, .5);
}
span {
color: hsl(0, 100%, 50%);
}
.class {
color: hsla(0, 100%, 50%, .5);
}
SCSS

it { should report_lint line: 2 }
it { should report_lint line: 5 }
it { should report_lint line: 8 }
it { should report_lint line: 11 }
it { should report_lint line: 14 }
end

context 'when transforming a variable value in a function call' do
let(:scss) { <<-SCSS }
p {
color: rgba($red, .5);
}
a {
color: lighten($red, 5%);
}
SCSS

it { should_not report_lint }
end
end

0 comments on commit 1759e74

Please sign in to comment.