|
| 1 | +" Author: Kevin Locke <[email protected]> |
| 2 | +" Description: jsonlint for JSON with comments |
| 3 | +" |
| 4 | +" Combines jsonlint with strip-json-comments for linting, |
| 5 | +" necessary until/unless https://github.com/zaach/jsonlint/issues/50 is fixed. |
| 6 | +" |
| 7 | +" Based on ale_linters/json/jsonlint.vim by KabbAmine <[email protected]> |
| 8 | +" and David Sierra <https://github.com/davidsierradz> |
| 9 | + |
| 10 | +call ale#Set('jsonc_jsonlint_executable', 'jsonlint') |
| 11 | +call ale#Set('jsonc_jsonlint_options', '') |
| 12 | +call ale#Set('jsonc_jsonlint_use_global', get(g:, 'ale_use_global_executables', 0)) |
| 13 | +call ale#Set('jsonc_sjc_executable', 'strip-json-comments') |
| 14 | +call ale#Set('jsonc_sjc_use_global', get(g:, 'ale_use_global_executables', 0)) |
| 15 | + |
| 16 | +function! ale_linters#jsonc#jsonlint#GetJsonlintExecutable(buffer) abort |
| 17 | + return ale#node#FindExecutable(a:buffer, 'jsonc_jsonlint', [ |
| 18 | + \ 'node_modules/.bin/jsonlint', |
| 19 | + \ 'node_modules/jsonlint/lib/cli.js', |
| 20 | + \]) |
| 21 | +endfunction |
| 22 | + |
| 23 | +function! ale_linters#jsonc#jsonlint#GetSjcExecutable(buffer) abort |
| 24 | + return ale#node#FindExecutable(a:buffer, 'jsonc_sjc', [ |
| 25 | + \ 'node_modules/.bin/strip-json-comments', |
| 26 | + \ 'node_modules/strip-json-comments-cli/cli.js', |
| 27 | + \]) |
| 28 | +endfunction |
| 29 | + |
| 30 | +" Returns non-empty string if all executables are available (used to |
| 31 | +" enable/disable this linter based on requisite command availability). |
| 32 | +" Since %e is not used in command string, returned path doesn't matter. |
| 33 | +function! ale_linters#jsonc#jsonlint#GetExecutable(buffer) abort |
| 34 | + if empty(ale_linters#jsonc#jsonlint#GetSjcExecutable(a:buffer)) |
| 35 | + return '' |
| 36 | + endif |
| 37 | + |
| 38 | + return ale_linters#jsonc#jsonlint#GetJsonlintExecutable(a:buffer) |
| 39 | +endfunction |
| 40 | + |
| 41 | +function! ale_linters#jsonc#jsonlint#GetCommand(buffer) abort |
| 42 | + let l:sjc_exe = ale_linters#jsonc#jsonlint#GetSjcExecutable(a:buffer) |
| 43 | + let l:jsonlint_exe = ale_linters#jsonc#jsonlint#GetJsonlintExecutable(a:buffer) |
| 44 | + |
| 45 | + " Note: Need %t so that ALE doesn't redirect stdin of jsonlint after pipe |
| 46 | + return ale#node#Executable(a:buffer, l:sjc_exe) |
| 47 | + \ . ' %t | ' |
| 48 | + \ . ale#node#Executable(a:buffer, l:jsonlint_exe) |
| 49 | + \ . ale#Pad(ale#Var(a:buffer, 'jsonc_jsonlint_options')) |
| 50 | + \ . ' --compact -' |
| 51 | +endfunction |
| 52 | + |
| 53 | +" FIXME: Copy of ale_linters#json#jsonlint#Handle |
| 54 | +" Is there a non-hacky way to reference it without duplication? |
| 55 | +function! ale_linters#jsonc#jsonlint#Handle(buffer, lines) abort |
| 56 | + " Matches patterns like the following: |
| 57 | + " line 2, col 15, found: 'STRING' - expected: 'EOF', '}', ',', ']'. |
| 58 | + let l:pattern = '^line \(\d\+\), col \(\d*\), \(.\+\)$' |
| 59 | + let l:output = [] |
| 60 | + |
| 61 | + for l:match in ale#util#GetMatches(a:lines, l:pattern) |
| 62 | + call add(l:output, { |
| 63 | + \ 'lnum': l:match[1] + 0, |
| 64 | + \ 'col': l:match[2] + 0, |
| 65 | + \ 'text': l:match[3], |
| 66 | + \}) |
| 67 | + endfor |
| 68 | + |
| 69 | + return l:output |
| 70 | +endfunction |
| 71 | + |
| 72 | +call ale#linter#Define('jsonc', { |
| 73 | +\ 'name': 'jsonlint', |
| 74 | +\ 'executable': function('ale_linters#jsonc#jsonlint#GetExecutable'), |
| 75 | +\ 'output_stream': 'stderr', |
| 76 | +\ 'command': function('ale_linters#jsonc#jsonlint#GetCommand'), |
| 77 | +\ 'callback': 'ale_linters#jsonc#jsonlint#Handle', |
| 78 | +\}) |
0 commit comments