Skip to content

Commit

Permalink
Merge pull request #3155 from Michsior14/fix/relative-compiler-paths
Browse files Browse the repository at this point in the history
fix(plugin): resolve compiler option paths relative to base
  • Loading branch information
kamilmysliwiec authored Nov 8, 2024
2 parents b230866 + a4e4950 commit 3c7451b
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 12 deletions.
50 changes: 40 additions & 10 deletions lib/plugin/utils/plugin-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,15 +171,13 @@ export function replaceImportPath(
? convertPath(options.pathToSource)
: posix.dirname(convertPath(fileName));

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;
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);
break;
}
}
Expand Down Expand Up @@ -210,7 +208,6 @@ export function replaceImportPath(
}

typeReference = typeReference.replace(importPath, relativePath);

if (options.readonly) {
const { typeName, typeImportStatement } =
convertToAsyncImport(typeReference);
Expand Down Expand Up @@ -397,3 +394,36 @@ 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: 2 additions & 1 deletion lib/plugin/visitors/controller-class.visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
} from '../utils/ast-utils';
import {
convertPath,
getAbsoluteCompilerOptionsPaths,
getDecoratorOrUndefinedByNames,
getTypeReferenceAsString,
hasPropertyKey
Expand Down Expand Up @@ -58,7 +59,7 @@ export class ControllerClassVisitor extends AbstractFileVisitor {
options: PluginOptions
) {
const typeChecker = program.getTypeChecker();
const compilerOptionsPaths = program.getCompilerOptions().paths ?? {};
const compilerOptionsPaths = getAbsoluteCompilerOptionsPaths(program);
if (!options.readonly) {
sourceFile = this.updateImports(sourceFile, ctx.factory, program);
}
Expand Down
3 changes: 2 additions & 1 deletion lib/plugin/visitors/model-class.visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
canReferenceNode,
convertPath,
extractTypeArgumentIfArray,
getAbsoluteCompilerOptionsPaths,
getDecoratorOrUndefinedByNames,
getTypeReferenceAsString,
hasPropertyKey,
Expand Down Expand Up @@ -72,7 +73,7 @@ export class ModelClassVisitor extends AbstractFileVisitor {
) {
const externalImports = getExternalImports(sourceFile);
const typeChecker = program.getTypeChecker();
const compilerOptionsPaths = program.getCompilerOptions().paths ?? {};
const compilerOptionsPaths = getAbsoluteCompilerOptionsPaths(program);
sourceFile = this.updateImports(sourceFile, ctx.factory, program);

const propertyNodeVisitorFactory =
Expand Down

0 comments on commit 3c7451b

Please sign in to comment.