Skip to content

Commit d9c2229

Browse files
authored
Merge pull request #32 from fatbasstard/fvb/empty-file-rule
2 parents 9b77ebe + d12dffa commit d9c2229

File tree

7 files changed

+133
-1
lines changed

7 files changed

+133
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ You can customize rule specific configurations. See [rule docs](docs/rules/) for
7171
|Rule|Description|Severity|Enabled by default|
7272
| --- | --- | --- | --- |
7373
|[formatter_blank_line](docs/rules/formatter_blank_line.md)|ensures that there are no extra blank lines|WARNING|✔
74+
|[formatter_empty_file](docs/rules/formatter_empty_file.md)|ensures that files are not empty|WARNING|✔
7475
|[formatter_eof](docs/rules/formatter_eof.md)|ensures that file end with new line|WARNING|✔
7576
|[formatter_max_len](docs/rules/formatter_max_len.md)|ensures the limitation of code line length|WARNING|✔
7677
|[formatter_trailing_comma](docs/rules/formatter_trailing_comma.md)|ensures that tuple element always end with comma|WARNING|✔

docs/rules/formatter_empty_file.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# formatter_empty_file
2+
3+
This rule ensures there are no empty Terraform files.
4+
5+
## Example
6+
7+
```
8+
$ tflint
9+
10+
1 issue(s) found:
11+
12+
empty_file.tf:0:0: Warning - file has no content (formatter_empty_file)
13+
```
14+
15+
## Why
16+
17+
Having empty files in a project is confusing and gives unused maintenance and confusing, e.g. when browsing through folders.
18+
19+
## How To Fix
20+
21+
Remove the empty files from the project.

main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ func main() {
2727
rules.NewFormatterMaxLenRule(),
2828
rules.NewFormatterEOFRule(),
2929
rules.NewFormatterBlankLineRule(),
30+
rules.NewFormatterEmptyFileRule(),
3031
},
3132
},
3233
})

rules/formatter_empty_file.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package rules
2+
3+
import (
4+
"github.com/hashicorp/hcl/v2"
5+
"github.com/terraform-linters/tflint-plugin-sdk/tflint"
6+
"github.com/thaim/tflint-ruleset-formatter/project"
7+
)
8+
9+
// FormatterEmptyFileRule checks if a Terraform file actually has any content
10+
type FormatterEmptyFileRule struct {
11+
tflint.DefaultRule
12+
}
13+
14+
func NewFormatterEmptyFileRule() *FormatterEmptyFileRule {
15+
return &FormatterEmptyFileRule{}
16+
}
17+
18+
func (r *FormatterEmptyFileRule) Name() string {
19+
return "formatter_empty_file"
20+
}
21+
22+
func (r *FormatterEmptyFileRule) Enabled() bool {
23+
return true
24+
}
25+
26+
func (r *FormatterEmptyFileRule) Severity() tflint.Severity {
27+
return tflint.WARNING
28+
}
29+
30+
func (r *FormatterEmptyFileRule) Link() string {
31+
return project.ReferenceLink(r.Name())
32+
}
33+
34+
func (r *FormatterEmptyFileRule) Check(runner tflint.Runner) error {
35+
files, err := runner.GetFiles()
36+
if err != nil {
37+
return err
38+
}
39+
for name, file := range files {
40+
if err := r.checkEmpty(runner, name, file); err != nil {
41+
return err
42+
}
43+
44+
}
45+
return nil
46+
}
47+
48+
func (r *FormatterEmptyFileRule) checkEmpty(runner tflint.Runner, filename string, file *hcl.File) error {
49+
if len(file.Bytes) == 0 {
50+
fileRange := hcl.Range{
51+
Filename: filename,
52+
Start: hcl.Pos{Line: 0, Column: 0},
53+
End: hcl.Pos{Line: 0, Column: 0},
54+
}
55+
56+
return runner.EmitIssue(
57+
r,
58+
"file has no content",
59+
fileRange,
60+
)
61+
}
62+
63+
return nil
64+
}

rules/formatter_empty_file_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package rules
2+
3+
import (
4+
"testing"
5+
6+
hcl "github.com/hashicorp/hcl/v2"
7+
"github.com/terraform-linters/tflint-plugin-sdk/helper"
8+
)
9+
10+
func Test_FormatterEmptyFile(t *testing.T) {
11+
cases := []struct {
12+
Name string
13+
Content string
14+
Expected helper.Issues
15+
}{
16+
{
17+
Name: "file has no content",
18+
Content: ``,
19+
Expected: helper.Issues{
20+
{
21+
Rule: NewFormatterEmptyFileRule(),
22+
Message: "file has no content",
23+
Range: hcl.Range{
24+
Filename: "resource.tf",
25+
Start: hcl.Pos{Line: 0, Column: 0},
26+
End: hcl.Pos{Line: 0, Column: 0},
27+
},
28+
},
29+
},
30+
},
31+
}
32+
33+
rule := NewFormatterEmptyFileRule()
34+
35+
for _, tc := range cases {
36+
runner := helper.TestRunner(t, map[string]string{"resource.tf": tc.Content})
37+
38+
if err := rule.Check(runner); err != nil {
39+
t.Fatalf("Unexpected error occurred: %s", err)
40+
}
41+
42+
helper.AssertIssues(t, tc.Expected, runner.Issues)
43+
}
44+
}

rules/generator/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ func main() {
3232
TODO:
3333
1. Remove all "TODO" comments from generated files.
3434
2. Write implementation of the rule.
35-
3. Add a link to the generated documentation into docs/rules/README.md`)
35+
3. Add the new rule to Rules in main.go
36+
4. Add a link to the generated documentation into docs/rules/README.md`)
3637
}
3738

3839
func toCamel(ruleName string) string {

testdata/empty_file.tf

Whitespace-only changes.

0 commit comments

Comments
 (0)