This repository was archived by the owner on Aug 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 149
Cleanup of redundancy code #38
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -5,57 +5,62 @@ def initialize(raw_css) | |
| @raw_css = raw_css | ||
| end | ||
|
|
||
| def border_property(property, dec, sel) | ||
| border_dec = Declaration.new(property, dec.value) | ||
| @parents[border_dec] ||= [] | ||
| (@parents[border_dec] << dec).uniq! | ||
| border_dec.parents = @parents[border_dec] | ||
|
|
||
| @matches[border_dec] ||= [] | ||
| @matches[border_dec] << sel | ||
| @matches[border_dec].uniq! | ||
| end | ||
|
|
||
| def redundancies(opts = {}) | ||
| minimum = opts[:minimum] | ||
| ignored_properties = opts[:ignored_properties] || [] | ||
| ignored_selectors = opts[:ignored_selectors] || [] | ||
| @minimum = opts[:minimum] | ||
| @ignored_properties = opts[:ignored_properties] || [] | ||
| @ignored_selectors = opts[:ignored_selectors] || [] | ||
|
|
||
| rule_sets = Parser::Css.parse(@raw_css) | ||
| matches = {} | ||
| parents = {} | ||
| @matches = {} | ||
| @parents = {} | ||
|
|
||
| rule_sets.each do |rule_set| | ||
| next if ignored_selectors.include?(rule_set.selectors.selectors) | ||
| next if @ignored_selectors.include?(rule_set.selectors.selectors) | ||
| sel = rule_set.selectors | ||
|
|
||
| rule_set.declarations.each do |dec| | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of moving this into a method, I think this key-appending logic is best refactored. I know I copy/pasted it a couple times throughout the class. That was going to be the first place I hit when I planned on refactoring this. |
||
| next if ignored_properties.include?(dec.property) | ||
| next if @ignored_properties.include?(dec.property) | ||
|
|
||
| if parser = shorthand_parser(dec.property) | ||
| if new_decs = parser.parse(dec.property, dec.value) | ||
| if dec.property == "border" | ||
| %w(border-top border-right border-bottom border-left).each do |property| | ||
| border_dec = Declaration.new(property, dec.value) | ||
| parents[border_dec] ||= [] | ||
| (parents[border_dec] << dec).uniq! | ||
| border_dec.parents = parents[border_dec] | ||
|
|
||
| matches[border_dec] ||= [] | ||
| matches[border_dec] << sel | ||
| matches[border_dec].uniq! | ||
| border_property(property, dec, sel) | ||
| end | ||
| end | ||
|
|
||
| new_decs.each do |new_dec| | ||
| # replace any non-derivatives with derivatives | ||
| existing = matches.delete(new_dec) || [] | ||
| existing = @matches.delete(new_dec) || [] | ||
| existing << sel | ||
| parents[new_dec] ||= [] | ||
| (parents[new_dec] << dec).uniq! | ||
| new_dec.parents = parents[new_dec] | ||
| matches[new_dec] = existing | ||
| matches[new_dec].uniq! | ||
| @parents[new_dec] ||= [] | ||
| (@parents[new_dec] << dec).uniq! | ||
| new_dec.parents = @parents[new_dec] | ||
| @matches[new_dec] = existing | ||
| @matches[new_dec].uniq! | ||
| end | ||
| end | ||
| end | ||
|
|
||
| matches[dec] ||= [] | ||
| matches[dec] << sel | ||
| matches[dec].uniq! | ||
| @matches[dec] ||= [] | ||
| @matches[dec] << sel | ||
| @matches[dec].uniq! | ||
| end | ||
| end | ||
|
|
||
| inverted_matches = {} | ||
| matches.each do |declaration, selector_groups| | ||
| @matches.each do |declaration, selector_groups| | ||
| if selector_groups.size > 1 | ||
| selector_groups.combination(2).each do |two_selectors| | ||
| inverted_matches[two_selectors] ||= [] | ||
|
|
@@ -86,9 +91,9 @@ def redundancies(opts = {}) | |
| end | ||
| end | ||
|
|
||
| if minimum | ||
| if @minimum | ||
| inverted_matches.delete_if do |key, declarations| | ||
| declarations.size < minimum | ||
| declarations.size < @minimum | ||
| end | ||
| end | ||
|
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I generally shy away from making instance variables within a method unless it makes sense for the object itself.