From da73add8af39fe2c1407c46f466ec4c2f918e1bc Mon Sep 17 00:00:00 2001 From: Jonathan Rehm Date: Fri, 27 Oct 2023 10:22:54 -0700 Subject: [PATCH 1/5] test: remove argument that function does not consume --- src/__tests__/utils.js | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) 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({ From 75b2ccc6de1e18033693524e12144bf3bfee5ba7 Mon Sep 17 00:00:00 2001 From: Jonathan Rehm Date: Fri, 27 Oct 2023 13:09:36 -0700 Subject: [PATCH 2/5] fix: update `resolveConfig` to work with Prettier 3 The `sync` function is gone and only async version exists. --- .changeset/great-cups-sparkle.md | 5 +++++ src/__mocks__/prettier.js | 4 +--- src/__tests__/index.js | 26 ++++---------------------- src/index.js | 10 +++------- 4 files changed, 13 insertions(+), 32 deletions(-) create mode 100644 .changeset/great-cups-sparkle.md 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/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/index.js b/src/index.js index 0d4bff22..26931a7f 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,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) { From 86639d4b663502e9291671ccdb0f06e6c30a4073 Mon Sep 17 00:00:00 2001 From: Jonathan Rehm Date: Fri, 27 Oct 2023 13:12:21 -0700 Subject: [PATCH 3/5] chore: update all-contributors --- .all-contributorsrc | 7 +++++++ README.md | 1 + 2 files changed, 8 insertions(+) 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/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

💻 From f989a9ce5d4ba72f6764046302a831544a0fa3fb Mon Sep 17 00:00:00 2001 From: JounQin Date: Sat, 28 Oct 2023 07:09:06 +0800 Subject: [PATCH 4/5] Update src/index.js --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 26931a7f..91abff84 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' }, - await getPrettierConfig(filePath, prettierPath) || {}, + await getPrettierConfig(filePath, prettierPath), options.prettierOptions ); From 7f93b14f5a7c5b677bffafb2dc3c60d3644cdf17 Mon Sep 17 00:00:00 2001 From: JounQin Date: Sat, 28 Oct 2023 07:09:42 +0800 Subject: [PATCH 5/5] Update src/index.js --- src/index.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/index.js b/src/index.js index 91abff84..d227dfdb 100644 --- a/src/index.js +++ b/src/index.js @@ -298,7 +298,6 @@ async function getESLintConfig(filePath, eslintPath, eslintOptions) { function getPrettierConfig(filePath, prettierPath) { const prettier = requireModule(prettierPath, 'prettier'); - return prettier.resolveConfig && prettier.resolveConfig(filePath); }