File tree 3 files changed +72
-0
lines changed
lib/overcommit/hook/pre_commit
spec/overcommit/hook/pre_commit
3 files changed +72
-0
lines changed Original file line number Diff line number Diff line change @@ -275,6 +275,13 @@ PreCommit:
275
275
- ' **/*.ex'
276
276
- ' **/*.exs'
277
277
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
+
278
285
EsLint :
279
286
enabled : false
280
287
description : ' Analyze with ESLint'
Original file line number Diff line number Diff line change
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>.+)\n In 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 number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments