Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix resolveConfig to work with Prettier 3 #942

Merged
merged 5 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,13 @@
"test",
"tool"
]
},
{
"login": "jkrehm",
"name": "Jonathan Rehm",
"avatar_url": "https://avatars.githubusercontent.com/u/999845?v=4",
"profile": "https://jonathan.rehm.me/",
"contributions": ["bug", "code"]
}
],
"repoType": "github",
Expand Down
5 changes: 5 additions & 0 deletions .changeset/great-cups-sparkle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'prettier-eslint': patch
---

fix: update `resolveConfig` to work with Prettier 3
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ Thanks goes to these people ([emoji key][emojis]):
<td align="center" valign="top" width="14.28%"><a href="https://campcode.dev/"><img src="https://avatars.githubusercontent.com/u/10620169?v=4?s=100" width="100px;" alt="Rebecca Vest"/><br /><sub><b>Rebecca Vest</b></sub></a><br /><a href="https://github.com/prettier/prettier-eslint/commits?author=idahogurl" title="Code">💻</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/chrisbobbe"><img src="https://avatars.githubusercontent.com/u/22248748?v=4?s=100" width="100px;" alt="Chris Bobbe"/><br /><sub><b>Chris Bobbe</b></sub></a><br /><a href="https://github.com/prettier/prettier-eslint/issues?q=author%3Achrisbobbe" title="Bug reports">🐛</a> <a href="https://github.com/prettier/prettier-eslint/commits?author=chrisbobbe" title="Code">💻</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://www.1stg.me/"><img src="https://avatars.githubusercontent.com/u/8336744?v=4?s=100" width="100px;" alt="JounQin"/><br /><sub><b>JounQin</b></sub></a><br /><a href="#question-JounQin" title="Answering Questions">💬</a> <a href="https://github.com/prettier/prettier-eslint/commits?author=JounQin" title="Code">💻</a> <a href="#design-JounQin" title="Design">🎨</a> <a href="https://github.com/prettier/prettier-eslint/commits?author=JounQin" title="Documentation">📖</a> <a href="#ideas-JounQin" title="Ideas, Planning, & Feedback">🤔</a> <a href="#infra-JounQin" title="Infrastructure (Hosting, Build-Tools, etc)">🚇</a> <a href="#maintenance-JounQin" title="Maintenance">🚧</a> <a href="#plugin-JounQin" title="Plugin/utility libraries">🔌</a> <a href="#projectManagement-JounQin" title="Project Management">📆</a> <a href="https://github.com/prettier/prettier-eslint/pulls?q=is%3Apr+reviewed-by%3AJounQin" title="Reviewed Pull Requests">👀</a> <a href="https://github.com/prettier/prettier-eslint/commits?author=JounQin" title="Tests">⚠️</a> <a href="#tool-JounQin" title="Tools">🔧</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://jonathan.rehm.me/"><img src="https://avatars.githubusercontent.com/u/999845?v=4?s=100" width="100px;" alt="Jonathan Rehm"/><br /><sub><b>Jonathan Rehm</b></sub></a><br /><a href="https://github.com/prettier/prettier-eslint/commits?author=jkrehm" title="Code">💻</a></td>
</tr>
</tbody>
</table>
Expand Down
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
21 changes: 7 additions & 14 deletions src/__tests__/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,7 @@ getPrettierOptionsFromESLintRulesTests.forEach(
const { prettier } = getOptionsForFormatting(
{ rules },
prettierOptions,
fallbackPrettierOptions,
eslintPath
fallbackPrettierOptions
);
expect(prettier).toMatchObject(options);
});
Expand Down Expand Up @@ -221,8 +220,7 @@ test('if fallbacks are provided, those are preferred over disabled eslint rules'
{},
{
singleQuote: true
},
eslintPath
}
);
expect(prettier).toMatchObject({ singleQuote: true });
});
Expand All @@ -233,8 +231,7 @@ test('if fallbacks are provided, those are used if not found in eslint', () => {
undefined,
{
singleQuote: false
},
eslintPath
}
);
expect(prettier).toMatchObject({ singleQuote: false });
});
Expand All @@ -253,8 +250,7 @@ test('eslint max-len.tabWidth value should be used for tabWidth when tabs are us
}
},
undefined,
undefined,
eslintPath
undefined
);

expect(prettier).toMatchObject({
Expand All @@ -270,8 +266,7 @@ test('eslint config has only necessary properties', () => {
rules: { 'no-with': 'error', quotes: [2, 'single'] }
},
undefined,
undefined,
eslintPath
undefined
);
expect(eslint).toMatchObject({
fix: true,
Expand All @@ -284,8 +279,7 @@ test('useEslintrc is set to the given config value', () => {
const { eslint } = getOptionsForFormatting(
{ useEslintrc: true, rules: {} },
undefined,
undefined,
eslintPath
undefined
);
expect(eslint).toMatchObject({ fix: true, useEslintrc: true });
});
Expand All @@ -299,8 +293,7 @@ test('Turn off unfixable rules', () => {
}
},
undefined,
undefined,
eslintPath
undefined
);

expect(eslint).toMatchObject({
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)) ||
{}
);

JounQin marked this conversation as resolved.
Show resolved Hide resolved
return prettier.resolveConfig && prettier.resolveConfig(filePath);
}

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