diff --git a/CHANGES.rst b/CHANGES.rst index 7240245ce..56fb19fc3 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -32,6 +32,8 @@ - Text prose with ``textlint`` [GH-1534] - 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/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..d14457b9c 100644 --- a/doc/languages.rst +++ b/doc/languages.rst @@ -1370,6 +1370,18 @@ 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 + + .. syntax-checker:: terraform-tflint + + Check Terraform with `tflint `_ + .. supported-language:: Text .. syntax-checker:: proselint diff --git a/flycheck.el b/flycheck.el index 3f592806d..af101fa8d 100644 --- a/flycheck.el +++ b/flycheck.el @@ -261,6 +261,8 @@ attention to case differences." sql-sqlint systemd-analyze tcl-nagelfar + terraform + terraform-tflint tex-chktex tex-lacheck texinfo @@ -10764,6 +10766,53 @@ 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)) + :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 "A TeX and LaTeX syntax and style checker using chktex. diff --git a/test/flycheck-test.el b/test/flycheck-test.el index bed1ed609..5b181abbf 100644 --- a/test/flycheck-test.el +++ b/test/flycheck-test.el @@ -4081,6 +4081,20 @@ 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 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 +} 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"; +}