Skip to content

Commit

Permalink
fix(getFromPackage): add parameter to filter imports and exports by n…
Browse files Browse the repository at this point in the history
…ames (#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
  • Loading branch information
adamviktora authored Mar 14, 2024
1 parent 7d61625 commit c038e79
Showing 1 changed file with 17 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<ImportDeclaration, ImportSpecifier>(
Expand All @@ -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 };
}

0 comments on commit c038e79

Please sign in to comment.