From 62317c89f356cf426eb383549d3527bc07916ea9 Mon Sep 17 00:00:00 2001 From: Kamil Mysliwiec Date: Fri, 15 Nov 2024 10:12:28 +0100 Subject: [PATCH] Revert "fix: properly import external enums" --- lib/plugin/utils/external-imports.util.ts | 54 ------------------- lib/plugin/visitors/model-class.visitor.ts | 46 +++++----------- .../project/cats/dto/create-cat.dto.ts | 8 +-- .../fixtures/serialized-meta.fixture.ts | 4 -- 4 files changed, 14 insertions(+), 98 deletions(-) delete mode 100644 lib/plugin/utils/external-imports.util.ts diff --git a/lib/plugin/utils/external-imports.util.ts b/lib/plugin/utils/external-imports.util.ts deleted file mode 100644 index 8906dfecf..000000000 --- a/lib/plugin/utils/external-imports.util.ts +++ /dev/null @@ -1,54 +0,0 @@ -import * as ts from 'typescript'; - -export function getExternalImports( - sourceFile: ts.SourceFile -): Record { - const externalImports: Record = {}; - - const importDeclarations = sourceFile.statements.filter( - ts.isImportDeclaration - ); - - for (const declaration of importDeclarations) { - const { moduleSpecifier, importClause } = declaration; - - // Skip relative imports - if ( - !ts.isStringLiteral(moduleSpecifier) || - moduleSpecifier.text[0] === '.' - ) { - continue; - } - - if ( - importClause?.namedBindings && - ts.isNamedImports(importClause.namedBindings) - ) { - const namedImports = importClause?.namedBindings as ts.NamedImports; - for (const namedImport of namedImports.elements) { - externalImports[namedImport.name.text] = moduleSpecifier.text; - } - } - } - return externalImports; -} - -export function replaceExternalImportsInTypeReference( - typeReference: string, - externalImports: Record -): string { - const regexp = /import\((.+)\).([^\]]+)(\])?/; - const match = regexp.exec(typeReference); - - if (match?.length >= 3) { - const [, importPath, importName] = match; - if (externalImports[importName]) { - return typeReference.replace( - importPath, - `"${externalImports[importName]}"` - ); - } - } - - return typeReference; -} diff --git a/lib/plugin/visitors/model-class.visitor.ts b/lib/plugin/visitors/model-class.visitor.ts index 69ece62d0..b6e9209ae 100644 --- a/lib/plugin/visitors/model-class.visitor.ts +++ b/lib/plugin/visitors/model-class.visitor.ts @@ -28,10 +28,6 @@ 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 @@ -70,7 +66,6 @@ export class ModelClassVisitor extends AbstractFileVisitor { program: ts.Program, options: PluginOptions ) { - const externalImports = getExternalImports(sourceFile); const typeChecker = program.getTypeChecker(); sourceFile = this.updateImports(sourceFile, ctx.factory, program); @@ -85,8 +80,7 @@ export class ModelClassVisitor extends AbstractFileVisitor { typeChecker, options, sourceFile, - metadata, - externalImports + metadata ); } else if ( options.parameterProperties && @@ -97,8 +91,7 @@ export class ModelClassVisitor extends AbstractFileVisitor { typeChecker, options, sourceFile, - metadata, - externalImports + metadata ); } return node; @@ -164,8 +157,7 @@ export class ModelClassVisitor extends AbstractFileVisitor { typeChecker: ts.TypeChecker, options: PluginOptions, sourceFile: ts.SourceFile, - metadata: ClassMetadata, - externalImports: Record + metadata: ClassMetadata ) { const isPropertyStatic = (node.modifiers || []).some( (modifier: ts.Modifier) => modifier.kind === ts.SyntaxKind.StaticKeyword @@ -212,8 +204,7 @@ export class ModelClassVisitor extends AbstractFileVisitor { options, sourceFile.fileName, sourceFile, - metadata, - externalImports + metadata ); } catch (err) { return node; @@ -225,8 +216,7 @@ export class ModelClassVisitor extends AbstractFileVisitor { typeChecker: ts.TypeChecker, options: PluginOptions, sourceFile: ts.SourceFile, - metadata: ClassMetadata, - externalImports: Record + metadata: ClassMetadata ) { constructorNode.forEachChild((node) => { if ( @@ -247,8 +237,7 @@ export class ModelClassVisitor extends AbstractFileVisitor { factory.createNodeArray(), options, sourceFile.fileName, - sourceFile, - externalImports + sourceFile ); const propertyName = node.name.getText(); @@ -314,8 +303,7 @@ export class ModelClassVisitor extends AbstractFileVisitor { options: PluginOptions, hostFilename: string, sourceFile: ts.SourceFile, - metadata: ClassMetadata, - externalImports: Record + metadata: ClassMetadata ) { const objectLiteralExpr = this.createDecoratorObjectLiteralExpr( factory, @@ -324,8 +312,7 @@ export class ModelClassVisitor extends AbstractFileVisitor { factory.createNodeArray(), options, hostFilename, - sourceFile, - externalImports + sourceFile ); this.addClassMetadata( compilerNode, @@ -345,8 +332,7 @@ export class ModelClassVisitor extends AbstractFileVisitor { existingProperties: ts.NodeArray = factory.createNodeArray(), options: PluginOptions = {}, hostFilename = '', - sourceFile?: ts.SourceFile, - externalImports: Record = {} + sourceFile?: ts.SourceFile ): ts.ObjectLiteralExpression { const isRequired = !node.questionToken; @@ -385,8 +371,7 @@ export class ModelClassVisitor extends AbstractFileVisitor { typeChecker, existingProperties, hostFilename, - options, - externalImports + options ) ]; if ( @@ -543,8 +528,7 @@ export class ModelClassVisitor extends AbstractFileVisitor { typeChecker: ts.TypeChecker, existingProperties: ts.NodeArray, hostFilename: string, - options: PluginOptions, - externalImports: Record + options: PluginOptions ) { const key = 'enum'; if (hasPropertyKey(key, existingProperties)) { @@ -580,12 +564,8 @@ export class ModelClassVisitor extends AbstractFileVisitor { } type = typeIsArrayTuple.type; } - const typeReferenceDescriptor = { - typeName: replaceExternalImportsInTypeReference( - getText(type, typeChecker), - externalImports - ) - }; + + const typeReferenceDescriptor = { typeName: getText(type, typeChecker) }; const enumIdentifier = typeReferenceToIdentifier( typeReferenceDescriptor, hostFilename, diff --git a/test/plugin/fixtures/project/cats/dto/create-cat.dto.ts b/test/plugin/fixtures/project/cats/dto/create-cat.dto.ts index 63fb9032f..b652a83da 100644 --- a/test/plugin/fixtures/project/cats/dto/create-cat.dto.ts +++ b/test/plugin/fixtures/project/cats/dto/create-cat.dto.ts @@ -1,4 +1,4 @@ -import { ConsoleLogger, HttpStatus } from '@nestjs/common'; +import { ConsoleLogger } from '@nestjs/common'; import { IsIn, IsNegative, @@ -99,12 +99,6 @@ export class CreateCatDto { }) enum: LettersEnum; - @ApiProperty({ - enum: HttpStatus, - enumName: 'HttpStatus' - }) - externalEnum: HttpStatus; - /** * Available language in the application * @example FR diff --git a/test/plugin/fixtures/serialized-meta.fixture.ts b/test/plugin/fixtures/serialized-meta.fixture.ts index d8d9eecff..acb24e414 100644 --- a/test/plugin/fixtures/serialized-meta.fixture.ts +++ b/test/plugin/fixtures/serialized-meta.fixture.ts @@ -139,10 +139,6 @@ export default async () => { required: true, enum: t['./cats/dto/pagination-query.dto'].LettersEnum }, - externalEnum: { - required: true, - enum: require('@nestjs/common').HttpStatus - }, state: { required: false, description: 'Available language in the application',