Skip to content

Commit

Permalink
fix: Correct .gitignore comment detection (#4083)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason3S committed Feb 1, 2023
1 parent 9de0bdd commit cadfbc4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
22 changes: 18 additions & 4 deletions packages/cspell-gitignore/src/GitIgnoreFile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand All @@ -34,6 +41,8 @@ describe('GitIgnoreFile', () => {
test('getGlobs', () => {
const gif = sampleGitIgnoreFile();
expect(gif.getGlobs(__dirname).sort()).toEqual([
'**/*#',
'**/*#/**',
'**/*.test.*',
'**/*.test.*/**',
'**/node_modules',
Expand Down Expand Up @@ -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<T>(v: Partial<T>): T {
// return expect.objectContaining(v);
// }
4 changes: 2 additions & 2 deletions packages/cspell-gitignore/src/GitIgnoreFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}))
Expand Down

0 comments on commit cadfbc4

Please sign in to comment.