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

Remove redundant condition #69

Merged
merged 2 commits into from
Feb 13, 2021
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"test:multipleTsconfigs": "eslint --ext ts,tsx tests/multipleTsconfigs",
"test:withJsExtension": "node tests/withJsExtension/test.js && eslint --ext ts,tsx tests/withJsExtension",
"test:withPaths": "eslint --ext ts,tsx tests/withPaths",
"test:withPathsAndNestedBaseUrl": "eslint --ext ts,tsx tests/withPathsAndNestedBaseUrl",
"test:withQuerystring": "eslint --ext ts,tsx tests/withQuerystring",
"test:withoutPaths": "eslint --ext ts,tsx tests/withoutPaths",
"type-coverage": "type-coverage --cache --detail --ignore-catch --strict --update"
Expand Down
19 changes: 5 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export function resolve(
}

initMappers(options)
const mappedPath = getMappedPath(source, file)
const mappedPath = getMappedPath(source)
if (mappedPath) {
log('matched ts path:', mappedPath)
}
Expand Down Expand Up @@ -153,19 +153,15 @@ function removeJsExtension(id: string) {
}

let mappersBuildForOptions: TsResolverOptions
let mappers:
| Array<(source: string, file: string) => string | undefined>
| undefined
let mappers: Array<(source: string) => string | undefined> | undefined

/**
* @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'
* @returns The mapped path of the module or undefined
*/
function getMappedPath(source: string, file: string) {
const paths = mappers!
.map(mapper => mapper(source, file))
.filter(path => !!path)
function getMappedPath(source: string) {
const paths = mappers!.map(mapper => mapper(source)).filter(path => !!path)

if (paths.length > 1) {
log('found multiple matching ts paths:', paths)
Expand Down Expand Up @@ -230,12 +226,7 @@ function initMappers(options: TsResolverOptions) {
configLoaderResult.paths,
)

return (source: string, file: string) => {
// exclude files that are not part of the config base url
if (!file.includes(configLoaderResult.absoluteBaseUrl)) {
return
}

return (source: string) => {
// look for files based on setup tsconfig "paths"
return matchPath(
source,
Expand Down
1 change: 1 addition & 0 deletions tests/withPathsAndNestedBaseUrl/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('../baseEslintConfig')(__dirname)
2 changes: 2 additions & 0 deletions tests/withPathsAndNestedBaseUrl/other/bar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// import using tsconfig.json `baseUrl`
import 'foo'
2 changes: 2 additions & 0 deletions tests/withPathsAndNestedBaseUrl/root/foo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// import using tsconfig.json path mapping
import 'other/bar'
9 changes: 9 additions & 0 deletions tests/withPathsAndNestedBaseUrl/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"compilerOptions": {
"baseUrl": "root/",
"paths": {
"other/*": ["../other/*"]
}
},
"files": ["root/foo.ts", "other/bar.ts"]
}