From 94ba256f14a7be20fd620c0c75f379208f6e842b Mon Sep 17 00:00:00 2001 From: Kamil Mysliwiec Date: Fri, 15 Nov 2024 10:09:35 +0100 Subject: [PATCH] Revert "fix(plugin): resolve compiler option paths relative to base" --- lib/plugin/utils/plugin-utils.ts | 50 ++++--------------- .../visitors/controller-class.visitor.ts | 3 +- lib/plugin/visitors/model-class.visitor.ts | 3 +- 3 files changed, 12 insertions(+), 44 deletions(-) diff --git a/lib/plugin/utils/plugin-utils.ts b/lib/plugin/utils/plugin-utils.ts index 0633c245e..188f72bda 100644 --- a/lib/plugin/utils/plugin-utils.ts +++ b/lib/plugin/utils/plugin-utils.ts @@ -171,13 +171,15 @@ export function replaceImportPath( ? convertPath(options.pathToSource) : posix.dirname(convertPath(fileName)); - for (const [optionName, optionPaths] of Object.entries( - compilerOptionsPaths - )) { - if (importPath.includes(optionName)) { - const optionPath = optionPaths[0]; - typeReference = typeReference.replace(optionName, optionPath); - importPath = importPath.replace(optionName, optionPath); + for (const [key, value] of Object.entries(compilerOptionsPaths)) { + const keyToMatch = key.replace('*', ''); + if (importPath.includes(keyToMatch)) { + const newImportPath = posix.join( + from, + importPath.replace(keyToMatch, value[0].replace('*', '')) + ); + typeReference = typeReference.replace(importPath, newImportPath); + importPath = newImportPath; break; } } @@ -208,6 +210,7 @@ export function replaceImportPath( } typeReference = typeReference.replace(importPath, relativePath); + if (options.readonly) { const { typeName, typeImportStatement } = convertToAsyncImport(typeReference); @@ -394,36 +397,3 @@ export function canReferenceNode(node: ts.Node, options: PluginOptions) { } return false; } - -/** - * Get modifies compiler options paths where - * - * - all `*` are removed from the aliases and paths - * - all paths are resolved to absolute paths - * - * If both `baseUrl` and `pathsBasePath` are not set, the current - * compilation directory is used as the base path for resolution. - */ -export function getAbsoluteCompilerOptionsPaths( - program: ts.Program -): ts.MapLike { - const compilerOptions = program.getCompilerOptions(); - const { paths } = compilerOptions; - if (!paths) { - return {}; - } - - const base = - (compilerOptions.pathsBasePath as string | undefined) || - compilerOptions.baseUrl || - program.getCurrentDirectory(); - - const result: ts.MapLike = {}; - for (const [key, list] of Object.entries(paths)) { - result[key.replace('/*', '')] = list.map((item) => { - const path = item.replace('/*', ''); - return isAbsolute(path) ? path : posix.join(base, path); - }); - } - return result; -} diff --git a/lib/plugin/visitors/controller-class.visitor.ts b/lib/plugin/visitors/controller-class.visitor.ts index 37f3c1b5a..87bb1f9f5 100644 --- a/lib/plugin/visitors/controller-class.visitor.ts +++ b/lib/plugin/visitors/controller-class.visitor.ts @@ -14,7 +14,6 @@ import { } from '../utils/ast-utils'; import { convertPath, - getAbsoluteCompilerOptionsPaths, getDecoratorOrUndefinedByNames, getTypeReferenceAsString, hasPropertyKey @@ -59,7 +58,7 @@ export class ControllerClassVisitor extends AbstractFileVisitor { options: PluginOptions ) { const typeChecker = program.getTypeChecker(); - const compilerOptionsPaths = getAbsoluteCompilerOptionsPaths(program); + const compilerOptionsPaths = program.getCompilerOptions().paths ?? {}; if (!options.readonly) { sourceFile = this.updateImports(sourceFile, ctx.factory, program); } diff --git a/lib/plugin/visitors/model-class.visitor.ts b/lib/plugin/visitors/model-class.visitor.ts index b03bfb5d4..e66770005 100644 --- a/lib/plugin/visitors/model-class.visitor.ts +++ b/lib/plugin/visitors/model-class.visitor.ts @@ -28,7 +28,6 @@ import { canReferenceNode, convertPath, extractTypeArgumentIfArray, - getAbsoluteCompilerOptionsPaths, getDecoratorOrUndefinedByNames, getTypeReferenceAsString, hasPropertyKey, @@ -73,7 +72,7 @@ export class ModelClassVisitor extends AbstractFileVisitor { ) { const externalImports = getExternalImports(sourceFile); const typeChecker = program.getTypeChecker(); - const compilerOptionsPaths = getAbsoluteCompilerOptionsPaths(program); + const compilerOptionsPaths = program.getCompilerOptions().paths ?? {}; sourceFile = this.updateImports(sourceFile, ctx.factory, program); const propertyNodeVisitorFactory =