Skip to content

Commit 22229c9

Browse files
Ruffengsds
authored andcommitted
Add Stylelint pre-commit hook (sds#590)
* Stylelint linter added in pre-hook * CODE REVIEW
1 parent d147f8d commit 22229c9

File tree

4 files changed

+75
-0
lines changed

4 files changed

+75
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,7 @@ issue](https://github.com/brigade/overcommit/issues/238) for more details.
546546
* [SlimLint](lib/overcommit/hook/pre_commit/slim_lint.rb)
547547
* [Sqlint](lib/overcommit/hook/pre_commit/sqlint.rb)
548548
* [Standard](lib/overcommit/hook/pre_commit/standard.rb)
549+
* [StyleLint](lib/overcommit/hook/pre_commit/stylelint.rb)
549550
* [TrailingWhitespace](lib/overcommit/hook/pre_commit/trailing_whitespace.rb)
550551
* [TravisLint](lib/overcommit/hook/pre_commit/travis_lint.rb)
551552
* [TsLint](lib/overcommit/hook/pre_commit/ts_lint.rb)

config/default.yml

+11
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,17 @@ PreCommit:
711711
install_command: 'npm install -g standard'
712712
include: '**/*.js'
713713

714+
Stylelint:
715+
enabled: false
716+
description: 'Check styles with Stylelint'
717+
require_executable: 'stylelint'
718+
flags: ['-f', 'compact']
719+
install_command: 'npm install -g stylelint'
720+
include:
721+
- '**/*.scss'
722+
- '**/*.css'
723+
- '**/*.less'
724+
714725
TsLint:
715726
enabled: false
716727
description: 'Analyze with TSLint'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module Overcommit::Hook::PreCommit
2+
# Runs `stylelint` against any modified CSS file.
3+
#
4+
# @see https://github.com/stylelint/stylelint
5+
class Stylelint < Base
6+
# example of output:
7+
# index.css: line 4, col 4, error - Expected indentation of 2 spaces (indentation)
8+
9+
MESSAGE_REGEX = /^(?<file>.+):\D*(?<line>\d).*$/
10+
11+
def run
12+
result = execute(command, args: applicable_files)
13+
output = result.stdout.chomp
14+
return :pass if result.success? && output.empty?
15+
extract_messages(
16+
output.split("\n"),
17+
MESSAGE_REGEX
18+
)
19+
end
20+
end
21+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
require 'spec_helper'
2+
3+
describe Overcommit::Hook::PreCommit::Stylelint do
4+
let(:config) { Overcommit::ConfigurationLoader.default_configuration }
5+
let(:context) { double('context') }
6+
subject { described_class.new(config, context) }
7+
8+
before do
9+
subject.stub(:applicable_files).and_return(%w[file1.scss file2.scss])
10+
end
11+
12+
context 'when stylelint exits successfully' do
13+
before do
14+
result = double('result')
15+
result.stub(:success?).and_return(true)
16+
result.stub(:stdout).and_return('')
17+
subject.stub(:execute).and_return(result)
18+
end
19+
20+
it { should pass }
21+
end
22+
23+
context 'when stylelint exits unsucessfully' do
24+
let(:result) { double('result') }
25+
26+
before do
27+
subject.stub(:execute).and_return(result)
28+
end
29+
30+
context 'and it reports an error' do
31+
before do
32+
result.stub(:success?).and_return(false)
33+
result.stub(:stdout).and_return([
34+
'index.css: line 4, col 4, error - Expected indentation of 2 spaces (indentation)',
35+
'form.css: line 10, col 6, error - Expected indentation of 4 spaces (indentation)',
36+
].join("\n"))
37+
end
38+
39+
it { should fail_hook }
40+
end
41+
end
42+
end

0 commit comments

Comments
 (0)