diff --git a/src/__tests__/index.js b/src/__tests__/index.js index 6c22b2aa..571d887d 100644 --- a/src/__tests__/index.js +++ b/src/__tests__/index.js @@ -335,6 +335,24 @@ test('calls prettier.resolveConfig.sync with the file path', () => { expect(prettierMock.resolveConfig.sync).toHaveBeenCalledWith(filePath) }) +test('does not raise an error if prettier.resolveConfig.sync is not defined', () => { + const filePath = require.resolve('../../tests/fixtures/paths/foo.js') + const originalPrettierMockResolveConfigSync = prettierMock.resolveConfig.sync + prettierMock.resolveConfig.sync = undefined + + function callingFormat() { + return format({ + filePath, + text: defaultInputText(), + eslintConfig: getESLintConfigWithDefaultRules(), + }) + } + + expect(callingFormat).not.toThrowError() + + prettierMock.resolveConfig.sync = originalPrettierMockResolveConfigSync +}) + test('logs if there is a problem making the CLIEngine', () => { const error = new Error('fake error') eslintMock.CLIEngine.mockImplementation(() => { diff --git a/src/index.js b/src/index.js index 1888cd88..0d22134a 100644 --- a/src/index.js +++ b/src/index.js @@ -227,7 +227,12 @@ function getConfig(filePath, eslintPath) { function getPrettierConfig(filePath, prettierPath) { const prettier = requireModule(prettierPath, 'prettier') - return prettier.resolveConfig.sync(filePath) + + // NOTE: Backward-compatibility with old prettier versions (<1.7) + // that don't have ``resolveConfig.sync` method. + return typeof prettier.resolveConfig.sync === 'undefined' ? + {} : + prettier.resolveConfig.sync(filePath) } function requireModule(modulePath, name) {