Skip to content

Commit

Permalink
Add support for thenable dynamic import w/ destructured arg (resolves #…
Browse files Browse the repository at this point in the history
  • Loading branch information
webpro committed Jun 21, 2024
1 parent 67404f3 commit 1343826
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
3 changes: 3 additions & 0 deletions packages/knip/fixtures/imports/dir/import-f.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const dynamic = () => 1;

export const named = { default: 1, identifier13: 13, identifier14: 14 };
4 changes: 3 additions & 1 deletion packages/knip/fixtures/imports/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ const fn = (_: any) => {};
const topLevel = await import('./top-level-await-import');
const { top } = await import('./top-level-await-import');

const dynamic = () => import('./dir/import-b').then(m => m.dynamic);
const dynamicB = () => import('./dir/import-b').then(m => m.dynamic);

const dynamicF = () => import('./dir/import-f').then(({ dynamic, named: renamed }) => [dynamic, renamed]);

import('./top-level-side-effects-call');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ export default visit(
return { identifier, specifier, pos };
});
}
} else if (ts.isObjectBindingPattern(arg.name)) {
// Pattern: import('specifier').then({ identifier } => identifier);
return arg.name.elements.map(element => {
const identifier = (element.propertyName ?? element.name).getText();
const alias = element.propertyName ? element.name.getText() : undefined;
return {
identifier,
alias,
specifier,
pos: element.pos,
};
});
}
}
return { identifier: 'default', specifier, pos };
Expand Down
4 changes: 2 additions & 2 deletions packages/knip/test/imports.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ test('Support various ways to import modules', async () => {

assert.deepEqual(counters, {
...baseCounters,
processed: 23,
total: 23,
processed: 24,
total: 24,
});
});

0 comments on commit 1343826

Please sign in to comment.