MCPcopy Create free account
hub / github.com/vale-cli/vale / NewFile

Function NewFile

internal/core/file.go:52–144  ·  view source on GitHub ↗

NewFile initializes a File.

(src string, config *Config)

Source from the content-addressed store, hash-verified

50
51// NewFile initializes a File.
52func NewFile(src string, config *Config) (*File, error) {
53 var format, ext string
54 var fbytes []byte
55 var lookup bool
56 path := src
57
58 if system.FileExists(src) {
59 fbytes, _ = os.ReadFile(src)
60 if config.Flags.InExt != ".txt" {
61 ext, format = FormatFromExt(config.Flags.InExt, config.Formats)
62 } else {
63 ext, format = FormatFromExt(src, config.Formats)
64 }
65 } else {
66 fbytes = []byte(src)
67 lookup = true
68 // For stdin, allow an explicit path override to drive path-based config.
69 if config.Flags.InPath != "" {
70 path = config.Flags.InPath
71 } else {
72 path = "stdin" + config.Flags.InExt
73 }
74 // If --ext was explicitly set, respect it; otherwise infer from the path.
75 if config.Flags.InExt != ".txt" {
76 ext, format = FormatFromExt(config.Flags.InExt, config.Formats)
77 } else {
78 ext, format = FormatFromExt(path, config.Formats)
79 }
80 }
81
82 filepaths := []string{path}
83 normed := system.ReplaceFileExt(path, config.Formats)
84
85 baseStyles := config.GBaseStyles
86 checks := make(map[string]bool)
87
88 for _, fp := range filepaths {
89 for _, sec := range config.StyleKeys {
90 if pat, found := config.SecToPat[sec]; found && pat.Match(fp) {
91 baseStyles = config.SBaseStyles[sec]
92 }
93 }
94
95 for _, sec := range config.RuleKeys {
96 if pat, found := config.SecToPat[sec]; found && pat.Match(fp) {
97 for k, v := range config.SChecks[sec] {
98 checks[k] = v
99 }
100 }
101 }
102 }
103
104 lang := "en"
105 for syntax, code := range config.FormatToLang {
106 sec, err := glob.Compile(syntax)
107 if err != nil {
108 return &File{}, err
109 } else if sec.Match(path) {

Callers 4

lintFileMethod · 0.92
TestExistenceFunction · 0.92
FuzzExistenceFunction · 0.92
transformFunction · 0.92

Calls 7

FileExistsFunction · 0.92
ReplaceFileExtFunction · 0.92
CompileFunction · 0.92
FormatFromExtFunction · 0.85
NewE100Function · 0.85
SanitizeFunction · 0.85
MatchMethod · 0.80

Tested by 2

TestExistenceFunction · 0.74
FuzzExistenceFunction · 0.74

Used in the wild real call sites across dependent graphs

searching dependent graphs…