| 1 | // Copyright 2012 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | // Package format implements standard formatting of Go source. |
| 6 | // |
| 7 | // Note that formatting of Go source code changes over time, so tools relying on |
| 8 | // consistent formatting should execute a specific version of the gofmt binary |
| 9 | // instead of using this package. That way, the formatting will be stable, and |
| 10 | // the tools won't need to be recompiled each time gofmt changes. |
| 11 | // |
| 12 | // For example, pre-submit checks that use this package directly would behave |
| 13 | // differently depending on what Go version each developer uses, causing the |
| 14 | // check to be inherently fragile. |
| 15 | package format |
| 16 | |
| 17 | import ( |
| 18 | "bytes" |
| 19 | "fmt" |
| 20 | "go/ast" |
| 21 | "go/parser" |
| 22 | "go/token" |
| 23 | "io" |
| 24 | |
| 25 | "mvdan.cc/gofumpt/internal/govendor/go/printer" |
| 26 | ) |
| 27 | |
| 28 | // Keep these in sync with cmd/gofmt/gofmt.go. |
| 29 | const ( |
| 30 | tabWidth = 8 |
| 31 | printerMode = printer.UseSpaces | printer.TabIndent | printerNormalizeNumbers |
| 32 | |
| 33 | // printerNormalizeNumbers means to canonicalize number literal prefixes |
| 34 | // and exponents while printing. See https://golang.org/doc/go1.13#gofmt. |
| 35 | // |
| 36 | // This value is defined in mvdan.cc/gofumpt/internal/govendor/go/printer specifically for mvdan.cc/gofumpt/internal/govendor/go/format and cmd/gofmt. |
| 37 | printerNormalizeNumbers = 1 << 30 |
| 38 | ) |
| 39 | |
| 40 | var config = printer.Config{Mode: printerMode, Tabwidth: tabWidth} |
| 41 | |
| 42 | const parserMode = parser.ParseComments | parser.SkipObjectResolution |
| 43 | |
| 44 | // Node formats node in canonical gofmt style and writes the result to dst. |
| 45 | // |
| 46 | // The node type must be *ast.File, *printer.CommentedNode, []ast.Decl, |
| 47 | // []ast.Stmt, or assignment-compatible to ast.Expr, ast.Decl, ast.Spec, |
| 48 | // or ast.Stmt. Node does not modify node. Imports are not sorted for |
| 49 | // nodes representing partial source files (for instance, if the node is |
| 50 | // not an *ast.File or a *printer.CommentedNode not wrapping an *ast.File). |
| 51 | // |
| 52 | // The function may return early (before the entire result is written) |
| 53 | // and return a formatting error, for instance due to an incorrect AST. |
| 54 | func Node(dst io.Writer, fset *token.FileSet, node any) error { |
| 55 | // Determine if we have a complete source file (file != nil). |
| 56 | var file *ast.File |
| 57 | var cnode *printer.CommentedNode |
| 58 | switch n := node.(type) { |
| 59 | case *ast.File: |
| 60 | file = n |
| 61 | case *printer.CommentedNode: |
| 62 | if f, ok := n.Node.(*ast.File); ok { |
| 63 | file = f |
| 64 | cnode = n |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | // Sort imports if necessary. |
| 69 | if file != nil && hasUnsortedImports(file) { |
| 70 | // Make a copy of the AST because ast.SortImports is destructive. |
| 71 | // TODO(gri) Do this more efficiently. |
| 72 | var buf bytes.Buffer |
| 73 | err := config.Fprint(&buf, fset, file) |
| 74 | if err != nil { |
| 75 | return err |
| 76 | } |
| 77 | file, err = parser.ParseFile(fset, "", buf.Bytes(), parserMode) |
| 78 | if err != nil { |
| 79 | // We should never get here. If we do, provide good diagnostic. |
| 80 | return fmt.Errorf("format.Node internal error (%s)", err) |
| 81 | } |
| 82 | ast.SortImports(fset, file) |
| 83 | |
| 84 | // Use new file with sorted imports. |
| 85 | node = file |
| 86 | if cnode != nil { |
| 87 | node = &printer.CommentedNode{Node: file, Comments: cnode.Comments} |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | return config.Fprint(dst, fset, node) |
| 92 | } |
| 93 | |
| 94 | // Source formats src in canonical gofmt style and returns the result |
| 95 | // or an (I/O or syntax) error. src is expected to be a syntactically |
| 96 | // correct Go source file, or a list of Go declarations or statements. |
| 97 | // |
| 98 | // If src is a partial source file, the leading and trailing space of src |
| 99 | // is applied to the result (such that it has the same leading and trailing |
| 100 | // space as src), and the result is indented by the same amount as the first |
| 101 | // line of src containing code. Imports are not sorted for partial source files. |
| 102 | func Source(src []byte) ([]byte, error) { |
| 103 | fset := token.NewFileSet() |
| 104 | file, sourceAdj, indentAdj, err := parse(fset, "", src, true) |
| 105 | if err != nil { |
| 106 | return nil, err |
| 107 | } |
| 108 | |
| 109 | if sourceAdj == nil { |
| 110 | // Complete source file. |
| 111 | // TODO(gri) consider doing this always. |
| 112 | ast.SortImports(fset, file) |
| 113 | } |
| 114 | |
| 115 | return format(fset, file, sourceAdj, indentAdj, src, config) |
| 116 | } |
| 117 | |
| 118 | func hasUnsortedImports(file *ast.File) bool { |
| 119 | for _, d := range file.Decls { |
| 120 | d, ok := d.(*ast.GenDecl) |
| 121 | if !ok || d.Tok != token.IMPORT { |
| 122 | // Not an import declaration, so we're done. |
| 123 | // Imports are always first. |
| 124 | return false |
| 125 | } |
| 126 | if d.Lparen.IsValid() { |
| 127 | // For now assume all grouped imports are unsorted. |
| 128 | // TODO(gri) Should check if they are sorted already. |
| 129 | return true |
| 130 | } |
| 131 | // Ungrouped imports are sorted by default. |
| 132 | } |
| 133 | return false |
| 134 | } |