Skip to content

Commit

Permalink
fix: handle typedPackageImport
Browse files Browse the repository at this point in the history
  • Loading branch information
gajus committed Aug 30, 2023
1 parent 3957a50 commit 8fddf7d
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 10 deletions.
14 changes: 4 additions & 10 deletions src/rules/requireExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,19 +186,13 @@ const handleAliasPath = (
return true;
}

const moduleRoot = findDirectory(resolvedImportPath, 'package.json', '/')
// This is a workaround for resolveImport resolving to @types/ of the package.
// This is going to fail if the actual package is not in the parent directory.
?.replace('/@types/', '/');
const moduleRoot = findDirectory(resolvedImportPath, 'package.json', '/');

// This would be an import from node_modules or a linked package.
if (moduleRoot) {
const moduleName = readPackageJson(
resolve(moduleRoot, 'package.json'),
).name;
const tail = moduleRoot.split('/node_modules/').pop();

if (importPath === moduleName) {
return true;
if (tail === importPath || tail === '@types/' + importPath) {
return false;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
// Note that this resolves to @types/chance, which is a TypeScript declaration file.
// Compare this test to the "typedPackageImport" test which imports a typed dependency.
import { Chance } from 'chance';
3 changes: 3 additions & 0 deletions tests/fixtures/requireExtension/typedPackageImport/subject.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Note that this resolves to roarr, which is a package with TypeScript types.
// Compare this test to the "packageTypesImport" test which imports dependency's types.
import { Roarr } from 'roarr';
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"compilerOptions": {
"baseUrl": "."
},
"include": [
"."
]
}
1 change: 1 addition & 0 deletions tests/rules/requireExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ ruleTester.run('require-extension', rule, {
validTest('pathsImportWithExtension'),
validTest('relativeImportIgnoreUnknownExtensions'),
validTest('relativeImportWithExtension'),
validTest('typedPackageImport'),
validTest('packageTypesImport'),
],
});

0 comments on commit 8fddf7d

Please sign in to comment.