Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stylelint linter added in pre-hook #590

Merged
merged 2 commits into from
Jul 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,7 @@ issue](https://github.com/brigade/overcommit/issues/238) for more details.
* [SlimLint](lib/overcommit/hook/pre_commit/slim_lint.rb)
* [Sqlint](lib/overcommit/hook/pre_commit/sqlint.rb)
* [Standard](lib/overcommit/hook/pre_commit/standard.rb)
* [StyleLint](lib/overcommit/hook/pre_commit/stylelint.rb)
* [TrailingWhitespace](lib/overcommit/hook/pre_commit/trailing_whitespace.rb)
* [TravisLint](lib/overcommit/hook/pre_commit/travis_lint.rb)
* [TsLint](lib/overcommit/hook/pre_commit/ts_lint.rb)
Expand Down
11 changes: 11 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,17 @@ PreCommit:
install_command: 'npm install -g standard'
include: '**/*.js'

Stylelint:
enabled: false
description: 'Check styles with Stylelint'
require_executable: 'stylelint'
flags: ['-f', 'compact']
install_command: 'npm install -g stylelint'
include:
- '**/*.scss'
- '**/*.css'
- '**/*.less'

TsLint:
enabled: false
description: 'Analyze with TSLint'
Expand Down
21 changes: 21 additions & 0 deletions lib/overcommit/hook/pre_commit/stylelint.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module Overcommit::Hook::PreCommit
# Runs `stylelint` against any modified CSS file.
#
# @see https://github.com/stylelint/stylelint
class Stylelint < Base
# example of output:
# index.css: line 4, col 4, error - Expected indentation of 2 spaces (indentation)

MESSAGE_REGEX = /^(?<file>.+):\D*(?<line>\d).*$/

def run
result = execute(command, args: applicable_files)
output = result.stdout.chomp
return :pass if result.success? && output.empty?
extract_messages(
output.split("\n"),
MESSAGE_REGEX
)
end
end
end
42 changes: 42 additions & 0 deletions spec/overcommit/hook/pre_commit/stylelint_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require 'spec_helper'

describe Overcommit::Hook::PreCommit::Stylelint do
let(:config) { Overcommit::ConfigurationLoader.default_configuration }
let(:context) { double('context') }
subject { described_class.new(config, context) }

before do
subject.stub(:applicable_files).and_return(%w[file1.scss file2.scss])
end

context 'when stylelint exits successfully' do
before do
result = double('result')
result.stub(:success?).and_return(true)
result.stub(:stdout).and_return('')
subject.stub(:execute).and_return(result)
end

it { should pass }
end

context 'when stylelint exits unsucessfully' do
let(:result) { double('result') }

before do
subject.stub(:execute).and_return(result)
end

context 'and it reports an error' do
before do
result.stub(:success?).and_return(false)
result.stub(:stdout).and_return([
'index.css: line 4, col 4, error - Expected indentation of 2 spaces (indentation)',
'form.css: line 10, col 6, error - Expected indentation of 4 spaces (indentation)',
].join("\n"))
end

it { should fail_hook }
end
end
end