webpro-nl / knip

✂️ Find unused files, dependencies and exports in your JavaScript and TypeScript projects. Knip it before you ship it!
https://knip.dev
ISC License
7.06k stars 177 forks source link

Do not treat trailing `#` in gitignore as comment #797

Closed mcous closed 1 month ago

mcous commented 1 month ago

Just started using knip today and it was instrumental in some really nice cleanup in our codebase. I hit #795 right off the bat, though, and it took me a while to figure out what was going on. I figured I could say "thanks" by opening up a quick PR to fix the issue!

The logic in the gitignore parser that strips trailing # ... off of .gitignore lines appears to be wrong. In my testing, you cannot add a comment to the end of a .gitignore line in this manner (syntax highlighting be damned):

# this is a comment
dist # this is not a comment

This tracks with git's documentation (emphasis mine) as well as my own messing around with git:

A line starting with # serves as a comment. Put a backslash (\) in front of the first hash for patterns that begin with a hash.

The previous parsing logic (added in https://github.com/webpro-nl/knip/commit/e455527fb2cc58981775ad538f5d4a64a3c70b5b) stripped all non-backslashed #... out. I updated the logic to only strip a leading \, if it's in front of a hash, so the following cases will be handled properly:

trailing-hash-ignore#
\#leading-hash-ignore
just#some-file
pkg-pr-new[bot] commented 1 month ago

Open in Stackblitz

bun add https://pkg.pr.new/knip@797

commit: 9b72edb

webpro commented 1 month ago

Thanks @mcous! Very much appreciate you made the leap from "dang this thing doesn't work at all" to a PR with a fix with tests! Will be released shortly.

webpro commented 1 month ago

:rocket: This pull request is included in v5.31.0. See Release 5.31.0 for release notes.

Using Knip in a commercial project? Please consider becoming a sponsor.