gofmt

package
v0.0.0-...-4cd6af7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 30, 2024 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package gofmt provides an analyzer which checks that files have been run through gofmt.

Index

Constants

This section is empty.

Variables

View Source
var Analyzer = &analysis.Analyzer{
	Name: "gofmt",
	Doc:  "checks if files have been run through `gofmt -s`",
	Run: func(pass *analysis.Pass) (any, error) {
		files := map[string]token.Pos{}
		for _, f := range pass.Files {
			fileName := pass.Fset.PositionFor(f.Pos(), true).Filename
			ext := filepath.Ext(fileName)
			if ext != "" && ext != ".go" {

				fileName = pass.Fset.PositionFor(f.Pos(), false).Filename
			}
			files[fileName] = f.Pos()
		}

		for f, pos := range files {
			diff, err := gofmt.RunRewrite(f, true, nil)
			if err != nil {
				return nil, errors.Wrap(err, "gofmt.RunRewrite")
			}
			if len(diff) > 0 {
				pass.Report(analysis.Diagnostic{
					Pos:     pos,
					Message: "not formatted with gofmt -s:\n" + string(diff),
				})
			}
		}
		return nil, nil
	},
}

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL