Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: resolve modules if folder contains a package.json file #187

Merged
merged 4 commits into from
Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/rare-plants-wait.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-import-resolver-typescript": patch
---

fix: resolve modules if folder contains a package.json file
8 changes: 6 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -267,7 +271,7 @@ function getMappedPath(
const originalExtensions = extensions
extensions = ['', ...extensions]

let paths: string[] | undefined = []
let paths: Array<string | undefined> | undefined = []

if (RELATIVE_PATH_PATTERN.test(source)) {
const resolved = path.resolve(path.dirname(file), source)
Expand All @@ -283,7 +287,7 @@ function getMappedPath(
]),
)
.flat(2)
.filter(isFile)
.filter(mappedPath => isFile(mappedPath) || isModule(mappedPath))
}

if (retry && paths.length === 0) {
Expand Down
3 changes: 3 additions & 0 deletions tests/withPaths/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
1 change: 1 addition & 0 deletions tests/withPaths/module/module.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {}
5 changes: 5 additions & 0 deletions tests/withPaths/module/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type": "commonjs",
"typings": "./module.d.ts",
"private": true
}