From 1dd958bae45dea4f70848260052c029b1e8a63ac Mon Sep 17 00:00:00 2001 From: Michael Ridgway <191142+mridgway@users.noreply.github.com> Date: Mon, 8 Apr 2024 17:12:04 -0700 Subject: [PATCH] fix(plugin/babel): bypass directly required plugins or presets in babel config --- packages/knip/fixtures/plugins/babel/.babelrc.js | 2 ++ packages/knip/src/plugins/babel/index.ts | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/knip/fixtures/plugins/babel/.babelrc.js b/packages/knip/fixtures/plugins/babel/.babelrc.js index 3e2145357..70ad5bce4 100644 --- a/packages/knip/fixtures/plugins/babel/.babelrc.js +++ b/packages/knip/fixtures/plugins/babel/.babelrc.js @@ -37,6 +37,8 @@ const config = { }, }, ], + // Simulates a required plugin, should not be added to the unspecified plugins array + function myCustomPlugin() {} ], }; diff --git a/packages/knip/src/plugins/babel/index.ts b/packages/knip/src/plugins/babel/index.ts index ed9e0154f..98942f6db 100644 --- a/packages/knip/src/plugins/babel/index.ts +++ b/packages/knip/src/plugins/babel/index.ts @@ -16,7 +16,7 @@ const isEnabled: IsPluginEnabled = ({ dependencies }) => hasDependency(dependenc const config = ['babel.config.{json,js,cjs,mjs,cts}', '.babelrc.{json,js,cjs,mjs,cts}', '.babelrc', 'package.json']; const getName = (value: string | [string, unknown]) => - typeof value === 'string' ? [value] : Array.isArray(value) ? [value[0]] : []; + [Array.isArray(value) ? value[0] : value].filter(name => typeof name === 'string'); export const getDependenciesFromConfig = (config: BabelConfigObj): string[] => { const presets = config.presets?.flatMap(getName).map(name => resolveName(name, 'preset')) ?? [];