Skip to content

Commit

Permalink
Do not treat trailing # in gitignore as comment (#797)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcous authored Oct 3, 2024
1 parent 847649d commit 7fc6355
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/knip/src/util/glob-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export const parseAndConvertGitignorePatterns = (patterns: string, ancestor?: st
.split(/\r?\n/)
.filter(line => line.trim() && !line.startsWith('#'))
.flatMap(line => {
const pattern = line.replace(/(?<!\\)#.*/, '').trim();
const pattern = line.replace(/^\\(?=#)/, '').trim();
if (ancestor && matchFrom) {
if (pattern.match(matchFrom)) return [pattern.replace(matchFrom, '$1')];
if (pattern.startsWith('/**/')) return [pattern.slice(1)];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,12 @@ test('parseAndConvertGitignorePatterns (ancestor)', async () => {
{ negated: false, patterns: ['c', 'c/**'] },
]);
});

test('parseAndConvertGitignorePatterns (hashes)', async () => {
const gitignorePatterns = ['#comment', 'ends-with-hash#', String.raw`\#starts-with-hash`];
const globPatterns = parse(gitignorePatterns.join(EOL));
assert.deepEqual(globPatterns, [
{ negated: false, patterns: ['**/ends-with-hash#', '**/ends-with-hash#/**'] },
{ negated: false, patterns: ['**/#starts-with-hash', '**/#starts-with-hash/**'] },
]);
});

0 comments on commit 7fc6355

Please sign in to comment.