Skip to content

Commit

Permalink
fix(extract): prevents classifying core modules as aliased-tsconfig-b…
Browse files Browse the repository at this point in the history
…ase-url
  • Loading branch information
sverweij committed Dec 13, 2023
1 parent 27485a4 commit c3b26a2
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
11 changes: 10 additions & 1 deletion src/extract/resolve/module-classifiers.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable max-lines */
import { isAbsolute, resolve as path_resolve } from "node:path";
import { join as posix_join } from "node:path/posix";
import { isMatch } from "picomatch";
Expand Down Expand Up @@ -219,6 +220,8 @@ function stripIndex(pModulePath) {
*
* https://www.typescriptlang.org/docs/handbook/modules/reference.html#baseurl
*
* Assumes the pModuleName is not relative.
*
* @param {string} pModuleName
* @param {string} pResolvedModuleName
* @param {string} pTSConfigBaseURL
Expand All @@ -228,7 +231,13 @@ function matchesTSConfigBaseURL(
pResolvedModuleName,
pTSConfigBaseURL,
) {
if (!pTSConfigBaseURL) {
// the pModuleName === pResolvedModuleName check is there to prevent
// false positives for core modules ('fs' resolved === 'fs') and modules that
// we couldn't resolve at all (e.g. 'this/does/not/exist' => 'this/does/not/exist')
//
// we could also check whether the moduleName is relative, but that's
// not efficient as that was already done before this function was called.
if (!pTSConfigBaseURL || pModuleName === pResolvedModuleName) {
return false;
}
// "If baseUrl is set, TypeScript will resolve non-relative module names
Expand Down
49 changes: 48 additions & 1 deletion test/extract/resolve/module-classifiers.get-alias-types.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ describe("[I] extract/resolve/module-classifiers - getAliasTypes", () => {
);
});

it("should return aliased and aliased-tsconfig for tsconfig alias", () => {
it("should return aliased, aliased-tsconfig and aliased-tsconfig-paths for tsconfig paths", () => {
const lManifest = {
name: "test",
version: "1.0.0",
Expand All @@ -312,4 +312,51 @@ describe("[I] extract/resolve/module-classifiers - getAliasTypes", () => {
["aliased", "aliased-tsconfig", "aliased-tsconfig-paths"],
);
});

it("should return aliased, aliased-tsconfig and aliased-tsconfig-base-url when it matches tsconfig base urls", () => {
const lManifest = {
name: "test",
version: "1.0.0",
dependencies: {},
};
const lTranspileOptions = {
tsConfig: {
options: {
baseUrl: "./src",
paths: {
"@tsconfig/*": ["./something-else/*"],
},
},
},
};
deepEqual(
getAliasTypes(
"package",
"src/package/index.js",
{},
lManifest,
lTranspileOptions,
),
["aliased", "aliased-tsconfig", "aliased-tsconfig-base-url"],
);
});

it("should NOT return aliased, aliased-tsconfig and aliased-tsconfig-base-url when it's a core module", () => {
const lManifest = {
name: "test",
version: "1.0.0",
dependencies: {},
};
const lTranspileOptions = {
tsConfig: {
options: {
baseUrl: "./",
paths: {
"@tsconfig/*": ["./something-else/*"],
},
},
},
};
deepEqual(getAliasTypes("fs", "fs", {}, lManifest, lTranspileOptions), []);
});
});

0 comments on commit c3b26a2

Please sign in to comment.