From 26668bb33a867413e6a549afab45b4beb9bd3fe0 Mon Sep 17 00:00:00 2001 From: Derek Xu Date: Wed, 18 Dec 2019 16:45:34 -0800 Subject: [PATCH] Added optional boolean to interface and class declarations --- .../named_declarations/member_declaration.ts | 14 ++++++++- .../parameter_declaration.ts | 1 - test/json/declarations/class_test.ts | 29 +++++++++++++++++++ test/json/declarations/interface_test.ts | 24 +++++++++++++++ test/json/merge_test.ts | 24 +++++++++++++++ 5 files changed, 90 insertions(+), 2 deletions(-) diff --git a/lib/json/named_declarations/member_declaration.ts b/lib/json/named_declarations/member_declaration.ts index 563ef29..4d2fafc 100644 --- a/lib/json/named_declarations/member_declaration.ts +++ b/lib/json/named_declarations/member_declaration.ts @@ -1,6 +1,6 @@ import * as ts from 'typescript'; -import {convertTypeNode, filterUndefined} from '../conversions'; +import {convertExpression, convertTypeNode, filterUndefined} from '../conversions'; import {ConvertedSyntaxKind} from '../converted_syntax_kinds'; import {Type} from '../types'; @@ -17,20 +17,32 @@ export abstract class MemberDeclaration extends NamedDeclaration { export class PropertyDeclaration extends MemberDeclaration { private type: Type; + private optional = false; + private initializer?: string; constructor(node: ts.PropertyDeclaration|ts.PropertySignature) { super(node, ConvertedSyntaxKind.PropertyDeclaration); this.type = convertTypeNode(node.type); + if (node.questionToken) { + this.optional = true; + } + if (node.initializer) { + this.initializer = convertExpression(node.initializer); + } } } export class MethodDeclaration extends MemberDeclaration { private type: Type; + private optional = false; private parameters: ParameterDeclaration[]; constructor(node: ts.MethodDeclaration|ts.MethodSignature) { super(node, ConvertedSyntaxKind.MethodDeclaration); this.type = convertTypeNode(node.type); + if (node.questionToken) { + this.optional = true; + } this.parameters = node.parameters.map((param: ts.ParameterDeclaration) => new ParameterDeclaration(param)) .filter(filterUndefined); diff --git a/lib/json/named_declarations/parameter_declaration.ts b/lib/json/named_declarations/parameter_declaration.ts index b3a6515..bd66b9f 100644 --- a/lib/json/named_declarations/parameter_declaration.ts +++ b/lib/json/named_declarations/parameter_declaration.ts @@ -17,7 +17,6 @@ export class ParameterDeclaration extends NamedDeclaration { this.type = convertTypeNode(node.type); if (node.questionToken) { - // TODO(derekx): Do we want to mark parameters with default values as optional? this.optional = true; } if (node.dotDotDotToken) { diff --git a/test/json/declarations/class_test.ts b/test/json/declarations/class_test.ts index 9e97a7e..83ca5c5 100644 --- a/test/json/declarations/class_test.ts +++ b/test/json/declarations/class_test.ts @@ -216,11 +216,13 @@ describe('classes', () => { { kind: ConvertedSyntaxKind.PropertyDeclaration, name: 'a', + optional: false, type: {kind: ConvertedSyntaxKind.KeywordType, typeName: 'number'} }, { kind: ConvertedSyntaxKind.PropertyDeclaration, name: 'b', + optional: false, type: {kind: ConvertedSyntaxKind.KeywordType, typeName: 'string'} } ] @@ -239,6 +241,7 @@ describe('classes', () => { members: [{ kind: ConvertedSyntaxKind.MethodDeclaration, name: 'f', + optional: false, type: {kind: ConvertedSyntaxKind.KeywordType, typeName: 'void'}, parameters: [] }] @@ -256,6 +259,7 @@ describe('classes', () => { members: [{ kind: ConvertedSyntaxKind.MethodDeclaration, name: 'f', + optional: false, type: {kind: ConvertedSyntaxKind.KeywordType, typeName: 'boolean'}, parameters: [ { @@ -291,6 +295,7 @@ describe('classes', () => { kind: ConvertedSyntaxKind.MethodDeclaration, modifiers: [{kind: ConvertedSyntaxKind.AbstractModifier}], name: 'f', + optional: false, type: {kind: ConvertedSyntaxKind.KeywordType, typeName: 'number'}, parameters: [] }] @@ -369,12 +374,14 @@ declare class X { kind: ConvertedSyntaxKind.PropertyDeclaration, modifiers: [{kind: ConvertedSyntaxKind.PrivateModifier}], name: '_a', + optional: false, type: {kind: ConvertedSyntaxKind.KeywordType, typeName: 'number'} }, { kind: ConvertedSyntaxKind.MethodDeclaration, modifiers: [{kind: ConvertedSyntaxKind.PrivateModifier}], name: 'b', + optional: false, type: {kind: ConvertedSyntaxKind.KeywordType, typeName: 'boolean'}, parameters: [] } @@ -400,12 +407,14 @@ declare class X { kind: ConvertedSyntaxKind.PropertyDeclaration, modifiers: [{kind: ConvertedSyntaxKind.ProtectedModifier}], name: 'a', + optional: false, type: {kind: ConvertedSyntaxKind.KeywordType, typeName: 'number'} }, { kind: ConvertedSyntaxKind.MethodDeclaration, modifiers: [{kind: ConvertedSyntaxKind.ProtectedModifier}], name: 'b', + optional: false, type: {kind: ConvertedSyntaxKind.KeywordType, typeName: 'boolean'}, parameters: [] } @@ -414,6 +423,24 @@ declare class X { })); }); + it('supports optional', () => { + expectTranslateJSON('declare class X { a?: number }').to.equal(prettyStringify({ + kind: ConvertedSyntaxKind.SourceFile, + fileName: 'demo/some/main.ts', + statements: [{ + kind: ConvertedSyntaxKind.ClassDeclaration, + modifiers: [], + name: 'X', + members: [{ + kind: ConvertedSyntaxKind.PropertyDeclaration, + name: 'a', + optional: true, + type: {kind: ConvertedSyntaxKind.KeywordType, typeName: 'number'} + }] + }] + })); + }); + it('supports readonly', () => { expectTranslateJSON('declare class X { readonly a: number }').to.equal(prettyStringify({ kind: ConvertedSyntaxKind.SourceFile, @@ -426,6 +453,7 @@ declare class X { kind: ConvertedSyntaxKind.PropertyDeclaration, modifiers: [{kind: ConvertedSyntaxKind.ReadonlyModifier}], name: 'a', + optional: false, type: {kind: ConvertedSyntaxKind.KeywordType, typeName: 'number'} }] }] @@ -444,6 +472,7 @@ declare class X { kind: ConvertedSyntaxKind.PropertyDeclaration, modifiers: [{kind: ConvertedSyntaxKind.StaticModifier}], name: 'a', + optional: false, type: {kind: ConvertedSyntaxKind.KeywordType, typeName: 'number'} }] }] diff --git a/test/json/declarations/interface_test.ts b/test/json/declarations/interface_test.ts index b208937..984f172 100644 --- a/test/json/declarations/interface_test.ts +++ b/test/json/declarations/interface_test.ts @@ -133,11 +133,13 @@ describe('interfaces', () => { { kind: ConvertedSyntaxKind.PropertyDeclaration, name: 'a', + optional: false, type: {kind: ConvertedSyntaxKind.KeywordType, typeName: 'number'} }, { kind: ConvertedSyntaxKind.PropertyDeclaration, name: 'b', + optional: false, type: {kind: ConvertedSyntaxKind.KeywordType, typeName: 'string'} } ] @@ -156,6 +158,7 @@ describe('interfaces', () => { members: [{ kind: ConvertedSyntaxKind.MethodDeclaration, name: 'f', + optional: false, type: {kind: ConvertedSyntaxKind.KeywordType, typeName: 'void'}, parameters: [] }] @@ -173,6 +176,7 @@ describe('interfaces', () => { members: [{ kind: ConvertedSyntaxKind.MethodDeclaration, name: 'f', + optional: false, type: {kind: ConvertedSyntaxKind.KeywordType, typeName: 'boolean'}, parameters: [ { @@ -208,6 +212,7 @@ describe('interfaces', () => { kind: ConvertedSyntaxKind.MethodDeclaration, modifiers: [{kind: ConvertedSyntaxKind.AbstractModifier}], name: 'f', + optional: false, type: {kind: ConvertedSyntaxKind.KeywordType, typeName: 'number'}, parameters: [] }] @@ -215,6 +220,24 @@ describe('interfaces', () => { })); }); + it('supports optional', () => { + expectTranslateJSON('declare interface X { a?: number }').to.equal(prettyStringify({ + kind: ConvertedSyntaxKind.SourceFile, + fileName: 'demo/some/main.ts', + statements: [{ + kind: ConvertedSyntaxKind.InterfaceDeclaration, + modifiers: [], + name: 'X', + members: [{ + kind: ConvertedSyntaxKind.PropertyDeclaration, + name: 'a', + optional: true, + type: {kind: ConvertedSyntaxKind.KeywordType, typeName: 'number'} + }] + }] + })); + }); + it('supports readonly', () => { expectTranslateJSON('declare interface X { readonly a: number }').to.equal(prettyStringify({ kind: ConvertedSyntaxKind.SourceFile, @@ -227,6 +250,7 @@ describe('interfaces', () => { kind: ConvertedSyntaxKind.PropertyDeclaration, modifiers: [{kind: ConvertedSyntaxKind.ReadonlyModifier}], name: 'a', + optional: false, type: {kind: ConvertedSyntaxKind.KeywordType, typeName: 'number'} }] }] diff --git a/test/json/merge_test.ts b/test/json/merge_test.ts index 28ba1e5..59ccd21 100644 --- a/test/json/merge_test.ts +++ b/test/json/merge_test.ts @@ -25,16 +25,19 @@ declare var X: { new(a: number, b: string): XType }; { kind: ConvertedSyntaxKind.PropertyDeclaration, name: 'a', + optional: false, type: {kind: ConvertedSyntaxKind.KeywordType, typeName: 'number'}, }, { kind: ConvertedSyntaxKind.PropertyDeclaration, name: 'b', + optional: false, type: {kind: ConvertedSyntaxKind.KeywordType, typeName: 'string'}, }, { kind: ConvertedSyntaxKind.MethodDeclaration, name: 'c', + optional: false, type: {kind: ConvertedSyntaxKind.KeywordType, typeName: 'boolean'}, parameters: [] } @@ -48,16 +51,19 @@ declare var X: { new(a: number, b: string): XType }; { kind: ConvertedSyntaxKind.PropertyDeclaration, name: 'a', + optional: false, type: {kind: ConvertedSyntaxKind.KeywordType, typeName: 'number'}, }, { kind: ConvertedSyntaxKind.PropertyDeclaration, name: 'b', + optional: false, type: {kind: ConvertedSyntaxKind.KeywordType, typeName: 'string'}, }, { kind: ConvertedSyntaxKind.MethodDeclaration, name: 'c', + optional: false, type: {kind: ConvertedSyntaxKind.KeywordType, typeName: 'boolean'}, parameters: [] }, @@ -112,16 +118,19 @@ declare var X: X; { kind: ConvertedSyntaxKind.PropertyDeclaration, name: 'a', + optional: false, type: {kind: ConvertedSyntaxKind.KeywordType, typeName: 'number'}, }, { kind: ConvertedSyntaxKind.PropertyDeclaration, name: 'b', + optional: false, type: {kind: ConvertedSyntaxKind.KeywordType, typeName: 'string'}, }, { kind: ConvertedSyntaxKind.MethodDeclaration, name: 'c', + optional: false, type: {kind: ConvertedSyntaxKind.KeywordType, typeName: 'boolean'}, parameters: [] } @@ -135,16 +144,19 @@ declare var X: X; { kind: ConvertedSyntaxKind.PropertyDeclaration, name: 'a', + optional: false, type: {kind: ConvertedSyntaxKind.KeywordType, typeName: 'number'}, }, { kind: ConvertedSyntaxKind.PropertyDeclaration, name: 'b', + optional: false, type: {kind: ConvertedSyntaxKind.KeywordType, typeName: 'string'}, }, { kind: ConvertedSyntaxKind.MethodDeclaration, name: 'c', + optional: false, type: {kind: ConvertedSyntaxKind.KeywordType, typeName: 'boolean'}, parameters: [] }, @@ -197,6 +209,7 @@ declare var X: { { kind: ConvertedSyntaxKind.PropertyDeclaration, name: 'n', + optional: false, type: {kind: ConvertedSyntaxKind.KeywordType, typeName: 'number'}, }, ] @@ -209,6 +222,7 @@ declare var X: { { kind: ConvertedSyntaxKind.PropertyDeclaration, name: 'n', + optional: false, type: {kind: ConvertedSyntaxKind.KeywordType, typeName: 'number'}, }, { @@ -227,6 +241,7 @@ declare var X: { kind: ConvertedSyntaxKind.PropertyDeclaration, modifiers: [{kind: ConvertedSyntaxKind.StaticModifier}], name: 'm', + optional: false, type: {kind: ConvertedSyntaxKind.KeywordType, typeName: 'string'}, }, ] @@ -262,6 +277,7 @@ declare var X: { { kind: ConvertedSyntaxKind.PropertyDeclaration, name: 'n', + optional: false, type: {kind: ConvertedSyntaxKind.KeywordType, typeName: 'number'}, }, ] @@ -274,6 +290,7 @@ declare var X: { { kind: ConvertedSyntaxKind.PropertyDeclaration, name: 'n', + optional: false, type: {kind: ConvertedSyntaxKind.KeywordType, typeName: 'number'}, }, { @@ -291,6 +308,7 @@ declare var X: { { kind: ConvertedSyntaxKind.PropertyDeclaration, name: 'm', + optional: false, type: {kind: ConvertedSyntaxKind.KeywordType, typeName: 'string'}, }, ] @@ -322,16 +340,19 @@ declare var X: { d: number; } { kind: ConvertedSyntaxKind.PropertyDeclaration, name: 'a', + optional: false, type: {kind: ConvertedSyntaxKind.KeywordType, typeName: 'number'}, }, { kind: ConvertedSyntaxKind.PropertyDeclaration, name: 'b', + optional: false, type: {kind: ConvertedSyntaxKind.KeywordType, typeName: 'string'}, }, { kind: ConvertedSyntaxKind.MethodDeclaration, name: 'c', + optional: false, type: {kind: ConvertedSyntaxKind.KeywordType, typeName: 'boolean'}, parameters: [] }, @@ -339,6 +360,7 @@ declare var X: { d: number; } kind: ConvertedSyntaxKind.PropertyDeclaration, modifiers: [{kind: ConvertedSyntaxKind.StaticModifier}], name: 'd', + optional: false, type: {kind: ConvertedSyntaxKind.KeywordType, typeName: 'number'}, } ] @@ -387,11 +409,13 @@ declare var X: { d: number; } // { // kind: ConvertedSyntaxKind.PropertyDeclaration, // name: 'a', + // optional: false, // type: {kind: ConvertedSyntaxKind.KeywordType, typeName: 'number'}, // }, // { // kind: ConvertedSyntaxKind.PropertyDeclaration, // name: 'b', + // optional: false, // type: {kind: ConvertedSyntaxKind.KeywordType, typeName: 'string'}, // }, // ]