Run full-file checkers in "check" command.
This commit is contained in:
@@ -1,11 +1,11 @@
|
|||||||
package editorconfig
|
package editorconfig
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/codegangsta/cli"
|
"github.com/codegangsta/cli"
|
||||||
"os"
|
"io/ioutil"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func CheckCommand(c *cli.Context) error {
|
func CheckCommand(c *cli.Context) error {
|
||||||
@@ -14,37 +14,50 @@ func CheckCommand(c *cli.Context) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if len(files) == 0 {
|
||||||
|
ExitBecauseOfInternalError("No files to check in " + strings.Join(c.Args(), ", "))
|
||||||
|
}
|
||||||
|
|
||||||
configs := FindConfigFiles(files)
|
configs := FindConfigFiles(files)
|
||||||
|
|
||||||
for _, f := range 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 {
|
if err != nil {
|
||||||
ExitBecauseOfInternalError("Could not read file: " + f)
|
ExitBecauseOfInternalError("Could not read file: " + f)
|
||||||
}
|
}
|
||||||
|
fileContent := string(fileContentInBytes)
|
||||||
|
|
||||||
// @todo - add "full file checkers" for end_of_line, insert_final_newline and charset.
|
// Run full-file checkers.
|
||||||
|
for ruleName, ruleValue := range rules {
|
||||||
scanner := bufio.NewScanner(fh)
|
if fullFileChecker, ok := fullFileCheckers[ruleName]; ok {
|
||||||
rules := GetRulesToApplyToSourcePath(f, configs)
|
result := fullFileChecker(ruleValue, fileContent)
|
||||||
|
|
||||||
lineNo := 0
|
|
||||||
for scanner.Scan() {
|
|
||||||
line := scanner.Text()
|
|
||||||
lineNo++
|
|
||||||
|
|
||||||
for ruleName, ruleValue := range rules {
|
|
||||||
if lineCheckers[ruleName] == nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
result := lineCheckers[ruleName](ruleValue, line)
|
|
||||||
if !result.isOk {
|
if !result.isOk {
|
||||||
fmt.Println(f + ": line " + strconv.Itoa(lineNo) + ": " + ruleName + ": " + result.messageIfNotOk)
|
fmt.Println(f + ": " + ruleName + ": " + result.messageIfNotOk)
|
||||||
// Don't show more than 1 error per line.
|
|
||||||
break
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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
|
return nil
|
||||||
|
|||||||
Reference in New Issue
Block a user