Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding other output formats #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 33 additions & 25 deletions splint.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
// Copyright 2011 Numrotron Inc.
// Use of this source code is governed by an MIT-style license
// that can be found in the LICENSE file.
// Use of this source code is governed by an MIT-style license that can be
// found in the LICENSE file.
//
// Developed at www.stathat.com by Patrick Crosby
// Contact us on twitter with any questions: twitter.com/stat_hat

// splint is a little Go application to analyze Go source files. It finds any functions that are
// too long or have too many parameters or results.
// splint is a little Go application to analyze Go source files. It finds any
// functions that are too long or have too many parameters or results.
package main

import (
"encoding/json"
"encoding/xml"
"flag"
"fmt"
"go/ast"
Expand All @@ -20,11 +21,13 @@ import (
"path"
)

var statementThreshold = flag.Int("s", 30, "function statement count threshold")
var paramThreshold = flag.Int("p", 5, "parameter list length threshold")
var resultThreshold = flag.Int("r", 5, "result list length threshold")
var outputJSON = flag.Bool("j", false, "output results as json")
var ignoreTestFiles = flag.Bool("i", true, "ignore test files")
var (
statementThreshold = flag.Int("s", 30, "function statement count threshold")
paramThreshold = flag.Int("p", 5, "parameter list length threshold")
resultThreshold = flag.Int("r", 5, "result list length threshold")
outputType = flag.String("o", "text", "output results as text, json or xml")
ignoreTestFiles = flag.Bool("i", true, "ignore test files")
)

type Parser struct {
filename string
Expand Down Expand Up @@ -82,7 +85,7 @@ func statementCount(n ast.Node) int {
}

func (p *Parser) outputFilename() {
if *outputJSON {
if *outputType != "text" {
return
}
if p.first {
Expand All @@ -99,7 +102,7 @@ func (p *Parser) checkFuncLength(x *ast.FuncDecl) {

p.summary.addStatement(p.filename, x.Name.String(), numStatements)

if *outputJSON == false {
if *outputType == "text" {
p.outputFilename()
fmt.Printf("function %s too long: %d\n", x.Name, numStatements)
}
Expand All @@ -112,7 +115,7 @@ func (p *Parser) checkParamCount(x *ast.FuncDecl) {
}

p.summary.addParam(p.filename, x.Name.String(), numFields)
if *outputJSON == false {
if *outputType == "text" {
p.outputFilename()
fmt.Printf("function %s has too many params: %d\n", x.Name, numFields)
}
Expand All @@ -125,7 +128,7 @@ func (p *Parser) checkResultCount(x *ast.FuncDecl) {
}

p.summary.addResult(p.filename, x.Name.String(), numResults)
if *outputJSON == false {
if *outputType == "text" {
p.outputFilename()
fmt.Printf("function %s has too many results: %d\n", x.Name, numResults)
}
Expand Down Expand Up @@ -158,19 +161,19 @@ func (p *Parser) Parse() {
}

func isTestFile(filename string) bool {
base := path.Base(filename)
match, err := path.Match("*_test.go", base)
if err != nil {
fmt.Println("match error:", err)
return false
}
return match
base := path.Base(filename)
match, err := path.Match("*_test.go", base)
if err != nil {
fmt.Println("match error:", err)
return false
}
return match
}

func parseFile(filename string, summary *Summary) {
if *ignoreTestFiles && isTestFile(filename) {
return
}
if *ignoreTestFiles && isTestFile(filename) {
return
}
parser := NewParser(filename, summary)
parser.Parse()
}
Expand All @@ -190,7 +193,7 @@ func main() {
parseFile(v, summary)
}

if *outputJSON {
if *outputType == "json" {
/*
buf := new(bytes.Buffer)
encoder := json.NewEncoder(buf)
Expand All @@ -205,7 +208,12 @@ func main() {
fmt.Println("json encode error:", err)
}
fmt.Println(string(data))

} else if *outputType == "xml" {
data, err := xml.MarshalIndent(summary, "", "\t")
if err != nil {
fmt.Println("xml encode error:", err)
}
fmt.Println(string(data))
} else {
fmt.Println()
fmt.Println("Number of functions above statement threshold:", summary.NumAboveStatementThreshold)
Expand Down