Skip to content

Commit a3521a0

Browse files
Add pre commit hook for erblint (sds#731)
1 parent ed0d0ec commit a3521a0

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

config/default.yml

+7
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,13 @@ PreCommit:
275275
- '**/*.ex'
276276
- '**/*.exs'
277277

278+
ErbLint:
279+
enabled: false
280+
description: 'Analyze with ERB Lint'
281+
required_executable: 'erblint'
282+
install_command: 'bundle install erb_lint'
283+
include: '**/*.html.erb'
284+
278285
EsLint:
279286
enabled: false
280287
description: 'Analyze with ESLint'
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# frozen_string_literal: true
2+
3+
module Overcommit::Hook::PreCommit
4+
# Runs `erblint` against any modified ERB files.
5+
#
6+
# @see https://github.com/Shopify/erb-lint
7+
class ErbLint < Base
8+
MESSAGE_REGEX = /(?<message>.+)\nIn file: (?<file>.+):(?<line>\d+)/
9+
10+
def run
11+
result = execute(command, args: applicable_files)
12+
return :pass if result.success?
13+
14+
extract_messages(
15+
result.stdout.split("\n\n")[1..-1],
16+
MESSAGE_REGEX
17+
)
18+
end
19+
end
20+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
describe Overcommit::Hook::PreCommit::ErbLint do
6+
let(:config) { Overcommit::ConfigurationLoader.default_configuration }
7+
let(:context) { double('context') }
8+
subject { described_class.new(config, context) }
9+
10+
before do
11+
subject.stub(:applicable_files).and_return(%w[file1.html.erb file2.html.erb])
12+
end
13+
14+
context 'when erblint exits successfully' do
15+
before do
16+
result = double('result')
17+
result.stub(:success?).and_return(true)
18+
subject.stub(:execute).and_return(result)
19+
end
20+
21+
it { should pass }
22+
end
23+
24+
context 'when erblint exits unsucessfully' do
25+
let(:result) { double('result') }
26+
27+
before do
28+
result.stub(:success?).and_return(false)
29+
subject.stub(:execute).and_return(result)
30+
end
31+
32+
context 'and it reports an error' do
33+
before do
34+
result.stub(:stdout).and_return(<<-MSG)
35+
Linting 1 files with 14 linters...
36+
37+
erb interpolation with '<%= (...).html_safe %>' in this context is never safe
38+
In file: app/views/posts/show.html.erb:10
39+
MSG
40+
end
41+
42+
it { should fail_hook }
43+
end
44+
end
45+
end

0 commit comments

Comments
 (0)