forked from sds/scss-lint
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
261 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
module SCSSLint | ||
# Check for allowed units | ||
class Linter::PropertyUnits < Linter | ||
include LinterRegistry | ||
|
||
def visit_prop(node) | ||
@node = node | ||
@global_allowed = config['global'].to_set | ||
@properties = config['properties'] | ||
@property = node.name.join | ||
@units = node.value.value.to_s.scan(/[a-zA-Z%]+/ix) | ||
|
||
return if @units.empty? | ||
|
||
global_allows_ok? && property_allows_ok? | ||
end | ||
|
||
private | ||
|
||
def global_allows_ok? | ||
not_allowed = units_not_allowed_globally | ||
unless property_units_defined? | ||
unless not_allowed.empty? | ||
add_lint(@node, "Units are not allowed globally: #{not_allowed.join(' ')}") | ||
end | ||
return false | ||
end | ||
true | ||
end | ||
|
||
def property_allows_ok? | ||
not_allowed = units_not_allowed_on_property | ||
unless not_allowed.empty? | ||
add_lint(@node, "Units are not allowed on #{@property}: #{not_allowed.join(' ')}") | ||
return false | ||
end | ||
true | ||
end | ||
|
||
def units_not_allowed_globally | ||
units_not_allowed @units, @global_allowed | ||
end | ||
|
||
def units_not_allowed_on_property | ||
units_not_allowed @units, @properties[property_key] | ||
end | ||
|
||
def units_not_allowed(units, allowed) | ||
units.select { |unit| !allowed.include?(unit) } | ||
end | ||
|
||
def property_units_defined? | ||
@properties.key? property_key | ||
end | ||
|
||
def property_key | ||
@property.gsub('-', '_') | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
require 'spec_helper' | ||
|
||
describe SCSSLint::Linter::PropertyUnits do | ||
context 'when global units are set and local are not set' do | ||
let(:linter_config) { { 'global' => ['rem'], 'properties' => {} } } | ||
|
||
context 'when unit is allowed' do | ||
let(:scss) { <<-SCSS } | ||
p { | ||
margin: 1rem; | ||
} | ||
SCSS | ||
|
||
it { should_not report_lint } | ||
end | ||
|
||
context 'when unit is not allowed' do | ||
let(:scss) { <<-SCSS } | ||
p { | ||
margin: 1px; | ||
} | ||
SCSS | ||
|
||
it { should report_lint line: 2 } | ||
end | ||
end | ||
|
||
context 'when global and local units are set' do | ||
let(:linter_config) { { 'global' => ['rem'], 'properties' => { 'font_size' => ['px'] } } } | ||
|
||
context 'when unit is allowed locally not globally' do | ||
let(:scss) { <<-SCSS } | ||
p { | ||
font-size: 16px; | ||
} | ||
SCSS | ||
|
||
it { should_not report_lint } | ||
end | ||
|
||
context 'when unit is allowed globally not locally' do | ||
let(:scss) { <<-SCSS } | ||
p { | ||
margin: 1rem; | ||
} | ||
SCSS | ||
|
||
it { should_not report_lint } | ||
end | ||
|
||
context 'when unit is not allowed globally nor locally' do | ||
let(:scss) { <<-SCSS } | ||
p { | ||
margin: 1vh; | ||
} | ||
SCSS | ||
|
||
it { should report_lint line: 2 } | ||
end | ||
end | ||
|
||
context 'when local units are set and global are not set' do | ||
let(:linter_config) { { 'global' => [], 'properties' => { 'margin' => ['rem'] } } } | ||
|
||
context 'when unit is allowed locally not globally' do | ||
let(:scss) { <<-SCSS } | ||
p { | ||
margin: 1rem; | ||
} | ||
SCSS | ||
|
||
it { should_not report_lint } | ||
end | ||
|
||
context 'when unit is not allowed locally nor globally' do | ||
let(:scss) { <<-SCSS } | ||
p { | ||
margin: 10px; | ||
} | ||
SCSS | ||
|
||
it { should report_lint line: 2 } | ||
end | ||
end | ||
|
||
context 'when multiple units are set on a property' do | ||
let(:linter_config) { { 'global' => [], 'properties' => { 'margin' => %w[rem em] } } } | ||
|
||
context 'when one of multiple units is used' do | ||
let(:scss) { <<-SCSS } | ||
p { | ||
margin: 1rem; | ||
} | ||
SCSS | ||
|
||
it { should_not report_lint } | ||
end | ||
|
||
context 'when another of multiple units is used' do | ||
let(:scss) { <<-SCSS } | ||
p { | ||
margin: 1em; | ||
} | ||
SCSS | ||
|
||
it { should_not report_lint } | ||
end | ||
|
||
context 'when a not allowed unit is used' do | ||
let(:scss) { <<-SCSS } | ||
p { | ||
margin: 10px; | ||
} | ||
SCSS | ||
|
||
it { should report_lint line: 2 } | ||
end | ||
end | ||
|
||
context 'when no local units are allowed' do | ||
let(:linter_config) { { 'global' => ['px'], 'properties' => { 'line_height' => [] } } } | ||
|
||
context 'when a disallowed unit is used' do | ||
let(:scss) { <<-SCSS } | ||
p { | ||
line-height: 10px; | ||
} | ||
SCSS | ||
|
||
it { should report_lint line: 2 } | ||
end | ||
|
||
context 'when no unit is used' do | ||
let(:scss) { <<-SCSS } | ||
p { | ||
line-height: 1; | ||
} | ||
SCSS | ||
|
||
it { should_not report_lint } | ||
end | ||
end | ||
end |