Skip to content

Commit

Permalink
fix: resolve non-relative imports being evaluated as a potential rela…
Browse files Browse the repository at this point in the history
…tive import

This both conflicted with the use of baseUrl absolute imports
and was in general a bug the resolution logic.

Resolves #16
  • Loading branch information
Limegrass committed Apr 17, 2024
1 parent 44445f0 commit 3512285
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/rules/import-alias.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ function isPermittedRelativeImport(
filepath: string,
projectBaseDir: string
) {
const isRelativeImport =
importModuleName.length > 0 && importModuleName[0] !== ".";
if (isRelativeImport) {
return false;
}

const importParts = importModuleName.split("/");
const relativeDepth = importParts.filter(
(moduleNamePart) => moduleNamePart === ".."
Expand Down
115 changes: 115 additions & 0 deletions tests/rules/import-alias.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,25 @@ function runTests(platform: "win32" | "posix") {
],
filename: "src/foo/index.ts",
},

// doesn't conflict with relativeImportOverrides
{
code: `export * from "src/potato";`,
errors: 1,
options: [
{
isAllowBaseUrlResolvedImport: false,
relativeImportOverrides: [
{
path: ".",
depth: 0,
},
],
},
],
filename: "src/rules/test.ts",
output: `export * from "#src/potato";`,
},
],
});

Expand Down Expand Up @@ -822,6 +841,25 @@ function runTests(platform: "win32" | "posix") {
],
filename: "src/foo/index.ts",
},

// doesn't conflict with relativeImportOverrides
{
code: `export { Potato } from "src/potato";`,
errors: 1,
options: [
{
isAllowBaseUrlResolvedImport: false,
relativeImportOverrides: [
{
path: ".",
depth: 0,
},
],
},
],
filename: "src/rules/test.ts",
output: `export { Potato } from "#src/potato";`,
},
],
});

Expand Down Expand Up @@ -1183,6 +1221,25 @@ function runTests(platform: "win32" | "posix") {
],
filename: "src/foo/index.ts",
},

// doesn't conflict with relativeImportOverrides
{
code: `import { Potato } from "src/potato";`,
errors: 1,
options: [
{
isAllowBaseUrlResolvedImport: false,
relativeImportOverrides: [
{
path: ".",
depth: 0,
},
],
},
],
filename: "src/rules/test.ts",
output: `import { Potato } from "#src/potato";`,
},
],
});

Expand Down Expand Up @@ -1544,6 +1601,25 @@ function runTests(platform: "win32" | "posix") {
],
filename: "src/foo/index.ts",
},

// doesn't conflict with relativeImportOverrides
{
code: `require("src/potato");`,
errors: 1,
options: [
{
isAllowBaseUrlResolvedImport: false,
relativeImportOverrides: [
{
path: ".",
depth: 0,
},
],
},
],
filename: "src/rules/test.ts",
output: `require("#src/potato");`,
},
],
});

Expand Down Expand Up @@ -1900,6 +1976,25 @@ function runTests(platform: "win32" | "posix") {
],
filename: "src/foo/index.ts",
},

// doesn't conflict with relativeImportOverrides
{
code: `jest.mock("src/potato");`,
errors: 1,
options: [
{
isAllowBaseUrlResolvedImport: false,
relativeImportOverrides: [
{
path: ".",
depth: 0,
},
],
},
],
filename: "src/rules/test.ts",
output: `jest.mock("#src/potato");`,
},
],
});

Expand Down Expand Up @@ -2295,6 +2390,26 @@ function runTests(platform: "win32" | "posix") {
],
filename: "src/foo/index.ts",
},

// doesn't conflict with relativeImportOverrides
{
code: `potato("src/potato");`,
errors: 1,
options: [
{
aliasImportFunctions: ["potato"],
isAllowBaseUrlResolvedImport: false,
relativeImportOverrides: [
{
path: ".",
depth: 0,
},
],
},
],
filename: "src/rules/test.ts",
output: `potato("#src/potato");`,
},
],
}
);
Expand Down

0 comments on commit 3512285

Please sign in to comment.