Skip to content

Commit 2f28d85

Browse files
fix: only match path alias once per moduleDeclaration (#187)
* Add test cases for path matching Co-authored-by: Lleyton Morris <lmorris@nexigen.digital>
1 parent a8cb43b commit 2f28d85

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

packages/skott/src/modules/walkers/ecmascript/typescript/path-alias.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { createRequire } from "node:module";
22
import path from "node:path";
33

4-
import { pipe, Option } from "effect";
4+
import { Option, pipe } from "effect";
55
import JSON5 from "json5";
66
import 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.` +

packages/skott/test/unit/ecmascript/typescript.spec.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff 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": `

0 commit comments

Comments
 (0)