From e4456a4e0e9622baf2af30acd34a28963df5f4fa Mon Sep 17 00:00:00 2001 From: Nathaniel Williams Date: Thu, 26 Oct 2017 19:32:33 +0100 Subject: [PATCH] Add tflint fot Terraform --- README.md | 1 + ale_linters/terraform/tflint.vim | 61 +++++++++++++++++++ doc/ale-terraform.txt | 29 +++++++++ doc/ale.txt | 3 + ...st_terraform_tflint_command_callback.vader | 32 ++++++++++ test/handler/test_tflint_handler.vader | 28 +++++++++ test/test_tflint_config_detection.vader | 18 ++++++ test/tflint-test-files/foo/.tflint.hcl | 0 test/tflint-test-files/foo/bar.tf | 0 9 files changed, 172 insertions(+) create mode 100644 ale_linters/terraform/tflint.vim create mode 100644 doc/ale-terraform.txt create mode 100644 test/command_callback/test_terraform_tflint_command_callback.vader create mode 100644 test/handler/test_tflint_handler.vader create mode 100644 test/test_tflint_config_detection.vader create mode 100644 test/tflint-test-files/foo/.tflint.hcl create mode 100644 test/tflint-test-files/foo/bar.tf diff --git a/README.md b/README.md index a8901785..10c63b86 100644 --- a/README.md +++ b/README.md @@ -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/) | diff --git a/ale_linters/terraform/tflint.vim b/ale_linters/terraform/tflint.vim new file mode 100644 index 00000000..f66a9635 --- /dev/null +++ b/ale_linters/terraform/tflint.vim @@ -0,0 +1,61 @@ +" Author: Nat Williams +" 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 diff --git a/doc/ale-terraform.txt b/doc/ale-terraform.txt new file mode 100644 index 00000000..ec86e9a0 --- /dev/null +++ b/doc/ale-terraform.txt @@ -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: + diff --git a/doc/ale.txt b/doc/ale.txt index 1bc639b8..8aea7f80 100644 --- a/doc/ale.txt +++ b/doc/ale.txt @@ -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` diff --git a/test/command_callback/test_terraform_tflint_command_callback.vader b/test/command_callback/test_terraform_tflint_command_callback.vader new file mode 100644 index 00000000..ba768152 --- /dev/null +++ b/test/command_callback/test_terraform_tflint_command_callback.vader @@ -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('')) diff --git a/test/handler/test_tflint_handler.vader b/test/handler/test_tflint_handler.vader new file mode 100644 index 00000000..95671b8a --- /dev/null +++ b/test/handler/test_tflint_handler.vader @@ -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" } ]' + \ ]) diff --git a/test/test_tflint_config_detection.vader b/test/test_tflint_config_detection.vader new file mode 100644 index 00000000..ac64c031 --- /dev/null +++ b/test/test_tflint_config_detection.vader @@ -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('')) diff --git a/test/tflint-test-files/foo/.tflint.hcl b/test/tflint-test-files/foo/.tflint.hcl new file mode 100644 index 00000000..e69de29b diff --git a/test/tflint-test-files/foo/bar.tf b/test/tflint-test-files/foo/bar.tf new file mode 100644 index 00000000..e69de29b