Skip to content

Commit 108344a

Browse files
jantimonazz
authored andcommitted
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.
1 parent 157d4b1 commit 108344a

File tree

4 files changed

+70
-16
lines changed

4 files changed

+70
-16
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,4 @@ For example `pretty-quick --since HEAD` will format only staged files.
108108

109109
## Configuration and Ignore Files
110110

111-
`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.
111+
`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.

src/__tests__/scm-git.test.js

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,18 @@ afterEach(() => {
1111
jest.clearAllMocks();
1212
});
1313

14-
const mockGitFs = (additionalUnstaged = '') => {
15-
mock({
16-
'/.git': {},
17-
'/raz.js': 'raz()',
18-
'/foo.js': 'foo()',
19-
'/bar.md': '# foo',
20-
});
14+
const mockGitFs = (additionalUnstaged = '', additionalFiles = {}) => {
15+
mock(
16+
Object.assign(
17+
{
18+
'/.git': {},
19+
'/raz.js': 'raz()',
20+
'/foo.js': 'foo()',
21+
'/bar.md': '# foo',
22+
},
23+
additionalFiles
24+
)
25+
);
2126
execa.sync.mockImplementation((command, args) => {
2227
if (command !== 'git') {
2328
throw new Error(`unexpected command: ${command}`);
@@ -241,4 +246,22 @@ describe('with git', () => {
241246
expect(onExamineFile).not.toHaveBeenCalledWith('./foo.js');
242247
expect(onExamineFile).not.toHaveBeenCalledWith('./bar.md');
243248
});
249+
250+
test('ignore files matching patterns from the repositories root .prettierignore', () => {
251+
const onWriteFile = jest.fn();
252+
mockGitFs('', {
253+
'/.prettierignore': '*.md',
254+
});
255+
prettyQuick('/sub-directory/', { since: 'banana', onWriteFile });
256+
expect(onWriteFile.mock.calls).toEqual([['./foo.js']]);
257+
});
258+
259+
test('ignore files matching patterns from the working directories .prettierignore', () => {
260+
const onWriteFile = jest.fn();
261+
mockGitFs('', {
262+
'/sub-directory/.prettierignore': '*.md',
263+
});
264+
prettyQuick('/sub-directory/', { since: 'banana', onWriteFile });
265+
expect(onWriteFile.mock.calls).toEqual([['./foo.js']]);
266+
});
244267
});

src/__tests__/scm-hg.test.js

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,17 @@ afterEach(() => {
1111
jest.clearAllMocks();
1212
});
1313

14-
const mockHgFs = () => {
15-
mock({
16-
'/.hg': {},
17-
'/foo.js': 'foo()',
18-
'/bar.md': '# foo',
19-
});
14+
const mockHgFs = (additionalFiles = {}) => {
15+
mock(
16+
Object.assign(
17+
{
18+
'/.hg': {},
19+
'/foo.js': 'foo()',
20+
'/bar.md': '# foo',
21+
},
22+
additionalFiles
23+
)
24+
);
2025
execa.sync.mockImplementation((command, args) => {
2126
if (command !== 'hg') {
2227
throw new Error(`unexpected command: ${command}`);
@@ -152,4 +157,22 @@ describe('with hg', () => {
152157
expect(onExamineFile).not.toHaveBeenCalledWith('./foo.js');
153158
expect(onExamineFile).not.toHaveBeenCalledWith('./bar.md');
154159
});
160+
161+
test('ignore files matching patterns from the repositories root .prettierignore', () => {
162+
const onWriteFile = jest.fn();
163+
mockHgFs({
164+
'/.prettierignore': '*.md',
165+
});
166+
prettyQuick('/sub-directory/', { since: 'banana', onWriteFile });
167+
expect(onWriteFile.mock.calls).toEqual([['./foo.js']]);
168+
});
169+
170+
test('ignore files matching patterns from the working directories .prettierignore', () => {
171+
const onWriteFile = jest.fn();
172+
mockHgFs({
173+
'/sub-directory/.prettierignore': '*.md',
174+
});
175+
prettyQuick('/sub-directory/', { since: 'banana', onWriteFile });
176+
expect(onWriteFile.mock.calls).toEqual([['./foo.js']]);
177+
});
155178
});

src/index.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,24 @@ export default (
2929

3030
onFoundSinceRevision && onFoundSinceRevision(scm.name, revision);
3131

32+
const rootIgnorer = createIgnorer(directory);
33+
const cwdIgnorer =
34+
currentDirectory !== directory
35+
? createIgnorer(currentDirectory)
36+
: () => true;
37+
3238
const changedFiles = scm
3339
.getChangedFiles(directory, revision, staged)
3440
.filter(isSupportedExtension)
35-
.filter(createIgnorer(directory));
41+
.filter(rootIgnorer)
42+
.filter(cwdIgnorer);
3643

3744
const unstagedFiles = staged
3845
? scm
3946
.getUnstagedChangedFiles(directory, revision)
4047
.filter(isSupportedExtension)
41-
.filter(createIgnorer(directory))
48+
.filter(rootIgnorer)
49+
.filter(cwdIgnorer)
4250
: [];
4351

4452
const wasFullyStaged = f => unstagedFiles.indexOf(f) < 0;

0 commit comments

Comments
 (0)