From 583c4bd49be233710894cd97d0f18e8c00195d6e Mon Sep 17 00:00:00 2001 From: Amy Boyd Date: Sat, 25 Jun 2016 12:37:56 +0100 Subject: [PATCH] Run full-file checkers in "check" command. --- editorconfig/check_command.go | 57 +++++++++++++++++++++-------------- 1 file changed, 35 insertions(+), 22 deletions(-) diff --git a/editorconfig/check_command.go b/editorconfig/check_command.go index 77e3222..dd03848 100644 --- a/editorconfig/check_command.go +++ b/editorconfig/check_command.go @@ -1,11 +1,11 @@ package editorconfig import ( - "bufio" "fmt" "github.com/codegangsta/cli" - "os" + "io/ioutil" "strconv" + "strings" ) func CheckCommand(c *cli.Context) error { @@ -14,37 +14,50 @@ func CheckCommand(c *cli.Context) error { return err } + if len(files) == 0 { + ExitBecauseOfInternalError("No files to check in " + strings.Join(c.Args(), ", ")) + } + configs := FindConfigFiles(files) for _, f := range files { - fh, err := os.Open(f) + rules := GetRulesToApplyToSourcePath(f, configs) + if len(rules) == 0 { + continue + } + + fileContentInBytes, err := ioutil.ReadFile(f) if err != nil { ExitBecauseOfInternalError("Could not read file: " + f) } + fileContent := string(fileContentInBytes) - // @todo - add "full file checkers" for end_of_line, insert_final_newline and charset. - - scanner := bufio.NewScanner(fh) - rules := GetRulesToApplyToSourcePath(f, configs) - - lineNo := 0 - for scanner.Scan() { - line := scanner.Text() - lineNo++ - - for ruleName, ruleValue := range rules { - if lineCheckers[ruleName] == nil { - continue - } - - result := lineCheckers[ruleName](ruleValue, line) + // Run full-file checkers. + for ruleName, ruleValue := range rules { + if fullFileChecker, ok := fullFileCheckers[ruleName]; ok { + result := fullFileChecker(ruleValue, fileContent) if !result.isOk { - fmt.Println(f + ": line " + strconv.Itoa(lineNo) + ": " + ruleName + ": " + result.messageIfNotOk) - // Don't show more than 1 error per line. - break + fmt.Println(f + ": " + ruleName + ": " + result.messageIfNotOk) } } } + + // Run line checkers. + lines := SplitIntoLines(fileContent) + lineNo := 1 + for _, line := range lines { + for ruleName, ruleValue := range rules { + if lineChecker, ok := lineCheckers[ruleName]; ok { + result := lineChecker(ruleValue, line) + if !result.isOk { + fmt.Println(f + ": line " + strconv.Itoa(lineNo) + ": " + ruleName + ": " + result.messageIfNotOk) + // Don't show more than 1 error per line. + break + } + } + } + lineNo++ + } } return nil