From 035b403ad5170c2fc1620dcd07a9b3daa86e385f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Leal?= Date: Wed, 8 Aug 2012 15:38:15 +0100 Subject: [PATCH 1/2] Grouping similar var declarations --- splint.go | 40 +++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/splint.go b/splint.go index 79e4fb5..a15495e 100644 --- a/splint.go +++ b/splint.go @@ -1,12 +1,12 @@ // 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 ( @@ -20,11 +20,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") + outputJSON = flag.Bool("j", false, "output results as json") + ignoreTestFiles = flag.Bool("i", true, "ignore test files") +) type Parser struct { filename string @@ -158,19 +160,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() } From 10a6d103acc18ea1b66bb9f68478d5f52100e92c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Leal?= Date: Wed, 8 Aug 2012 15:49:25 +0100 Subject: [PATCH 2/2] Adding new output formats Changed flag outputJSON (j) to outputType (o). This enables the application to output the result in new formats (ex. xml) --- splint.go | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/splint.go b/splint.go index a15495e..0673b5c 100644 --- a/splint.go +++ b/splint.go @@ -11,6 +11,7 @@ package main import ( "encoding/json" + "encoding/xml" "flag" "fmt" "go/ast" @@ -24,7 +25,7 @@ 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") - outputJSON = flag.Bool("j", false, "output results as json") + outputType = flag.String("o", "text", "output results as text, json or xml") ignoreTestFiles = flag.Bool("i", true, "ignore test files") ) @@ -84,7 +85,7 @@ func statementCount(n ast.Node) int { } func (p *Parser) outputFilename() { - if *outputJSON { + if *outputType != "text" { return } if p.first { @@ -101,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) } @@ -114,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) } @@ -127,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) } @@ -192,7 +193,7 @@ func main() { parseFile(v, summary) } - if *outputJSON { + if *outputType == "json" { /* buf := new(bytes.Buffer) encoder := json.NewEncoder(buf) @@ -207,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)