Skip to content

Commit c47ae8c

Browse files
committed
Support stylelint 16+, where messages are emitted on stderr
Before Stylelint 16, Stylelint emitted its messages on stdout. Hence Overcommit's Stylelint hook was built to read output from stdout. Starting with Stylelint 16, Stylelint now emits its messages to stderr, which means the existing Overcommit implementation no longer works. This PR updates Overcommit's Stylelint plugin to check for messages on _both_ stdout and stderr. That way <16 and >=16 versions of Stylelint are supported. Fixes sds#823
1 parent a14ec63 commit c47ae8c

2 files changed

Lines changed: 29 additions & 2 deletions

File tree

‎lib/overcommit/hook/pre_commit/stylelint.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class Stylelint < Base
1212

1313
def run
1414
result = execute(command, args: applicable_files)
15-
output = result.stdout.chomp
15+
output = result.stdout + result.stderr.chomp
1616
return :pass if result.success? && output.empty?
1717

1818
extract_messages(

‎spec/overcommit/hook/pre_commit/stylelint_spec.rb‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@
1515
before do
1616
result = double('result')
1717
result.stub(:success?).and_return(true)
18+
result.stub(:stderr).and_return('')
1819
result.stub(:stdout).and_return('')
1920
subject.stub(:execute).and_return(result)
2021
end
2122

2223
it { should pass }
2324
end
2425

25-
context 'when stylelint exits unsucessfully' do
26+
context 'when stylelint exits unsucessfully with messages on stdout (stylelint < 16)' do
2627
let(:result) { double('result') }
2728

2829
before do
@@ -32,6 +33,7 @@
3233
context 'and it reports an error' do
3334
before do
3435
result.stub(:success?).and_return(false)
36+
result.stub(:stderr).and_return('')
3537
result.stub(:stdout).and_return([
3638
'index.css: line 4, col 4, error - Expected indentation of 2 spaces (indentation)',
3739
'form.css: line 10, col 6, error - Expected indentation of 4 spaces (indentation)',
@@ -45,4 +47,29 @@
4547
end
4648
end
4749
end
50+
51+
context 'when stylelint exits unsucessfully with messages on stderr (stylelint >= 16)' do
52+
let(:result) { double('result') }
53+
54+
before do
55+
subject.stub(:execute).and_return(result)
56+
end
57+
58+
context 'and it reports an error' do
59+
before do
60+
result.stub(:success?).and_return(false)
61+
result.stub(:stdout).and_return('')
62+
result.stub(:stderr).and_return([
63+
'index.css: line 4, col 4, error - Expected indentation of 2 spaces (indentation)',
64+
'form.css: line 10, col 6, error - Expected indentation of 4 spaces (indentation)',
65+
].join("\n"))
66+
end
67+
68+
it { should fail_hook }
69+
70+
it 'extracts lines numbers correctly from output' do
71+
expect(subject.run.map(&:line)).to eq([4, 10])
72+
end
73+
end
74+
end
4875
end

0 commit comments

Comments
 (0)