Skip to content

Commit

Permalink
refactor(resolve): move null & undefined logics into primitive transf…
Browse files Browse the repository at this point in the history
…ormer
  • Loading branch information
jackey8616 committed Oct 17, 2024
1 parent 6b32d5b commit 8d86fdb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ export class PrimitiveTransformer extends Transformer {
return 'void';
case SyntaxKind.UndefinedKeyword:
return 'undefined';
case SyntaxKind.NullKeyword:
return 'null';
default:
return undefined;
}
};
}

public transform(typeNode: TypeNode, parentNode?: Node): Tsoa.PrimitiveType | undefined {
public transform(typeNode: TypeNode, parentNode?: Node): Tsoa.Type | undefined {
const resolvedType = PrimitiveTransformer.resolveKindToPrimitive(typeNode.kind);
if (!resolvedType) {
return;
Expand All @@ -39,15 +41,17 @@ export class PrimitiveTransformer extends Transformer {
case 'void':
case 'undefined':
return { dataType: resolvedType };
case 'null':
return {
dataType: 'enum',
enums: [null],
};
default:
return assertNever(resolvedType);
}
}

private transformNumber(
defaultNumberType: NonNullable<"double" | "float" | "integer" | "long" | undefined>,
parentNode?: Node,
): Tsoa.PrimitiveType {
private transformNumber(defaultNumberType: NonNullable<'double' | 'float' | 'integer' | 'long' | undefined>, parentNode?: Node): Tsoa.PrimitiveType {
if (!parentNode) {
return { dataType: defaultNumberType };
}
Expand All @@ -74,4 +78,4 @@ export class PrimitiveTransformer extends Transformer {
}
}

type ResolvesToPrimitive = 'number' | 'string' | 'boolean' | 'void' | 'undefined' | undefined;
type ResolvesToPrimitive = 'number' | 'string' | 'boolean' | 'void' | 'undefined' | 'null' | undefined;
15 changes: 0 additions & 15 deletions packages/cli/src/metadataGeneration/typeResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,6 @@ export class TypeResolver {
return primitiveType;
}

if (this.typeNode.kind === ts.SyntaxKind.NullKeyword) {
const enumType: Tsoa.EnumType = {
dataType: 'enum',
enums: [null],
};
return enumType;
}

if (this.typeNode.kind === ts.SyntaxKind.UndefinedKeyword) {
const undefinedType: Tsoa.UndefinedType = {
dataType: 'undefined',
};
return undefinedType;
}

if (ts.isArrayTypeNode(this.typeNode)) {
const arrayMetaType: Tsoa.ArrayType = {
dataType: 'array',
Expand Down

0 comments on commit 8d86fdb

Please sign in to comment.