Add tflint fot Terraform

This commit is contained in:
Nathaniel Williams 2017-10-26 19:32:33 +01:00 committed by w0rp
parent 6ed456f99c
commit e4456a4e0e
9 changed files with 172 additions and 0 deletions

View File

@ -141,6 +141,7 @@ formatting.
| SQL | [sqlint](https://github.com/purcell/sqlint) |
| Swift | [swiftlint](https://github.com/realm/SwiftLint), [swiftformat](https://github.com/nicklockwood/SwiftFormat) |
| Tcl | [nagelfar](http://nagelfar.sourceforge.net) !! |
| Terraform | [tflint](https://github.com/wata727/tflint) |
| Texinfo | [proselint](http://proselint.com/), [write-good](https://github.com/btford/write-good)|
| Text^ | [proselint](http://proselint.com/), [vale](https://github.com/ValeLint/vale), [write-good](https://github.com/btford/write-good) |
| Thrift | [thrift](http://thrift.apache.org/) |

View File

@ -0,0 +1,61 @@
" Author: Nat Williams <nat.williams@gmail.com>
" Description: tflint for Terraform files
"
" See: https://www.terraform.io/
" https://github.com/wata727/tflint
call ale#Set('terraform_tflint_options', '')
call ale#Set('terraform_tflint_executable', 'tflint')
function! ale_linters#terraform#tflint#Handle(buffer, lines) abort
let l:output = []
for l:error in ale#util#FuzzyJSONDecode(a:lines, [])
if l:error.type is# 'ERROR'
let l:type = 'E'
elseif l:error.type is# 'NOTICE'
let l:type = 'I'
else
let l:type = 'W'
endif
call add(l:output, {
\ 'lnum': l:error.line,
\ 'text': l:error.message,
\ 'type': l:type,
\})
endfor
return l:output
endfunction
function! ale_linters#terraform#tflint#GetExecutable(buffer) abort
return ale#Var(a:buffer, 'terraform_tflint_executable')
endfunction
function! ale_linters#terraform#tflint#GetCommand(buffer) abort
let l:cmd = ale#Escape(ale#Var(a:buffer, 'terraform_tflint_executable'))
let l:config_file = ale#path#FindNearestFile(a:buffer, '.tflint.hcl')
if !empty(l:config_file)
let l:cmd .= ' --config ' . ale#Escape(l:config_file)
endif
let l:opts = ale#Var(a:buffer, 'terraform_tflint_options')
if !empty(l:opts)
let l:cmd .= ' ' . l:opts
endif
let l:cmd .= ' -f json'
return l:cmd
endfunction
call ale#linter#Define('terraform', {
\ 'name': 'tflint',
\ 'executable_callback': 'ale_linters#terraform#tflint#GetExecutable',
\ 'command_callback': 'ale_linters#terraform#tflint#GetCommand',
\ 'callback': 'ale_linters#terraform#tflint#Handle',
\})
" vim:sw=4

29
doc/ale-terraform.txt Normal file
View File

@ -0,0 +1,29 @@
===============================================================================
ALE Terraform Integration *ale-terraform-options*
===============================================================================
tflint *ale-terraform-tflint*
g:ale_terraform_tflint_executable *g:ale_terraform_tflint_executable*
*b:ale_terraform_tflint_executable*
Type: |String|
Default: `'tflint'`
This variable can be changed to use a different executable for tflint.
g:ale_terraform_tflint_options *g:ale_terraform_tflint_options*
*b:ale_terraform_tflint_options*
Type: |String|
Default: `'-f json'`
This variable can be changed to pass different options to tflint. Ale does
expect json output from tflint, so if you change this, you'll probably want
to include '-f json' in your new value.
===============================================================================
vim:tw=78:ts=2:sts=2:sw=2:ft=help:norl:

View File

@ -181,6 +181,8 @@ CONTENTS *ale-contents*
stylelint...........................|ale-stylus-stylelint|
tcl...................................|ale-tcl-options|
nagelfar............................|ale-tcl-nagelfar|
terraform.............................|ale-terraform-options|
tflint..............................|ale-terraform-tflint|
tex...................................|ale-tex-options|
chktex..............................|ale-tex-chktex|
lacheck.............................|ale-tex-lacheck|
@ -320,6 +322,7 @@ Notes:
* SQL: `sqlint`
* Swift: `swiftlint`, `swiftformat`
* Tcl: `nagelfar`!!
* Terraform: `tflint`
* Texinfo: `proselint`, `write-good`
* Text^: `proselint`, `vale`, `write-good`
* Thrift: `thrift`

View File

@ -0,0 +1,32 @@
Before:
Save g:ale_terraform_tflint_executable
Save g:ale_terraform_tflint_options
runtime ale_linters/terraform/tflint.vim
After:
Restore
call ale#linter#Reset()
Execute(The default executable should be configurable):
AssertEqual 'tflint', ale_linters#terraform#tflint#GetExecutable(bufnr(''))
let g:ale_terraform_tflint_executable = 'asdf'
AssertEqual 'asdf', ale_linters#terraform#tflint#GetExecutable(bufnr(''))
Execute(The default command should be good):
let g:ale_terraform_tflint_executable = 'tflint'
AssertEqual
\ ale#Escape('tflint') . ' -f json',
\ ale_linters#terraform#tflint#GetCommand(bufnr(''))
Execute(Overriding things should work):
let g:ale_terraform_tflint_executable = 'fnord'
let g:ale_terraform_tflint_options = '--whatever'
AssertEqual
\ ale#Escape('fnord') . ' --whatever -f json',
\ ale_linters#terraform#tflint#GetCommand(bufnr(''))

View File

@ -0,0 +1,28 @@
Before:
runtime! ale_linters/terraform/tflint.vim
After:
call ale#linter#Reset()
Execute(The tflint handler should parse items correctly):
AssertEqual
\ [
\ {
\ 'lnum': 12,
\ 'text': 'be warned, traveller',
\ 'type': 'W',
\ },
\ {
\ 'lnum': 9,
\ 'text': 'error message',
\ 'type': 'E',
\ },
\ {
\ 'lnum': 5,
\ 'text': 'just so ya know',
\ 'type': 'I',
\ },
\ ],
\ ale_linters#terraform#tflint#Handle(123, [
\ '[ { "detector": "aws_db_instance_readable_password", "type": "WARNING", "message": "be warned, traveller", "line": 12, "file": "github.com/wata727/example-module/aws_db_instance.tf", "link": "https://github.com/wata727/tflint/blob/master/docs/aws_db_instance_readable_password.md" }, { "detector": "aws_elasticache_cluster_invalid_type", "type": "ERROR", "message": "error message", "line": 9, "file": "github.com/wata727/example-module/aws_elasticache_cluster.tf", "link": "https://github.com/wata727/tflint/blob/master/docs/aws_elasticache_cluster_invalid_type.md" }, { "detector": "aws_instance_not_specified_iam_profile", "type": "NOTICE", "message": "just so ya know", "line": 5, "file": "github.com/wata727/example-module/aws_instance.tf", "link": "https://github.com/wata727/tflint/blob/master/docs/aws_instance_not_specified_iam_profile.md" } ]'
\ ])

View File

@ -0,0 +1,18 @@
Before:
call ale#test#SetDirectory('/testplugin/test')
runtime ale_linters/terraform/tflint.vim
After:
call ale#test#RestoreDirectory()
call ale#linter#Reset()
Execute(adjacent config file should be found):
call ale#test#SetFilename('tflint-test-files/foo/bar.tf')
AssertEqual
\ (
\ ale#Escape('tflint')
\ . ' --config '
\ . ale#Escape(ale#path#Winify(g:dir . '/tflint-test-files/foo/.tflint.hcl'))
\ . ' -f json'
\ ),
\ ale_linters#terraform#tflint#GetCommand(bufnr(''))

View File

View File