From 0ff0d23287df0af5b2caefaac38caf346d739fd7 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 8 Oct 2016 16:29:25 +0800 Subject: [PATCH] update HCL vendor This fixes primarily printer issues with JSON, list comments, and bare comments not attached to anything in particular. --- .../github.com/hashicorp/hcl/hcl/ast/ast.go | 3 +- .../hashicorp/hcl/hcl/parser/parser.go | 6 +++ .../hashicorp/hcl/hcl/printer/nodes.go | 52 +++++++++++++++++-- .../hashicorp/hcl/json/parser/parser.go | 9 ++++ vendor/vendor.json | 52 +++++++++---------- 5 files changed, 90 insertions(+), 32 deletions(-) diff --git a/vendor/github.com/hashicorp/hcl/hcl/ast/ast.go b/vendor/github.com/hashicorp/hcl/hcl/ast/ast.go index ea3734f09..6e5ef654b 100644 --- a/vendor/github.com/hashicorp/hcl/hcl/ast/ast.go +++ b/vendor/github.com/hashicorp/hcl/hcl/ast/ast.go @@ -156,7 +156,8 @@ func (o *ObjectKey) Pos() token.Pos { type LiteralType struct { Token token.Token - // associated line comment, only when used in a list + // comment types, only used when in a list + LeadComment *CommentGroup LineComment *CommentGroup } diff --git a/vendor/github.com/hashicorp/hcl/hcl/parser/parser.go b/vendor/github.com/hashicorp/hcl/hcl/parser/parser.go index f46ed4cc0..0aa080f92 100644 --- a/vendor/github.com/hashicorp/hcl/hcl/parser/parser.go +++ b/vendor/github.com/hashicorp/hcl/hcl/parser/parser.go @@ -337,6 +337,12 @@ func (p *Parser) listType() (*ast.ListType, error) { return nil, err } + // If there is a lead comment, apply it + if p.leadComment != nil { + node.LeadComment = p.leadComment + p.leadComment = nil + } + l.Add(node) needComma = true case token.COMMA: diff --git a/vendor/github.com/hashicorp/hcl/hcl/printer/nodes.go b/vendor/github.com/hashicorp/hcl/hcl/printer/nodes.go index 218b56a81..83f6a4f0c 100644 --- a/vendor/github.com/hashicorp/hcl/hcl/printer/nodes.go +++ b/vendor/github.com/hashicorp/hcl/hcl/printer/nodes.go @@ -62,6 +62,14 @@ func (p *printer) collectComments(node ast.Node) { ast.Walk(node, func(nn ast.Node) (ast.Node, bool) { switch t := nn.(type) { case *ast.LiteralType: + if t.LeadComment != nil { + for _, comment := range t.LeadComment.List { + if _, ok := standaloneComments[comment.Pos()]; ok { + delete(standaloneComments, comment.Pos()) + } + } + } + if t.LineComment != nil { for _, comment := range t.LineComment.List { if _, ok := standaloneComments[comment.Pos()]; ok { @@ -95,7 +103,6 @@ func (p *printer) collectComments(node ast.Node) { } sort.Sort(ByPosition(p.standaloneComments)) - } // output prints creates b printable HCL output and returns it. @@ -104,11 +111,14 @@ func (p *printer) output(n interface{}) []byte { switch t := n.(type) { case *ast.File: + // File doesn't trace so we add the tracing here + defer un(trace(p, "File")) return p.output(t.Node) case *ast.ObjectList: + defer un(trace(p, "ObjectList")) + var index int var nextItem token.Pos - var commented bool for { // TODO(arslan): refactor below comment printing, we have the same in objectType for _, c := range p.standaloneComments { @@ -121,7 +131,10 @@ func (p *printer) output(n interface{}) []byte { if comment.Pos().After(p.prev) && comment.Pos().Before(nextItem) { // if we hit the end add newlines so we can print the comment - if index == len(t.Items) { + // we don't do this if prev is invalid which means the + // beginning of the file since the first comment should + // be at the first line. + if p.prev.IsValid() && index == len(t.Items) { buf.Write([]byte{newline, newline}) } @@ -140,7 +153,7 @@ func (p *printer) output(n interface{}) []byte { } buf.Write(p.output(t.Items[index])) - if !commented && index != len(t.Items)-1 { + if index != len(t.Items)-1 { buf.Write([]byte{newline, newline}) } index++ @@ -435,11 +448,33 @@ func (p *printer) list(l *ast.ListType) []byte { } insertSpaceBeforeItem := false + lastHadLeadComment := false for i, item := range l.List { if item.Pos().Line != l.Lbrack.Line { // multiline list, add newline before we add each item buf.WriteByte(newline) insertSpaceBeforeItem = false + + // If we have a lead comment, then we want to write that first + leadComment := false + if lit, ok := item.(*ast.LiteralType); ok && lit.LeadComment != nil { + leadComment = true + + // If this isn't the first item and the previous element + // didn't have a lead comment, then we need to add an extra + // newline to properly space things out. If it did have a + // lead comment previously then this would be done + // automatically. + if i > 0 && !lastHadLeadComment { + buf.WriteByte(newline) + } + + for _, comment := range lit.LeadComment.List { + buf.Write(p.indent([]byte(comment.Text))) + buf.WriteByte(newline) + } + } + // also indent each line val := p.output(item) curLen := len(val) @@ -458,9 +493,16 @@ func (p *printer) list(l *ast.ListType) []byte { } } - if i == len(l.List)-1 { + lastItem := i == len(l.List)-1 + if lastItem { buf.WriteByte(newline) } + + if leadComment && !lastItem { + buf.WriteByte(newline) + } + + lastHadLeadComment = leadComment } else { if insertSpaceBeforeItem { buf.WriteByte(blank) diff --git a/vendor/github.com/hashicorp/hcl/json/parser/parser.go b/vendor/github.com/hashicorp/hcl/json/parser/parser.go index 3a62ec3f6..acf95941f 100644 --- a/vendor/github.com/hashicorp/hcl/json/parser/parser.go +++ b/vendor/github.com/hashicorp/hcl/json/parser/parser.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/hashicorp/hcl/hcl/ast" + hcltoken "github.com/hashicorp/hcl/hcl/token" "github.com/hashicorp/hcl/json/scanner" "github.com/hashicorp/hcl/json/token" ) @@ -103,6 +104,14 @@ func (p *Parser) objectItem() (*ast.ObjectItem, error) { switch p.tok.Type { case token.COLON: + pos := p.tok.Pos + o.Assign = hcltoken.Pos{ + Filename: pos.Filename, + Offset: pos.Offset, + Line: pos.Line, + Column: pos.Column, + } + o.Val, err = p.objectValue() if err != nil { return nil, err diff --git a/vendor/vendor.json b/vendor/vendor.json index 26c3b34db..73008374e 100644 --- a/vendor/vendor.json +++ b/vendor/vendor.json @@ -1168,68 +1168,68 @@ { "checksumSHA1": "8OPDk+bKyRGJoKcS4QNw9F7dpE8=", "path": "github.com/hashicorp/hcl", - "revision": "ef8133da8cda503718a74741312bf50821e6de79", - "revisionTime": "2016-09-16T13:01:00Z" + "revision": "6f5bfed9a0a22222fbe4e731ae3481730ba41e93", + "revisionTime": "2016-10-08T07:35:57Z" }, { - "checksumSHA1": "67DfevLBglV52Y2eAuhFc/xQni0=", + "checksumSHA1": "XQmjDva9JCGGkIecOgwtBEMCJhU=", "path": "github.com/hashicorp/hcl/hcl/ast", - "revision": "99df0eb941dd8ddbc83d3f3605a34f6a686ac85e", - "revisionTime": "2016-09-02T16:52:19Z" + "revision": "6f5bfed9a0a22222fbe4e731ae3481730ba41e93", + "revisionTime": "2016-10-08T07:35:57Z" }, { "checksumSHA1": "5HVecyfmcTm6OTffEi6LGayQf5M=", "path": "github.com/hashicorp/hcl/hcl/fmtcmd", - "revision": "99df0eb941dd8ddbc83d3f3605a34f6a686ac85e", - "revisionTime": "2016-09-02T16:52:19Z" + "revision": "6f5bfed9a0a22222fbe4e731ae3481730ba41e93", + "revisionTime": "2016-10-08T07:35:57Z" }, { - "checksumSHA1": "l2oQxBsZRwn6eZjf+whXr8c9+8c=", + "checksumSHA1": "un4pN4yL5bl6LL3CgWacFbIeHVg=", "path": "github.com/hashicorp/hcl/hcl/parser", - "revision": "99df0eb941dd8ddbc83d3f3605a34f6a686ac85e", - "revisionTime": "2016-09-02T16:52:19Z" + "revision": "6f5bfed9a0a22222fbe4e731ae3481730ba41e93", + "revisionTime": "2016-10-08T07:35:57Z" }, { - "checksumSHA1": "CSmwxPOTz7GSpnWPF9aGkbVeR64=", + "checksumSHA1": "QjoxNbg+jBmtewexLaBZ8EJEl24=", "path": "github.com/hashicorp/hcl/hcl/printer", - "revision": "99df0eb941dd8ddbc83d3f3605a34f6a686ac85e", - "revisionTime": "2016-09-02T16:52:19Z" + "revision": "6f5bfed9a0a22222fbe4e731ae3481730ba41e93", + "revisionTime": "2016-10-08T07:35:57Z" }, { "checksumSHA1": "lgR7PSAZ0RtvAc9OCtCnNsF/x8g=", "path": "github.com/hashicorp/hcl/hcl/scanner", - "revision": "99df0eb941dd8ddbc83d3f3605a34f6a686ac85e", - "revisionTime": "2016-09-02T16:52:19Z" + "revision": "6f5bfed9a0a22222fbe4e731ae3481730ba41e93", + "revisionTime": "2016-10-08T07:35:57Z" }, { "checksumSHA1": "JlZmnzqdmFFyb1+2afLyR3BOE/8=", "path": "github.com/hashicorp/hcl/hcl/strconv", - "revision": "99df0eb941dd8ddbc83d3f3605a34f6a686ac85e", - "revisionTime": "2016-09-02T16:52:19Z" + "revision": "6f5bfed9a0a22222fbe4e731ae3481730ba41e93", + "revisionTime": "2016-10-08T07:35:57Z" }, { "checksumSHA1": "c6yprzj06ASwCo18TtbbNNBHljA=", "path": "github.com/hashicorp/hcl/hcl/token", - "revision": "99df0eb941dd8ddbc83d3f3605a34f6a686ac85e", - "revisionTime": "2016-09-02T16:52:19Z" + "revision": "6f5bfed9a0a22222fbe4e731ae3481730ba41e93", + "revisionTime": "2016-10-08T07:35:57Z" }, { - "checksumSHA1": "jQ45CCc1ed/nlV7bbSnx6z72q1M=", + "checksumSHA1": "fpQQdjFUZOoslYuFNKZMSO0N0ik=", "path": "github.com/hashicorp/hcl/json/parser", - "revision": "99df0eb941dd8ddbc83d3f3605a34f6a686ac85e", - "revisionTime": "2016-09-02T16:52:19Z" + "revision": "6f5bfed9a0a22222fbe4e731ae3481730ba41e93", + "revisionTime": "2016-10-08T07:35:57Z" }, { "checksumSHA1": "YdvFsNOMSWMLnY6fcliWQa0O5Fw=", "path": "github.com/hashicorp/hcl/json/scanner", - "revision": "99df0eb941dd8ddbc83d3f3605a34f6a686ac85e", - "revisionTime": "2016-09-02T16:52:19Z" + "revision": "6f5bfed9a0a22222fbe4e731ae3481730ba41e93", + "revisionTime": "2016-10-08T07:35:57Z" }, { "checksumSHA1": "fNlXQCQEnb+B3k5UDL/r15xtSJY=", "path": "github.com/hashicorp/hcl/json/token", - "revision": "99df0eb941dd8ddbc83d3f3605a34f6a686ac85e", - "revisionTime": "2016-09-02T16:52:19Z" + "revision": "6f5bfed9a0a22222fbe4e731ae3481730ba41e93", + "revisionTime": "2016-10-08T07:35:57Z" }, { "checksumSHA1": "OrnLOmhc0FcHYs02wtbu1siIsnM=",