Skip to content

Commit

Permalink
Merge pull request #3168 from nestjs/revert-3029-fix/external-enums
Browse files Browse the repository at this point in the history
Revert "fix: properly import external enums"
  • Loading branch information
kamilmysliwiec authored Nov 15, 2024
2 parents 3d83335 + 62317c8 commit 1453a17
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 98 deletions.
54 changes: 0 additions & 54 deletions lib/plugin/utils/external-imports.util.ts

This file was deleted.

46 changes: 13 additions & 33 deletions lib/plugin/visitors/model-class.visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);

Expand All @@ -85,8 +80,7 @@ export class ModelClassVisitor extends AbstractFileVisitor {
typeChecker,
options,
sourceFile,
metadata,
externalImports
metadata
);
} else if (
options.parameterProperties &&
Expand All @@ -97,8 +91,7 @@ export class ModelClassVisitor extends AbstractFileVisitor {
typeChecker,
options,
sourceFile,
metadata,
externalImports
metadata
);
}
return node;
Expand Down Expand Up @@ -164,8 +157,7 @@ export class ModelClassVisitor extends AbstractFileVisitor {
typeChecker: ts.TypeChecker,
options: PluginOptions,
sourceFile: ts.SourceFile,
metadata: ClassMetadata,
externalImports: Record<string, string>
metadata: ClassMetadata
) {
const isPropertyStatic = (node.modifiers || []).some(
(modifier: ts.Modifier) => modifier.kind === ts.SyntaxKind.StaticKeyword
Expand Down Expand Up @@ -212,8 +204,7 @@ export class ModelClassVisitor extends AbstractFileVisitor {
options,
sourceFile.fileName,
sourceFile,
metadata,
externalImports
metadata
);
} catch (err) {
return node;
Expand All @@ -225,8 +216,7 @@ export class ModelClassVisitor extends AbstractFileVisitor {
typeChecker: ts.TypeChecker,
options: PluginOptions,
sourceFile: ts.SourceFile,
metadata: ClassMetadata,
externalImports: Record<string, string>
metadata: ClassMetadata
) {
constructorNode.forEachChild((node) => {
if (
Expand All @@ -247,8 +237,7 @@ export class ModelClassVisitor extends AbstractFileVisitor {
factory.createNodeArray(),
options,
sourceFile.fileName,
sourceFile,
externalImports
sourceFile
);

const propertyName = node.name.getText();
Expand Down Expand Up @@ -314,8 +303,7 @@ export class ModelClassVisitor extends AbstractFileVisitor {
options: PluginOptions,
hostFilename: string,
sourceFile: ts.SourceFile,
metadata: ClassMetadata,
externalImports: Record<string, string>
metadata: ClassMetadata
) {
const objectLiteralExpr = this.createDecoratorObjectLiteralExpr(
factory,
Expand All @@ -324,8 +312,7 @@ export class ModelClassVisitor extends AbstractFileVisitor {
factory.createNodeArray(),
options,
hostFilename,
sourceFile,
externalImports
sourceFile
);
this.addClassMetadata(
compilerNode,
Expand All @@ -345,8 +332,7 @@ export class ModelClassVisitor extends AbstractFileVisitor {
existingProperties: ts.NodeArray<ts.PropertyAssignment> = factory.createNodeArray(),
options: PluginOptions = {},
hostFilename = '',
sourceFile?: ts.SourceFile,
externalImports: Record<string, string> = {}
sourceFile?: ts.SourceFile
): ts.ObjectLiteralExpression {
const isRequired = !node.questionToken;

Expand Down Expand Up @@ -385,8 +371,7 @@ export class ModelClassVisitor extends AbstractFileVisitor {
typeChecker,
existingProperties,
hostFilename,
options,
externalImports
options
)
];
if (
Expand Down Expand Up @@ -543,8 +528,7 @@ export class ModelClassVisitor extends AbstractFileVisitor {
typeChecker: ts.TypeChecker,
existingProperties: ts.NodeArray<ts.PropertyAssignment>,
hostFilename: string,
options: PluginOptions,
externalImports: Record<string, string>
options: PluginOptions
) {
const key = 'enum';
if (hasPropertyKey(key, existingProperties)) {
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 1 addition & 7 deletions test/plugin/fixtures/project/cats/dto/create-cat.dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ConsoleLogger, HttpStatus } from '@nestjs/common';
import { ConsoleLogger } from '@nestjs/common';
import {
IsIn,
IsNegative,
Expand Down Expand Up @@ -99,12 +99,6 @@ export class CreateCatDto {
})
enum: LettersEnum;

@ApiProperty({
enum: HttpStatus,
enumName: 'HttpStatus'
})
externalEnum: HttpStatus;

/**
* Available language in the application
* @example FR
Expand Down
4 changes: 0 additions & 4 deletions test/plugin/fixtures/serialized-meta.fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 1453a17

Please sign in to comment.