Skip to content

Commit

Permalink
Revert "fix(plugin): resolve compiler option paths relative to base"
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec authored Nov 15, 2024
1 parent fe0adc3 commit 94ba256
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 44 deletions.
50 changes: 10 additions & 40 deletions lib/plugin/utils/plugin-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -208,6 +210,7 @@ export function replaceImportPath(
}

typeReference = typeReference.replace(importPath, relativePath);

if (options.readonly) {
const { typeName, typeImportStatement } =
convertToAsyncImport(typeReference);
Expand Down Expand Up @@ -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<string[]> {
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<string[]> = {};
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;
}
3 changes: 1 addition & 2 deletions lib/plugin/visitors/controller-class.visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
} from '../utils/ast-utils';
import {
convertPath,
getAbsoluteCompilerOptionsPaths,
getDecoratorOrUndefinedByNames,
getTypeReferenceAsString,
hasPropertyKey
Expand Down Expand Up @@ -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);
}
Expand Down
3 changes: 1 addition & 2 deletions lib/plugin/visitors/model-class.visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import {
canReferenceNode,
convertPath,
extractTypeArgumentIfArray,
getAbsoluteCompilerOptionsPaths,
getDecoratorOrUndefinedByNames,
getTypeReferenceAsString,
hasPropertyKey,
Expand Down Expand Up @@ -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 =
Expand Down

0 comments on commit 94ba256

Please sign in to comment.