Skip to content

Commit d9fcc8c

Browse files
authored
filepath.Clean() the include paths (#124)
This normalizes the include path strings for use throughout the linter, parser, and checks. See: https://pkg.go.dev/path/filepath#Clean
1 parent eca3bc3 commit d9fcc8c

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ Use `--stdin-name` to customize the filename used in output messages.
4444
$ thriftlint --stdin-name filename.thrift - < filename.thrift
4545
```
4646

47+
Include paths specified via `-I` or in the configuration file are normalized
48+
using lexical path cleaning, which removes trailing slashes, resolves `.` and
49+
`..` elements, and eliminates redundant separators. This means `includes/`,
50+
`includes`, and `./includes/` are treated identically.
51+
4752
Messages are reported to standard output using a familiar parseable format:
4853

4954
```

linter.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ func WithLogger(logger *log.Logger) Option {
4848
// WithIncludes is an Option that adds Thrift include paths to the linter.
4949
func WithIncludes(includes []string) Option {
5050
return func(l *Linter) {
51-
l.includes = includes
51+
for _, dir := range includes {
52+
l.includes = append(l.includes, filepath.Clean(dir))
53+
}
5254
}
5355
}
5456

linter_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,11 @@ func TestWithLogger(t *testing.T) {
3333
}
3434

3535
func TestWithIncludes(t *testing.T) {
36-
includes := []string{"a", "b"}
36+
includes := []string{"a", "b/", "../c/..", "/d", "./e", ""}
37+
expected := []string{"a", "b", "..", "/d", "e", "."}
3738
linter := NewLinter(Checks{}, WithIncludes(includes))
38-
if !reflect.DeepEqual(linter.includes, includes) {
39-
t.Errorf("expected includes to be %v, got %v", includes, linter.includes)
39+
if !reflect.DeepEqual(linter.includes, expected) {
40+
t.Errorf("expected includes to be %v, got %v", expected, linter.includes)
4041
}
4142
}
4243

0 commit comments

Comments
 (0)