From 9596f74b166597a817b41a6ded54f3c132fbd847 Mon Sep 17 00:00:00 2001 From: Roman Bataev Date: Wed, 30 Oct 2019 20:53:58 -0700 Subject: [PATCH] Add linting directories. --- core/parse.go | 1 + core/procs.go | 32 ++++++++++++++++++++++----- main.go | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 88 insertions(+), 6 deletions(-) diff --git a/core/parse.go b/core/parse.go index 4feed6d49..227ed33f3 100644 --- a/core/parse.go +++ b/core/parse.go @@ -156,6 +156,7 @@ type ( unusedFnParameters bool fnWithEmptyBody bool ignoredUnusedNamespaces Set + IgnoredFileRegexes []*regexp.Regexp } Keywords struct { tag Keyword diff --git a/core/procs.go b/core/procs.go index 48738e4d0..2f91f5977 100644 --- a/core/procs.go +++ b/core/procs.go @@ -1697,18 +1697,21 @@ func ProcessReplData() { } func findConfigFile(filename string, workingDir string, findDir bool) string { + var err error configName := ".joker" if findDir { configName = ".jokerd" } - filename, err := filepath.Abs(filename) - if err != nil { - fmt.Fprintln(Stderr, "Error reading config file "+filename+": ", err) - return "" + if filename != "" { + filename, err = filepath.Abs(filename) + if err != nil { + fmt.Fprintln(Stderr, "Error reading config file "+filename+": ", err) + return "" + } } if workingDir != "" { - workingDir, err = filepath.Abs(workingDir) + workingDir, err := filepath.Abs(workingDir) if err != nil { fmt.Fprintln(Stderr, "Error resolving working directory"+workingDir+": ", err) return "" @@ -1801,6 +1804,25 @@ func ReadConfig(filename string, workingDir string) { return } } + ok, ignoredFileRegexes := configMap.Get(MakeKeyword("ignored-file-regexes")) + if ok { + seq, ok1 := ignoredFileRegexes.(Seqable) + if ok1 { + s := seq.Seq() + for !s.IsEmpty() { + regex, ok2 := s.First().(Regex) + if !ok2 { + printConfigError(configFileName, ":ignored-file-regexes elements must be regexes, got "+s.First().GetType().ToString(false)) + return + } + WARNINGS.IgnoredFileRegexes = append(WARNINGS.IgnoredFileRegexes, regex.R) + s = s.Rest() + } + } else { + printConfigError(configFileName, ":ignored-file-regexes value must be a vector, got "+ignoredFileRegexes.GetType().ToString(false)) + return + } + } ok, knownNamespaces := configMap.Get(MakeKeyword("known-namespaces")) if ok { if _, ok1 := knownNamespaces.(Seqable); !ok1 { diff --git a/main.go b/main.go index d4db12ffa..1e70e0ff3 100644 --- a/main.go +++ b/main.go @@ -315,6 +315,58 @@ func lintFile(filename string, dialect Dialect, workingDir string) { } } +func matchesDialect(path string, dialect Dialect) bool { + ext := ".clj" + switch dialect { + case CLJS: + ext = ".cljs" + case JOKER: + ext = ".joke" + case EDN: + ext = ".edn" + } + return strings.HasSuffix(path, ext) +} + +func isIgnored(path string) bool { + for _, r := range WARNINGS.IgnoredFileRegexes { + m := r.FindStringSubmatchIndex(path) + if len(m) > 0 { + if m[1]-m[0] == len(path) { + return true + } + } + } + return false +} + +func lintDir(dirname string, dialect Dialect) { + var err error + phase := PARSE + if dialect == EDN { + phase = READ + } + ns := GLOBAL_ENV.CurrentNamespace() + ReadConfig("", dirname) + configureLinterMode(dialect, "", dirname) + filepath.Walk(dirname, func(path string, info os.FileInfo, err error) error { + if err != nil { + fmt.Fprintln(Stderr, "Error: ", err) + return nil + } + if !info.IsDir() && matchesDialect(path, dialect) && !isIgnored(path) { + GLOBAL_ENV.CoreNamespace.Resolve("*loaded-libs*").Value = EmptySet() + err = processFile(path, phase) + GLOBAL_ENV.SetCurrentNamespace(ns) + } + return nil + }) + if err == nil { + WarnOnUnusedNamespaces() + WarnOnUnusedVars() + } +} + func dialectFromArg(arg string) Dialect { switch strings.ToLower(arg) { case "clj": @@ -774,7 +826,14 @@ func main() { if dialect == UNKNOWN { dialect = detectDialect(filename) } - lintFile(filename, dialect, workingDir) + if filename != "" { + lintFile(filename, dialect, workingDir) + } else if workingDir != "" { + lintDir(workingDir, dialect) + } else { + fmt.Fprintf(Stderr, "Error: Missing --file or --working-dir argument.\n") + ExitJoker(16) + } if PROBLEM_COUNT > 0 { ExitJoker(1) }