-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathminformat.go
39 lines (36 loc) · 1.09 KB
/
minformat.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Package minformat implements Go source code minification routines.
package minformat
import (
"bytes"
"go/parser"
"go/token"
"io"
)
// Node formats node by removing as much whitespace as possible and writes the result to w.
//
// Result contains no comments.
//
// The node type is defined as interface{} for compatibility with go/format.Node function.
// Only ast.Node types are supported right now.
//
// The function may return early (before the entire result is written) and return a formatting error,
// for instance due to an incorrect AST.
func Node(w io.Writer, fset *token.FileSet, node interface{}) error {
var m minifier
m.Fprint(w, fset, node)
return nil
}
// Source formats src by removing as mych whitespace as possible and returns the result.
//
// src is expected to be a syntactically correct Go source file.
func Source(src []byte) ([]byte, error) {
const parserMode = 0
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, "source-input", src, parserMode)
if err != nil {
return nil, err
}
var buf bytes.Buffer
err = Node(&buf, fset, f)
return buf.Bytes(), err
}