core

package
v3.9.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 5, 2024 License: MIT Imports: 23 Imported by: 0

Documentation

Overview

Package core contains configuration and utility internals.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ConfigDir is the default location for Vale's configuration files.
	//
	// This was introduced in v3.0.0 as a means of standardizing the location
	// of Vale's configuration files.
	//
	// This directory is relative to the user's specified `StylesPath`, which
	// can be set via the `--config` flag, the `VALE_CONFIG_PATH` environment
	// variable, or the default search process.
	//
	// NOTE: The config pipeline is stored in the top-level `.vale-config`
	// directory. See `cmd/vale/sync.go`.
	ConfigDir = "config"

	// PipeDir is the default location for Vale's configuration pipeline.
	PipeDir = ".vale-config"

	VocabDir  = filepath.Join(ConfigDir, "vocabularies")
	DictDir   = filepath.Join(ConfigDir, "dictionaries")
	TmplDir   = filepath.Join(ConfigDir, "templates")
	IgnoreDir = filepath.Join(ConfigDir, "ignore")
	ActionDir = filepath.Join(ConfigDir, "actions")
	ScriptDir = filepath.Join(ConfigDir, "scripts")
)
View Source
var AlertLevels = []string{"suggestion", "warning", "error"}

AlertLevels holds the possible values for "level" in an external rule.

View Source
var CommentsByNormedExt = map[string]map[string]string{
	".c": {
		"inline":     `(?:^|\s)(?:(//.+)|(/\*.+\*/))`,
		"blockStart": `(/\*.*)`,
		"blockEnd":   `(.*\*/)`,
	},
	".clj": {
		"inline":     `(;+.+)`,
		"blockStart": `$^`,
		"blockEnd":   `$^`,
	},
	".r": {
		"inline":     `(#.+)`,
		"blockStart": `$^`,
		"blockEnd":   `$^`,
	},
	".ps1": {
		"inline":     `(#.+)`,
		"blockStart": `(<#.*)`,
		"blockEnd":   `(.*#>)`,
	},
	".php": {
		"inline":     `(//.+)|(/\*.+\*/)|(#.+)`,
		"blockStart": `(/\*.*)`,
		"blockEnd":   `(.*\*/)`,
	},
	".lua": {
		"inline":     `(-- .+)`,
		"blockStart": `(-{2,3}\[\[.*)`,
		"blockEnd":   `(.*\]\])`,
	},
	".hs": {
		"inline":     `(-- .+)`,
		"blockStart": `(\{-.*)`,
		"blockEnd":   `(.*-\})`,
	},
}

CommentsByNormedExt determines what parts of a file we should lint -- e.g., we only want to lint // or /* comments in a C++ file. Multiple formats are mapped to a single extension (e.g., .java -> .c) because many languages use the same comment delimiters.

Deprecated: When possible, we now use tree-sitter grammars to determine the comment delimiters for a given file. See the `lint/code` package for more information.

TODO: This should be removed once we have tree-sitter grammars for all languages.

ConfigDirs is a list of all directories that contain user-defined, non-style configuration files.

View Source
var ConfigVars = map[string]string{
	"VALE_CONFIG_PATH": "Override the default search process by specifying a .vale.ini file.",
	"VALE_STYLES_PATH": "Specify the location of the default StylesPath.",
}

ConfigVars is a list of all supported environment variables.

View Source
var FormatByExtension = map[string][]string{
	`\.(?:[rc]?py[3w]?|[Ss][Cc]onstruct)$`:     {".py", "code"},
	`\.(?:adoc|asciidoc|asc)$`:                 {".adoc", "markup"},
	`\.(?:clj|cljs|cljc|cljd)$`:                {".clj", "code"},
	`\.(?:cpp|cc|c|cp|cxx|c\+\+|h|hpp|h\+\+)$`: {".cpp", "code"},
	`\.(?:css)$`:                      {".css", "code"},
	`\.(?:cs|csx)$`:                   {".c", "code"},
	`\.(?:dita)$`:                     {".dita", "markup"},
	`\.(?:go)$`:                       {".go", "code"},
	`\.(?:hs)$`:                       {".hs", "code"},
	`\.(?:html|htm|shtml|xhtml)$`:     {".html", "markup"},
	`\.(?:java|bsh)$`:                 {".c", "code"},
	`\.(?:jl)$`:                       {".jl", "code"},
	`\.(?:js|jsx)$`:                   {".js", "code"},
	`\.(?:lua)$`:                      {".lua", "code"},
	`\.(?:md|mdown|markdown|markdn)$`: {".md", "markup"},
	`\.(?:org)$`:                      {".org", "markup"},
	`\.(?:php)$`:                      {".php", "code"},
	`\.(?:pl|pm|pod)$`:                {".r", "code"},
	`\.(?:proto)$`:                    {".proto", "code"},
	`\.(?:ps1|psm1|psd1)$`:            {".ps1", "code"},
	`\.(?:rb|Gemfile|Rakefile|Brewfile|gemspec)$`: {".rb", "code"},
	`\.(?:rs)$`:        {".rs", "code"},
	`\.(?:rst|rest)$`:  {".rst", "markup"},
	`\.(?:r|R)$`:       {".r", "code"},
	`\.(?:sass|less)$`: {".c", "code"},
	`\.(?:scala|sbt)$`: {".c", "code"},
	`\.(?:swift)$`:     {".c", "code"},
	`\.(?:ts|tsx)$`:    {".ts", "code"},
	`\.(?:txt)$`:       {".txt", "text"},
	`\.(?:xml)$`:       {".xml", "markup"},
	`\.(?:yaml|yml)$`:  {".yml", "code"},
}

FormatByExtension associates a file extension with its "normed" extension and its format (markup, code or text).

View Source
var LevelToInt = map[string]int{
	"suggestion": 0,
	"warning":    1,
	"error":      2,
}

LevelToInt allows us to easily compare levels in lint.go.

Functions

func AllStringsInSlice

func AllStringsInSlice(strings []string, slice []string) bool

AllStringsInSlice determines if `slice` contains the `strings`.

func CapFirst

func CapFirst(s string) string

CapFirst capitalizes the first letter of a string.

func CondSprintf

func CondSprintf(format string, v ...interface{}) string

CondSprintf is sprintf, ignores extra arguments.

func DefaultConfig

func DefaultConfig() (string, error)

DefaultConfig returns the path to the default configuration file.

We don't create this file automatically because there's no actual notion of a "default" configuration -- it's just a file loation.

NOTE: if this file does not exist *and* the user has not specified a project-specific configuration file, Vale raises an error.

func DefaultStylesPath

func DefaultStylesPath() (string, error)

DefaultStylesPath returns the path to the default styles directory.

NOTE: the default styles directory is only used if neither the project-specific nor the global configuration file specify a `StylesPath`.

func FileExists

func FileExists(filename string) bool

FileExists determines if the path given by `filename` exists.

func FindAsset

func FindAsset(cfg *Config, path string) string

FindAsset tries to locate a Vale-related resource by looking in the user-defined StylesPath.

func FindConfigAsset added in v3.2.0

func FindConfigAsset(cfg *Config, name, dir string) string

FindConfigAsset tries to locate a Vale-related resource by looking in the user-defined StylesPath(s).

func FormatAlert

func FormatAlert(a *Alert, limit int, level, name string)

FormatAlert ensures that all required fields have data.

func FormatFromExt

func FormatFromExt(path string, mapping map[string]string) (string, string)

FormatFromExt takes a file extension and returns its [normExt, format] list, if supported.

func FormatMessage

func FormatMessage(msg string, subs ...string) string

FormatMessage inserts `subs` into `msg`.

func FromFile

func FromFile(cfg *Config, dry bool) (*ini.File, error)

FromFile loads an INI configuration from a file.

func FromString

func FromString(src string, cfg *Config, dry bool) (*ini.File, error)

FromString loads an INI configuration from a string.

func GetNormedExt added in v3.5.0

func GetNormedExt(ext string) string

func GetPackages

func GetPackages(src string) ([]string, error)

Get the user-defined packages from a `.vale.ini` file.

func HasAnySuffix

func HasAnySuffix(s string, suffixes []string) bool

func IgnoreFiles

func IgnoreFiles(stylesPath string) ([]string, error)

IgnoreFiles returns a list of all user-defined ignore files.

func InRange

func InRange(n int, r []int) bool

InRange determines if the range r contains the integer n.

func Indent

func Indent(text, indent string) string

Indent adds padding to every line of `text`.

func IntInSlice

func IntInSlice(a int, slice []int) bool

IntInSlice determines if `slice` contains the int `a`.

func IsCode added in v3.4.0

func IsCode(s string) bool

IsCode returns `true` if s is a code-like token.

func IsDir

func IsDir(filename string) bool

IsDir determines if the path given by `filename` is a directory.

func IsLetter

func IsLetter(s string) bool

IsLetter returns `true` if s contains all letter characters and false if not.

func IsPhrase

func IsPhrase(s string) bool

IsPhrase returns `true` is s is a phrase-like token.

This is used to differentiate regex tokens from non-regex.

func NewE100

func NewE100(context string, err error) error

NewE100 creates a new, formatted "unexpected" error.

Since E100 errors can occur anywhere, we include a "context" that makes it clear where exactly the error was generated.

func NewE201

func NewE201(msg, value, path string, finder errorCondition) error

NewE201 creates a formatted user-generated error.

201 errors involve a specific configuration asset and should contain parsable location information on their last line of the form:

<path>:<line>:<start>:<end>

func NewE201FromPosition

func NewE201FromPosition(msg, file string, goal int) error

NewE201FromPosition creates a new E201 error from an in-file location.

func NewE201FromTarget

func NewE201FromTarget(msg, value, file string) error

NewE201FromTarget creates a new E201 error from a target string.

func NewError

func NewError(code, title, msg string) error

NewError creates a colored error from the given information.

The standard format is

``` <code> [<context>] <title>

<msg> ```

func ReplaceAllStringSubmatchFunc

func ReplaceAllStringSubmatchFunc(re *regexp.Regexp, str string, repl func([]string) string) string

func ReplaceExt

func ReplaceExt(fp string, formats map[string]string) string

ReplaceExt replaces the extension of `fp` with `ext` if the extension of `fp` is in `formats`.

This is used in places where we need to normalize file extensions (e.g., `foo.mdx` -> `foo.md`) in order to respect format associations.

func Sanitize

func Sanitize(txt string) string

Sanitize prepares text for our check functions.

func ShouldIgnoreDirectory

func ShouldIgnoreDirectory(directoryName string) bool

ShouldIgnoreDirectory will check if directory should be ignored

func SplitLines

func SplitLines(data []byte, atEOF bool) (adv int, token []byte, err error)

SplitLines splits on CRLF, CR not followed by LF, and LF.

func StringInSlice

func StringInSlice(a string, slice []string) bool

StringInSlice determines if `slice` contains the string `a`.

func StringsToInterface

func StringsToInterface(strings []string) []interface{}

StringsToInterface converts a slice of strings to an interface.

func StripANSI

func StripANSI(s string) string

StripANSI removes all ANSI characters from the given string.

func Substitute

func Substitute(src, sub string, char rune) (string, bool)

Substitute replaces the substring `sub` with a string of asterisks.

func TextToContext

func TextToContext(text string, meta *nlp.Info) []nlp.TaggedWord

func ToSentence

func ToSentence(words []string, andOrOr string) string

ToSentence converts a slice of terms into sentence.

func Which

func Which(cmds []string) string

Which checks for the existence of any command in `cmds`.

func WhitespaceToSpace

func WhitespaceToSpace(msg string) string

WhitespaceToSpace converts newlines and multiple spaces (e.g., " ") into a single space.

Types

type Action

type Action struct {
	Name   string   // the name of the action -- e.g, 'replace'
	Params []string // a slice of parameters for the given action
}

An Action represents a possible solution to an Alert.

type Alert

type Alert struct {
	Action      Action   // a possible solution
	Span        []int    // the [begin, end] location within a line
	Offset      []string `json:"-"` // tokens to ignore before this match
	Check       string   // the name of the check
	Description string   // why `Message` is meaningful
	Link        string   // reference material
	Message     string   // the output message
	Severity    string   // 'suggestion', 'warning', or 'error'
	Match       string   // the actual matched text
	Line        int      // the source line
	Limit       int      `json:"-"` // the max times to report
	Hide        bool     `json:"-"` // should we hide this alert?
}

An Alert represents a potential error in prose.

type ByName

type ByName []*File

ByName sorts Files by their path.

func (ByName) Len

func (a ByName) Len() int

func (ByName) Less

func (a ByName) Less(i, j int) bool

func (ByName) Swap

func (a ByName) Swap(i, j int)

type ByPosition

type ByPosition []Alert

ByPosition sorts Alerts by line and column.

func (ByPosition) Len

func (a ByPosition) Len() int

func (ByPosition) Less

func (a ByPosition) Less(i, j int) bool

func (ByPosition) Swap

func (a ByPosition) Swap(i, j int)

type CLIFlags

type CLIFlags struct {
	AlertLevel   string
	Built        string
	Glob         string
	InExt        string
	Output       string
	Path         string
	Sources      string
	Filter       string
	Local        bool
	NoExit       bool
	Normalize    bool
	Relative     bool
	Remote       bool
	Simple       bool
	Sorted       bool
	Wrap         bool
	Version      bool
	Help         bool
	IgnoreGlobal bool
}

CLIFlags holds the values that are defined at runtime by the user.

For example, `vale --minAlertLevel=error`.

type Config

type Config struct {
	// General configuration
	BlockIgnores      map[string][]string        // A list of blocks to ignore
	Checks            []string                   // All checks to load
	Formats           map[string]string          // A map of unknown -> known formats
	Asciidoctor       map[string]string          // A map of asciidoctor attributes
	FormatToLang      map[string]string          // A map of format to lang ID
	GBaseStyles       []string                   // Global base style
	GChecks           map[string]bool            // Global checks
	IgnoredClasses    []string                   // A list of HTML classes to ignore
	IgnoredScopes     []string                   // A list of HTML tags to ignore
	MinAlertLevel     int                        // Lowest alert level to display
	Vocab             []string                   // The active project
	RuleToLevel       map[string]string          // Single-rule level changes
	SBaseStyles       map[string][]string        // Syntax-specific base styles
	SChecks           map[string]map[string]bool // Syntax-specific checks
	SkippedScopes     []string                   // A list of HTML blocks to ignore
	Stylesheets       map[string]string          // XSLT stylesheet
	TokenIgnores      map[string][]string        // A list of tokens to ignore
	CommentDelimiters map[string][2]string       // Strings to treat as comment delimiters. Indicates the start and end delimiters.
	WordTemplate      string                     // The template used in YAML -> regexp list conversions
	RootINI           string                     // the path to the project's .vale.ini file
	Paths             []string                   // A list of paths to search for styles
	ConfigFiles       []string                   // A list of configuration files to load

	AcceptedTokens []string `json:"-"` // Project-specific vocabulary (okay)
	RejectedTokens []string `json:"-"` // Project-specific vocabulary (avoid)

	FallbackPath string               `json:"-"`
	SecToPat     map[string]glob.Glob `json:"-"`
	Styles       []string             `json:"-"`

	NLPEndpoint string // An external API to call for NLP-related work.

	// Command-line configuration
	Flags *CLIFlags `json:"-"`

	StyleKeys []string `json:"-"`
	RuleKeys  []string `json:"-"`
}

Config holds the configuration values from both the CLI and `.vale.ini`.

func NewConfig

func NewConfig(flags *CLIFlags) (*Config, error)

NewConfig initializes a Config with its default values.

func ReadPipeline

func ReadPipeline(flags *CLIFlags, dry bool) (*Config, error)

ReadPipeline loads Vale's configuration according to the local search process.

A `dry` run means that we can't expect the `StylesPath` to fully formed yet. For example, some assets may not have been downloaded yet via the `sync` command.

func (*Config) AddConfigFile added in v3.1.0

func (c *Config) AddConfigFile(name string)

AddConfigFile adds a new configuration file to the current list.

func (*Config) AddStylesPath added in v3.1.0

func (c *Config) AddStylesPath(path string)

AddStylesPath adds a new path to the current list.

func (*Config) AddWordListFile

func (c *Config) AddWordListFile(name string, accept bool) error

AddWordListFile adds vocab terms from a provided file.

func (*Config) ConfigFile added in v3.3.0

func (c *Config) ConfigFile() string

ConfigFile returns the last configuration file in the list.

This represents the user's project-agnostic configuration file -- i.e., the last one that was added.

func (*Config) Root added in v3.4.1

func (c *Config) Root() (string, error)

Root returns the first configuration file in the list.

func (*Config) SearchPaths added in v3.1.0

func (c *Config) SearchPaths() []string

func (*Config) String

func (c *Config) String() string

func (*Config) StylesPath

func (c *Config) StylesPath() string

GetStylesPath returns the last path in the list.

This represents the user's project-specific styles directory -- i.e., the last one that was added.

type ConfigSrc

type ConfigSrc int

ConfigSrc is a source of configuration values.

This could be a local file, a string, or a remote URL.

const (
	FileSrc ConfigSrc = iota
	StringSrc
)

type File

type File struct {
	NLP        nlp.Info          // -
	Summary    bytes.Buffer      // holds content to be included in summarization checks
	Alerts     []Alert           // all alerts associated with this file
	BaseStyles []string          // base style assigned in .vale
	Lines      []string          // the File's Content split into lines
	Sequences  []string          // tracks various info (e.g., defined abbreviations)
	Content    string            // the raw file contents
	Format     string            // 'code', 'markup' or 'prose'
	NormedExt  string            // the normalized extension (see util/format.go)
	Path       string            // the full path
	NormedPath string            // the normalized path
	Transform  string            // XLST transform
	RealExt    string            // actual file extension
	Checks     map[string]bool   // syntax-specific checks assigned in .vale
	ChkToCtx   map[string]string // maps a temporary context to a particular check
	Comments   map[string]bool   // comment control statements
	Metrics    map[string]int    // count-based metrics

	Lookup bool // -
	// contains filtered or unexported fields
}

A File represents a linted text file.

func NewFile

func NewFile(src string, config *Config) (*File, error)

NewFile initializes a File.

func (*File) AddAlert

func (f *File) AddAlert(a Alert, blk nlp.Block, lines, pad int, lookup bool)

AddAlert calculates the in-text location of an Alert and adds it to a File.

func (*File) ComputeMetrics

func (f *File) ComputeMetrics() (map[string]interface{}, error)

ComputeMetrics returns all of f's metrics.

func (*File) FindLoc

func (f *File) FindLoc(ctx, s string, pad, count int, a Alert) (int, []int)

FindLoc calculates the line and span of an Alert.

func (*File) QueryComments

func (f *File) QueryComments(check string) bool

QueryComments checks if there has been an in-text comment for this check.

func (*File) ResetComments

func (f *File) ResetComments()

ResetComments resets the state of all checks back to active.

func (*File) SetText

func (f *File) SetText(s string)

SetText updates the file's content, lines, and history.

func (*File) SortedAlerts

func (f *File) SortedAlerts() []Alert

SortedAlerts returns all of f's alerts sorted by line and column.

func (*File) UpdateComments

func (f *File) UpdateComments(comment string)

UpdateComments sets a new status based on comment.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL