scss_lint-auto_correct 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ecfcbc916da720f957affb069abd0cb138469b9e
4
- data.tar.gz: 681f8344e431b2ed948a72efa3446ee1b954a4ea
3
+ metadata.gz: fba2efff4f1df225710a78b728482a14cc8131a4
4
+ data.tar.gz: 12f5ab2d724a3b3127d39246d9e5441bfb130b5c
5
5
  SHA512:
6
- metadata.gz: b26b98b6112520ff69a4e95547e9bb6211c5f1446481f982e8aab95d3e316281ab3be3a3315af687572cc4d488334e06d0adbdf09dc40edfcc341b283777b188
7
- data.tar.gz: 4d28006157b8d380470eef8d1205f573c9840524b90dbcf44399e58aafedb4b5d39a211d613d9bc4ca242eeea6ee0e7636d9efca0903fbb16a501ca8a65b5127
6
+ metadata.gz: c9442a72c3feefe5d8c588510f560f031a175ae7d9c5ad2ef0795bb82f9400a0e63c52b26ca872be2607ad311129dce7cb1591793ab1e29b2682afaf5ddb2f4e
7
+ data.tar.gz: 8fe3ac34f4de8717ca5db69bf396f0cecaa6dbfb91ebcb08601dddaf23e3c36cf92028ad263beb5e4d4761e1a4547db681be5c81b4c82528e9ca99d09fa67cc9
@@ -61,6 +61,7 @@ module SCSSLint::AutoCorrect
61
61
  puts " --verbose, -v"
62
62
  puts " --dry"
63
63
  puts " --list"
64
+ puts " --force Ignore config disabling of corrector"
64
65
  puts "Only apply specific correctors, using any combination of:"
65
66
  @correctors.each do |c|
66
67
  puts " --#{c.short_name}"
@@ -123,8 +124,9 @@ module SCSSLint::AutoCorrect
123
124
 
124
125
  def invoke_correctors(contents)
125
126
  @correctors.each do |corrector|
126
- config = corrector.config_name ? @config.options['linters'].fetch(corrector.config_name) : {}
127
- contents = corrector.new(config).call(contents)
127
+ config = corrector.linter_name ? @config.options['linters'].fetch(corrector.linter_name) : {}
128
+ instance = corrector.new(config)
129
+ contents = instance.call(contents) if instance.enabled? || option?(:force)
128
130
  end
129
131
  contents
130
132
  end
@@ -1,6 +1,6 @@
1
1
  module SCSSLint::AutoCorrect::Correctors
2
2
  class Base
3
- def self.config_name
3
+ def self.linter_name
4
4
  end
5
5
 
6
6
  def self.short_name
@@ -0,0 +1,53 @@
1
+ module SCSSLint::AutoCorrect::Correctors
2
+ class PropertySortOrder < Base
3
+ def initialize(config = {})
4
+ super
5
+ @result = []
6
+ @buffer = []
7
+ @enabled = true
8
+ @sorter = SCSSLint::AutoCorrect::PropertySorter.new rules: @config["order"]
9
+ end
10
+
11
+ def call(source)
12
+ source.lines.each { |line| visit_line(line) }
13
+ flush_buffer!
14
+ @result.join
15
+ end
16
+
17
+ def visit_line(line)
18
+ if line =~ /scss-lint:disable.*PropertySortOrder/
19
+ @result << line
20
+ @enabled = false
21
+ return
22
+ end
23
+ if line =~ /scss-lint:enable.*PropertySortOrder/
24
+ @result << line
25
+ @enabled = true
26
+ return
27
+ end
28
+ if @enabled && @sorter.property?(line)
29
+ @buffer << line
30
+ else
31
+ flush_buffer!
32
+ @result << line
33
+ end
34
+ end
35
+
36
+ def flush_buffer!
37
+ @result += reduce_buffer(@buffer)
38
+ @buffer = []
39
+ end
40
+
41
+ def reduce_buffer(lines)
42
+ @sorter.sort_lines(lines)
43
+ end
44
+
45
+ def self.linter_name
46
+ "PropertySortOrder"
47
+ end
48
+
49
+ def self.description
50
+ "Sorts properties correctly"
51
+ end
52
+ end
53
+ end
@@ -4,6 +4,10 @@ module SCSSLint::AutoCorrect::Correctors
4
4
  contents.gsub(/#(([A-Za-z0-9])\2){3}/) { |m| "##{m[1]}#{m[3]}#{m[5]}" }
5
5
  end
6
6
 
7
+ def self.linter_name
8
+ "HexLength"
9
+ end
10
+
7
11
  def self.description
8
12
  "Shortens hex color codes to 3 letters, when possible"
9
13
  end
@@ -22,5 +22,9 @@ module SCSSLint::AutoCorrect::Correctors
22
22
  def self.priority
23
23
  5
24
24
  end
25
+
26
+ def self.linter_name
27
+ "ColorKeyword"
28
+ end
25
29
  end
26
30
  end
@@ -0,0 +1,92 @@
1
+ require "scss_lint/constants"
2
+
3
+ module SCSSLint::AutoCorrect
4
+ class PropertySorter
5
+ def initialize(rules: "")
6
+ @rules = rules
7
+ end
8
+
9
+ def preferred_order
10
+ return @preferred_order unless @preferred_order.nil?
11
+ path = File.join \
12
+ SCSSLint::SCSS_LINT_DATA,
13
+ "property-sort-orders",
14
+ "#{@rules}.txt"
15
+
16
+ @preferred_order = []
17
+
18
+ if File.file?(path)
19
+ @preferred_order = File.open(path) \
20
+ .read
21
+ .split("\n")
22
+ .reject { |line| line =~ /^\s*#/ || line.strip == "" }
23
+ end
24
+
25
+ @preferred_order
26
+ end
27
+
28
+ def property?(line)
29
+ parsed = parse_line(line)
30
+ return false if parsed.nil?
31
+ !!parsed[:property]
32
+ # !preferred_order.index(parsed[:property]).nil?
33
+ end
34
+
35
+ def parse_line(line)
36
+ mm = line.match(/^\s*([^:]+):/)
37
+ full = mm[1]
38
+ if full[0] == "-"
39
+ mm2 = full.match(/^-([^-]+)-(.*)$/)
40
+ {
41
+ vendor: mm2[1],
42
+ property: mm2[2],
43
+ line: line
44
+ }
45
+ else
46
+ {
47
+ property: full,
48
+ line: line
49
+ }
50
+ end
51
+ rescue
52
+ nil
53
+ end
54
+
55
+ def compare_lines(a, b)
56
+ compare_properties \
57
+ parse_line(a),
58
+ parse_line(b)
59
+ end
60
+
61
+ def compare_properties(a, b)
62
+ if a[:property] == b[:property]
63
+ compare_by_vendor(a, b)
64
+ elsif preferred_order.any?
65
+ compare_by_order(a, b)
66
+ else
67
+ a[:property] <=> b[:property]
68
+ end
69
+ end
70
+
71
+ def compare_by_order(a, b)
72
+ (preferred_order.index(a[:property]) || Float::INFINITY) <=>
73
+ (preferred_order.index(b[:property]) || Float::INFINITY)
74
+ end
75
+
76
+ def compare_by_vendor(a, b)
77
+ if a[:vendor] && b[:vendor]
78
+ a[:vendor] <=> b[:vendor]
79
+ elsif a[:vendor]
80
+ -1
81
+ elsif b[:vendor]
82
+ 1
83
+ else
84
+ 0
85
+ end
86
+ end
87
+
88
+ def sort_lines(lines)
89
+ lines.sort { |a,b| compare_lines(a, b) }
90
+ end
91
+ end
92
+ end
@@ -1,4 +1,4 @@
1
1
  # Defines the gem version.
2
2
  module SCSSLint::AutoCorrect
3
- VERSION = '1.0.0'.freeze
3
+ VERSION = '1.1.0'.freeze
4
4
  end
@@ -0,0 +1,4 @@
1
+ .lorem {
2
+ height: 55px;
3
+ color: #fff;
4
+ }
@@ -0,0 +1,4 @@
1
+ .lorem {
2
+ color: #fff;
3
+ height: 55px;
4
+ }
@@ -0,0 +1,6 @@
1
+ .lorem {
2
+ # scss-lint:disable PropertySortOrder
3
+ color: #fff;
4
+ height: 55px;
5
+ # scss-lint:enable PropertySortOrder
6
+ }
@@ -0,0 +1,24 @@
1
+ require_relative "../test_helper"
2
+
3
+ class PropertySortOrderTest < Minitest::Test
4
+ describe "SCSSLint::AutoCorrect::Correctors::PropertySortOrder" do
5
+ let(:corrector) { SCSSLint::AutoCorrect::Correctors::PropertySortOrder }
6
+
7
+ it "basically works" do
8
+ corrector.new
9
+ end
10
+
11
+ it "process input" do
12
+ input = load_file "unsorted_properties.scss"
13
+ expected = load_file "unsorted_properties_corrected.scss"
14
+ output = corrector.new.call input
15
+ assert_equal expected, output
16
+ end
17
+
18
+ it "ignores blocks, when pre-processor instructions are present" do
19
+ input = load_file "unsorted_properties_disable_rule.scss"
20
+ output = corrector.new.call input
21
+ assert_equal input, output
22
+ end
23
+ end
24
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: scss_lint-auto_correct
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dorian Marié
@@ -46,16 +46,22 @@ files:
46
46
  - lib/scss_lint/auto_correct/correctors/lines_end_with_semicolons.rb
47
47
  - lib/scss_lint/auto_correct/correctors/newlines_for_each_selector.rb
48
48
  - lib/scss_lint/auto_correct/correctors/no_spaces_before_semicolons.rb
49
+ - lib/scss_lint/auto_correct/correctors/property_sort_order.rb
49
50
  - lib/scss_lint/auto_correct/correctors/short_versions_of_numbers.rb
50
51
  - lib/scss_lint/auto_correct/correctors/shorter_versions_of_colors.rb
51
52
  - lib/scss_lint/auto_correct/correctors/spacing_before_selectors.rb
52
53
  - lib/scss_lint/auto_correct/correctors/use_hex_instead_of_color_keywords.rb
53
54
  - lib/scss_lint/auto_correct/correctors/variable_names.rb
55
+ - lib/scss_lint/auto_correct/property_sorter.rb
54
56
  - lib/scss_lint/auto_correct/version.rb
55
57
  - test/fixtures/has_uppercase_colors.scss
58
+ - test/fixtures/unsorted_properties.scss
59
+ - test/fixtures/unsorted_properties_corrected.scss
60
+ - test/fixtures/unsorted_properties_disable_rule.scss
56
61
  - test/test_helper.rb
57
62
  - test/unit/cli_test.rb
58
63
  - test/unit/downcase_colors_test.rb
64
+ - test/unit/property_sort_order_test.rb
59
65
  homepage: https://github.com/Dorian/scss-lint-auto-correct
60
66
  licenses:
61
67
  - MIT
@@ -82,6 +88,10 @@ specification_version: 4
82
88
  summary: Auto corrections for SCSS lint tool
83
89
  test_files:
84
90
  - test/fixtures/has_uppercase_colors.scss
91
+ - test/fixtures/unsorted_properties.scss
92
+ - test/fixtures/unsorted_properties_corrected.scss
93
+ - test/fixtures/unsorted_properties_disable_rule.scss
85
94
  - test/test_helper.rb
86
95
  - test/unit/cli_test.rb
87
96
  - test/unit/downcase_colors_test.rb
97
+ - test/unit/property_sort_order_test.rb