From 103cc517ef5c2bdd8e043dba5a8facf860d0f6be Mon Sep 17 00:00:00 2001 From: Jonathan Rehm Date: Fri, 27 Oct 2023 16:18:20 -0700 Subject: [PATCH] fix `resolveConfig` to work with Prettier 3 (#942) Co-authored-by: Jonathan Rehm Co-authored-by: JounQin --- .all-contributorsrc | 7 +++++++ .changeset/great-cups-sparkle.md | 5 +++++ README.md | 1 + src/__mocks__/prettier.js | 4 +--- src/__tests__/index.js | 26 ++++---------------------- src/__tests__/utils.js | 21 +++++++-------------- src/index.js | 9 ++------- 7 files changed, 27 insertions(+), 46 deletions(-) create mode 100644 .changeset/great-cups-sparkle.md diff --git a/.all-contributorsrc b/.all-contributorsrc index e9eefd18..68f992b6 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -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", diff --git a/.changeset/great-cups-sparkle.md b/.changeset/great-cups-sparkle.md new file mode 100644 index 00000000..08ab0906 --- /dev/null +++ b/.changeset/great-cups-sparkle.md @@ -0,0 +1,5 @@ +--- +'prettier-eslint': patch +--- + +fix: update `resolveConfig` to work with Prettier 3 diff --git a/README.md b/README.md index 59b62cc8..8a90eaf0 100644 --- a/README.md +++ b/README.md @@ -307,6 +307,7 @@ Thanks goes to these people ([emoji key][emojis]): Rebecca Vest
Rebecca Vest

💻 Chris Bobbe
Chris Bobbe

🐛 💻 JounQin
JounQin

💬 💻 🎨 📖 🤔 🚇 🚧 🔌 📆 👀 ⚠️ 🔧 + Jonathan Rehm
Jonathan Rehm

💻 diff --git a/src/__mocks__/prettier.js b/src/__mocks__/prettier.js index 25c77e92..52362e84 100644 --- a/src/__mocks__/prettier.js +++ b/src/__mocks__/prettier.js @@ -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) { diff --git a/src/__tests__/index.js b/src/__tests__/index.js index 3ef58b07..514f5e75 100644 --- a/src/__tests__/index.js +++ b/src/__tests__/index.js @@ -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__ = {}; @@ -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 () => { diff --git a/src/__tests__/utils.js b/src/__tests__/utils.js index 9fef9680..62f1fa69 100644 --- a/src/__tests__/utils.js +++ b/src/__tests__/utils.js @@ -190,8 +190,7 @@ getPrettierOptionsFromESLintRulesTests.forEach( const { prettier } = getOptionsForFormatting( { rules }, prettierOptions, - fallbackPrettierOptions, - eslintPath + fallbackPrettierOptions ); expect(prettier).toMatchObject(options); }); @@ -221,8 +220,7 @@ test('if fallbacks are provided, those are preferred over disabled eslint rules' {}, { singleQuote: true - }, - eslintPath + } ); expect(prettier).toMatchObject({ singleQuote: true }); }); @@ -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 }); }); @@ -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({ @@ -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, @@ -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 }); }); @@ -299,8 +293,7 @@ test('Turn off unfixable rules', () => { } }, undefined, - undefined, - eslintPath + undefined ); expect(eslint).toMatchObject({ diff --git a/src/index.js b/src/index.js index 0d4bff22..d227dfdb 100644 --- a/src/index.js +++ b/src/index.js @@ -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 ); @@ -298,12 +298,7 @@ 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) {