From 112fcf7dd57a077f70ae39bb03d192f5c832aa2e Mon Sep 17 00:00:00 2001 From: "jiangzhi.xie" Date: Mon, 8 Jan 2018 23:19:17 +0800 Subject: [PATCH] Add a luac linter for Lua --- README.md | 2 +- ale_linters/lua/luac.vim | 40 +++++++++++++++++++ doc/ale-lua.txt | 9 +++++ doc/ale.txt | 3 +- .../test_luac_command_callback.vader | 16 ++++++++ test/handler/test_luac_handler.vader | 36 +++++++++++++++++ ...dler.vader => test_luacheck_handler.vader} | 0 7 files changed, 104 insertions(+), 2 deletions(-) create mode 100644 ale_linters/lua/luac.vim create mode 100644 test/command_callback/test_luac_command_callback.vader create mode 100644 test/handler/test_luac_handler.vader rename test/handler/{test_lua_handler.vader => test_luacheck_handler.vader} (100%) diff --git a/README.md b/README.md index 55be933a..c393e499 100644 --- a/README.md +++ b/README.md @@ -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) | diff --git a/ale_linters/lua/luac.vim b/ale_linters/lua/luac.vim new file mode 100644 index 00000000..4a6bb403 --- /dev/null +++ b/ale_linters/lua/luac.vim @@ -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', +\}) diff --git a/doc/ale-lua.txt b/doc/ale-lua.txt index 74d6b94a..b6fab379 100644 --- a/doc/ale-lua.txt +++ b/doc/ale-lua.txt @@ -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* diff --git a/doc/ale.txt b/doc/ale.txt index 25c1abe0..6cebbce6 100644 --- a/doc/ale.txt +++ b/doc/ale.txt @@ -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` diff --git a/test/command_callback/test_luac_command_callback.vader b/test/command_callback/test_luac_command_callback.vader new file mode 100644 index 00000000..f9eb4d3f --- /dev/null +++ b/test/command_callback/test_luac_command_callback.vader @@ -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))) diff --git a/test/handler/test_luac_handler.vader b/test/handler/test_luac_handler.vader new file mode 100644 index 00000000..3a2e769c --- /dev/null +++ b/test/handler/test_luac_handler.vader @@ -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 '')''', + \ ]) + diff --git a/test/handler/test_lua_handler.vader b/test/handler/test_luacheck_handler.vader similarity index 100% rename from test/handler/test_lua_handler.vader rename to test/handler/test_luacheck_handler.vader