dave / dst

Decorated Syntax Tree - manipulate Go source with perfect fidelity.
Other
1.27k stars 56 forks source link

Empty lines inside structs/functions are sometimes removed #65

Open horenmar opened 2 years ago

horenmar commented 2 years ago

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
dave commented 2 years ago

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 commented 2 years ago

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
}