Skip to content

Commit

Permalink
fix: update resolveConfig to work with Prettier 3
Browse files Browse the repository at this point in the history
The `sync` function is gone and only async version exists.
  • Loading branch information
jkrehm-cvna committed Oct 27, 2023
1 parent da73add commit b53b2b4
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 32 deletions.
4 changes: 1 addition & 3 deletions src/__mocks__/prettier.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ const mockFormatSpy = jest.fn(mockFormat);

Object.assign(prettier, {
format: mockFormatSpy,
resolveConfig: {
sync: jest.fn(prettier.resolveConfig.sync)
}
resolveConfig: jest.fn()
});

function mockFormat(...args) {
Expand Down
26 changes: 4 additions & 22 deletions src/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ beforeEach(() => {
eslintMock.mock.lintText.mockClear();
eslintMock.mock.calculateConfigForFile.mockClear();
prettierMock.format.mockClear();
prettierMock.resolveConfig.sync.mockClear();
prettierMock.resolveConfig.mockClear();
fsMock.readFileSync.mockClear();
loglevelMock.mock.clearAll();
global.__PRETTIER_ESLINT_TEST_STATE__ = {};
Expand Down Expand Up @@ -376,33 +376,15 @@ test('logs error if it cannot read the file from the filePath', async () => {
fsMock.readFileSync = originalMock;
});

test('calls prettier.resolveConfig.sync with the file path', async () => {
test('calls prettier.resolveConfig with the file path', async () => {
const filePath = require.resolve('../../tests/fixtures/paths/foo.js');
await format({
filePath,
text: defaultInputText(),
eslintConfig: getESLintConfigWithDefaultRules()
});
expect(prettierMock.resolveConfig.sync).toHaveBeenCalledTimes(1);
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;
expect(prettierMock.resolveConfig).toHaveBeenCalledTimes(1);
expect(prettierMock.resolveConfig).toHaveBeenCalledWith(filePath);
});

test('does not raise an error if prettier.resolveConfig is not defined', async () => {
Expand Down
10 changes: 3 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ async function format(options) {
// Let prettier infer the parser using the filepath, if present. Otherwise
// assume the file is JS and default to the babel parser.
filePath ? { filepath: filePath } : { parser: 'babel' },
getPrettierConfig(filePath, prettierPath),
await getPrettierConfig(filePath, prettierPath) || {},
options.prettierOptions
);

Expand Down Expand Up @@ -298,12 +298,8 @@ async function getESLintConfig(filePath, eslintPath, eslintOptions) {

function getPrettierConfig(filePath, prettierPath) {
const prettier = requireModule(prettierPath, 'prettier');
return (
(prettier.resolveConfig &&
prettier.resolveConfig.sync &&
prettier.resolveConfig.sync(filePath)) ||
{}
);

return prettier.resolveConfig && prettier.resolveConfig(filePath);
}

function getModulePath(filePath = __filename, moduleName) {
Expand Down

0 comments on commit b53b2b4

Please sign in to comment.