Skip to content

Commit

Permalink
Merge pull request #847 from gmeligio/types-nested-file
Browse files Browse the repository at this point in the history
fix: detect correctly package name when importing nested file in @types package
  • Loading branch information
rumpl committed Nov 9, 2023
2 parents adfeb8e + 4e1c1f4 commit 77fe0e2
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 8 deletions.
13 changes: 7 additions & 6 deletions src/detector/importDeclaration.js
Expand Up @@ -5,13 +5,14 @@ export default function detectImportDeclaration(node, deps) {
return [];
}

// TypeScript "import type X from 'foo'" - doesn't need to depend on the
// TypeScript "import type X from 'foo'" and "import type X from 'foo/bar'"- doesn't need to depend on the
// actual module, instead it can rely on `@types/<module>` instead.
if (
node.importKind === 'type' &&
deps.includes(`@types/${node.source.value}`)
) {
return [`@types/${node.source.value}`];

const packageName = node.source.value.split('/')[0];
const typesPackageName = `@types/${packageName}`;

if (node.importKind === 'type' && deps.includes(typesPackageName)) {
return [typesPackageName];
}

return extractInlineWebpack(node.source.value);
Expand Down
1 change: 1 addition & 0 deletions test/fake_modules/typescript/package.json
Expand Up @@ -4,6 +4,7 @@
"@types/org__org-pkg": "0.0.1",
"@types/react": "0.0.1",
"@types/node": "0.0.1",
"@types/another-typeless-module": "0.0.1",
"@types/typeless-module": "0.0.1",
"react": "0.0.1",
"ts-dep-1": "0.0.1",
Expand Down
6 changes: 4 additions & 2 deletions test/fake_modules/typescript/typeOnly.ts
@@ -1,5 +1,7 @@
import type { Foo } from 'typeless-module';
import type { Bar } from 'another-typeless-module/nested-declaration-file';

const bar: Foo = { prop: 'here' };
const foo: Foo = { prop: 'here' };
const bar: Bar = { prop: 'there' };

export default bar;
export default { foo, bar };
1 change: 1 addition & 0 deletions test/spec.js
Expand Up @@ -227,6 +227,7 @@ export default [
'@types/react': ['component.tsx'],
'@types/node': ['esnext.ts'],
'@types/org__org-pkg': ['esnext.ts'],
'@types/another-typeless-module': ['typeOnly.ts'],
'@types/typeless-module': ['typeOnly.ts'],
'@org/org-pkg': ['esnext.ts'],
'ts-dep-1': ['index.ts'],
Expand Down

0 comments on commit 77fe0e2

Please sign in to comment.