Skip to content

Commit

Permalink
fix: remove redundant condition (#69)
Browse files Browse the repository at this point in the history
* Failing test

* Remove redundant condition

Fixes #68
  • Loading branch information
OliverJAsh authored and JounQin committed Feb 16, 2021
1 parent 82ef357 commit ba62e65
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 14 deletions.
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"]
}

0 comments on commit ba62e65

Please sign in to comment.