Skip to content

Commit

Permalink
fix(backward-compatibility): don't throw if resolveConfig.sync is und…
Browse files Browse the repository at this point in the history
…efined (#127)

Some people still want to use older versions of prettier which does not have the
`resolveConfig.sync` method defined. In this case we will now just pretend like there was a prettier
config but that it was empty.
  • Loading branch information
robwise authored and Kent C. Dodds committed Sep 20, 2017
1 parent efb0a2f commit 977977a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
18 changes: 18 additions & 0 deletions src/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down
7 changes: 6 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 977977a

Please sign in to comment.