Skip to content

Commit fcf0740

Browse files
authored
fix(ls-metrics): prevent panic on empty directory (#1144)
Co-authored-by: VXNCXNX <VXNCXNX@users.noreply.github.com>
1 parent dd09d42 commit fcf0740

2 files changed

Lines changed: 23 additions & 0 deletions

File tree

‎cmd/vale/command.go‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,14 @@ func printMetrics(args []string, _ *core.CLIFlags) error {
236236
return err
237237
}
238238

239+
// `FileExists` is true for a directory, and a directory that holds no
240+
// lintable file lints to nothing. Reporting that is better than printing
241+
// `{}`, which already means "a file Vale read and found no metrics in".
242+
if len(linted) == 0 {
243+
return core.NewE100("ls-metrics", fmt.Errorf(
244+
"'%s' contains no lintable files", args[0]))
245+
}
246+
239247
computed, _ := linted[0].ComputeMetrics()
240248
return printJSON(computed)
241249
}

‎testdata/e2e/metrics.yaml‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,21 @@ cases:
3434
want: |
3535
{}
3636
37+
- name: empty-directory
38+
about: the argument only has to exist, and a directory does -- so one
39+
whose every file is skipped (`node_modules` is ignored by default)
40+
lints to nothing, and saying so beats indexing an empty result, which
41+
used to panic
42+
files:
43+
empty/node_modules/test.md: |
44+
# Title
45+
args: ls-metrics empty
46+
exit: 2
47+
contains: |
48+
E100 [ls-metrics] Runtime error
49+
50+
'empty' contains no lintable files
51+
3752
- name: formula
3853
about: a `metric` rule reads the same counts -- the weighted total spells
3954
them out, so 221 means 2 H2s, 2 list items, and 1 blockquote

0 commit comments

Comments
 (0)