Skip to content

Commit

Permalink
Revert "fix(plugin): support compiler options paths mapping"
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec authored Nov 15, 2024
1 parent 10573a0 commit f456068
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 133 deletions.
24 changes: 3 additions & 21 deletions lib/plugin/utils/plugin-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,7 @@ export function getTypeReferenceAsString(
if (type.aliasSymbol) {
return { typeName: 'Object', arrayDepth };
}
if (
typeChecker.getApparentType(type).getSymbol().getEscapedName() ===
'String'
) {
if (typeChecker.getApparentType(type).getSymbol().getEscapedName() === 'String') {
return { typeName: String.name, arrayDepth };
}
return { typeName: undefined };
Expand All @@ -143,8 +140,7 @@ export function hasPropertyKey(
export function replaceImportPath(
typeReference: string,
fileName: string,
options: PluginOptions,
compilerOptionsPaths: ts.MapLike<string[]>
options: PluginOptions
) {
if (!typeReference.includes('import')) {
return { typeReference, importPath: null };
Expand All @@ -171,19 +167,6 @@ 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;
break;
}
}

let relativePath = posix.relative(from, importPath);
relativePath = relativePath[0] !== '.' ? './' + relativePath : relativePath;

Expand Down Expand Up @@ -267,8 +250,7 @@ export function isAutoGeneratedEnumUnion(
return undefined;
}
const undefinedTypeIndex = type.types.findIndex(
(type: any) =>
type.intrinsicName === 'undefined' || type.intrinsicName === 'null'
(type: any) => type.intrinsicName === 'undefined' || type.intrinsicName === 'null'
);
if (undefinedTypeIndex < 0) {
return undefined;
Expand Down
6 changes: 2 additions & 4 deletions lib/plugin/utils/type-reference-to-identifier.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ export function typeReferenceToIdentifier(
options: PluginOptions,
factory: ts.NodeFactory,
type: ts.Type,
typeImports: Record<string, string>,
compilerOptionsPaths: ts.MapLike<string[]>
typeImports: Record<string, string>
) {
if (options.readonly) {
assertReferenceableType(
Expand All @@ -28,8 +27,7 @@ export function typeReferenceToIdentifier(
const { typeReference, importPath, typeName } = replaceImportPath(
typeReferenceDescriptor.typeName,
hostFilename,
options,
compilerOptionsPaths
options
);

let identifier: ts.Identifier;
Expand Down
22 changes: 7 additions & 15 deletions lib/plugin/visitors/controller-class.visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ export class ControllerClassVisitor extends AbstractFileVisitor {
options: PluginOptions
) {
const typeChecker = program.getTypeChecker();
const compilerOptionsPaths = program.getCompilerOptions().paths ?? {};
if (!options.readonly) {
sourceFile = this.updateImports(sourceFile, ctx.factory, program);
}
Expand All @@ -73,8 +72,7 @@ export class ControllerClassVisitor extends AbstractFileVisitor {
typeChecker,
options,
sourceFile,
metadata,
compilerOptionsPaths
metadata
);
if (!options.readonly) {
return updatedNode;
Expand Down Expand Up @@ -123,8 +121,7 @@ export class ControllerClassVisitor extends AbstractFileVisitor {
typeChecker: ts.TypeChecker,
options: PluginOptions,
sourceFile: ts.SourceFile,
metadata: ClassMetadata,
compilerOptionsPaths: ts.MapLike<string[]>
metadata: ClassMetadata
): ts.MethodDeclaration {
const hostFilename = sourceFile.fileName;
const decorators =
Expand Down Expand Up @@ -168,8 +165,7 @@ export class ControllerClassVisitor extends AbstractFileVisitor {
factory.createNodeArray(),
hostFilename,
metadata,
options,
compilerOptionsPaths
options
);
const updatedDecorators = [
...apiOperationDecoratorsArray,
Expand Down Expand Up @@ -393,8 +389,7 @@ export class ControllerClassVisitor extends AbstractFileVisitor {
existingProperties: ts.NodeArray<ts.PropertyAssignment> = factory.createNodeArray(),
hostFilename: string,
metadata: ClassMetadata,
options: PluginOptions,
compilerOptionsPaths: ts.MapLike<string[]>
options: PluginOptions
): ts.ObjectLiteralExpression {
let properties = [];

Expand All @@ -412,8 +407,7 @@ export class ControllerClassVisitor extends AbstractFileVisitor {
typeChecker,
existingProperties,
hostFilename,
options,
compilerOptionsPaths
options
)
]);
const objectLiteralExpr = factory.createObjectLiteralExpression(
Expand Down Expand Up @@ -443,8 +437,7 @@ export class ControllerClassVisitor extends AbstractFileVisitor {
typeChecker: ts.TypeChecker,
existingProperties: ts.NodeArray<ts.PropertyAssignment>,
hostFilename: string,
options: PluginOptions,
compilerOptionsPaths: ts.MapLike<string[]>
options: PluginOptions
) {
if (hasPropertyKey('type', existingProperties)) {
return undefined;
Expand All @@ -467,8 +460,7 @@ export class ControllerClassVisitor extends AbstractFileVisitor {
options,
factory,
type,
this._typeImports,
compilerOptionsPaths
this._typeImports
);
return factory.createPropertyAssignment('type', identifier);
}
Expand Down
81 changes: 27 additions & 54 deletions lib/plugin/visitors/model-class.visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ import { posix } from 'path';
import * as ts from 'typescript';
import { factory, PropertyAssignment } from 'typescript';
import { ApiHideProperty, ApiProperty } from '../../decorators';
import {
decoratorsProperties,
decoratorsPropertiesMappingType
} from '../../services/decorators-properties';
import { PluginOptions } from '../merge-options';
import { METADATA_FACTORY_NAME } from '../plugin-constants';
import { pluginDebugLogger } from '../plugin-debug-logger';
Expand All @@ -20,10 +16,6 @@ import {
getTsDocTagsOfNode,
isEnum
} from '../utils/ast-utils';
import {
getExternalImports,
replaceExternalImportsInTypeReference
} from '../utils/external-imports.util';
import {
canReferenceNode,
convertPath,
Expand All @@ -36,6 +28,14 @@ import {
} from '../utils/plugin-utils';
import { typeReferenceToIdentifier } from '../utils/type-reference-to-identifier.util';
import { AbstractFileVisitor } from './abstract.visitor';
import {
getExternalImports,
replaceExternalImportsInTypeReference
} from '../utils/external-imports.util';
import {
decoratorsProperties,
decoratorsPropertiesMappingType
} from '../../services/decorators-properties';

type ClassMetadata = Record<string, ts.ObjectLiteralExpression>;

Expand Down Expand Up @@ -72,7 +72,6 @@ export class ModelClassVisitor extends AbstractFileVisitor {
) {
const externalImports = getExternalImports(sourceFile);
const typeChecker = program.getTypeChecker();
const compilerOptionsPaths = program.getCompilerOptions().paths ?? {};
sourceFile = this.updateImports(sourceFile, ctx.factory, program);

const propertyNodeVisitorFactory =
Expand All @@ -87,8 +86,7 @@ export class ModelClassVisitor extends AbstractFileVisitor {
options,
sourceFile,
metadata,
externalImports,
compilerOptionsPaths
externalImports
);
} else if (
options.parameterProperties &&
Expand All @@ -100,8 +98,7 @@ export class ModelClassVisitor extends AbstractFileVisitor {
options,
sourceFile,
metadata,
externalImports,
compilerOptionsPaths
externalImports
);
}
return node;
Expand Down Expand Up @@ -168,8 +165,7 @@ export class ModelClassVisitor extends AbstractFileVisitor {
options: PluginOptions,
sourceFile: ts.SourceFile,
metadata: ClassMetadata,
externalImports: Record<string, string>,
compilerOptionsPaths: ts.MapLike<string[]>
externalImports: Record<string, string>
) {
const isPropertyStatic = (node.modifiers || []).some(
(modifier: ts.Modifier) => modifier.kind === ts.SyntaxKind.StaticKeyword
Expand Down Expand Up @@ -217,8 +213,7 @@ export class ModelClassVisitor extends AbstractFileVisitor {
sourceFile.fileName,
sourceFile,
metadata,
externalImports,
compilerOptionsPaths
externalImports
);
} catch (err) {
return node;
Expand All @@ -231,8 +226,7 @@ export class ModelClassVisitor extends AbstractFileVisitor {
options: PluginOptions,
sourceFile: ts.SourceFile,
metadata: ClassMetadata,
externalImports: Record<string, string>,
compilerOptionsPaths: ts.MapLike<string[]>
externalImports: Record<string, string>
) {
constructorNode.forEachChild((node) => {
if (
Expand All @@ -254,8 +248,7 @@ export class ModelClassVisitor extends AbstractFileVisitor {
options,
sourceFile.fileName,
sourceFile,
externalImports,
compilerOptionsPaths
externalImports
);

const propertyName = node.name.getText();
Expand Down Expand Up @@ -322,8 +315,7 @@ export class ModelClassVisitor extends AbstractFileVisitor {
hostFilename: string,
sourceFile: ts.SourceFile,
metadata: ClassMetadata,
externalImports: Record<string, string>,
compilerOptionsPaths: ts.MapLike<string[]>
externalImports: Record<string, string>
) {
const objectLiteralExpr = this.createDecoratorObjectLiteralExpr(
factory,
Expand All @@ -333,8 +325,7 @@ export class ModelClassVisitor extends AbstractFileVisitor {
options,
hostFilename,
sourceFile,
externalImports,
compilerOptionsPaths
externalImports
);
this.addClassMetadata(
compilerNode,
Expand All @@ -355,8 +346,7 @@ export class ModelClassVisitor extends AbstractFileVisitor {
options: PluginOptions = {},
hostFilename = '',
sourceFile?: ts.SourceFile,
externalImports: Record<string, string> = {},
compilerOptionsPaths: ts.MapLike<string[]> = {}
externalImports: Record<string, string> = {}
): ts.ObjectLiteralExpression {
const isRequired = !node.questionToken;

Expand All @@ -373,9 +363,7 @@ export class ModelClassVisitor extends AbstractFileVisitor {
typeChecker,
existingProperties,
hostFilename,
options,
externalImports,
compilerOptionsPaths
options
),
...this.createDescriptionAndTsDocTagPropertyAssignments(
factory,
Expand All @@ -398,8 +386,7 @@ export class ModelClassVisitor extends AbstractFileVisitor {
existingProperties,
hostFilename,
options,
externalImports,
compilerOptionsPaths
externalImports
)
];
if (
Expand All @@ -424,9 +411,7 @@ export class ModelClassVisitor extends AbstractFileVisitor {
typeChecker: ts.TypeChecker,
existingProperties: ts.NodeArray<ts.PropertyAssignment>,
hostFilename: string,
options: PluginOptions,
externalImports: Record<string, string>,
compilerOptionsPaths: ts.MapLike<string[]>
options: PluginOptions
): ts.PropertyAssignment[] {
const key = 'type';
if (hasPropertyKey(key, existingProperties)) {
Expand All @@ -441,9 +426,7 @@ export class ModelClassVisitor extends AbstractFileVisitor {
typeChecker,
existingProperties,
hostFilename,
options,
externalImports,
compilerOptionsPaths
options
);
return [factory.createPropertyAssignment(key, initializer)];
} else if (ts.isUnionTypeNode(node)) {
Expand All @@ -460,9 +443,7 @@ export class ModelClassVisitor extends AbstractFileVisitor {
typeChecker,
existingProperties,
hostFilename,
options,
externalImports,
compilerOptionsPaths
options
);
if (!isNullable) {
return propertyAssignments;
Expand Down Expand Up @@ -494,8 +475,7 @@ export class ModelClassVisitor extends AbstractFileVisitor {
options,
factory,
type,
this._typeImports,
compilerOptionsPaths
this._typeImports
);

const initializer = factory.createArrowFunction(
Expand All @@ -515,9 +495,7 @@ export class ModelClassVisitor extends AbstractFileVisitor {
typeChecker: ts.TypeChecker,
existingProperties: ts.NodeArray<ts.PropertyAssignment>,
hostFilename: string,
options: PluginOptions,
externalImports: Record<string, string>,
compilerOptionsPaths: ts.MapLike<string[]>
options: PluginOptions
) {
const propertyAssignments = Array.from(node.members || []).map((member) => {
const literalExpr = this.createDecoratorObjectLiteralExpr(
Expand All @@ -526,10 +504,7 @@ export class ModelClassVisitor extends AbstractFileVisitor {
typeChecker,
existingProperties,
options,
hostFilename,
undefined,
externalImports,
compilerOptionsPaths
hostFilename
);
return factory.createPropertyAssignment(
factory.createIdentifier(member.name.getText()),
Expand Down Expand Up @@ -569,8 +544,7 @@ export class ModelClassVisitor extends AbstractFileVisitor {
existingProperties: ts.NodeArray<ts.PropertyAssignment>,
hostFilename: string,
options: PluginOptions,
externalImports: Record<string, string>,
compilerOptionsPaths: ts.MapLike<string[]>
externalImports: Record<string, string>
) {
const key = 'enum';
if (hasPropertyKey(key, existingProperties)) {
Expand Down Expand Up @@ -618,8 +592,7 @@ export class ModelClassVisitor extends AbstractFileVisitor {
options,
factory,
type,
this._typeImports,
compilerOptionsPaths
this._typeImports
);

const enumProperty = factory.createPropertyAssignment(key, enumIdentifier);
Expand Down
4 changes: 0 additions & 4 deletions test/plugin/fixtures/project/cats/dto/absolute-owner.dto.ts

This file was deleted.

Loading

0 comments on commit f456068

Please sign in to comment.