From 108344ad58e1bacd81f78c2f027dd2889dd5d136 Mon Sep 17 00:00:00 2001 From: Jan Nicklas Date: Wed, 16 Jan 2019 08:44:29 +0100 Subject: [PATCH] fix: also use .prettierignore from current working directory (#14) (#58) This pull request tries to fix #14 with a zero config approach. If the `process.cwd()` is not the same as the git root it will also try to load the .prettierignore file from there. --- README.md | 2 +- src/__tests__/scm-git.test.js | 37 ++++++++++++++++++++++++++++------- src/__tests__/scm-hg.test.js | 35 +++++++++++++++++++++++++++------ src/index.js | 12 ++++++++++-- 4 files changed, 70 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index c80287c..ac4f328 100644 --- a/README.md +++ b/README.md @@ -108,4 +108,4 @@ For example `pretty-quick --since HEAD` will format only staged files. ## Configuration and Ignore Files -`pretty-quick` will respect your [`.prettierrc`](https://prettier.io/docs/en/configuration), [`.prettierignore`](https://prettier.io/docs/en/ignore#ignoring-files), and [`.editorconfig`](http://editorconfig.org/) files, so there's no additional setup required. Configuration files will be found by searching up the file system. `.prettierignore` files are only found from the working directory that the command was executed from. +`pretty-quick` will respect your [`.prettierrc`](https://prettier.io/docs/en/configuration), [`.prettierignore`](https://prettier.io/docs/en/ignore#ignoring-files), and [`.editorconfig`](http://editorconfig.org/) files, so there's no additional setup required. Configuration files will be found by searching up the file system. `.prettierignore` files are only found from the repository root and the working directory that the command was executed from. diff --git a/src/__tests__/scm-git.test.js b/src/__tests__/scm-git.test.js index 53436c9..fafcf00 100644 --- a/src/__tests__/scm-git.test.js +++ b/src/__tests__/scm-git.test.js @@ -11,13 +11,18 @@ afterEach(() => { jest.clearAllMocks(); }); -const mockGitFs = (additionalUnstaged = '') => { - mock({ - '/.git': {}, - '/raz.js': 'raz()', - '/foo.js': 'foo()', - '/bar.md': '# foo', - }); +const mockGitFs = (additionalUnstaged = '', additionalFiles = {}) => { + mock( + Object.assign( + { + '/.git': {}, + '/raz.js': 'raz()', + '/foo.js': 'foo()', + '/bar.md': '# foo', + }, + additionalFiles + ) + ); execa.sync.mockImplementation((command, args) => { if (command !== 'git') { throw new Error(`unexpected command: ${command}`); @@ -241,4 +246,22 @@ describe('with git', () => { expect(onExamineFile).not.toHaveBeenCalledWith('./foo.js'); expect(onExamineFile).not.toHaveBeenCalledWith('./bar.md'); }); + + test('ignore files matching patterns from the repositories root .prettierignore', () => { + const onWriteFile = jest.fn(); + mockGitFs('', { + '/.prettierignore': '*.md', + }); + prettyQuick('/sub-directory/', { since: 'banana', onWriteFile }); + expect(onWriteFile.mock.calls).toEqual([['./foo.js']]); + }); + + test('ignore files matching patterns from the working directories .prettierignore', () => { + const onWriteFile = jest.fn(); + mockGitFs('', { + '/sub-directory/.prettierignore': '*.md', + }); + prettyQuick('/sub-directory/', { since: 'banana', onWriteFile }); + expect(onWriteFile.mock.calls).toEqual([['./foo.js']]); + }); }); diff --git a/src/__tests__/scm-hg.test.js b/src/__tests__/scm-hg.test.js index fab4538..10a50b9 100644 --- a/src/__tests__/scm-hg.test.js +++ b/src/__tests__/scm-hg.test.js @@ -11,12 +11,17 @@ afterEach(() => { jest.clearAllMocks(); }); -const mockHgFs = () => { - mock({ - '/.hg': {}, - '/foo.js': 'foo()', - '/bar.md': '# foo', - }); +const mockHgFs = (additionalFiles = {}) => { + mock( + Object.assign( + { + '/.hg': {}, + '/foo.js': 'foo()', + '/bar.md': '# foo', + }, + additionalFiles + ) + ); execa.sync.mockImplementation((command, args) => { if (command !== 'hg') { throw new Error(`unexpected command: ${command}`); @@ -152,4 +157,22 @@ describe('with hg', () => { expect(onExamineFile).not.toHaveBeenCalledWith('./foo.js'); expect(onExamineFile).not.toHaveBeenCalledWith('./bar.md'); }); + + test('ignore files matching patterns from the repositories root .prettierignore', () => { + const onWriteFile = jest.fn(); + mockHgFs({ + '/.prettierignore': '*.md', + }); + prettyQuick('/sub-directory/', { since: 'banana', onWriteFile }); + expect(onWriteFile.mock.calls).toEqual([['./foo.js']]); + }); + + test('ignore files matching patterns from the working directories .prettierignore', () => { + const onWriteFile = jest.fn(); + mockHgFs({ + '/sub-directory/.prettierignore': '*.md', + }); + prettyQuick('/sub-directory/', { since: 'banana', onWriteFile }); + expect(onWriteFile.mock.calls).toEqual([['./foo.js']]); + }); }); diff --git a/src/index.js b/src/index.js index f1fb2d5..280c831 100644 --- a/src/index.js +++ b/src/index.js @@ -29,16 +29,24 @@ export default ( onFoundSinceRevision && onFoundSinceRevision(scm.name, revision); + const rootIgnorer = createIgnorer(directory); + const cwdIgnorer = + currentDirectory !== directory + ? createIgnorer(currentDirectory) + : () => true; + const changedFiles = scm .getChangedFiles(directory, revision, staged) .filter(isSupportedExtension) - .filter(createIgnorer(directory)); + .filter(rootIgnorer) + .filter(cwdIgnorer); const unstagedFiles = staged ? scm .getUnstagedChangedFiles(directory, revision) .filter(isSupportedExtension) - .filter(createIgnorer(directory)) + .filter(rootIgnorer) + .filter(cwdIgnorer) : []; const wasFullyStaged = f => unstagedFiles.indexOf(f) < 0;