Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

Commit

Permalink
fix(prettierignore): properly ignore files again
Browse files Browse the repository at this point in the history
PR #404 changed to using Prettier's `getFileInfo` and `resolveConfig` functions to determine
whether a file is formattable. We were not correctly invoking the API for `getFileInfo` so every
file was coming up as not ignored.

Fixes #446
  • Loading branch information
robwise committed Aug 23, 2018
1 parent f9e96ff commit dd4c849
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
8 changes: 7 additions & 1 deletion dist/helpers/isFileFormattable.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@
const _ = require('lodash/fp');
const getPrettierInstance = require('./getPrettierInstance');
const { getCurrentFilePath, isCurrentFilePathDefined } = require('../editorInterface');
const { findCachedFromFilePath } = require('./general');

const getNearestPrettierignorePath = filePath => findCachedFromFilePath(filePath, '.prettierignore');

const getPrettierFileInfoForCurrentFilePath = (editor
// $FlowFixMe: getFileInfo.sync needs to be addded to flow-typed
) => getPrettierInstance(editor).getFileInfo.sync(getCurrentFilePath(editor), {}, '.prettierignore');
) => getPrettierInstance(editor).getFileInfo.sync(getCurrentFilePath(editor), {
// $FlowIssue: we know filepath is defined at this point
ignorePath: getNearestPrettierignorePath(getCurrentFilePath(editor))
});

const doesFileInfoIndicateFormattable = fileInfo => fileInfo && !fileInfo.ignored && !!fileInfo.inferredParser;

Expand Down
9 changes: 8 additions & 1 deletion src/helpers/isFileFormattable.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@
const _ = require('lodash/fp');
const getPrettierInstance = require('./getPrettierInstance');
const { getCurrentFilePath, isCurrentFilePathDefined } = require('../editorInterface');
const { findCachedFromFilePath } = require('./general');

const getNearestPrettierignorePath = (filePath: FilePath): ?FilePath =>
findCachedFromFilePath(filePath, '.prettierignore');

const getPrettierFileInfoForCurrentFilePath = (editor: TextEditor): Prettier$FileInfo =>
// $FlowFixMe: getFileInfo.sync needs to be addded to flow-typed
getPrettierInstance(editor).getFileInfo.sync(getCurrentFilePath(editor), {}, '.prettierignore');
getPrettierInstance(editor).getFileInfo.sync(getCurrentFilePath(editor), {
// $FlowIssue: we know filepath is defined at this point
ignorePath: getNearestPrettierignorePath(getCurrentFilePath(editor)),
});

const doesFileInfoIndicateFormattable = (fileInfo: Prettier$FileInfo): boolean =>
fileInfo && !fileInfo.ignored && !!fileInfo.inferredParser;
Expand Down
5 changes: 4 additions & 1 deletion src/helpers/isFileFormattable.test.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
jest.mock('./getPrettierInstance');
jest.mock('../editorInterface');
jest.mock('./general');

const isFileFormattable = require('./isFileFormattable');
const buildMockEditor = require('../../tests/mocks/textEditor');
const getPrettierInstance = require('./getPrettierInstance');
const { getCurrentFilePath, isCurrentFilePathDefined } = require('../editorInterface');
const { findCachedFromFilePath } = require('./general');

const mockEditor = buildMockEditor();

beforeEach(() => {
isCurrentFilePathDefined.mockImplementation(() => true);
getCurrentFilePath.mockImplementation(() => 'xyz.js');
findCachedFromFilePath.mockImplementation(() => '.prettierignore');
});

const mockGetFileInfoSyncFunc = syncFunc =>
Expand All @@ -23,7 +26,7 @@ it('calls prettier.getFileInfo.sync with the proper arguments', () => {

isFileFormattable(mockEditor);

expect(sync).toHaveBeenCalledWith('xyz.js', {}, '.prettierignore');
expect(sync).toHaveBeenCalledWith('xyz.js', { ignorePath: '.prettierignore' });
});

it('returns true if the file is formattable', () => {
Expand Down

0 comments on commit dd4c849

Please sign in to comment.