Skip to content

Commit

Permalink
refactor: more package clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
jdkato committed Nov 14, 2020
1 parent 2936d7a commit 1134bb4
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 33 deletions.
2 changes: 1 addition & 1 deletion core/core.go
Expand Up @@ -296,7 +296,7 @@ func FormatAlert(a *Alert, limit int, level, name string) {
a.Check = name
}
a.Limit = limit
a.Message = fixOutputSpacing(a.Message)
a.Message = WhitespaceToSpace(a.Message)
}

// AddAlert calculates the in-text location of an Alert and adds it to a File.
Expand Down
4 changes: 4 additions & 0 deletions core/location.go
Expand Up @@ -2,11 +2,15 @@ package core

import (
"strings"
"sync"
"unicode/utf8"

"github.com/jdkato/regexp"
)

// This is used to store patterns as we compute them in `initialPosition`.
var cache = sync.Map{}

// initialPosition calculates the position of a match (given by the location in
// the reference document, `loc`) in the source document (`ctx`).
func initialPosition(ctx, txt string, a Alert) (int, string) {
Expand Down
27 changes: 11 additions & 16 deletions core/util.go
Expand Up @@ -7,26 +7,17 @@ import (
"os"
"os/exec"
"strings"
"sync"
"unicode"

"github.com/jdkato/prose/tag"
"github.com/jdkato/prose/tokenize"
"github.com/jdkato/regexp"
)

const ansi = "[\u001B\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[a-zA-Z\\d]*)*)?\u0007)|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PRZcf-ntqry=><~]))"

var reANSI = regexp.MustCompile(ansi)

// ExeDir is our starting location.
var ExeDir string

// This is used to store patterns as we compute them in `initialPosition`.
var cache = sync.Map{}

// DefaultIgnoreDirectories is list of directories which will be ignored by vale, out of the box
var DefaultIgnoreDirectories = []string{
var defaultIgnoreDirectories = []string{
"node_modules", ".git",
}

Expand All @@ -37,20 +28,24 @@ var sanitizer = strings.NewReplacer(

var spaces = regexp.MustCompile(" +")

func fixOutputSpacing(msg string) string {
msg = strings.Replace(msg, "\n", " ", -1)
msg = spaces.ReplaceAllString(msg, " ")
return msg
}
var reANSI = regexp.MustCompile("[\u001B\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[a-zA-Z\\d]*)*)?\u0007)|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PRZcf-ntqry=><~]))")

// StripANSI removes all ANSI characters from the given string.
func StripANSI(s string) string {
return reANSI.ReplaceAllString(s, "")
}

// WhitespaceToSpace converts newlines and multiple spaces (e.g., " ") into a
// single space.
func WhitespaceToSpace(msg string) string {
msg = strings.Replace(msg, "\n", " ", -1)
msg = spaces.ReplaceAllString(msg, " ")
return msg
}

// ShouldIgnoreDirectory will check if directory should be ignored
func ShouldIgnoreDirectory(directoryName string) bool {
for _, directory := range DefaultIgnoreDirectories {
for _, directory := range defaultIgnoreDirectories {
if directory == directoryName {
return true
}
Expand Down
16 changes: 0 additions & 16 deletions ui/common.go
@@ -1,28 +1,12 @@
package ui

import (
"encoding/json"
"sort"

"github.com/errata-ai/vale/v2/config"
"github.com/errata-ai/vale/v2/core"
)

func pluralize(s string, n int) string {
if n != 1 {
return s + "s"
}
return s
}

func getJSON(data interface{}) string {
b, err := json.MarshalIndent(data, "", " ")
if err != nil {
return err.Error()
}
return string(b)
}

// PrintAlerts prints the given alerts in the user-specified format.
func PrintAlerts(linted []*core.File, config *config.Config) (bool, error) {
if config.Sorted {
Expand Down
18 changes: 18 additions & 0 deletions ui/util.go
@@ -0,0 +1,18 @@
package ui

import "encoding/json"

func pluralize(s string, n int) string {
if n != 1 {
return s + "s"
}
return s
}

func getJSON(data interface{}) string {
b, err := json.MarshalIndent(data, "", " ")
if err != nil {
return err.Error()
}
return string(b)
}

0 comments on commit 1134bb4

Please sign in to comment.