Add a luac linter for Lua

This commit is contained in:
jiangzhi.xie 2018-01-08 23:19:17 +08:00
parent f1747901cc
commit 112fcf7dd5
7 changed files with 104 additions and 2 deletions

View File

@ -118,7 +118,7 @@ formatting.
| LaTeX | [alex](https://github.com/wooorm/alex) !!, [chktex](http://www.nongnu.org/chktex/), [lacheck](https://www.ctan.org/pkg/lacheck), [proselint](http://proselint.com/), [redpen](http://redpen.cc/), [vale](https://github.com/ValeLint/vale), [write-good](https://github.com/btford/write-good) |
| Less | [lessc](https://www.npmjs.com/package/less), [prettier](https://github.com/prettier/prettier), [stylelint](https://github.com/stylelint/stylelint) |
| LLVM | [llc](https://llvm.org/docs/CommandGuide/llc.html) |
| Lua | [luacheck](https://github.com/mpeterv/luacheck) |
| Lua | [luac](https://www.lua.org/manual/5.1/luac.html), [luacheck](https://github.com/mpeterv/luacheck) |
| Mail | [alex](https://github.com/wooorm/alex) !!, [proselint](http://proselint.com/), [vale](https://github.com/ValeLint/vale) |
| Make | [checkmake](https://github.com/mrtazz/checkmake) |
| Markdown | [alex](https://github.com/wooorm/alex) !!, [mdl](https://github.com/mivok/markdownlint), [proselint](http://proselint.com/), [redpen](http://redpen.cc/), [remark-lint](https://github.com/wooorm/remark-lint) !!, [vale](https://github.com/ValeLint/vale), [write-good](https://github.com/btford/write-good) |

40
ale_linters/lua/luac.vim Normal file
View File

@ -0,0 +1,40 @@
" Author: Jon Xie https://github.com/xiejiangzhi
" Description: luac linter for lua files
call ale#Set('lua_luac_executable', 'luac')
function! ale_linters#lua#luac#GetExecutable(buffer) abort
return ale#Var(a:buffer, 'lua_luac_executable')
endfunction
function! ale_linters#lua#luac#GetCommand(buffer) abort
let l:executable = ale_linters#lua#luac#GetExecutable(a:buffer)
return ale#Escape(l:executable) . ' -p - '
endfunction
function! ale_linters#lua#luac#Handle(buffer, lines) abort
" Matches patterns line the following:
"
" luac: stdin:5: '=' expected near ')'
" luac: stdin:8: ')' expected (to close '(' at line 6) near '123'
let l:pattern = '\v^.*:(\d+): (.+)$'
let l:output = []
for l:match in ale#util#GetMatches(a:lines, l:pattern)
call add(l:output, {
\ 'lnum': l:match[1] + 0,
\ 'type': 'E',
\ 'text': l:match[2],
\})
endfor
return l:output
endfunction
call ale#linter#Define('lua', {
\ 'name': 'luac',
\ 'executable_callback': 'ale_linters#lua#luac#GetExecutable',
\ 'command_callback': 'ale_linters#lua#luac#GetCommand',
\ 'output_stream': 'stderr',
\ 'callback': 'ale_linters#lua#luac#Handle',
\})

View File

@ -1,6 +1,15 @@
===============================================================================
ALE Lua Integration *ale-lua-options*
===============================================================================
luac *ale-lua-luac*
g:ale_lua_luac_executable *g:ale_lua_luac_executable*
*b:ale_lua_luac_executable*
Type: |String|
Default: `'luac'`
This variable can be changed to change the path to luac.
===============================================================================
luacheck *ale-lua-luacheck*

View File

@ -125,6 +125,7 @@ CONTENTS *ale-contents*
llvm..................................|ale-llvm-options|
llc.................................|ale-llvm-llc|
lua...................................|ale-lua-options|
luac................................|ale-lua-luac|
luacheck............................|ale-lua-luacheck|
markdown..............................|ale-markdown-options|
write-good..........................|ale-markdown-write-good|
@ -323,7 +324,7 @@ Notes:
* LaTeX (tex): `alex`!!, `chktex`, `lacheck`, `proselint`, `redpen`, `vale`, `write-good`
* Less: `lessc`, `prettier`, `stylelint`
* LLVM: `llc`
* Lua: `luacheck`
* Lua: `luac`, `luacheck`
* Mail: `alex`!!, `proselint`, `vale`
* Make: `checkmake`
* Markdown: `alex`!!, `mdl`, `proselint`, `redpen`, `remark-lint`, `vale`, `write-good`

View File

@ -0,0 +1,16 @@
Before:
runtime ale_linters/lua/luac.vim
After:
call ale#linter#Reset()
Execute(The default command should be correct):
AssertEqual ale#Escape('luac') . ' -p -',
\ join(split(ale_linters#lua#luac#GetCommand(1)))
Execute(The luac executable should be configurable):
let g:ale_lua_luac_executable = 'luac.sh'
AssertEqual 'luac.sh', ale_linters#lua#luac#GetExecutable(1)
AssertEqual ale#Escape('luac.sh') . ' -p -',
\ join(split(ale_linters#lua#luac#GetCommand(1)))

View File

@ -0,0 +1,36 @@
Before:
Save g:ale_warn_about_trailing_whitespace
let g:ale_warn_about_trailing_whitespace = 1
runtime ale_linters/lua/luac.vim
After:
Restore
call ale#linter#Reset()
Execute(The luac handler should parse lines correctly):
AssertEqual
\ [
\ {
\ 'lnum': 1,
\ 'text': 'line contains trailing whitespace',
\ 'type': 'E',
\ },
\ {
\ 'lnum': 3,
\ 'text': 'unexpected symbol near ''-''',
\ 'type': 'E',
\ },
\ {
\ 'lnum': 5,
\ 'text': '''='' expected near '')''',
\ 'type': 'E',
\ },
\ ],
\ ale_linters#lua#luac#Handle(347, [
\ 'luac /file/path/here.lua:1: line contains trailing whitespace',
\ 'luac /file/path/here.lua:3: unexpected symbol near ''-''',
\ 'luac /file/path/here.lua:5: ''='' expected near '')''',
\ ])