dst icon indicating copy to clipboard operation
dst copied to clipboard

Empty lines inside structs/functions are sometimes removed

Open horenmar opened this issue 3 years ago • 2 comments

I have a source file that uses cgo a lot, and running it through dst causes (some) empty lines in structs/functions to be removed.

I tried some testing, and I am not entirely sure what causes the empty lines to be removed, because e.g. this struct is left alone

type StructTest struct {
	aaa int32

	b int32
}

but a similar struct

type AType struct {
	c      *C.StructNameCensored
	f SomeComplexGoType

	using int32
}

has the empty line removed, so it looks like this:

type AType struct {
	c     *C.StructNameCensored
	f     SomeComplexGoType
	using int32
}

I also have a similar issue inside (some) functions -- I guess this is related to the C APIs? However, the newline between the package line and the copyright notice is also removed:

// Copyright 2020 Pexeso Inc. All Rights reserved.

package matcher

horenmar avatar Apr 24 '22 19:04 horenmar

Thanks for this. It unlikely that I'll have any time to fix bugs like these but it's good to have them documented here.

dave avatar Apr 24 '22 20:04 dave

Hery @horenmar I'm unable to reproduce this bug... Here's the code I'm using... perhaps you can tweak it to show when this bug appears?

package main

import (
	"go/format"
	"go/token"
	"log"
	"os"

	"github.com/dave/dst/decorator"
)

func main() {

	code := `package main
        
type AType struct {
	c *C.StructNameCensored
	f SomeComplexGoType

	using int32
}`

	file, err := decorator.NewDecorator(token.NewFileSet()).Parse(code)
	if err != nil {
		log.Fatal(err)
	}

	restoredFset, restoredFile, err := decorator.RestoreFile(file)
	if err != nil {
		log.Fatal(err)
	}

	if err := format.Node(os.Stdout, restoredFset, restoredFile); err != nil {
		log.Fatal(err)
	}
}

Output:

package main

type AType struct {
        c *C.StructNameCensored
        f SomeComplexGoType

        using int32
}

dave avatar May 31 '22 13:05 dave