Skip to content

Commit 4740bf7

Browse files
committed
Add autofix support
1 parent 599e680 commit 4740bf7

File tree

5 files changed

+22
-15
lines changed

5 files changed

+22
-15
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ require (
99
github.com/hashicorp/go-version v1.6.0
1010
github.com/hashicorp/hcl/v2 v2.16.2
1111
github.com/hashicorp/terraform-registry-address v0.2.0
12-
github.com/terraform-linters/tflint-plugin-sdk v0.16.1
12+
github.com/terraform-linters/tflint-plugin-sdk v0.16.2-0.20230506152036-8a5cb28ddb82
1313
github.com/zclconf/go-cty v1.13.1
1414
)
1515

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,8 +407,8 @@ github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1F
407407
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
408408
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
409409
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
410-
github.com/terraform-linters/tflint-plugin-sdk v0.16.1 h1:fBfLL8KzP3pkQrNp3iQxaGoKBoMo2sFYoqmhuo6yc+A=
411-
github.com/terraform-linters/tflint-plugin-sdk v0.16.1/go.mod h1:ltxVy04PRwptL6P/Ugz2ZeTNclYapClrLn/kVFXJGzo=
410+
github.com/terraform-linters/tflint-plugin-sdk v0.16.2-0.20230506152036-8a5cb28ddb82 h1:UowlR8wsn99YE47LNR8JqdwQvtykLCDbj1zLY964oaI=
411+
github.com/terraform-linters/tflint-plugin-sdk v0.16.2-0.20230506152036-8a5cb28ddb82/go.mod h1:ltxVy04PRwptL6P/Ugz2ZeTNclYapClrLn/kVFXJGzo=
412412
github.com/ulikunitz/xz v0.5.10 h1:t92gobL9l3HE202wg3rlk19F6X+JOxl9BBrCCMYEYd8=
413413
github.com/ulikunitz/xz v0.5.10/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
414414
github.com/vmihailenco/msgpack/v5 v5.3.5 h1:5gO0H1iULLWGhs2H5tbAHIZTV8/cYafcFOr9znI5mJU=

rules/terraform_comment_syntax.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,22 @@ func (r *TerraformCommentSyntaxRule) checkComments(runner tflint.Runner, filenam
7979
}
8080

8181
if strings.HasPrefix(string(token.Bytes), "//") {
82-
if err := runner.EmitIssue(
82+
if err := runner.EmitIssueWithFix(
8383
r,
8484
"Single line comments should begin with #",
8585
token.Range,
86+
func(f *tflint.Fixer) error {
87+
targetRange := hcl.Range{
88+
Filename: filename,
89+
Start: token.Range.Start,
90+
End: hcl.Pos{
91+
Line: token.Range.Start.Line,
92+
Column: token.Range.Start.Column + 2,
93+
Byte: token.Range.Start.Byte + 2,
94+
},
95+
}
96+
return f.Replace(targetRange, "#")
97+
},
8698
); err != nil {
8799
return err
88100
}

rules/terraform_unused_declarations.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,11 @@ func (r *TerraformUnusedDeclarationsRule) Check(rr tflint.Runner) error {
9292
}
9393
}
9494
for _, local := range decl.Locals {
95-
if err := runner.EmitIssue(
95+
if err := runner.EmitIssueWithFix(
9696
r,
9797
fmt.Sprintf(`local.%s is declared but not used`, local.Name),
9898
local.DefRange,
99+
func(f *tflint.Fixer) error { return f.RemoveAttribute(local.DefRange) },
99100
); err != nil {
100101
return err
101102
}

terraform/ruleset.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ func (r *RuleSet) ApplyConfig(body *hclext.BodyContent) error {
8484
}
8585

8686
r.EnabledRules = []tflint.Rule{}
87+
r.Fix = r.globalConfig.Fix
8788
for _, rule := range r.PresetRules["all"] {
8889
enabled := rule.Enabled()
8990
if len(only) > 0 {
@@ -107,14 +108,7 @@ func (r *RuleSet) ApplyConfig(body *hclext.BodyContent) error {
107108
return nil
108109
}
109110

110-
// Check runs inspection for each rule by applying Runner.
111-
func (r *RuleSet) Check(rr tflint.Runner) error {
112-
runner := NewRunner(rr)
113-
114-
for _, rule := range r.EnabledRules {
115-
if err := rule.Check(runner); err != nil {
116-
return fmt.Errorf("Failed to check `%s` rule: %s", rule.Name(), err)
117-
}
118-
}
119-
return nil
111+
// NewRunner injects a custom runner
112+
func (r *RuleSet) NewRunner(runner tflint.Runner) (tflint.Runner, error) {
113+
return NewRunner(runner), nil
120114
}

0 commit comments

Comments
 (0)