-
-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: don't ignore file extensions defined in .prettierrc overrides (#111
- Loading branch information
Showing
6 changed files
with
68 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,20 @@ | ||
const path = require('path'); | ||
|
||
const prettierMock = { | ||
format: jest.fn().mockImplementation((input) => 'formatted:' + input), | ||
resolveConfig: { | ||
sync: jest.fn().mockImplementation((file) => ({ file })), | ||
}, | ||
getSupportInfo: jest.fn().mockReturnValue({ | ||
languages: [ | ||
{ | ||
name: 'JavaScript', | ||
extensions: ['.js'], | ||
}, | ||
{ | ||
name: 'Markdown', | ||
extensions: ['.md'], | ||
}, | ||
], | ||
}), | ||
getFileInfo: { | ||
sync: jest.fn().mockImplementation((file) => { | ||
const ext = path.extname(file); | ||
if (ext === '.js' || ext === '.md') { | ||
return { ignored: false, inferredParser: 'babel' }; | ||
} else { | ||
return { ignored: false, inferredParser: null }; | ||
} | ||
}), | ||
}, | ||
}; | ||
|
||
module.exports = prettierMock; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import isSupportedExtension from '../isSupportedExtension'; | ||
import prettier from 'prettier'; | ||
|
||
afterEach(() => jest.clearAllMocks()); | ||
|
||
test('return true when file with supported extension passed in', () => { | ||
expect(isSupportedExtension(true)('banana.js')).toEqual(true); | ||
expect(prettier.getFileInfo.sync).toHaveBeenCalledWith('banana.js', { | ||
resolveConfig: true, | ||
}); | ||
}); | ||
|
||
test('return false when file with not supported extension passed in', () => { | ||
expect(isSupportedExtension(true)('banana.txt')).toEqual(false); | ||
expect(prettier.getFileInfo.sync).toHaveBeenCalledWith('banana.txt', { | ||
resolveConfig: true, | ||
}); | ||
}); | ||
|
||
test('do not resolve config when false passed', () => { | ||
expect(isSupportedExtension(false)('banana.txt')).toEqual(false); | ||
expect(prettier.getFileInfo.sync).toHaveBeenCalledWith('banana.txt', { | ||
resolveConfig: false, | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,4 @@ | ||
import { getSupportInfo } from 'prettier'; | ||
import { getFileInfo } from 'prettier'; | ||
|
||
const extensions = getSupportInfo().languages.reduce( | ||
(prev, language) => prev.concat(language.extensions || []), | ||
[], | ||
); | ||
|
||
export default (file) => extensions.some((ext) => file.endsWith(ext)); | ||
export default (resolveConfig) => (file) => | ||
Boolean(getFileInfo.sync(file, { resolveConfig }).inferredParser); |