diff --git a/.changeset/rare-plants-wait.md b/.changeset/rare-plants-wait.md new file mode 100644 index 0000000..bd40eb2 --- /dev/null +++ b/.changeset/rare-plants-wait.md @@ -0,0 +1,5 @@ +--- +"eslint-import-resolver-typescript": patch +--- + +fix: resolve modules if folder contains a package.json file diff --git a/src/index.ts b/src/index.ts index b8e1707..b7656f9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -250,6 +250,10 @@ const isFile = (path?: string | undefined): path is string => { } } +const isModule = (modulePath?: string | undefined): modulePath is string => { + return !!modulePath && isFile(path.resolve(modulePath, 'package.json')) +} + /** * @param {string} source the module to resolve; i.e './some-module' * @param {string} file the importing file's full path; i.e. '/usr/local/bin/file.js' @@ -267,7 +271,7 @@ function getMappedPath( const originalExtensions = extensions extensions = ['', ...extensions] - let paths: string[] | undefined = [] + let paths: Array | undefined = [] if (RELATIVE_PATH_PATTERN.test(source)) { const resolved = path.resolve(path.dirname(file), source) @@ -283,7 +287,7 @@ function getMappedPath( ]), ) .flat(2) - .filter(isFile) + .filter(mappedPath => isFile(mappedPath) || isModule(mappedPath)) } if (retry && paths.length === 0) { diff --git a/tests/withPaths/index.ts b/tests/withPaths/index.ts index 8d011e9..594e79d 100644 --- a/tests/withPaths/index.ts +++ b/tests/withPaths/index.ts @@ -10,6 +10,9 @@ import 'folder/tsxImportee' import 'folder/subfolder/tsImportee' import 'folder/subfolder/tsxImportee' +// import module with typings set in package.json +import 'folder/module' + // import from node_module import 'typescript' import 'dummy.js' diff --git a/tests/withPaths/module/module.d.ts b/tests/withPaths/module/module.d.ts new file mode 100644 index 0000000..336ce12 --- /dev/null +++ b/tests/withPaths/module/module.d.ts @@ -0,0 +1 @@ +export {} diff --git a/tests/withPaths/module/package.json b/tests/withPaths/module/package.json new file mode 100644 index 0000000..f69fe0b --- /dev/null +++ b/tests/withPaths/module/package.json @@ -0,0 +1,5 @@ +{ + "type": "commonjs", + "typings": "./module.d.ts", + "private": true +}