Skip to content

Commit

Permalink
fix(no-missing-import): Resolve tsconfig paths relative to the tsconf…
Browse files Browse the repository at this point in the history
…ig (#343)

* test: Add failing test for 314

* fix: Resolve tsconfig paths relative to the tsconfig
  • Loading branch information
scagood authored Oct 9, 2024
1 parent 87fb484 commit 6cd7954
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/util/import-target.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,15 @@ function getTSConfigAliases(context) {

const paths = tsConfig?.config?.compilerOptions?.paths

if (paths == null) {
if (tsConfig?.path == null || paths == null) {
return
}

return Object.entries(paths).map(([name, alias]) => ({
name: removeTrailWildcard(name),
alias: removeTrailWildcard(alias),
alias: removeTrailWildcard(alias).map(relative =>
resolve(tsConfig.path, "..", relative)
),
}))
}

Expand Down
5 changes: 5 additions & 0 deletions tests/fixtures/no-missing/issue-314/module/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function x() {
return true;
}

export default x;
Empty file.
13 changes: 13 additions & 0 deletions tests/fixtures/no-missing/issue-314/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"compilerOptions": {
"baseUrl": "./src/",
"module": "node16",
"target": "es2022",
"moduleResolution": "node16",
"paths": {
"@module": ["./module/index.ts"]
},
"strict": true
},
"include": ["src", "module"]
}
8 changes: 8 additions & 0 deletions tests/lib/rules/no-missing-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,14 @@ ruleTester.run("no-missing-import", rule, {
filename: fixture("ts-paths/test.ts"),
code: "import before from '@wild/where.js';",
},
{
// name: "tsconfig - compilerOptions.paths - direct reference",
filename: fixture("issue-314/src/example.ts"),
code: "import('@module');",
languageOptions: {
ecmaVersion: "latest",
},
},

{
// name: 'Ensure type only packages can be imported',
Expand Down

0 comments on commit 6cd7954

Please sign in to comment.