From cadfbc489f17db348459a09e7bd49514d07152a2 Mon Sep 17 00:00:00 2001 From: Jason Dent Date: Wed, 1 Feb 2023 18:31:09 +0100 Subject: [PATCH] fix: Correct .gitignore comment detection (#4083) --- .../src/GitIgnoreFile.test.ts | 22 +++++++++++++++---- .../cspell-gitignore/src/GitIgnoreFile.ts | 4 ++-- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/packages/cspell-gitignore/src/GitIgnoreFile.test.ts b/packages/cspell-gitignore/src/GitIgnoreFile.test.ts index e6f4c53c7632..9e6ea0d3b2bb 100644 --- a/packages/cspell-gitignore/src/GitIgnoreFile.test.ts +++ b/packages/cspell-gitignore/src/GitIgnoreFile.test.ts @@ -17,13 +17,20 @@ describe('GitIgnoreFile', () => { }); test.each` - file | expected - ${__filename} | ${true} - ${path.join(__dirname, 'file.ts')} | ${false} - ${path.join(__dirname, '../file.test.ts')} | ${false} + file | expected + ${__filename} | ${true} + ${path.join(__dirname, 'file.ts')} | ${false} + ${path.join(__dirname, '../file.test.ts')} | ${false} + ${path.join(__dirname, '/src/file.test.ts')} | ${true} + ${path.join(__dirname, 'hello.py')} | ${false} + ${path.join(__dirname, 'hello#')} | ${true} + ${path.join(__dirname, '#tag')} | ${false} + ${path.join(__dirname, 'src/hidden#/source.py')} | ${true} `('isIgnored $file', ({ file, expected }) => { const gif = sampleGitIgnoreFile(); expect(gif.isIgnored(file)).toBe(expected); + const gifWin = sampleGitIgnoreFileWin(); + expect(gifWin.isIgnored(file)).toBe(expected); }); test('loadGitIgnoreFile .gitignore', async () => { @@ -34,6 +41,8 @@ describe('GitIgnoreFile', () => { test('getGlobs', () => { const gif = sampleGitIgnoreFile(); expect(gif.getGlobs(__dirname).sort()).toEqual([ + '**/*#', + '**/*#/**', '**/*.test.*', '**/*.test.*/**', '**/node_modules', @@ -122,12 +131,17 @@ node_modules /temp /coverage/** +*# `; function sampleGitIgnoreFile(): GitIgnoreFile { return GitIgnoreFile.parseGitignore(sampleGitIgnore, path.join(__dirname, '.gitignore')); } +function sampleGitIgnoreFileWin(): GitIgnoreFile { + return GitIgnoreFile.parseGitignore(sampleGitIgnore.replace(/\r?\n/, '\r\n'), path.join(__dirname, '.gitignore')); +} + // function oc(v: Partial): T { // return expect.objectContaining(v); // } diff --git a/packages/cspell-gitignore/src/GitIgnoreFile.ts b/packages/cspell-gitignore/src/GitIgnoreFile.ts index 105c8406a720..db6d1f21b7e9 100644 --- a/packages/cspell-gitignore/src/GitIgnoreFile.ts +++ b/packages/cspell-gitignore/src/GitIgnoreFile.ts @@ -51,9 +51,9 @@ export class GitIgnoreFile { static parseGitignore(content: string, gitignoreFilename: string): GitIgnoreFile { const options = { root: path.dirname(gitignoreFilename) }; const globs = content - .split('\n') + .split(/\r?\n/g) .map((glob, index) => ({ - glob: glob.replace(/#.*/, '').trim(), + glob: glob.replace(/^#.*/, ''), source: gitignoreFilename, line: index + 1, }))