File tree 4 files changed +78
-0
lines changed
lib/overcommit/hook/pre_commit
spec/overcommit/hook/pre_commit
4 files changed +78
-0
lines changed Original file line number Diff line number Diff line change @@ -498,6 +498,7 @@ issue](https://github.com/sds/overcommit/issues/238) for more details.
498
498
* [CoffeeLint](lib/overcommit/hook/pre_commit/coffee_lint.rb)
499
499
* [Credo](lib/overcommit/hook/pre_commit/credo.rb)
500
500
* [CssLint](lib/overcommit/hook/pre_commit/css_lint.rb)
501
+ * [DartAnalyzer](lib/overcommit/hook/pre_commit/dart_analyzer.rb)
501
502
* [Dogma](lib/overcommit/hook/pre_commit/dogma.rb)
502
503
* [ErbLint](lib/overcommit/hook/pre_commit/erb_lint.rb)
503
504
* [EsLint](lib/overcommit/hook/pre_commit/es_lint.rb)
Original file line number Diff line number Diff line change @@ -266,6 +266,14 @@ PreCommit:
266
266
install_command : ' npm install -g csslint'
267
267
include : ' **/*.css'
268
268
269
+ DartAnalyzer :
270
+ enabled : false
271
+ description : ' Analyze with dartanalyzer'
272
+ required_executable : ' dartanalyzer'
273
+ flags : []
274
+ include :
275
+ - ' **/*.dart'
276
+
269
277
Dogma :
270
278
enabled : false
271
279
description : ' Analyze with dogma'
Original file line number Diff line number Diff line change
1
+ # frozen_string_literal: true
2
+
3
+ module Overcommit ::Hook ::PreCommit
4
+ # Runs `dartanalyzer` against modified Dart files.
5
+ # @see https://dart.dev/tools/dartanalyzer
6
+ class DartAnalyzer < Base
7
+ MESSAGE_REGEX = /(?<type>.*)•\ (?<message>[^•]+)•\ (?<file>[^:]+):(?<line>\d +):(\d +)\. */
8
+
9
+ def run
10
+ result = execute ( command , args : applicable_files )
11
+ return :pass if result . success?
12
+
13
+ extract_messages (
14
+ result . stdout . split ( "\n " ) . grep ( MESSAGE_REGEX ) ,
15
+ MESSAGE_REGEX ,
16
+ lambda do |type |
17
+ type . include? ( 'error' ) ? :error : :warning
18
+ end
19
+ )
20
+ end
21
+ end
22
+ 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 ::DartAnalyzer 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.dart file2.dart ] )
12
+ end
13
+
14
+ context 'when dartanalyzer 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 dartanalyzer 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 ( [
35
+ 'Analyzing file1.dart...' ,
36
+ 'error • message_ommitted • lib/file1.dart:35:3 • rule' ,
37
+ 'Analyzing file2.dart...' ,
38
+ 'hint • message_ommitted • lib/file2.dart:100:13 • rule' ,
39
+ 'info • message_ommitted • lib/file2.dart:113:16 • rule' ,
40
+ '3 lints found.'
41
+ ] . join ( "\n " ) )
42
+ end
43
+
44
+ it { should fail_hook }
45
+ end
46
+ end
47
+ end
You can’t perform that action at this time.
0 commit comments