From 78929f60cdcc58ae10facf9263f683135ee1b1c3 Mon Sep 17 00:00:00 2001 From: Kazuma Watanabe Date: Tue, 2 May 2023 12:48:06 +0000 Subject: [PATCH] Add autofix support --- go.mod | 2 +- go.sum | 4 ++-- rules/terraform_comment_syntax.go | 14 +++++++++++++- rules/terraform_unused_declarations.go | 3 ++- terraform/ruleset.go | 14 ++++---------- 5 files changed, 22 insertions(+), 15 deletions(-) diff --git a/go.mod b/go.mod index f9703bd..7649940 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,7 @@ require ( github.com/hashicorp/go-version v1.6.0 github.com/hashicorp/hcl/v2 v2.16.2 github.com/hashicorp/terraform-registry-address v0.2.0 - github.com/terraform-linters/tflint-plugin-sdk v0.16.1 + github.com/terraform-linters/tflint-plugin-sdk v0.16.2-0.20230506154420-4f23c16c6801 github.com/zclconf/go-cty v1.13.1 ) diff --git a/go.sum b/go.sum index 3859a9c..998e8ca 100644 --- a/go.sum +++ b/go.sum @@ -407,8 +407,8 @@ github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1F github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= -github.com/terraform-linters/tflint-plugin-sdk v0.16.1 h1:fBfLL8KzP3pkQrNp3iQxaGoKBoMo2sFYoqmhuo6yc+A= -github.com/terraform-linters/tflint-plugin-sdk v0.16.1/go.mod h1:ltxVy04PRwptL6P/Ugz2ZeTNclYapClrLn/kVFXJGzo= +github.com/terraform-linters/tflint-plugin-sdk v0.16.2-0.20230506154420-4f23c16c6801 h1:4s6pIfOOaiF89JClxNeRHTzYHM+0XU4U73mm5LxPJ0o= +github.com/terraform-linters/tflint-plugin-sdk v0.16.2-0.20230506154420-4f23c16c6801/go.mod h1:ltxVy04PRwptL6P/Ugz2ZeTNclYapClrLn/kVFXJGzo= github.com/ulikunitz/xz v0.5.10 h1:t92gobL9l3HE202wg3rlk19F6X+JOxl9BBrCCMYEYd8= github.com/ulikunitz/xz v0.5.10/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14= github.com/vmihailenco/msgpack/v5 v5.3.5 h1:5gO0H1iULLWGhs2H5tbAHIZTV8/cYafcFOr9znI5mJU= diff --git a/rules/terraform_comment_syntax.go b/rules/terraform_comment_syntax.go index ff162ef..719579d 100644 --- a/rules/terraform_comment_syntax.go +++ b/rules/terraform_comment_syntax.go @@ -79,10 +79,22 @@ func (r *TerraformCommentSyntaxRule) checkComments(runner tflint.Runner, filenam } if strings.HasPrefix(string(token.Bytes), "//") { - if err := runner.EmitIssue( + if err := runner.EmitIssueWithFix( r, "Single line comments should begin with #", token.Range, + func(f *tflint.Fixer) error { + targetRange := hcl.Range{ + Filename: filename, + Start: token.Range.Start, + End: hcl.Pos{ + Line: token.Range.Start.Line, + Column: token.Range.Start.Column + 2, + Byte: token.Range.Start.Byte + 2, + }, + } + return f.Replace(targetRange, "#") + }, ); err != nil { return err } diff --git a/rules/terraform_unused_declarations.go b/rules/terraform_unused_declarations.go index 5387a7c..e10225e 100644 --- a/rules/terraform_unused_declarations.go +++ b/rules/terraform_unused_declarations.go @@ -92,10 +92,11 @@ func (r *TerraformUnusedDeclarationsRule) Check(rr tflint.Runner) error { } } for _, local := range decl.Locals { - if err := runner.EmitIssue( + if err := runner.EmitIssueWithFix( r, fmt.Sprintf(`local.%s is declared but not used`, local.Name), local.DefRange, + func(f *tflint.Fixer) error { return f.RemoveAttribute(local.DefRange) }, ); err != nil { return err } diff --git a/terraform/ruleset.go b/terraform/ruleset.go index 4c7d037..bb732b3 100644 --- a/terraform/ruleset.go +++ b/terraform/ruleset.go @@ -84,6 +84,7 @@ func (r *RuleSet) ApplyConfig(body *hclext.BodyContent) error { } r.EnabledRules = []tflint.Rule{} + r.Fix = r.globalConfig.Fix for _, rule := range r.PresetRules["all"] { enabled := rule.Enabled() if len(only) > 0 { @@ -107,14 +108,7 @@ func (r *RuleSet) ApplyConfig(body *hclext.BodyContent) error { return nil } -// Check runs inspection for each rule by applying Runner. -func (r *RuleSet) Check(rr tflint.Runner) error { - runner := NewRunner(rr) - - for _, rule := range r.EnabledRules { - if err := rule.Check(runner); err != nil { - return fmt.Errorf("Failed to check `%s` rule: %s", rule.Name(), err) - } - } - return nil +// NewRunner injects a custom runner +func (r *RuleSet) NewRunner(runner tflint.Runner) (tflint.Runner, error) { + return NewRunner(runner), nil }