Skip to content

Commit

Permalink
refactor: don't load Vale if it's not needed
Browse files Browse the repository at this point in the history
  • Loading branch information
jdkato committed Jan 16, 2024
1 parent 6ae5d52 commit 08b2c42
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions internal/check/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"

"github.com/karrick/godirwalk"
"golang.org/x/exp/maps"

"github.com/errata-ai/vale/v2/internal/core"
"github.com/errata-ai/vale/v2/internal/nlp"
Expand Down Expand Up @@ -194,6 +195,10 @@ func (mgr *Manager) addCheck(file []byte, chkName, path string) error {
}

func (mgr *Manager) loadDefaultRules() error {
if !mgr.needsStyle("Vale") {
return nil
}

for _, style := range defaultStyles {
if core.StringInSlice(style, mgr.styles) {
return fmt.Errorf("'%v' collides with built-in style", style)
Expand Down Expand Up @@ -293,3 +298,33 @@ func (mgr *Manager) hasStyle(name string) bool {
styles := append(mgr.styles, defaultStyles...) //nolint:gocritic
return core.StringInSlice(name, styles)
}

func (mgr *Manager) needsStyle(name string) bool {
cfg := mgr.Config

if core.StringInSlice(name, cfg.GBaseStyles) {
return true
}

for _, s := range maps.Keys(cfg.GChecks) {
if strings.HasPrefix(s, name) {
return true
}
}

for _, s := range cfg.SBaseStyles {
if core.StringInSlice(name, s) {
return true
}
}

for _, s := range cfg.SChecks {
for _, chk := range maps.Keys(s) {
if strings.HasPrefix(chk, name) {
return true
}
}
}

return false
}

0 comments on commit 08b2c42

Please sign in to comment.