From dd4c849352151412d423662c9b1759397de5472d Mon Sep 17 00:00:00 2001 From: Rob Wise Date: Wed, 22 Aug 2018 22:30:18 -0400 Subject: [PATCH] fix(prettierignore): properly ignore files again 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 --- dist/helpers/isFileFormattable.js | 8 +++++++- src/helpers/isFileFormattable.js | 9 ++++++++- src/helpers/isFileFormattable.test.js | 5 ++++- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/dist/helpers/isFileFormattable.js b/dist/helpers/isFileFormattable.js index 1adc13ee..fe93f4c9 100644 --- a/dist/helpers/isFileFormattable.js +++ b/dist/helpers/isFileFormattable.js @@ -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; diff --git a/src/helpers/isFileFormattable.js b/src/helpers/isFileFormattable.js index a9ebb6ab..b558e281 100644 --- a/src/helpers/isFileFormattable.js +++ b/src/helpers/isFileFormattable.js @@ -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; diff --git a/src/helpers/isFileFormattable.test.js b/src/helpers/isFileFormattable.test.js index 921b4707..3ab88eda 100644 --- a/src/helpers/isFileFormattable.test.js +++ b/src/helpers/isFileFormattable.test.js @@ -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 => @@ -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', () => {