File tree Expand file tree Collapse file tree 2 files changed +47
-4
lines changed
src/modules/walkers/ecmascript/typescript Expand file tree Collapse file tree 2 files changed +47
-4
lines changed Original file line number Diff line number Diff line change 11import { createRequire } from "node:module" ;
22import path from "node:path" ;
33
4- import { pipe , Option } from "effect" ;
4+ import { Option , pipe } from "effect" ;
55import JSON5 from "json5" ;
66import type { CompilerOptions } from "typescript" ;
77
@@ -32,9 +32,11 @@ function resolveAliasToRelativePath(
3232 `Extracting "${ moduleDeclaration } " base path using path alias base directory "${ baseAliasDirname } "`
3333 ) ;
3434
35- const modulePathWithoutAliasBaseDirname = moduleDeclaration . split (
36- baseAliasDirname . concat ( "/" )
37- ) [ 1 ] ;
35+ const baseDirWithSlash = baseAliasDirname . concat ( "/" ) ;
36+ const startIndex =
37+ moduleDeclaration . indexOf ( baseDirWithSlash ) + baseDirWithSlash . length ;
38+ const modulePathWithoutAliasBaseDirname =
39+ moduleDeclaration . substring ( startIndex ) ;
3840
3941 logger . info (
4042 `Attempt to map alias path to real file-system path.` +
Original file line number Diff line number Diff line change @@ -1215,6 +1215,46 @@ describe("When traversing a TypeScript project", () => {
12151215 }
12161216 } ) ;
12171217 } ) ;
1218+
1219+ it ( "should replace only the first occurrence when path alias appears multiple times in import path" , async ( ) => {
1220+ const tsConfig = {
1221+ compilerOptions : {
1222+ baseUrl : "." ,
1223+ paths : {
1224+ components : [ "src/components" ]
1225+ }
1226+ }
1227+ } ;
1228+
1229+ mountFakeFileSystem ( {
1230+ "index.ts" : `
1231+ import { Button } from "components/example/components/Button";
1232+ ` ,
1233+ "src/components/example/components/Button.ts" : `
1234+ export function Button(): string {}
1235+ ` ,
1236+ "tsconfig.json" : JSON . stringify ( tsConfig )
1237+ } ) ;
1238+
1239+ const { graph } = await buildSkottProjectUsingInMemoryFileExplorer ( {
1240+ entrypoint : "index.ts" ,
1241+ includeBaseDir : false ,
1242+ trackThirdParty : true
1243+ } ) ;
1244+
1245+ expect ( graph ) . to . be . deep . equal ( {
1246+ "index.ts" : {
1247+ adjacentTo : [ "src/components/example/components/Button.ts" ] ,
1248+ id : "index.ts" ,
1249+ body : fakeNodeBody
1250+ } ,
1251+ "src/components/example/components/Button.ts" : {
1252+ adjacentTo : [ ] ,
1253+ id : "src/components/example/components/Button.ts" ,
1254+ body : fakeNodeBody
1255+ }
1256+ } ) ;
1257+ } ) ;
12181258 } ) ;
12191259
12201260 describe ( "When providing a base tsconfig extending other configs" , ( ) => {
@@ -1339,6 +1379,7 @@ describe("When traversing a TypeScript project", () => {
13391379 ` ,
13401380 "src/foo.ts" : `
13411381 import './index.js';
1382+ import { Button } from "components/components/Button";
13421383 export const foo = { doSomething: () => 'Hello, world!' };
13431384 ` ,
13441385 "src/index.js" : `
You can’t perform that action at this time.
0 commit comments