No longer preserve CRLF line endings

Matching the behavior of Go tooling, we are going to accept "\r\n" line
endings, but standaradize on "\n" being the official format.

Normalize line endings to "\n in the Parse function, so that we
never get stray "\r" characters in the source.
This commit is contained in:
James Bardin 2016-11-30 16:16:03 -05:00
parent 86950618a5
commit a97a4ff95f
7 changed files with 112 additions and 8 deletions

@ -3,6 +3,7 @@
package parser package parser
import ( import (
"bytes"
"errors" "errors"
"fmt" "fmt"
"strings" "strings"
@ -36,6 +37,11 @@ func newParser(src []byte) *Parser {
// Parse returns the fully parsed source and returns the abstract syntax tree. // Parse returns the fully parsed source and returns the abstract syntax tree.
func Parse(src []byte) (*ast.File, error) { func Parse(src []byte) (*ast.File, error) {
// normalize all line endings
// since the scanner and output only work with "\n" line endings, we may
// end up with dangling "\r" characters in the parsed data.
src = bytes.Replace(src, []byte("\r\n"), []byte("\n"), -1)
p := newParser(src) p := newParser(src)
return p.Parse() return p.Parse()
} }

@ -389,6 +389,7 @@ func TestCommentGroup(t *testing.T) {
groups int groups int
}{ }{
{"# Hello\n# World", 1}, {"# Hello\n# World", 1},
{"# Hello\r\n# Windows", 1},
} }
for _, tc := range cases { for _, tc := range cases {
@ -420,6 +421,10 @@ func TestParse(t *testing.T) {
"comment.hcl", "comment.hcl",
false, false,
}, },
{
"comment_crlf.hcl",
false,
},
{ {
"comment_lastline.hcl", "comment_lastline.hcl",
false, false,
@ -460,6 +465,10 @@ func TestParse(t *testing.T) {
"complex.hcl", "complex.hcl",
false, false,
}, },
{
"complex_crlf.hcl",
false,
},
{ {
"types.hcl", "types.hcl",
false, false,

@ -0,0 +1,15 @@
// Foo
/* Bar */
/*
/*
Baz
*/
# Another
# Multiple
# Lines
foo = "bar"

@ -0,0 +1,42 @@
variable "foo" {
default = "bar"
description = "bar"
}
variable "groups" { }
provider "aws" {
access_key = "foo"
secret_key = "bar"
}
provider "do" {
api_key = "${var.foo}"
}
resource "aws_security_group" "firewall" {
count = 5
}
resource aws_instance "web" {
ami = "${var.foo}"
security_groups = [
"foo",
"${aws_security_group.firewall.foo}",
"${element(split(\",\", var.groups)}",
]
network_interface = {
device_index = 0
description = "Main network interface"
}
}
resource "aws_instance" "db" {
security_groups = "${aws_security_group.firewall.*.id}"
VPC = "foo"
depends_on = ["aws_instance.web"]
}
output "web_ip" {
value = "${aws_instance.web.private_ip}"
}

@ -62,6 +62,5 @@ func Format(src []byte) ([]byte, error) {
// Add trailing newline to result // Add trailing newline to result
buf.WriteString("\n") buf.WriteString("\n")
return buf.Bytes(), nil return buf.Bytes(), nil
} }

@ -1,8 +1,3 @@
// +build !windows
// TODO(jen20): These need fixing on Windows but printer is not used right now
// and red CI is making it harder to process other bugs, so ignore until
// we get around to fixing them.package printer
package printer package printer
import ( import (
@ -32,6 +27,7 @@ var data = []entry{
{"complexhcl.input", "complexhcl.golden"}, {"complexhcl.input", "complexhcl.golden"},
{"list.input", "list.golden"}, {"list.input", "list.golden"},
{"comment.input", "comment.golden"}, {"comment.input", "comment.golden"},
{"comment_crlf.input", "comment.golden"},
{"comment_aligned.input", "comment_aligned.golden"}, {"comment_aligned.input", "comment_aligned.golden"},
{"comment_array.input", "comment_array.golden"}, {"comment_array.input", "comment_array.golden"},
{"comment_multiline_indent.input", "comment_multiline_indent.golden"}, {"comment_multiline_indent.input", "comment_multiline_indent.golden"},
@ -104,8 +100,8 @@ func diff(aname, bname string, a, b []byte) error {
for i := 0; i < len(a) && i < len(b); i++ { for i := 0; i < len(a) && i < len(b); i++ {
ch := a[i] ch := a[i]
if ch != b[i] { if ch != b[i] {
fmt.Fprintf(&buf, "\n%s:%d:%d: %s", aname, line, i-offs+1, lineAt(a, offs)) fmt.Fprintf(&buf, "\n%s:%d:%d: %q", aname, line, i-offs+1, lineAt(a, offs))
fmt.Fprintf(&buf, "\n%s:%d:%d: %s", bname, line, i-offs+1, lineAt(b, offs)) fmt.Fprintf(&buf, "\n%s:%d:%d: %q", bname, line, i-offs+1, lineAt(b, offs))
fmt.Fprintf(&buf, "\n\n") fmt.Fprintf(&buf, "\n\n")
break break
} }

37
hcl/printer/testdata/comment_crlf.input vendored Normal file

@ -0,0 +1,37 @@
// A standalone comment is a comment which is not attached to any kind of node
// This comes from Terraform, as a test
variable "foo" {
# Standalone comment should be still here
default = "bar"
description = "bar" # yooo
}
/* This is a multi line standalone
comment*/
// fatih arslan
/* This is a developer test
account and a multine comment */
developer = [ "fatih", "arslan"] // fatih arslan
# One line here
numbers = [1,2] // another line here
# Another comment
variable = {
description = "bar" # another yooo
foo {
# Nested standalone
bar = "fatih"
}
}
// lead comment
foo {
bar = "fatih" // line comment 2
} // line comment 3