From 7ce960ae519e97e26e3c99c4faebe9d5290b487f Mon Sep 17 00:00:00 2001 From: w0rp Date: Fri, 7 Jul 2017 10:47:09 +0100 Subject: [PATCH] Fix #735 - Support old versions of Flow by only adding --respect-pragma for supported versions --- ale_linters/javascript/flow.vim | 28 +++++++++++++++++++++++++--- autoload/ale/engine.vim | 10 ++++++++++ autoload/ale/linter.vim | 1 + test/test_flow_command.vader | 15 +++++++++++++-- test/test_linter_retrieval.vader | 8 ++++---- 5 files changed, 53 insertions(+), 9 deletions(-) diff --git a/ale_linters/javascript/flow.vim b/ale_linters/javascript/flow.vim index 1b13e5d..416a1ed 100644 --- a/ale_linters/javascript/flow.vim +++ b/ale_linters/javascript/flow.vim @@ -10,7 +10,12 @@ function! ale_linters#javascript#flow#GetExecutable(buffer) abort \]) endfunction -function! ale_linters#javascript#flow#GetCommand(buffer) abort +function! ale_linters#javascript#flow#VersionCheck(buffer) abort + return ale#Escape(ale_linters#javascript#flow#GetExecutable(a:buffer)) + \ . ' --version' +endfunction + +function! ale_linters#javascript#flow#GetCommand(buffer, version_lines) abort let l:flow_config = ale#path#FindNearestFile(a:buffer, '.flowconfig') if empty(l:flow_config) @@ -18,8 +23,21 @@ function! ale_linters#javascript#flow#GetCommand(buffer) abort return '' endif + let l:use_respect_pragma = 1 + + " If we can parse the version number, then only use --respect-pragma + " if the version is >= 0.36.0, which added the argument. + for l:match in ale#util#GetMatches(a:version_lines, '\v\d+\.\d+\.\d+$') + let l:use_respect_pragma = ale#semver#GreaterOrEqual( + \ ale#semver#Parse(l:match[0]), + \ [0, 36, 0] + \) + endfor + return ale#Escape(ale_linters#javascript#flow#GetExecutable(a:buffer)) - \ . ' check-contents --respect-pragma --json --from ale %s' + \ . ' check-contents' + \ . (l:use_respect_pragma ? ' --respect-pragma': '') + \ . ' --json --from ale %s' endfunction function! ale_linters#javascript#flow#Handle(buffer, lines) abort @@ -74,6 +92,10 @@ endfunction call ale#linter#Define('javascript', { \ 'name': 'flow', \ 'executable_callback': 'ale_linters#javascript#flow#GetExecutable', -\ 'command_callback': 'ale_linters#javascript#flow#GetCommand', +\ 'command_chain': [ +\ {'callback': 'ale_linters#javascript#flow#VersionCheck'}, +\ {'callback': 'ale_linters#javascript#flow#GetCommand'}, +\ ], \ 'callback': 'ale_linters#javascript#flow#Handle', +\ 'add_newline': 1, \}) diff --git a/autoload/ale/engine.vim b/autoload/ale/engine.vim index acfc030..fa26a37 100644 --- a/autoload/ale/engine.vim +++ b/autoload/ale/engine.vim @@ -387,6 +387,16 @@ function! s:RunJob(options) abort let l:read_buffer = 0 endif + " Add a newline to commands which need it. + " This is only used for Flow for now, and is not documented. + if l:linter.add_newline + if has('win32') + let l:command = l:command . '; echo.' + else + let l:command = l:command . '; echo' + endif + endif + let l:command = ale#job#PrepareCommand(l:command) let l:job_options = { \ 'mode': 'nl', diff --git a/autoload/ale/linter.vim b/autoload/ale/linter.vim index 4138b87..1c99a0c 100644 --- a/autoload/ale/linter.vim +++ b/autoload/ale/linter.vim @@ -50,6 +50,7 @@ function! ale#linter#PreProcess(linter) abort endif let l:obj = { + \ 'add_newline': get(a:linter, 'add_newline', 0), \ 'name': get(a:linter, 'name'), \ 'lsp': get(a:linter, 'lsp', ''), \} diff --git a/test/test_flow_command.vader b/test/test_flow_command.vader index f7754f6..f45c93c 100644 --- a/test/test_flow_command.vader +++ b/test/test_flow_command.vader @@ -8,10 +8,21 @@ Execute(flow should return a command to run if a .flowconfig file exists): silent! cd /testplugin/test :e! flow/a/sub/dummy - AssertEqual '''flow'' check-contents --respect-pragma --json --from ale %s', ale_linters#javascript#flow#GetCommand(bufnr('%')) + AssertEqual '''flow'' check-contents --respect-pragma --json --from ale %s', ale_linters#javascript#flow#GetCommand(bufnr('%'), []) + +Execute(flow should should not use --respect-pragma for old versions): + silent! cd /testplugin/test + :e! flow/a/sub/dummy + + AssertEqual + \ '''flow'' check-contents --json --from ale %s', + \ ale_linters#javascript#flow#GetCommand(bufnr('%'), [ + \ 'Warning: `flow --version` is deprecated in favor of `flow version`', + \ 'Flow, a static type checker for JavaScript, version 0.27.0', + \ ]) Execute(flow should not return a command to run if no .flowconfig file exists): silent! cd /testplugin/test :e! flow/b/sub/dummy - AssertEqual '', ale_linters#javascript#flow#GetCommand(bufnr('%')) + AssertEqual '', ale_linters#javascript#flow#GetCommand(bufnr('%'), []) diff --git a/test/test_linter_retrieval.vader b/test/test_linter_retrieval.vader index d701234..afb540d 100644 --- a/test/test_linter_retrieval.vader +++ b/test/test_linter_retrieval.vader @@ -1,8 +1,8 @@ Before: Save g:ale_linters, g:ale_linter_aliases - let g:testlinter1 = {'name': 'testlinter1', 'executable': 'testlinter1', 'command': 'testlinter1', 'callback': 'testCB1', 'output_stream': 'stdout', 'read_buffer': 1, 'lint_file': 0, 'aliases': [], 'lsp': ''} - let g:testlinter2 = {'name': 'testlinter2', 'executable': 'testlinter2', 'command': 'testlinter2', 'callback': 'testCB2', 'output_stream': 'stdout', 'read_buffer': 0, 'lint_file': 1, 'aliases': [], 'lsp': ''} + let g:testlinter1 = {'name': 'testlinter1', 'executable': 'testlinter1', 'command': 'testlinter1', 'callback': 'testCB1', 'output_stream': 'stdout', 'read_buffer': 1, 'lint_file': 0, 'aliases': [], 'lsp': '', 'add_newline': 0} + let g:testlinter2 = {'name': 'testlinter2', 'executable': 'testlinter2', 'command': 'testlinter2', 'callback': 'testCB2', 'output_stream': 'stdout', 'read_buffer': 0, 'lint_file': 1, 'aliases': [], 'lsp': '', 'add_newline': 0} call ale#linter#Reset() After: @@ -105,7 +105,7 @@ Execute (The local alias option shouldn't completely replace the global one): AssertEqual [g:testlinter1, g:testlinter2], ale#linter#Get('testft1') Execute (Linters should be loaded from disk appropriately): - AssertEqual [{'name': 'testlinter', 'output_stream': 'stdout', 'executable': 'testlinter', 'command': 'testlinter', 'callback': 'testCB', 'read_buffer': 1, 'lint_file': 0, 'aliases': [], 'lsp': ''}], ale#linter#Get('testft') + AssertEqual [{'name': 'testlinter', 'output_stream': 'stdout', 'executable': 'testlinter', 'command': 'testlinter', 'callback': 'testCB', 'read_buffer': 1, 'lint_file': 0, 'aliases': [], 'lsp': '', 'add_newline': 0}], ale#linter#Get('testft') Execute (Linters for later filetypes should replace the former ones): @@ -123,5 +123,5 @@ Execute (Linters for later filetypes should replace the former ones): \}) AssertEqual [ - \ {'output_stream': 'stdout', 'lint_file': 0, 'read_buffer': 1, 'name': 'eslint', 'executable': 'x', 'lsp': '', 'aliases': [], 'command': 'x', 'callback': 'x'} + \ {'output_stream': 'stdout', 'lint_file': 0, 'read_buffer': 1, 'name': 'eslint', 'executable': 'x', 'lsp': '', 'aliases': [], 'command': 'x', 'callback': 'x', 'add_newline': 0} \], ale#linter#Get('javascript.typescript')