Skip to content

Commit

Permalink
Minor refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
webpro committed Jun 27, 2024
1 parent c0a0f0b commit efafcdc
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions packages/knip/src/typescript/getImportsAndExports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,11 @@ const getImportsAndExports = (
}

// Skip some work by handling only top-level import/export assignments
const isTopLevel = node.parent === sourceFile || node.parent?.parent === sourceFile;
const isTopLevel =
node.parent &&
('commonJsModuleIndicator' in sourceFile
? node.parent.parent === sourceFile || node.parent === sourceFile
: node.parent === sourceFile);

if (isTopLevel) {
for (const visitor of visitors.import) {
Expand Down Expand Up @@ -408,8 +412,8 @@ const getImportsAndExports = (
// biome-ignore lint/suspicious/noAssignInExpressions: <explanation>
while (index < text.length && (index = text.indexOf(id, index)) !== -1) {
if (!isIdChar(text.charAt(index - 1)) && !isIdChar(text.charAt(index + id.length))) {
const isDeclaration = index === item.pos || index === item.pos + 1; // off-by-one from `stripQuotes`
if (!isDeclaration) {
const isExportDeclaration = index === item.pos || index === item.pos + 1; // off-by-one from `stripQuotes`
if (!isExportDeclaration) {
// @ts-expect-error ts.getTokenAtPosition is internal fn
const symbol = typeChecker.getSymbolAtLocation(ts.getTokenAtPosition(sourceFile, index));
if (symbol) {
Expand Down Expand Up @@ -439,8 +443,11 @@ const getImportsAndExports = (
}
};

const isSetRefs = ignoreExportsUsedInFile;
for (const item of exports.values()) {
if (ignoreExportsUsedInFile) setRefs(item);
if (isSetRefs === true || (typeof isSetRefs === 'object' && item.type !== 'unknown' && !!isSetRefs[item.type])) {
setRefs(item);
}
for (const member of item.members) {
setRefs(member);
member.symbol = undefined;
Expand Down

0 comments on commit efafcdc

Please sign in to comment.