Skip to content

Commit

Permalink
Merge pull request #1586 from marsam/add-terraform-checkers
Browse files Browse the repository at this point in the history
Add terraform checkers
  • Loading branch information
cpitclaudel committed Jun 19, 2019
2 parents a296e07 + d2cd2aa commit 8970b2f
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Expand Up @@ -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:

Expand Down
1 change: 1 addition & 0 deletions Cask
Expand Up @@ -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")
Expand Down
12 changes: 12 additions & 0 deletions doc/languages.rst
Expand Up @@ -1370,6 +1370,18 @@ to view the docstring of the syntax checker. Likewise, you may use

Check Tcl syntax with `Nagelfar <http://nagelfar.sourceforge.net/>`_.

.. 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 <https://github.com/wata727/tflint>`_

.. supported-language:: Text

.. syntax-checker:: proselint
Expand Down
49 changes: 49 additions & 0 deletions flycheck.el
Expand Up @@ -261,6 +261,8 @@ attention to case differences."
sql-sqlint
systemd-analyze
tcl-nagelfar
terraform
terraform-tflint
tex-chktex
tex-lacheck
texinfo
Expand Down Expand Up @@ -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 <stdin> 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.

Expand Down
14 changes: 14 additions & 0 deletions test/flycheck-test.el
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions test/resources/language/terraform/error.tf
@@ -0,0 +1,4 @@
resource "aws_instance" "web" {
ami = "ami-b73b63a0"
instance_type = "t1.2xlarge" # invalid type
}
3 changes: 3 additions & 0 deletions test/resources/language/terraform/syntax-error.tf
@@ -0,0 +1,3 @@
provider "aws" {
region = "us-east-1";
}

0 comments on commit 8970b2f

Please sign in to comment.