2018-01-22 04:04:41 +02:00
|
|
|
package linter
|
2017-08-28 01:57:16 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"go/ast"
|
|
|
|
"go/parser"
|
|
|
|
"go/token"
|
|
|
|
)
|
|
|
|
|
|
|
|
// File abstraction used for representing files.
|
|
|
|
type File struct {
|
|
|
|
Name string
|
2018-01-22 04:04:41 +02:00
|
|
|
pkg *Package
|
2017-08-28 01:57:16 +02:00
|
|
|
Content []byte
|
|
|
|
ast *ast.File
|
|
|
|
}
|
|
|
|
|
2018-01-22 04:04:41 +02:00
|
|
|
// NewFile creates a new file
|
|
|
|
func NewFile(name string, content []byte, pkg *Package) (*File, error) {
|
|
|
|
f, err := parser.ParseFile(pkg.Fset, name, content, parser.ParseComments)
|
2017-08-28 01:57:16 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &File{
|
|
|
|
Name: name,
|
|
|
|
Content: content,
|
2018-01-22 04:04:41 +02:00
|
|
|
pkg: pkg,
|
2017-08-28 01:57:16 +02:00
|
|
|
ast: f,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToPosition returns line and column for given position.
|
|
|
|
func (f *File) ToPosition(pos token.Pos) token.Position {
|
2018-01-22 04:04:41 +02:00
|
|
|
return f.pkg.Fset.Position(pos)
|
2017-08-28 01:57:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetAST returns the AST of the file
|
|
|
|
func (f *File) GetAST() *ast.File {
|
|
|
|
return f.ast
|
|
|
|
}
|
2018-01-22 04:04:41 +02:00
|
|
|
|
|
|
|
func (f *File) isMain() bool {
|
|
|
|
if f.GetAST().Name.Name == "main" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|