func (v *fileVisitor) method(n *ast.FuncDecl) *Method {
	method := &Method{Name: n.Name.Name}
	method.Lines = []*Line{}

	start := v.fset.Position(n.Pos())
	end := v.fset.Position(n.End())
	startLine := start.Line
	startCol := start.Column
	endLine := end.Line
	endCol := end.Column
	// The blocks are sorted, so we can stop counting as soon as we reach the end of the relevant block.
	for _, b := range v.profile.Blocks {
		if b.StartLine > endLine || (b.StartLine == endLine && b.StartCol >= endCol) {
			// Past the end of the function.
			break
		}
		if b.EndLine < startLine || (b.EndLine == startLine && b.EndCol <= startCol) {
			// Before the beginning of the function
			continue
		}
		for i := b.StartLine; i <= b.EndLine; i++ {
			method.Lines = append(method.Lines, &Line{Number: i, Hits: int64(b.Count)})
		}
	}
	return method
}
Example #2
0
func processFunction(funcDecl *ast.FuncDecl) {
	m := function{}

	m.bodyStart = fset.Position(funcDecl.Pos()).Line
	m.bodyEnd = fset.Position(funcDecl.End()).Line
	m.variables = getFunctionVariables(funcDecl)

	addFoundFunctions(m)
}
Example #3
0
func (f *file) checkFunctionLine(funcDecl *ast.FuncDecl) {
	lineLimit := f.config.FunctionLine
	if lineLimit <= 0 {
		return
	}
	start := f.fset.Position(funcDecl.Pos())

	startLine := start.Line
	endLine := f.fset.Position(funcDecl.End()).Line
	lineCount := endLine - startLine
	if lineCount > lineLimit {
		problem := genFuncLineProblem(funcDecl.Name.Name, lineCount, lineLimit, start)
		f.problems = append(f.problems, problem)
	}
}
func exampleOutput(fun *ast.FuncDecl, comments []*ast.CommentGroup) string {
	// find the last comment in the function
	var last *ast.CommentGroup
	for _, cg := range comments {
		if cg.Pos() < fun.Pos() {
			continue
		}
		if cg.End() > fun.End() {
			break
		}
		last = cg
	}
	if last != nil {
		// test that it begins with the correct prefix
		text := last.Text()
		if loc := outputPrefix.FindStringIndex(text); loc != nil {
			return strings.TrimSpace(text[loc[1]:])
		}
	}
	return "" // no suitable comment found
}