Skip to content

Commit

Permalink
Fix path alias resolution (#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
daniilsapa authored Nov 17, 2024
1 parent bfc5475 commit 9c1fec9
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/green-flies-exercise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@feature-sliced/steiger-plugin': patch
---

Fix path alias resolution when baseUrl is present in tsconfig
50 changes: 48 additions & 2 deletions packages/steiger-plugin-fsd/src/_lib/collect-related-ts-configs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,21 @@ function makeRelativePathAliasesAbsolute(
firstConfigWithPaths: CollectRelatedTsConfigsPayload,
) {
const { tsconfig: mergedConfig } = finalConfig
const {
compilerOptions: { paths, baseUrl },
} = mergedConfig
const absolutePaths: Record<string, Array<string>> = {}

if (!firstConfigWithPaths.tsconfigFile) {
return mergedConfig.compilerOptions.paths
}

for (const entries of Object.entries(mergedConfig.compilerOptions.paths)) {
for (const entries of Object.entries(paths)) {
const [key, paths] = entries as [key: string, paths: Array<string>]
absolutePaths[key] = paths.map((relativePath: string) =>
resolve(dirname(firstConfigWithPaths.tsconfigFile!), relativePath),
baseUrl
? resolve(dirname(firstConfigWithPaths.tsconfigFile!), baseUrl, relativePath)
: resolve(dirname(firstConfigWithPaths.tsconfigFile!), relativePath),
)
}

Expand Down Expand Up @@ -245,4 +250,45 @@ if (import.meta.vitest) {

expect(collectRelatedTsConfigs(payload)).toEqual(expectedResult)
})

test('correctly resolves paths if baseUrl is set', () => {
const payload: CollectRelatedTsConfigsPayload = {
tsconfigFile: resolve(joinFromRoot('user', 'projects', 'project-0', 'tsconfig.json')),
tsconfig: {
compilerOptions: {
baseUrl: './src',
paths: {
'~': ['./'],
'~/*': ['./*'],
},
strict: true,
noUncheckedIndexedAccess: false,
forceConsistentCasingInFileNames: true,
noImplicitOverride: true,
module: 'ESNext',
noEmit: true,
},
},
}

const expectedResult = [
{
compilerOptions: {
baseUrl: './src',
paths: {
'~': [resolve(joinFromRoot('user', 'projects', 'project-0', 'src'))],
'~/*': [resolve(joinFromRoot('user', 'projects', 'project-0', 'src', '*'))],
},
strict: true,
noUncheckedIndexedAccess: false,
forceConsistentCasingInFileNames: true,
noImplicitOverride: true,
module: 'ESNext',
noEmit: true,
},
},
]

expect(collectRelatedTsConfigs(payload)).toEqual(expectedResult)
})
}

0 comments on commit 9c1fec9

Please sign in to comment.