]> git.madduck.net Git - etc/vim.git/blob - .vim/bundle/ale/ale_linters/powershell/psscriptanalyzer.vim

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Merge commit '76265755a1add77121c8f9dabb3e9bb70fe9a972' as '.vim/bundle/ale'
[etc/vim.git] / .vim / bundle / ale / ale_linters / powershell / psscriptanalyzer.vim
1 " Author: Jesse Harris - https://github.com/zigford
2 " Description: This file adds support for lintng powershell scripts
3 "   using the PSScriptAnalyzer module.
4
5 " let g:ale_powershell_psscriptanalyzer_exclusions =
6 " \ 'PSAvoidUsingWriteHost,PSAvoidGlobalVars'
7 call ale#Set('powershell_psscriptanalyzer_exclusions', '')
8 call ale#Set('powershell_psscriptanalyzer_executable', 'pwsh')
9 call ale#Set('powershell_psscriptanalyzer_module',
10 \ 'psscriptanalyzer')
11
12 function! ale_linters#powershell#psscriptanalyzer#GetExecutable(buffer) abort
13     return ale#Var(a:buffer, 'powershell_psscriptanalyzer_executable')
14 endfunction
15
16 " Run Invoke-ScriptAnalyzer and output each linting message as 4 separate lines
17 " for each parsing
18 function! ale_linters#powershell#psscriptanalyzer#GetCommand(buffer) abort
19     let l:exclude_option = ale#Var(
20     \   a:buffer, 'powershell_psscriptanalyzer_exclusions')
21     let l:module = ale#Var(
22     \   a:buffer, 'powershell_psscriptanalyzer_module')
23     let l:script = ['Param($Script);
24     \   Invoke-ScriptAnalyzer "$Script" '
25     \   . (!empty(l:exclude_option) ? '-Exclude ' . l:exclude_option : '')
26     \   . '| ForEach-Object {
27     \   $_.Line;
28     \   $_.Severity;
29     \   $_.Message;
30     \   $_.RuleName}']
31
32     return ale#powershell#RunPowerShell(
33     \   a:buffer,
34     \   'powershell_psscriptanalyzer',
35     \   l:script)
36 endfunction
37
38 " add every 4 lines to an item(Dict) and every item to a list
39 " return the list
40 function! ale_linters#powershell#psscriptanalyzer#Handle(buffer, lines) abort
41     let l:output = []
42     let l:lcount = 0
43
44     for l:line in a:lines
45         if l:lcount is# 0
46             " the very first line
47             let l:item = {'lnum': str2nr(l:line)}
48         elseif l:lcount is# 1
49             if l:line is# 'Error'
50                 let l:item['type'] = 'E'
51             elseif l:line is# 'Information'
52                 let l:item['type'] = 'I'
53             else
54                 let l:item['type'] = 'W'
55             endif
56         elseif l:lcount is# 2
57             let l:item['text'] = l:line
58         elseif l:lcount is# 3
59             let l:item['code'] = l:line
60             call add(l:output, l:item)
61             let l:lcount = -1
62         endif
63
64         let l:lcount = l:lcount + 1
65     endfor
66
67     return l:output
68 endfunction
69
70 call ale#linter#Define('powershell', {
71 \   'name': 'psscriptanalyzer',
72 \   'executable': function('ale_linters#powershell#psscriptanalyzer#GetExecutable'),
73 \   'command': function('ale_linters#powershell#psscriptanalyzer#GetCommand'),
74 \   'output_stream': 'stdout',
75 \   'callback': 'ale_linters#powershell#psscriptanalyzer#Handle',
76 \})