diff --git a/ale_linters/pug/puglint.vim b/ale_linters/pug/puglint.vim index 3f817c3..6c29efe 100644 --- a/ale_linters/pug/puglint.vim +++ b/ale_linters/pug/puglint.vim @@ -1,10 +1,48 @@ " Author: w0rp - " Description: pug-lint for checking Pug/Jade files. +call ale#Set('pug_puglint_options', '') +call ale#Set('pug_puglint_executable', 'pug-lint') +call ale#Set('pug_puglint_use_global', 0) + +function! ale_linters#pug#puglint#GetExecutable(buffer) abort + return ale#node#FindExecutable(a:buffer, 'pug_puglint', [ + \ 'node_modules/.bin/pug-lint', + \]) +endfunction + +function! s:FindConfig(buffer) abort + for l:filename in [ + \ '.pug-lintrc', + \ '.pug-lintrc.js', + \ '.pug-lintrc.json', + \ 'package.json', + \] + let l:config = ale#path#FindNearestFile(a:buffer, l:filename) + + if !empty(l:config) + return l:config + endif + endfor + + return '' +endfunction + +function! ale_linters#pug#puglint#GetCommand(buffer) abort + let l:executable = ale_linters#pug#puglint#GetExecutable(a:buffer) + let l:options = ale#Var(a:buffer, 'pug_puglint_options') + let l:config = s:FindConfig(a:buffer) + + return ale#Escape(l:executable) + \ . (!empty(l:options) ? ' ' . l:options : '') + \ . (!empty(l:config) ? ' -c ' . ale#Escape(l:config) : '') + \ . ' -r inline %t' +endfunction + call ale#linter#Define('pug', { \ 'name': 'puglint', -\ 'executable': 'pug-lint', +\ 'executable_callback': 'ale_linters#pug#puglint#GetExecutable', \ 'output_stream': 'stderr', -\ 'command': 'pug-lint -r inline %t', +\ 'command_callback': 'ale_linters#pug#puglint#GetCommand', \ 'callback': 'ale#handlers#unix#HandleAsError', \}) diff --git a/doc/ale-pug.txt b/doc/ale-pug.txt new file mode 100644 index 0000000..ad984dc --- /dev/null +++ b/doc/ale-pug.txt @@ -0,0 +1,44 @@ +=============================================================================== +ALE Pug Integration *ale-pug-options* + + +------------------------------------------------------------------------------- +puglint *ale-pug-puglint* + +The puglint linter will detect configuration files based on the path to the +filename automatically. Configuration files will be loaded in this order: + +1. `.pug-lintrc` +2. `.pug-lintrc.js` +3. `.pug-lintrc.json` +4. `package.json` + +You might need to create a configuration file for your project to get +meaningful results. + +g:ale_pug_puglint_executable *g:ale_pug_puglint_executable* + *b:ale_pug_puglint_executable* + Type: |String| + Default: `'pug-lint'` + + See |ale-integrations-local-executables| + + +g:ale_pug_puglint_options *g:ale_pug_puglint_options* + *b:ale_pug_puglint_options* + Type: |String| + Default: `''` + + This variable can be set to pass additional options to pug-lint. + + +g:ale_pug_puglint_use_global *g:ale_pug_puglint_use_global* + *b:ale_pug_puglint_use_global* + Type: |Number| + Default: `0` + + See |ale-integrations-local-executables| + + +------------------------------------------------------------------------------- + vim:tw=78:ts=2:sts=2:sw=2:ft=help:norl: diff --git a/doc/ale.txt b/doc/ale.txt index f9532fc..d9c989b 100644 --- a/doc/ale.txt +++ b/doc/ale.txt @@ -72,6 +72,8 @@ CONTENTS *ale-contents* php...................................|ale-php-options| phpcs...............................|ale-php-phpcs| phpmd...............................|ale-php-phpmd| + pug...................................|ale-pug-options| + puglint.............................|ale-pug-puglint| python................................|ale-python-options| autopep8............................|ale-python-autopep8| flake8..............................|ale-python-flake8| diff --git a/test/command_callback/puglint_project/node_modules/.bin/pug-lint b/test/command_callback/puglint_project/node_modules/.bin/pug-lint new file mode 100644 index 0000000..e69de29 diff --git a/test/command_callback/puglint_project/package.json b/test/command_callback/puglint_project/package.json new file mode 100644 index 0000000..e69de29 diff --git a/test/command_callback/puglint_project/puglint_rc_dir/.pug-lintrc b/test/command_callback/puglint_project/puglint_rc_dir/.pug-lintrc new file mode 100644 index 0000000..e69de29 diff --git a/test/command_callback/puglint_project/puglint_rc_js_dir/.pug-lintrc.js b/test/command_callback/puglint_project/puglint_rc_js_dir/.pug-lintrc.js new file mode 100644 index 0000000..e69de29 diff --git a/test/command_callback/puglint_project/puglint_rc_json_dir/.pug-lintrc.json b/test/command_callback/puglint_project/puglint_rc_json_dir/.pug-lintrc.json new file mode 100644 index 0000000..e69de29 diff --git a/test/command_callback/test_puglint_command_callback.vader b/test/command_callback/test_puglint_command_callback.vader new file mode 100644 index 0000000..830d63b --- /dev/null +++ b/test/command_callback/test_puglint_command_callback.vader @@ -0,0 +1,72 @@ +Before: + Save g:ale_pug_puglint_options + Save g:ale_pug_puglint_executable + Save g:ale_pug_puglint_use_global + + let g:ale_pug_puglint_options = '' + let g:ale_pug_puglint_executable = 'pug-lint' + let g:ale_pug_puglint_use_global = 0 + + silent! cd /testplugin/test/command_callback + let g:dir = getcwd() + + runtime ale_linters/pug/puglint.vim + +After: + Restore + + silent execute 'cd ' . fnameescape(g:dir) + unlet! g:dir + + call ale#linter#Reset() + +Execute(puglint should detect local executables and package.json): + call ale#test#SetFilename('puglint_project/test.pug') + + AssertEqual + \ g:dir . '/puglint_project/node_modules/.bin/pug-lint', + \ ale_linters#pug#puglint#GetExecutable(bufnr('')) + AssertEqual + \ ale#Escape(g:dir . '/puglint_project/node_modules/.bin/pug-lint') + \ . ' -c ' . ale#Escape(g:dir . '/puglint_project/package.json') + \ . ' -r inline %t', + \ ale_linters#pug#puglint#GetCommand(bufnr('')) + +Execute(puglint should use global executables if configured): + let g:ale_pug_puglint_use_global = 1 + + call ale#test#SetFilename('puglint_project/test.pug') + + AssertEqual 'pug-lint', ale_linters#pug#puglint#GetExecutable(bufnr('')) + AssertEqual + \ ale#Escape('pug-lint') + \ . ' -c ' . ale#Escape(g:dir . '/puglint_project/package.json') + \ . ' -r inline %t', + \ ale_linters#pug#puglint#GetCommand(bufnr('')) + +Execute(puglint should detect .pug-lintrc): + call ale#test#SetFilename('puglint_project/puglint_rc_dir/subdir/test.pug') + + AssertEqual + \ ale#Escape(g:dir . '/puglint_project/node_modules/.bin/pug-lint') + \ . ' -c ' . ale#Escape(g:dir . '/puglint_project/puglint_rc_dir/.pug-lintrc') + \ . ' -r inline %t', + \ ale_linters#pug#puglint#GetCommand(bufnr('')) + +Execute(puglint should detect .pug-lintrc.js): + call ale#test#SetFilename('puglint_project/puglint_rc_js_dir/subdir/test.pug') + + AssertEqual + \ ale#Escape(g:dir . '/puglint_project/node_modules/.bin/pug-lint') + \ . ' -c ' . ale#Escape(g:dir . '/puglint_project/puglint_rc_js_dir/.pug-lintrc.js') + \ . ' -r inline %t', + \ ale_linters#pug#puglint#GetCommand(bufnr('')) + +Execute(puglint should detect .pug-lintrc.json): + call ale#test#SetFilename('puglint_project/puglint_rc_json_dir/subdir/test.pug') + + AssertEqual + \ ale#Escape(g:dir . '/puglint_project/node_modules/.bin/pug-lint') + \ . ' -c ' . ale#Escape(g:dir . '/puglint_project/puglint_rc_json_dir/.pug-lintrc.json') + \ . ' -r inline %t', + \ ale_linters#pug#puglint#GetCommand(bufnr(''))