Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: also use .prettierignore from current working directory (#14) #58

Merged
merged 1 commit into from Jan 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -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.
37 changes: 30 additions & 7 deletions src/__tests__/scm-git.test.js
Expand Up @@ -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}`);
Expand Down Expand Up @@ -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']]);
});
});
35 changes: 29 additions & 6 deletions src/__tests__/scm-hg.test.js
Expand Up @@ -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}`);
Expand Down Expand Up @@ -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']]);
});
});
12 changes: 10 additions & 2 deletions src/index.js
Expand Up @@ -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;
Expand Down