Skip to content

Commit 7a91daf

Browse files
fix: resolve modules if folder contains a package.json file (#187)
Co-authored-by: JounQin <admin@1stg.me>
1 parent 232cf21 commit 7a91daf

File tree

5 files changed

+20
-2
lines changed

5 files changed

+20
-2
lines changed

.changeset/rare-plants-wait.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"eslint-import-resolver-typescript": patch
3+
---
4+
5+
fix: resolve modules if folder contains a package.json file

src/index.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,10 @@ const isFile = (path?: string | undefined): path is string => {
250250
}
251251
}
252252

253+
const isModule = (modulePath?: string | undefined): modulePath is string => {
254+
return !!modulePath && isFile(path.resolve(modulePath, 'package.json'))
255+
}
256+
253257
/**
254258
* @param {string} source the module to resolve; i.e './some-module'
255259
* @param {string} file the importing file's full path; i.e. '/usr/local/bin/file.js'
@@ -267,7 +271,7 @@ function getMappedPath(
267271
const originalExtensions = extensions
268272
extensions = ['', ...extensions]
269273

270-
let paths: string[] | undefined = []
274+
let paths: Array<string | undefined> | undefined = []
271275

272276
if (RELATIVE_PATH_PATTERN.test(source)) {
273277
const resolved = path.resolve(path.dirname(file), source)
@@ -283,7 +287,7 @@ function getMappedPath(
283287
]),
284288
)
285289
.flat(2)
286-
.filter(isFile)
290+
.filter(mappedPath => isFile(mappedPath) || isModule(mappedPath))
287291
}
288292

289293
if (retry && paths.length === 0) {

tests/withPaths/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import 'folder/tsxImportee'
1010
import 'folder/subfolder/tsImportee'
1111
import 'folder/subfolder/tsxImportee'
1212

13+
// import module with typings set in package.json
14+
import 'folder/module'
15+
1316
// import from node_module
1417
import 'typescript'
1518
import 'dummy.js'

tests/withPaths/module/module.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export {}

tests/withPaths/module/package.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"type": "commonjs",
3+
"typings": "./module.d.ts",
4+
"private": true
5+
}

0 commit comments

Comments
 (0)