Skip to content

Commit f4d6b21

Browse files
authored
Merge pull request #41 from terraform-linters/remove_metadata
tflint: Remove Metadata
2 parents a6a54a4 + 75cee3a commit f4d6b21

File tree

6 files changed

+97
-20
lines changed

6 files changed

+97
-20
lines changed

helper/runner.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,18 @@ func (r *Runner) EvaluateExpr(expr hcl.Expression, ret interface{}) error {
145145
return gocty.FromCtyValue(val, ret)
146146
}
147147

148+
// EmitIssueOnExpr adds an issue to the runner itself.
149+
func (r *Runner) EmitIssueOnExpr(rule tflint.Rule, message string, expr hcl.Expression) error {
150+
r.Issues = append(r.Issues, &Issue{
151+
Rule: rule,
152+
Message: message,
153+
Range: expr.Range(),
154+
})
155+
return nil
156+
}
157+
148158
// EmitIssue adds an issue to the runner itself.
149-
func (r *Runner) EmitIssue(rule tflint.Rule, message string, location hcl.Range, meta tflint.Metadata) error {
159+
func (r *Runner) EmitIssue(rule tflint.Rule, message string, location hcl.Range) error {
150160
r.Issues = append(r.Issues, &Issue{
151161
Rule: rule,
152162
Message: message,

helper/runner_test.go

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,38 @@ func (r *dummyRule) Severity() string { return tflint.ERROR }
341341
func (r *dummyRule) Link() string { return "" }
342342
func (r *dummyRule) Check(tflint.Runner) error { return nil }
343343

344+
func Test_EmitIssueOnExpr(t *testing.T) {
345+
src := `
346+
resource "aws_instance" "foo" {
347+
instance_type = "t2.micro"
348+
}`
349+
350+
runner := TestRunner(t, map[string]string{"main.tf": src})
351+
352+
err := runner.WalkResourceAttributes("aws_instance", "instance_type", func(attribute *hcl.Attribute) error {
353+
if err := runner.EmitIssueOnExpr(&dummyRule{}, "issue found", attribute.Expr); err != nil {
354+
t.Fatal(err)
355+
}
356+
return nil
357+
})
358+
if err != nil {
359+
t.Fatal(err)
360+
}
361+
362+
expected := Issues{
363+
{
364+
Rule: &dummyRule{},
365+
Message: "issue found",
366+
Range: hcl.Range{Filename: "main.tf", Start: hcl.Pos{Line: 3, Column: 19}, End: hcl.Pos{Line: 3, Column: 29}},
367+
},
368+
}
369+
370+
opt := cmpopts.IgnoreFields(hcl.Pos{}, "Byte")
371+
if !cmp.Equal(expected, runner.Issues, opt) {
372+
t.Fatalf("Diff: %s", cmp.Diff(expected, runner.Issues, opt))
373+
}
374+
}
375+
344376
func Test_EmitIssue(t *testing.T) {
345377
src := `
346378
resource "aws_instance" "foo" {
@@ -350,7 +382,7 @@ resource "aws_instance" "foo" {
350382
runner := TestRunner(t, map[string]string{"main.tf": src})
351383

352384
err := runner.WalkResourceAttributes("aws_instance", "instance_type", func(attribute *hcl.Attribute) error {
353-
if err := runner.EmitIssue(&dummyRule{}, "issue found", attribute.Expr.Range(), tflint.Metadata{}); err != nil {
385+
if err := runner.EmitIssue(&dummyRule{}, "issue found", attribute.Expr.Range()); err != nil {
354386
t.Fatal(err)
355387
}
356388
return nil

tflint/client/client.go

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -140,20 +140,34 @@ func (c *Client) EvaluateExpr(expr hcl.Expression, ret interface{}) error {
140140
return nil
141141
}
142142

143-
// EmitIssue calls the server-side EmitIssue method with the passed rule, range and metadata.
144-
func (c *Client) EmitIssue(rule tflint.Rule, message string, location hcl.Range, meta tflint.Metadata) error {
143+
// EmitIssueOnExpr calls the server-side EmitIssue method with the passed expression.
144+
func (c *Client) EmitIssueOnExpr(rule tflint.Rule, message string, expr hcl.Expression) error {
145145
req := &EmitIssueRequest{
146146
Rule: encodeRule(rule),
147147
Message: message,
148-
Location: location,
148+
Location: expr.Range(),
149149
}
150150

151-
if meta.Expr != nil {
152-
src, err := ioutil.ReadFile(meta.Expr.Range().Filename)
153-
if err != nil {
154-
return err
155-
}
156-
req.Expr, req.ExprRange = encodeExpr(src, meta.Expr)
151+
src, err := ioutil.ReadFile(expr.Range().Filename)
152+
if err != nil {
153+
return err
154+
}
155+
req.Expr, req.ExprRange = encodeExpr(src, expr)
156+
157+
if err := c.rpcClient.Call("Plugin.EmitIssue", &req, new(interface{})); err != nil {
158+
return err
159+
}
160+
return nil
161+
}
162+
163+
// EmitIssue calls the server-side EmitIssue method with the passed rule and range.
164+
// You should use EmitIssueOnExpr if you want to emit an issue for an expression.
165+
// This API provides a lower level interface.
166+
func (c *Client) EmitIssue(rule tflint.Rule, message string, location hcl.Range) error {
167+
req := &EmitIssueRequest{
168+
Rule: encodeRule(rule),
169+
Message: message,
170+
Location: location,
157171
}
158172

159173
if err := c.rpcClient.Call("Plugin.EmitIssue", &req, new(interface{})); err != nil {

tflint/client/client_test.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,29 @@ func (*testRule) Severity() string { return "Error" }
311311
func (*testRule) Link() string { return "" }
312312
func (*testRule) Check(tflint.Runner) error { return nil }
313313

314+
func Test_EmitIssueOnExpr(t *testing.T) {
315+
client, server := startMockServer(t)
316+
defer server.Listener.Close()
317+
318+
file, err := ioutil.TempFile("", "tflint-test-evaluateExpr-*.tf")
319+
if err != nil {
320+
t.Fatal(err)
321+
}
322+
defer os.Remove(file.Name())
323+
if _, err := file.Write([]byte("1")); err != nil {
324+
t.Fatal(err)
325+
}
326+
327+
expr, diags := hclsyntax.ParseExpression([]byte("1"), file.Name(), hcl.Pos{Line: 1, Column: 1})
328+
if diags.HasErrors() {
329+
t.Fatal(diags)
330+
}
331+
332+
if err := client.EmitIssueOnExpr(&testRule{}, file.Name(), expr); err != nil {
333+
t.Fatal(err)
334+
}
335+
}
336+
314337
func Test_EmitIssue(t *testing.T) {
315338
client, server := startMockServer(t)
316339
defer server.Listener.Close()
@@ -329,7 +352,7 @@ func Test_EmitIssue(t *testing.T) {
329352
t.Fatal(diags)
330353
}
331354

332-
if err := client.EmitIssue(&testRule{}, file.Name(), hcl.Range{}, tflint.Metadata{Expr: expr}); err != nil {
355+
if err := client.EmitIssue(&testRule{}, file.Name(), expr.Range()); err != nil {
333356
t.Fatal(err)
334357
}
335358
}

tflint/interface.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,13 @@ type Runner interface {
2525
// to determine whether to continue processing.
2626
EvaluateExpr(expr hcl.Expression, ret interface{}) error
2727

28+
// EmitIssue sends an issue with an expression to TFLint. You need to pass the message of the issue and the expression.
29+
EmitIssueOnExpr(rule Rule, message string, expr hcl.Expression) error
30+
2831
// EmitIssue sends an issue to TFLint. You need to pass the message of the issue and the range.
29-
EmitIssue(rule Rule, message string, location hcl.Range, meta Metadata) error
32+
// You should use EmitIssueOnExpr if you want to emit an issue for an expression.
33+
// This API provides a lower level interface.
34+
EmitIssue(rule Rule, message string, location hcl.Range) error
3035

3136
// EnsureNoError is a helper for error handling. Depending on the type of error generated by EvaluateExpr,
3237
// determine whether to exit, skip, or continue. If it is continued, the passed function will be executed.

tflint/issue.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package tflint
22

3-
import hcl "github.com/hashicorp/hcl/v2"
4-
53
// List of issue severity levels. The rules implemented by a plugin can be set to any severity.
64
const (
75
// ERROR is possible errors
@@ -11,8 +9,3 @@ const (
119
// NOTICE is not important, it's mentioned
1210
NOTICE = "Notice"
1311
)
14-
15-
// Metadata is an additional data sent to the host process to build the issue.
16-
type Metadata struct {
17-
Expr hcl.Expression
18-
}

0 commit comments

Comments
 (0)