Skip to content

Commit

Permalink
Introduce autofix API
Browse files Browse the repository at this point in the history
  • Loading branch information
wata727 committed May 2, 2023
1 parent b8f431f commit d07f142
Show file tree
Hide file tree
Showing 10 changed files with 423 additions and 330 deletions.
5 changes: 5 additions & 0 deletions helper/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,11 @@ func (r *Runner) EmitIssue(rule tflint.Rule, message string, location hcl.Range)
return nil
}

func (r *Runner) EmitIssueWithFix(rule tflint.Rule, message string, location hcl.Range, fixFunc func(f *tflint.Fixer) error) error {
// TODO: Implement
return nil
}

// EnsureNoError is a method that simply runs a function if there is no error.
//
// Deprecated: Use EvaluateExpr with a function callback. e.g. EvaluateExpr(expr, func (val T) error {}, ...)
Expand Down
2 changes: 1 addition & 1 deletion plugin/host2plugin/host2plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ func (s *mockServer) EvaluateExpr(expr hcl.Expression, opts tflint.EvaluateExprO
return cty.Value{}, nil
}

func (s *mockServer) EmitIssue(rule tflint.Rule, message string, location hcl.Range) error {
func (s *mockServer) EmitIssue(rule tflint.Rule, message string, location hcl.Range, fixed map[string][]byte) error {
return nil
}

Expand Down
8 changes: 7 additions & 1 deletion plugin/host2plugin/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,13 @@ func (s *GRPCServer) Check(ctx context.Context, req *proto.Check_Request) (*prot
}
defer conn.Close()

runner, err := s.impl.NewRunner(&plugin2host.GRPCClient{Client: proto.NewRunnerClient(conn)})
client := proto.NewRunnerClient(conn)
resp, err := client.GetFiles(ctx, &proto.GetFiles_Request{})
if err != nil {
return nil, toproto.Error(codes.FailedPrecondition, err)
}

runner, err := s.impl.NewRunner(&plugin2host.GRPCClient{Client: client, Fixer: tflint.NewFixer(resp.Files)})
if err != nil {
return nil, toproto.Error(codes.FailedPrecondition, err)
}
Expand Down
15 changes: 15 additions & 0 deletions plugin/plugin2host/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
// GRPCClient is a plugin-side implementation. Plugin can send requests through the client to host's gRPC server.
type GRPCClient struct {
Client proto.RunnerClient
Fixer *tflint.Fixer
}

var _ tflint.Runner = &GRPCClient{}
Expand Down Expand Up @@ -423,6 +424,20 @@ func (c *GRPCClient) EmitIssue(rule tflint.Rule, message string, location hcl.Ra
return nil
}

func (c *GRPCClient) EmitIssueWithFix(rule tflint.Rule, message string, location hcl.Range, fixFunc func(f *tflint.Fixer) error) error {
if err := fixFunc(c.Fixer); err != nil {
return err
}

_, err := c.Client.EmitIssue(context.Background(), &proto.EmitIssue_Request{Rule: toproto.Rule(rule), Message: message, Range: toproto.Range(location), Fixed: c.Fixer.Changes()})
if err != nil {
return fromproto.Error(err)
}
// TODO: ApplyChanges() should be called if the issue is not ignored.
c.Fixer.ApplyChanges()
return nil
}

// EnsureNoError is a helper for error handling. Depending on the type of error generated by EvaluateExpr,
// determine whether to exit, skip, or continue. If it is continued, the passed function will be executed.
//
Expand Down
12 changes: 6 additions & 6 deletions plugin/plugin2host/plugin2host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type mockServerImpl struct {
getFiles func() map[string][]byte
getRuleConfigContent func(string, *hclext.BodySchema) (*hclext.BodyContent, map[string][]byte, error)
evaluateExpr func(hcl.Expression, tflint.EvaluateExprOption) (cty.Value, error)
emitIssue func(tflint.Rule, string, hcl.Range) error
emitIssue func(tflint.Rule, string, hcl.Range, map[string][]byte) error
}

func newMockServer(impl mockServerImpl) *mockServer {
Expand Down Expand Up @@ -99,9 +99,9 @@ func (s *mockServer) EvaluateExpr(expr hcl.Expression, opts tflint.EvaluateExprO
return cty.Value{}, nil
}

func (s *mockServer) EmitIssue(rule tflint.Rule, message string, location hcl.Range) error {
func (s *mockServer) EmitIssue(rule tflint.Rule, message string, location hcl.Range, fixed map[string][]byte) error {
if s.impl.emitIssue != nil {
return s.impl.emitIssue(rule, message, location)
return s.impl.emitIssue(rule, message, location, fixed)
}
return nil
}
Expand Down Expand Up @@ -2359,15 +2359,15 @@ func TestEmitIssue(t *testing.T) {
tests := []struct {
Name string
Args func() (tflint.Rule, string, hcl.Range)
ServerImpl func(tflint.Rule, string, hcl.Range) error
ServerImpl func(tflint.Rule, string, hcl.Range, map[string][]byte) error
ErrCheck func(error) bool
}{
{
Name: "emit issue",
Args: func() (tflint.Rule, string, hcl.Range) {
return &Rule{}, "this is test", hcl.Range{Filename: "test.tf", Start: hcl.Pos{Line: 2, Column: 2}, End: hcl.Pos{Line: 2, Column: 10}}
},
ServerImpl: func(rule tflint.Rule, message string, location hcl.Range) error {
ServerImpl: func(rule tflint.Rule, message string, location hcl.Range, fixed map[string][]byte) error {
if rule.Name() != "test_rule" {
return fmt.Errorf("rule.Name() should be test_rule, but %s", rule.Name())
}
Expand Down Expand Up @@ -2400,7 +2400,7 @@ func TestEmitIssue(t *testing.T) {
Args: func() (tflint.Rule, string, hcl.Range) {
return &Rule{}, "this is test", hcl.Range{Filename: "test.tf", Start: hcl.Pos{Line: 2, Column: 2}, End: hcl.Pos{Line: 2, Column: 10}}
},
ServerImpl: func(tflint.Rule, string, hcl.Range) error {
ServerImpl: func(tflint.Rule, string, hcl.Range, map[string][]byte) error {
return errors.New("unexpected error")
},
ErrCheck: func(err error) bool {
Expand Down
4 changes: 2 additions & 2 deletions plugin/plugin2host/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type Server interface {
GetFiles(tflint.ModuleCtxType) map[string][]byte
GetRuleConfigContent(string, *hclext.BodySchema) (*hclext.BodyContent, map[string][]byte, error)
EvaluateExpr(hcl.Expression, tflint.EvaluateExprOption) (cty.Value, error)
EmitIssue(rule tflint.Rule, message string, location hcl.Range) error
EmitIssue(rule tflint.Rule, message string, location hcl.Range, fixed map[string][]byte) error
}

// GetOriginalwd gets the original working directory.
Expand Down Expand Up @@ -172,7 +172,7 @@ func (s *GRPCServer) EmitIssue(ctx context.Context, req *proto.EmitIssue_Request
return nil, status.Error(codes.InvalidArgument, "range should not be null")
}

err := s.Impl.EmitIssue(fromproto.Rule(req.Rule), req.Message, fromproto.Range(req.Range))
err := s.Impl.EmitIssue(fromproto.Rule(req.Rule), req.Message, fromproto.Range(req.Range), req.Fixed)
if err != nil {
return nil, toproto.Error(codes.FailedPrecondition, err)
}
Expand Down
Loading

0 comments on commit d07f142

Please sign in to comment.