Skip to content

Commit

Permalink
adds support for package.json shared prettier configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisvltn authored Nov 27, 2023
1 parent 5c59fdc commit 7cdabc5
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
3 changes: 3 additions & 0 deletions packages/knip/fixtures/plugins/prettier/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"prettier": "@company/prettier-config"
}
5 changes: 5 additions & 0 deletions packages/knip/src/plugins/prettier/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ const findPrettierDependencies: GenericPluginCallback = async (configFilePath, {
? manifest.prettier
: await load(configFilePath);

// https://prettier.io/docs/en/configuration.html#sharing-configurations
if (typeof localConfig === 'string') {
return [localConfig];
}

return localConfig && Array.isArray(localConfig.plugins)
? localConfig.plugins.filter((plugin): plugin is string => typeof plugin === 'string')
: [];
Expand Down
8 changes: 7 additions & 1 deletion packages/knip/test/plugins/prettier.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@ import { resolve, join } from '../../src/util/path.js';

const cwd = resolve('fixtures/plugins/prettier');

test('Find dependencies in Prettier configuration', async () => {
test('Find dependencies in Prettier configuration (.prettierrc)', async () => {
const configFilePath = join(cwd, '.prettierrc');
const dependencies = await prettier.findDependencies(configFilePath, {});
assert.deepEqual(dependencies, ['prettier-plugin-xml']);
});

test('Find dependencies in Prettier configuration (package.json)', async () => {
const configFilePath = join(cwd, 'package.json');
const dependencies = await prettier.findDependencies(configFilePath, {});
assert.deepEqual(dependencies, ['@company/prettier-config']);
});

0 comments on commit 7cdabc5

Please sign in to comment.