From 235fc90e226ccc0430b9faa0cd3a226d9be6f31c Mon Sep 17 00:00:00 2001 From: w0rp Date: Tue, 18 Jul 2017 23:57:33 +0100 Subject: Fix #308 - Check Dart files with dartanalyzer --- README.md | 1 + ale_linters/dart/dartanalyzer.vim | 40 ++++++++++++++++++++++ doc/ale-dart.txt | 36 +++++++++++++++++++ doc/ale.txt | 3 ++ test/command_callback/dart_paths/.packages | 0 .../test_dartanalyzer_command_callback.vader | 40 ++++++++++++++++++++++ test/handler/test_dartanalyzer_handler.vader | 28 +++++++++++++++ 7 files changed, 148 insertions(+) create mode 100644 ale_linters/dart/dartanalyzer.vim create mode 100644 doc/ale-dart.txt create mode 100644 test/command_callback/dart_paths/.packages create mode 100644 test/command_callback/test_dartanalyzer_command_callback.vader create mode 100644 test/handler/test_dartanalyzer_handler.vader diff --git a/README.md b/README.md index 926402aa..f0101f12 100644 --- a/README.md +++ b/README.md @@ -71,6 +71,7 @@ name. That seems to be the fairest way to arrange this table. | CSS | [csslint](http://csslint.net/), [stylelint](https://github.com/stylelint/stylelint) | | Cython (pyrex filetype) | [cython](http://cython.org/) | | D | [dmd](https://dlang.org/dmd-linux.html) | +| Dart | [dartanalyzer](https://github.com/dart-lang/sdk/tree/master/pkg/analyzer_cli) | | Dockerfile | [hadolint](https://github.com/lukasmartinelli/hadolint) | | Elixir | [credo](https://github.com/rrrene/credo), [dogma](https://github.com/lpil/dogma) | | Elm | [elm-make](https://github.com/elm-lang/elm-make) | diff --git a/ale_linters/dart/dartanalyzer.vim b/ale_linters/dart/dartanalyzer.vim new file mode 100644 index 00000000..8f440571 --- /dev/null +++ b/ale_linters/dart/dartanalyzer.vim @@ -0,0 +1,40 @@ +" Author: w0rp +" Description: Check Dart files with dartanalyzer + +call ale#Set('dart_dartanalyzer_executable', 'dartanalyzer') + +function! ale_linters#dart#dartanalyzer#GetExecutable(buffer) abort + return ale#Var(a:buffer, 'dart_dartanalyzer_executable') +endfunction + +function! ale_linters#dart#dartanalyzer#GetCommand(buffer) abort + let l:executable = ale_linters#dart#dartanalyzer#GetExecutable(a:buffer) + let l:path = ale#path#FindNearestFile(a:buffer, '.packages') + + return ale#Escape(l:executable) + \ . (!empty(l:path) ? ' --packages ' . ale#Escape(l:path) : '') + \ . ' %t' +endfunction + +function! ale_linters#dart#dartanalyzer#Handle(buffer, lines) abort + let l:pattern = '\v^ ([a-z]+) . (.+) at (.+):(\d+):(\d+) . (.+)$' + let l:output = [] + + for l:match in ale#util#GetMatches(a:lines, l:pattern) + call add(l:output, { + \ 'type': l:match[1] ==# 'error' ? 'E' : 'W', + \ 'text': l:match[6] . ': ' . l:match[2], + \ 'lnum': str2nr(l:match[4]), + \ 'col': str2nr(l:match[5]), + \}) + endfor + + return l:output +endfunction + +call ale#linter#Define('dart', { +\ 'name': 'dartanalyzer', +\ 'executable_callback': 'ale_linters#dart#dartanalyzer#GetExecutable', +\ 'command_callback': 'ale_linters#dart#dartanalyzer#GetCommand', +\ 'callback': 'ale_linters#dart#dartanalyzer#Handle', +\}) diff --git a/doc/ale-dart.txt b/doc/ale-dart.txt new file mode 100644 index 00000000..37ba6fa0 --- /dev/null +++ b/doc/ale-dart.txt @@ -0,0 +1,36 @@ +=============================================================================== +ALE Dart Integration *ale-dart-options* + + +=============================================================================== +dartanalyzer *ale-dart-dartanalyzer* + +Installation +------------------------------------------------------------------------------- + +Install Dart via whatever means. `dartanalyzer` will be included in the SDK. + +You can add the SDK to `$PATH`, as described here: +https://www.dartlang.org/tools/sdk + +If you have installed Dart on Linux, you can also try the following: > + " Set the executable path for dartanalyzer to the absolute path to it. + let g:ale_dart_dartanalyzer_executable = '/usr/lib/dart/bin/dartanalyzer' +< +... or similarly for wherever your Dart SDK lives. This should work without +having to modify `$PATH`. + + +Options +------------------------------------------------------------------------------- + +g:ale_dart_dartanalyzer_executable *g:ale_dart_dartanalyzer_executable* + *b:ale_dart_dartanalyzer_executable* + Type: |String| + Default: `'dartanalyzer'` + + This variable can be set to change the path to dartanalyzer. + + +=============================================================================== + vim:tw=78:ts=2:sts=2:sw=2:ft=help:norl: diff --git a/doc/ale.txt b/doc/ale.txt index be79cedf..360d5afe 100644 --- a/doc/ale.txt +++ b/doc/ale.txt @@ -31,6 +31,8 @@ CONTENTS *ale-contents* stylelint...........................|ale-css-stylelint| cmake.................................|ale-cmake-options| cmakelint...........................|ale-cmake-cmakelint| + dart..................................|ale-dart-options| + dartanalyzer........................|ale-dart-dartanalyzer| erlang................................|ale-erlang-options| erlc................................|ale-erlang-erlc| syntaxerl...........................|ale-erlang-syntaxerl| @@ -172,6 +174,7 @@ The following languages and tools are supported. * CSS: 'csslint', 'stylelint' * Cython (pyrex filetype): 'cython' * D: 'dmd' +* Dart: 'dartanalyzer' * Dockerfile: 'hadolint' * Elixir: 'credo', 'dogma' * Elm: 'elm-make' diff --git a/test/command_callback/dart_paths/.packages b/test/command_callback/dart_paths/.packages new file mode 100644 index 00000000..e69de29b diff --git a/test/command_callback/test_dartanalyzer_command_callback.vader b/test/command_callback/test_dartanalyzer_command_callback.vader new file mode 100644 index 00000000..c26028de --- /dev/null +++ b/test/command_callback/test_dartanalyzer_command_callback.vader @@ -0,0 +1,40 @@ +Before: + Save g:ale_dart_dartanalyzer_executable + unlet! g:ale_dart_dartanalyzer_executable + + runtime ale_linters/dart/dartanalyzer.vim + + call ale#test#SetDirectory('/testplugin/test/command_callback') + +After: + Restore + + call ale#test#RestoreDirectory() + call ale#linter#Reset() + +Execute(The default command and executable should be correct): + AssertEqual + \ 'dartanalyzer', + \ ale_linters#dart#dartanalyzer#GetExecutable(bufnr('')) + AssertEqual + \ ale#Escape('dartanalyzer') . ' %t', + \ ale_linters#dart#dartanalyzer#GetCommand(bufnr('')) + +Execute(The executable should be configurable): + let g:ale_dart_dartanalyzer_executable = '/usr/lib/dart/bin/dartanalyzer' + + AssertEqual + \ '/usr/lib/dart/bin/dartanalyzer', + \ ale_linters#dart#dartanalyzer#GetExecutable(bufnr('')) + AssertEqual + \ ale#Escape('/usr/lib/dart/bin/dartanalyzer') . ' %t', + \ ale_linters#dart#dartanalyzer#GetCommand(bufnr('')) + +Execute(The .packages file should be set if detected): + call ale#test#SetFilename('dart_paths/foo') + + AssertEqual + \ ale#Escape('dartanalyzer') + \ . ' --packages ' . ale#Escape(g:dir . '/dart_paths/.packages') + \ . ' %t', + \ ale_linters#dart#dartanalyzer#GetCommand(bufnr('')) diff --git a/test/handler/test_dartanalyzer_handler.vader b/test/handler/test_dartanalyzer_handler.vader new file mode 100644 index 00000000..954850c5 --- /dev/null +++ b/test/handler/test_dartanalyzer_handler.vader @@ -0,0 +1,28 @@ +Before: + runtime ale_linters/dart/dartanalyzer.vim + +After: + call ale#linter#Reset() + +Execute(Basic problems should be parsed correctly): + AssertEqual + \ [ + \ { + \ 'type': 'E', + \ 'text': 'expected_token: Expected to find ''}''', + \ 'lnum': 5, + \ 'col': 1, + \ }, + \ { + \ 'type': 'W', + \ 'text': 'invalid_assignment: A value of type ''String'' can''t be assigned to a variable of type ''int''', + \ 'lnum': 2, + \ 'col': 16, + \ }, + \ ], + \ ale_linters#dart#dartanalyzer#Handle(bufnr(''), [ + \ 'Analyzing main.dart...', + \ ' error • Expected to find ''}'' at main.dart:5:1 • expected_token', + \ ' warning • A value of type ''String'' can''t be assigned to a variable of type ''int'' at main.dart:2:16 • invalid_assignment', + \ '1 error and 1 warning found.', + \ ]) -- cgit v1.2.3