From 3512285ca5940d808416154f4e915e58c513af2c Mon Sep 17 00:00:00 2001 From: limegrass Date: Tue, 16 Apr 2024 18:33:45 -0700 Subject: [PATCH] fix: resolve non-relative imports being evaluated as a potential relative import This both conflicted with the use of baseUrl absolute imports and was in general a bug the resolution logic. Resolves #16 --- src/rules/import-alias.ts | 6 ++ tests/rules/import-alias.test.ts | 115 +++++++++++++++++++++++++++++++ 2 files changed, 121 insertions(+) diff --git a/src/rules/import-alias.ts b/src/rules/import-alias.ts index 873caea..1609c00 100644 --- a/src/rules/import-alias.ts +++ b/src/rules/import-alias.ts @@ -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 === ".." diff --git a/tests/rules/import-alias.test.ts b/tests/rules/import-alias.test.ts index ca46d85..b97b05d 100644 --- a/tests/rules/import-alias.test.ts +++ b/tests/rules/import-alias.test.ts @@ -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";`, + }, ], }); @@ -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";`, + }, ], }); @@ -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";`, + }, ], }); @@ -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");`, + }, ], }); @@ -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");`, + }, ], }); @@ -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");`, + }, ], } );