Skip to content

Commit fd3c3c0

Browse files
authored
Add dartanalyzer pre-commit hook (sds#739)
* Add dartanalyzer pre-commit hook * Fix lint issues * Add lint message type categorization for DartAnalyzer
1 parent 7312d5e commit fd3c3c0

File tree

4 files changed

+78
-0
lines changed

4 files changed

+78
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,7 @@ issue](https://github.com/sds/overcommit/issues/238) for more details.
498498
* [CoffeeLint](lib/overcommit/hook/pre_commit/coffee_lint.rb)
499499
* [Credo](lib/overcommit/hook/pre_commit/credo.rb)
500500
* [CssLint](lib/overcommit/hook/pre_commit/css_lint.rb)
501+
* [DartAnalyzer](lib/overcommit/hook/pre_commit/dart_analyzer.rb)
501502
* [Dogma](lib/overcommit/hook/pre_commit/dogma.rb)
502503
* [ErbLint](lib/overcommit/hook/pre_commit/erb_lint.rb)
503504
* [EsLint](lib/overcommit/hook/pre_commit/es_lint.rb)

config/default.yml

+8
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,14 @@ PreCommit:
266266
install_command: 'npm install -g csslint'
267267
include: '**/*.css'
268268

269+
DartAnalyzer:
270+
enabled: false
271+
description: 'Analyze with dartanalyzer'
272+
required_executable: 'dartanalyzer'
273+
flags: []
274+
include:
275+
- '**/*.dart'
276+
269277
Dogma:
270278
enabled: false
271279
description: 'Analyze with dogma'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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

0 commit comments

Comments
 (0)