Skip to content

Commit

Permalink
Merge pull request #32 from fatbasstard/fvb/empty-file-rule
Browse files Browse the repository at this point in the history
  • Loading branch information
thaim authored Aug 28, 2023
2 parents 9b77ebe + d12dffa commit d9c2229
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ You can customize rule specific configurations. See [rule docs](docs/rules/) for
|Rule|Description|Severity|Enabled by default|
| --- | --- | --- | --- |
|[formatter_blank_line](docs/rules/formatter_blank_line.md)|ensures that there are no extra blank lines|WARNING|✔
|[formatter_empty_file](docs/rules/formatter_empty_file.md)|ensures that files are not empty|WARNING|✔
|[formatter_eof](docs/rules/formatter_eof.md)|ensures that file end with new line|WARNING|✔
|[formatter_max_len](docs/rules/formatter_max_len.md)|ensures the limitation of code line length|WARNING|✔
|[formatter_trailing_comma](docs/rules/formatter_trailing_comma.md)|ensures that tuple element always end with comma|WARNING|✔
Expand Down
21 changes: 21 additions & 0 deletions docs/rules/formatter_empty_file.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# formatter_empty_file

This rule ensures there are no empty Terraform files.

## Example

```
$ tflint
1 issue(s) found:
empty_file.tf:0:0: Warning - file has no content (formatter_empty_file)
```

## Why

Having empty files in a project is confusing and gives unused maintenance and confusing, e.g. when browsing through folders.

## How To Fix

Remove the empty files from the project.
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func main() {
rules.NewFormatterMaxLenRule(),
rules.NewFormatterEOFRule(),
rules.NewFormatterBlankLineRule(),
rules.NewFormatterEmptyFileRule(),
},
},
})
Expand Down
64 changes: 64 additions & 0 deletions rules/formatter_empty_file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package rules

import (
"github.com/hashicorp/hcl/v2"
"github.com/terraform-linters/tflint-plugin-sdk/tflint"
"github.com/thaim/tflint-ruleset-formatter/project"
)

// FormatterEmptyFileRule checks if a Terraform file actually has any content
type FormatterEmptyFileRule struct {
tflint.DefaultRule
}

func NewFormatterEmptyFileRule() *FormatterEmptyFileRule {
return &FormatterEmptyFileRule{}
}

func (r *FormatterEmptyFileRule) Name() string {
return "formatter_empty_file"
}

func (r *FormatterEmptyFileRule) Enabled() bool {
return true
}

func (r *FormatterEmptyFileRule) Severity() tflint.Severity {
return tflint.WARNING
}

func (r *FormatterEmptyFileRule) Link() string {
return project.ReferenceLink(r.Name())
}

func (r *FormatterEmptyFileRule) Check(runner tflint.Runner) error {
files, err := runner.GetFiles()
if err != nil {
return err
}
for name, file := range files {
if err := r.checkEmpty(runner, name, file); err != nil {
return err
}

}
return nil
}

func (r *FormatterEmptyFileRule) checkEmpty(runner tflint.Runner, filename string, file *hcl.File) error {
if len(file.Bytes) == 0 {
fileRange := hcl.Range{
Filename: filename,
Start: hcl.Pos{Line: 0, Column: 0},
End: hcl.Pos{Line: 0, Column: 0},
}

return runner.EmitIssue(
r,
"file has no content",
fileRange,
)
}

return nil
}
44 changes: 44 additions & 0 deletions rules/formatter_empty_file_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package rules

import (
"testing"

hcl "github.com/hashicorp/hcl/v2"
"github.com/terraform-linters/tflint-plugin-sdk/helper"
)

func Test_FormatterEmptyFile(t *testing.T) {
cases := []struct {
Name string
Content string
Expected helper.Issues
}{
{
Name: "file has no content",
Content: ``,
Expected: helper.Issues{
{
Rule: NewFormatterEmptyFileRule(),
Message: "file has no content",
Range: hcl.Range{
Filename: "resource.tf",
Start: hcl.Pos{Line: 0, Column: 0},
End: hcl.Pos{Line: 0, Column: 0},
},
},
},
},
}

rule := NewFormatterEmptyFileRule()

for _, tc := range cases {
runner := helper.TestRunner(t, map[string]string{"resource.tf": tc.Content})

if err := rule.Check(runner); err != nil {
t.Fatalf("Unexpected error occurred: %s", err)
}

helper.AssertIssues(t, tc.Expected, runner.Issues)
}
}
3 changes: 2 additions & 1 deletion rules/generator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ func main() {
TODO:
1. Remove all "TODO" comments from generated files.
2. Write implementation of the rule.
3. Add a link to the generated documentation into docs/rules/README.md`)
3. Add the new rule to Rules in main.go
4. Add a link to the generated documentation into docs/rules/README.md`)
}

func toCamel(ruleName string) string {
Expand Down
Empty file added testdata/empty_file.tf
Empty file.

0 comments on commit d9c2229

Please sign in to comment.