From d4f82aa4cf4f377fb98c33c3ccac16b4a4d302e8 Mon Sep 17 00:00:00 2001 From: Mario Rodas Date: Tue, 18 Jun 2019 21:37:49 -0500 Subject: [PATCH 1/2] Add terraform checker --- CHANGES.rst | 1 + Cask | 1 + doc/languages.rst | 8 ++++++++ flycheck.el | 14 ++++++++++++++ test/flycheck-test.el | 8 ++++++++ test/resources/language/terraform/syntax-error.tf | 3 +++ 6 files changed, 35 insertions(+) create mode 100644 test/resources/language/terraform/syntax-error.tf diff --git a/CHANGES.rst b/CHANGES.rst index 3a9d5c66b..cc36d0f08 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -32,6 +32,7 @@ - Text prose with ``textlint`` [GH-1534] - VHDL with ``ghdl`` [GH-1160] - mypy with ``python-mypy`` [GH-1354] + - terraform with ``terraform fmt`` [GH-1586] - New features: diff --git a/Cask b/Cask index 3bf86c740..17799d00a 100644 --- a/Cask +++ b/Cask @@ -49,6 +49,7 @@ (depends-on "scss-mode") (depends-on "slim-mode") (depends-on "systemd") + (depends-on "terraform-mode") (depends-on "tuareg") (depends-on "typescript-mode") (depends-on "web-mode") diff --git a/doc/languages.rst b/doc/languages.rst index 230a1e4f4..b4c8b7b02 100644 --- a/doc/languages.rst +++ b/doc/languages.rst @@ -1370,6 +1370,14 @@ to view the docstring of the syntax checker. Likewise, you may use Check Tcl syntax with `Nagelfar `_. +.. supported-language:: Terraform + + .. syntax-checker:: terraform + + Check Terraform syntax with `terraform fmt`_ + + .. _terraform fmt: https://www.terraform.io/docs/commands/fmt.html + .. supported-language:: Text .. syntax-checker:: proselint diff --git a/flycheck.el b/flycheck.el index 73bd46c33..16e816943 100644 --- a/flycheck.el +++ b/flycheck.el @@ -261,6 +261,7 @@ attention to case differences." sql-sqlint systemd-analyze tcl-nagelfar + terraform tex-chktex tex-lacheck texinfo @@ -10758,6 +10759,19 @@ See URL `http://nagelfar.sourceforge.net/'." (error line-start (file-name) ": " line ": E " (message) line-end)) :modes tcl-mode) +(flycheck-define-checker terraform + "A Terraform syntax checker with `terraform fmt'. + +See URL `https://www.terraform.io/docs/commands/fmt.html'." + :command ("terraform" "fmt" "-no-color" "-") + :standard-input t + :error-patterns + ((error line-start "Error: " (one-or-more not-newline) + "\n\n on line " line ":\n (source code not available)\n\n" + (message (one-or-more (and (one-or-more (not (any ?\n))) ?\n))) + line-end)) + :modes terraform-mode) + (flycheck-define-checker tex-chktex "A TeX and LaTeX syntax and style checker using chktex. diff --git a/test/flycheck-test.el b/test/flycheck-test.el index bed1ed609..b506ed80a 100644 --- a/test/flycheck-test.el +++ b/test/flycheck-test.el @@ -4081,6 +4081,14 @@ Why not: '(12 nil error "Wrong number of arguments \(4\) to \"set\"" :checker tcl-nagelfar))) +(flycheck-ert-def-checker-test terraform terraform nil + (flycheck-ert-should-syntax-check + "language/terraform/syntax-error.tf" 'terraform-mode + '(2 nil error "The \";\" character is not valid. Use newlines to separate arguments and blocks,\nand commas to separate items in collection values." + :checker terraform) + '(2 nil error "An argument definition must end with a newline." + :checker terraform))) + (flycheck-ert-def-checker-test markdown-markdownlint-cli markdown nil (flycheck-ert-should-syntax-check "language/markdown.md" 'markdown-mode diff --git a/test/resources/language/terraform/syntax-error.tf b/test/resources/language/terraform/syntax-error.tf new file mode 100644 index 000000000..73d50d90a --- /dev/null +++ b/test/resources/language/terraform/syntax-error.tf @@ -0,0 +1,3 @@ +provider "aws" { + region = "us-east-1"; +} From d2cd2aae6d599e5e80220cee27d4131a4f8399d1 Mon Sep 17 00:00:00 2001 From: Mario Rodas Date: Tue, 18 Jun 2019 21:46:44 -0500 Subject: [PATCH 2/2] Add terraform-tflint checker --- CHANGES.rst | 1 + doc/languages.rst | 4 +++ flycheck.el | 35 ++++++++++++++++++++++ test/flycheck-test.el | 6 ++++ test/resources/language/terraform/error.tf | 4 +++ 5 files changed, 50 insertions(+) create mode 100644 test/resources/language/terraform/error.tf diff --git a/CHANGES.rst b/CHANGES.rst index cc36d0f08..8ea15846d 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -33,6 +33,7 @@ - VHDL with ``ghdl`` [GH-1160] - mypy with ``python-mypy`` [GH-1354] - terraform with ``terraform fmt`` [GH-1586] + - terraform-tflint with ``tflint`` [GH-1586] - New features: diff --git a/doc/languages.rst b/doc/languages.rst index b4c8b7b02..d14457b9c 100644 --- a/doc/languages.rst +++ b/doc/languages.rst @@ -1378,6 +1378,10 @@ to view the docstring of the syntax checker. Likewise, you may use .. _terraform fmt: https://www.terraform.io/docs/commands/fmt.html + .. syntax-checker:: terraform-tflint + + Check Terraform with `tflint `_ + .. supported-language:: Text .. syntax-checker:: proselint diff --git a/flycheck.el b/flycheck.el index 16e816943..34fb61dcc 100644 --- a/flycheck.el +++ b/flycheck.el @@ -262,6 +262,7 @@ attention to case differences." systemd-analyze tcl-nagelfar terraform + terraform-tflint tex-chktex tex-lacheck texinfo @@ -10770,6 +10771,40 @@ See URL `https://www.terraform.io/docs/commands/fmt.html'." "\n\n on line " line ":\n (source code not available)\n\n" (message (one-or-more (and (one-or-more (not (any ?\n))) ?\n))) line-end)) + :next-checkers ((warning . terraform-tflint)) + :modes terraform-mode) + +(defun flycheck-parse-tflint-linter (output checker buffer) + "Parse tflint warnings from JSON OUTPUT. + +CHECKER and BUFFER denote the CHECKER that returned OUTPUT and +the BUFFER that was checked respectively. + +See URL `https://github.com/wata727/tflint' for more +information about tflint." + (mapcar (lambda (err) + (let-alist err + (flycheck-error-new-at + .line + nil + (pcase .type + (`"ERROR" 'error) + (`"WARNING" 'warning) + ;; Default to error + (_ 'error)) + .message + :id .detector + :checker checker + :buffer buffer + :filename (buffer-file-name buffer)))) + (car (flycheck-parse-json output)))) + +(flycheck-define-checker terraform-tflint + "A Terraform checker using tflint. + +See URL `https://github.com/wata727/tflint'." + :command ("tflint" "--error-with-issues" "--format=json" source) + :error-parser flycheck-parse-tflint-linter :modes terraform-mode) (flycheck-define-checker tex-chktex diff --git a/test/flycheck-test.el b/test/flycheck-test.el index b506ed80a..5b181abbf 100644 --- a/test/flycheck-test.el +++ b/test/flycheck-test.el @@ -4089,6 +4089,12 @@ Why not: '(2 nil error "An argument definition must end with a newline." :checker terraform))) +(flycheck-ert-def-checker-test terraform-tflint terraform nil + (flycheck-ert-should-syntax-check + "language/terraform/error.tf" 'terraform-mode + '(3 nil error "\"t1.2xlarge\" is invalid instance type." + :id "aws_instance_invalid_type" :checker terraform-tflint))) + (flycheck-ert-def-checker-test markdown-markdownlint-cli markdown nil (flycheck-ert-should-syntax-check "language/markdown.md" 'markdown-mode diff --git a/test/resources/language/terraform/error.tf b/test/resources/language/terraform/error.tf new file mode 100644 index 000000000..aeaa9e159 --- /dev/null +++ b/test/resources/language/terraform/error.tf @@ -0,0 +1,4 @@ +resource "aws_instance" "web" { + ami = "ami-b73b63a0" + instance_type = "t1.2xlarge" # invalid type +}