Class: SCSSLint::AutoCorrect::PropertySorter
- Inherits:
-
Object
- Object
- SCSSLint::AutoCorrect::PropertySorter
- Defined in:
- lib/scss_lint/auto_correct/property_sorter.rb
Instance Method Summary collapse
- #compare_by_order(a, b) ⇒ Object
- #compare_by_vendor(a, b) ⇒ Object
- #compare_lines(a, b) ⇒ Object
- #compare_properties(a, b) ⇒ Object
-
#initialize(rules: "") ⇒ PropertySorter
constructor
A new instance of PropertySorter.
- #parse_line(line) ⇒ Object
- #preferred_order ⇒ Object
- #property?(line) ⇒ Boolean
- #sort_lines(lines) ⇒ Object
Constructor Details
#initialize(rules: "") ⇒ PropertySorter
Returns a new instance of PropertySorter.
5 6 7 |
# File 'lib/scss_lint/auto_correct/property_sorter.rb', line 5 def initialize(rules: "") @rules = rules end |
Instance Method Details
#compare_by_order(a, b) ⇒ Object
74 75 76 77 |
# File 'lib/scss_lint/auto_correct/property_sorter.rb', line 74 def compare_by_order(a, b) (preferred_order.index(a[:property]) || Float::INFINITY) <=> (preferred_order.index(b[:property]) || Float::INFINITY) end |
#compare_by_vendor(a, b) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/scss_lint/auto_correct/property_sorter.rb', line 79 def compare_by_vendor(a, b) if a[:vendor] && b[:vendor] a[:vendor] <=> b[:vendor] elsif a[:vendor] -1 elsif b[:vendor] 1 else 0 end end |
#compare_lines(a, b) ⇒ Object
58 59 60 61 62 |
# File 'lib/scss_lint/auto_correct/property_sorter.rb', line 58 def compare_lines(a, b) compare_properties \ parse_line(a), parse_line(b) end |
#compare_properties(a, b) ⇒ Object
64 65 66 67 68 69 70 71 72 |
# File 'lib/scss_lint/auto_correct/property_sorter.rb', line 64 def compare_properties(a, b) if a[:property] == b[:property] compare_by_vendor(a, b) elsif preferred_order.any? compare_by_order(a, b) else a[:property] <=> b[:property] end end |
#parse_line(line) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/scss_lint/auto_correct/property_sorter.rb', line 35 def parse_line(line) mm = line.match(/^\s*([^:]+):/) full = mm[1] ['$', '@', '('].each do |char| return nil if full.include? char end if full[0] == "-" mm2 = full.match(/^-([^-]+)-(.*)$/) { vendor: mm2[1], property: mm2[2], line: line } else { property: full, line: line } end rescue nil end |
#preferred_order ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/scss_lint/auto_correct/property_sorter.rb', line 9 def preferred_order return @preferred_order unless @preferred_order.nil? path = File.join \ SCSSLint::SCSS_LINT_DATA, "property-sort-orders", "#{@rules}.txt" @preferred_order = [] if File.file?(path) @preferred_order = File.open(path) \ .read .split("\n") .reject { |line| line =~ /^\s*#/ || line.strip == "" } end @preferred_order end |
#property?(line) ⇒ Boolean
28 29 30 31 32 33 |
# File 'lib/scss_lint/auto_correct/property_sorter.rb', line 28 def property?(line) parsed = parse_line(line) return false if parsed.nil? !!parsed[:property] # !preferred_order.index(parsed[:property]).nil? end |
#sort_lines(lines) ⇒ Object
91 92 93 |
# File 'lib/scss_lint/auto_correct/property_sorter.rb', line 91 def sort_lines(lines) lines.sort { |a,b| compare_lines(a, b) } end |