From 3e295a995efb785bcb74e48f91a28901cba487ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ensar=20Saraj=C4=8Di=C4=87?= Date: Thu, 28 Jan 2021 18:19:10 +0100 Subject: [PATCH 1/3] Add dartanalyzer pre-commit hook --- README.md | 1 + config/default.yml | 8 ++++ .../hook/pre_commit/dart_analyzer.rb | 17 +++++++ .../hook/pre_commit/dart_analyzer_spec.rb | 45 +++++++++++++++++++ 4 files changed, 71 insertions(+) create mode 100644 lib/overcommit/hook/pre_commit/dart_analyzer.rb create mode 100644 spec/overcommit/hook/pre_commit/dart_analyzer_spec.rb diff --git a/README.md b/README.md index e1c3911b..ab023c7e 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/config/default.yml b/config/default.yml index 748476e8..7207ade6 100644 --- a/config/default.yml +++ b/config/default.yml @@ -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' diff --git a/lib/overcommit/hook/pre_commit/dart_analyzer.rb b/lib/overcommit/hook/pre_commit/dart_analyzer.rb new file mode 100644 index 00000000..a0bc06dd --- /dev/null +++ b/lib/overcommit/hook/pre_commit/dart_analyzer.rb @@ -0,0 +1,17 @@ +module Overcommit::Hook::PreCommit + # Runs `dartanalyzer` against modified Dart files. + # @see https://dart.dev/tools/dartanalyzer + class DartAnalyzer < Base + MESSAGE_REGEX = /.*•\ (?[^•]+)•\ (?[^:]+):(?\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 + ) + end + end +end diff --git a/spec/overcommit/hook/pre_commit/dart_analyzer_spec.rb b/spec/overcommit/hook/pre_commit/dart_analyzer_spec.rb new file mode 100644 index 00000000..cc559891 --- /dev/null +++ b/spec/overcommit/hook/pre_commit/dart_analyzer_spec.rb @@ -0,0 +1,45 @@ +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...', + 'lint • message_ommitted • lib/file1.dart:35:3 • rule', + 'Analyzing file2.dart...', + 'lint • message_ommitted • lib/file2.dart:100:13 • rule', + 'lint • message_ommitted • lib/file2.dart:113:16 • rule', + '3 lints found.' + ].join("\n")) + end + + it { should fail_hook } + end + end +end From 0d3c986997aa05d79342d6f3790bf3521170a411 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ensar=20Saraj=C4=8Di=C4=87?= Date: Fri, 29 Jan 2021 09:15:36 +0100 Subject: [PATCH 2/3] Fix lint issues --- lib/overcommit/hook/pre_commit/dart_analyzer.rb | 2 ++ spec/overcommit/hook/pre_commit/dart_analyzer_spec.rb | 2 ++ 2 files changed, 4 insertions(+) diff --git a/lib/overcommit/hook/pre_commit/dart_analyzer.rb b/lib/overcommit/hook/pre_commit/dart_analyzer.rb index a0bc06dd..fdfa6d93 100644 --- a/lib/overcommit/hook/pre_commit/dart_analyzer.rb +++ b/lib/overcommit/hook/pre_commit/dart_analyzer.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Overcommit::Hook::PreCommit # Runs `dartanalyzer` against modified Dart files. # @see https://dart.dev/tools/dartanalyzer diff --git a/spec/overcommit/hook/pre_commit/dart_analyzer_spec.rb b/spec/overcommit/hook/pre_commit/dart_analyzer_spec.rb index cc559891..79be815b 100644 --- a/spec/overcommit/hook/pre_commit/dart_analyzer_spec.rb +++ b/spec/overcommit/hook/pre_commit/dart_analyzer_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe Overcommit::Hook::PreCommit::DartAnalyzer do From c5d6d36265dc60dee206015e62038fecc690ce0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ensar=20Saraj=C4=8Di=C4=87?= Date: Sat, 30 Jan 2021 18:59:57 +0100 Subject: [PATCH 3/3] Add lint message type categorization for DartAnalyzer --- lib/overcommit/hook/pre_commit/dart_analyzer.rb | 7 +++++-- spec/overcommit/hook/pre_commit/dart_analyzer_spec.rb | 6 +++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/overcommit/hook/pre_commit/dart_analyzer.rb b/lib/overcommit/hook/pre_commit/dart_analyzer.rb index fdfa6d93..a1c99448 100644 --- a/lib/overcommit/hook/pre_commit/dart_analyzer.rb +++ b/lib/overcommit/hook/pre_commit/dart_analyzer.rb @@ -4,7 +4,7 @@ module Overcommit::Hook::PreCommit # Runs `dartanalyzer` against modified Dart files. # @see https://dart.dev/tools/dartanalyzer class DartAnalyzer < Base - MESSAGE_REGEX = /.*•\ (?[^•]+)•\ (?[^:]+):(?\d+):(\d+)\.*/ + MESSAGE_REGEX = /(?.*)•\ (?[^•]+)•\ (?[^:]+):(?\d+):(\d+)\.*/ def run result = execute(command, args: applicable_files) @@ -12,7 +12,10 @@ def run extract_messages( result.stdout.split("\n").grep(MESSAGE_REGEX), - MESSAGE_REGEX + MESSAGE_REGEX, + lambda do |type| + type.include?('error') ? :error : :warning + end ) end end diff --git a/spec/overcommit/hook/pre_commit/dart_analyzer_spec.rb b/spec/overcommit/hook/pre_commit/dart_analyzer_spec.rb index 79be815b..1013c503 100644 --- a/spec/overcommit/hook/pre_commit/dart_analyzer_spec.rb +++ b/spec/overcommit/hook/pre_commit/dart_analyzer_spec.rb @@ -33,10 +33,10 @@ before do result.stub(:stdout).and_return([ 'Analyzing file1.dart...', - 'lint • message_ommitted • lib/file1.dart:35:3 • rule', + 'error • message_ommitted • lib/file1.dart:35:3 • rule', 'Analyzing file2.dart...', - 'lint • message_ommitted • lib/file2.dart:100:13 • rule', - 'lint • message_ommitted • lib/file2.dart:113:16 • rule', + 'hint • message_ommitted • lib/file2.dart:100:13 • rule', + 'info • message_ommitted • lib/file2.dart:113:16 • rule', '3 lints found.' ].join("\n")) end