From c038e797bbaf1dc58291a32af07d01a1fbe953fa Mon Sep 17 00:00:00 2001 From: adamviktora <84135613+adamviktora@users.noreply.github.com> Date: Thu, 14 Mar 2024 18:26:43 +0100 Subject: [PATCH] fix(getFromPackage): add parameter to filter imports and exports by names (#606) * fix(getFromPackage): add parameter to filter imports and exports by names In moveSpecifiers helper, getFromPackage was called with third parameter and filtering by names was expected. * refactor(getFromPackage): use const and early return --- .../src/rules/helpers/getFromPackage.ts | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/packages/eslint-plugin-pf-codemods/src/rules/helpers/getFromPackage.ts b/packages/eslint-plugin-pf-codemods/src/rules/helpers/getFromPackage.ts index e03da22ef..4a435fc6f 100644 --- a/packages/eslint-plugin-pf-codemods/src/rules/helpers/getFromPackage.ts +++ b/packages/eslint-plugin-pf-codemods/src/rules/helpers/getFromPackage.ts @@ -54,7 +54,11 @@ function getSpecifiers< ); } -export function getFromPackage(context: Rule.RuleContext, packageName: string) { +export function getFromPackage( + context: Rule.RuleContext, + packageName: string, + specifierNames?: string[] +) { const astBody = context.getSourceCode().ast.body; const imports = getSpecifiers( @@ -68,5 +72,16 @@ export function getFromPackage(context: Rule.RuleContext, packageName: string) { packageName ); - return { imports, exports }; + if (!specifierNames) { + return { imports, exports }; + } + + const specifiedImports = imports.filter((specifier) => + specifierNames.includes(specifier.imported.name) + ); + const specifiedExports = exports.filter((specifier) => + specifierNames.includes(specifier.exported.name) + ); + + return { imports: specifiedImports, exports: specifiedExports }; }