Skip to content
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 @@ -498,6 +498,7 @@ issue](https://github.com/sds/overcommit/issues/238) for more details.
* [CoffeeLint](lib/overcommit/hook/pre_commit/coffee_lint.rb)
* [Credo](lib/overcommit/hook/pre_commit/credo.rb)
* [CssLint](lib/overcommit/hook/pre_commit/css_lint.rb)
* [DartAnalyzer](lib/overcommit/hook/pre_commit/dart_analyzer.rb)
* [Dogma](lib/overcommit/hook/pre_commit/dogma.rb)
* [ErbLint](lib/overcommit/hook/pre_commit/erb_lint.rb)
* [EsLint](lib/overcommit/hook/pre_commit/es_lint.rb)
Expand Down
8 changes: 8 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,14 @@ PreCommit:
install_command: 'npm install -g csslint'
include: '**/*.css'

DartAnalyzer:
enabled: false
description: 'Analyze with dartanalyzer'
required_executable: 'dartanalyzer'
flags: []
include:
- '**/*.dart'

Dogma:
enabled: false
description: 'Analyze with dogma'
Expand Down
22 changes: 22 additions & 0 deletions lib/overcommit/hook/pre_commit/dart_analyzer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

module Overcommit::Hook::PreCommit
# Runs `dartanalyzer` against modified Dart files.
# @see https://dart.dev/tools/dartanalyzer
class DartAnalyzer < Base
MESSAGE_REGEX = /(?<type>.*)•\ (?<message>[^•]+)•\ (?<file>[^:]+):(?<line>\d+):(\d+)\.*/

def run
result = execute(command, args: applicable_files)
return :pass if result.success?

extract_messages(
result.stdout.split("\n").grep(MESSAGE_REGEX),
MESSAGE_REGEX,
lambda do |type|
type.include?('error') ? :error : :warning
end
)
end
end
end
47 changes: 47 additions & 0 deletions spec/overcommit/hook/pre_commit/dart_analyzer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# frozen_string_literal: true

require 'spec_helper'

describe Overcommit::Hook::PreCommit::DartAnalyzer 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.dart file2.dart])
end

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

it { should pass }
end

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

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

context 'and it reports an error' do
before do
result.stub(:stdout).and_return([
'Analyzing file1.dart...',
'error • message_ommitted • lib/file1.dart:35:3 • rule',
'Analyzing file2.dart...',
'hint • message_ommitted • lib/file2.dart:100:13 • rule',
'info • message_ommitted • lib/file2.dart:113:16 • rule',
'3 lints found.'
].join("\n"))
end

it { should fail_hook }
end
end
end