go-sourcemap / sourcemap

Source maps consumer for Golang
http://godoc.org/github.com/go-sourcemap/sourcemap
BSD 2-Clause "Simplified" License
71 stars 20 forks source link

Request: Reconstruct original file #15

Open cameronbrill opened 3 years ago

cameronbrill commented 3 years ago

be able to reconstruct the original file from sourcemap. something like the following:

smap, err := sourcemap.Parse(mapURL, b)
if err != nil {
    panic(err)
}

originalFile, err := smap.Reconstruct()

or, for the purpose of not loading a whole un-minified file into memory, we can pass in an int denoting number of lines you want reconstructed when you call Source(). Then, return a slice containing n strings which each represent a like. maybe something like this:

smap, err := sourcemap.Parse(mapURL, b)
if err != nil {
    panic(err)
}
line, column, peekCount := 5, 6789, 10
var originalLines []string
file, fn, line, col, originalLines, ok := smap.Source(line, column, peekCount)

originalLines would then have 5 lines above the mapped line, the mapped line, and 4 lines below, like so:

{
    "package main",
    "",
    "import \"fmt\"",
    "",
    "func main() {",
    "\tfor i := 0; i < 5; i++ {", // Mapped line
    "\t\tfmt.Println(\"howdy\")",
    "\t}",
    "}",
    "",
}
cameronbrill commented 3 years ago

I might try to implement this