Skip to content

Commit

Permalink
Add autofix support
Browse files Browse the repository at this point in the history
  • Loading branch information
wata727 committed May 6, 2023
1 parent 599e680 commit 4740bf7
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 15 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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.20230506152036-8a5cb28ddb82
github.com/zclconf/go-cty v1.13.1
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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.20230506152036-8a5cb28ddb82 h1:UowlR8wsn99YE47LNR8JqdwQvtykLCDbj1zLY964oaI=
github.com/terraform-linters/tflint-plugin-sdk v0.16.2-0.20230506152036-8a5cb28ddb82/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=
Expand Down
14 changes: 13 additions & 1 deletion rules/terraform_comment_syntax.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion rules/terraform_unused_declarations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
14 changes: 4 additions & 10 deletions terraform/ruleset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}

0 comments on commit 4740bf7

Please sign in to comment.