Skip to content

Commit 16918da

Browse files
committed
Adds --no-match-shorthand so users can opt out of shorthand matching
I'm fine with keeping shorthand matching the default, but I respect some people don't like it. refs: zmoazeni#20
1 parent c80ad2a commit 16918da

File tree

4 files changed

+19
-2
lines changed

4 files changed

+19
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* Adds --compass-with-config that lets users specify a config
1212
* Fixes parser error bug when trying to parse blank files
1313
* Fixes bug where rules from multiple files aren't consolidated
14+
* Adds --no-match-shorthand to allow users to opt out of shorthand matching
1415

1516
## 1.0.0 - 4/7/2013 ##
1617

lib/csscss/cli.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ def initialize(argv)
88
@compass = false
99
@ignored_properties = []
1010
@ignored_selectors = []
11+
@match_shorthand = true
1112
end
1213

1314
def run
@@ -47,7 +48,8 @@ def execute
4748
redundancies = RedundancyAnalyzer.new(all_contents).redundancies(
4849
minimum: @minimum,
4950
ignored_properties: @ignored_properties,
50-
ignored_selectors: @ignored_selectors
51+
ignored_selectors: @ignored_selectors,
52+
match_shorthand: @match_shorthand
5153
)
5254

5355
if @json
@@ -108,6 +110,10 @@ def parse(argv)
108110
enable_compass(config)
109111
end
110112

113+
opts.on("--[no-]match-shorthand", "Expands shorthand rules and matches on explicit rules (default is true)") do |match_shorthand|
114+
@match_shorthand = match_shorthand
115+
end
116+
111117
opts.on("-j", "--[no-]json", "Output results in JSON") do |j|
112118
@json = j
113119
end

lib/csscss/redundancy_analyzer.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ def redundancies(opts = {})
99
minimum = opts[:minimum]
1010
ignored_properties = opts[:ignored_properties] || []
1111
ignored_selectors = opts[:ignored_selectors] || []
12+
match_shorthand = opts.fetch(:match_shorthand, true)
1213

1314
rule_sets = Parser::Css.parse(@raw_css)
1415
matches = {}
@@ -20,7 +21,7 @@ def redundancies(opts = {})
2021
rule_set.declarations.each do |dec|
2122
next if ignored_properties.include?(dec.property)
2223

23-
if parser = shorthand_parser(dec.property)
24+
if match_shorthand && parser = shorthand_parser(dec.property)
2425
if new_decs = parser.parse(dec.property, dec.value)
2526
if dec.property == "border"
2627
%w(border-top border-right border-bottom border-left).each do |property|

test/csscss/redundancy_analyzer_test.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,15 @@ module Csscss
184184
})
185185
end
186186

187+
it "doesn't match shorthand when explicitly turned off" do
188+
css = %$
189+
.foo { background-color: #fff }
190+
.bar { background: #fff }
191+
$
192+
193+
RedundancyAnalyzer.new(css).redundancies(match_shorthand:false).must_equal({})
194+
end
195+
187196
it "3-way case consolidation" do
188197
css = %$
189198
.bar { background: #fff }

0 commit comments

Comments
 (0)