Skip to content

Commit

Permalink
chore(transformer): Merge isLiteralRuntimeTypeNode and IsLiteralOrPri…
Browse files Browse the repository at this point in the history
…mitive
  • Loading branch information
martinjlowm committed May 9, 2020
1 parent add49a1 commit d69e0b6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 23 deletions.
38 changes: 16 additions & 22 deletions src/transformer/descriptor/helper/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,22 @@ type Declaration = ts.InterfaceDeclaration | ts.ClassDeclaration | ts.TypeAliasD
type ImportDeclaration = ts.ImportEqualsDeclaration | ts.ImportOrExportSpecifier | ts.ImportClause;

export namespace TypescriptHelper {
export function IsLiteralOrPrimitive(typeNode: ts.Node): boolean {
return ts.isLiteralTypeNode(typeNode) ||
typeNode.kind === ts.SyntaxKind.StringKeyword ||
typeNode.kind === ts.SyntaxKind.BooleanKeyword ||
typeNode.kind === ts.SyntaxKind.NumberKeyword ||
typeNode.kind === ts.SyntaxKind.ArrayType;
export interface PrimitiveTypeNode extends ts.TypeNode {
kind: ts.SyntaxKind.LiteralType | ts.SyntaxKind.NumberKeyword | ts.SyntaxKind.ObjectKeyword | ts.SyntaxKind.BooleanKeyword | ts.SyntaxKind.StringKeyword | ts.SyntaxKind.ArrayType;
}

export function IsLiteralOrPrimitive(typeNode: ts.Node): typeNode is PrimitiveTypeNode {
switch (typeNode.kind) {
case ts.SyntaxKind.LiteralType:
case ts.SyntaxKind.NumberKeyword:
case ts.SyntaxKind.ObjectKeyword:
case ts.SyntaxKind.BooleanKeyword:
case ts.SyntaxKind.StringKeyword:
case ts.SyntaxKind.ArrayType:
return true;
}

return false;
}

export function GetDeclarationFromNode(node: ts.Node): ts.Declaration {
Expand Down Expand Up @@ -119,22 +129,6 @@ export namespace TypescriptHelper {
return !!((symbol.flags & ts.SymbolFlags.Alias) || (symbol.flags & ts.SymbolFlags.AliasExcludes));
}

export interface RuntimeTypeNode extends ts.TypeNode {
kind: ts.SyntaxKind.NumberKeyword | ts.SyntaxKind.ObjectKeyword | ts.SyntaxKind.BooleanKeyword | ts.SyntaxKind.StringKeyword | ts.SyntaxKind.UndefinedKeyword;
}

export function isLiteralRuntimeTypeNode(typeNode: ts.TypeNode): typeNode is RuntimeTypeNode {
switch (typeNode.kind) {
case ts.SyntaxKind.NumberKeyword:
case ts.SyntaxKind.ObjectKeyword:
case ts.SyntaxKind.BooleanKeyword:
case ts.SyntaxKind.StringKeyword:
return true;
}

return false;
}

function isImportExportDeclaration(declaration: ts.Declaration): declaration is ImportDeclaration {
return ts.isImportEqualsDeclaration(declaration) || ts.isImportOrExportSpecifier(declaration) || ts.isImportClause(declaration);
}
Expand Down
2 changes: 1 addition & 1 deletion src/transformer/descriptor/method/method.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function CreateTypeEquality(signatureType: ts.TypeNode | undefined, primaryDecla
);
}

if (TypescriptHelper.isLiteralRuntimeTypeNode(signatureType)) {
if (TypescriptHelper.IsLiteralOrPrimitive(signatureType)) {
return ts.createStrictEquality(
ts.createTypeOf(identifier),
signatureType ? ts.createStringLiteral(signatureType.getText()) : ts.createVoidZero(),
Expand Down

0 comments on commit d69e0b6

Please sign in to comment.