Skip to content

Commit

Permalink
Added optional boolean to interface and class declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
derekxu16 committed Dec 19, 2019
1 parent a80589c commit 26668bb
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 2 deletions.
14 changes: 13 additions & 1 deletion lib/json/named_declarations/member_declaration.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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);
Expand Down
1 change: 0 additions & 1 deletion lib/json/named_declarations/parameter_declaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
29 changes: 29 additions & 0 deletions test/json/declarations/class_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'}
}
]
Expand All @@ -239,6 +241,7 @@ describe('classes', () => {
members: [{
kind: ConvertedSyntaxKind.MethodDeclaration,
name: 'f',
optional: false,
type: {kind: ConvertedSyntaxKind.KeywordType, typeName: 'void'},
parameters: []
}]
Expand All @@ -256,6 +259,7 @@ describe('classes', () => {
members: [{
kind: ConvertedSyntaxKind.MethodDeclaration,
name: 'f',
optional: false,
type: {kind: ConvertedSyntaxKind.KeywordType, typeName: 'boolean'},
parameters: [
{
Expand Down Expand Up @@ -291,6 +295,7 @@ describe('classes', () => {
kind: ConvertedSyntaxKind.MethodDeclaration,
modifiers: [{kind: ConvertedSyntaxKind.AbstractModifier}],
name: 'f',
optional: false,
type: {kind: ConvertedSyntaxKind.KeywordType, typeName: 'number'},
parameters: []
}]
Expand Down Expand Up @@ -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: []
}
Expand All @@ -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: []
}
Expand All @@ -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,
Expand All @@ -426,6 +453,7 @@ declare class X {
kind: ConvertedSyntaxKind.PropertyDeclaration,
modifiers: [{kind: ConvertedSyntaxKind.ReadonlyModifier}],
name: 'a',
optional: false,
type: {kind: ConvertedSyntaxKind.KeywordType, typeName: 'number'}
}]
}]
Expand All @@ -444,6 +472,7 @@ declare class X {
kind: ConvertedSyntaxKind.PropertyDeclaration,
modifiers: [{kind: ConvertedSyntaxKind.StaticModifier}],
name: 'a',
optional: false,
type: {kind: ConvertedSyntaxKind.KeywordType, typeName: 'number'}
}]
}]
Expand Down
24 changes: 24 additions & 0 deletions test/json/declarations/interface_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'}
}
]
Expand All @@ -156,6 +158,7 @@ describe('interfaces', () => {
members: [{
kind: ConvertedSyntaxKind.MethodDeclaration,
name: 'f',
optional: false,
type: {kind: ConvertedSyntaxKind.KeywordType, typeName: 'void'},
parameters: []
}]
Expand All @@ -173,6 +176,7 @@ describe('interfaces', () => {
members: [{
kind: ConvertedSyntaxKind.MethodDeclaration,
name: 'f',
optional: false,
type: {kind: ConvertedSyntaxKind.KeywordType, typeName: 'boolean'},
parameters: [
{
Expand Down Expand Up @@ -208,13 +212,32 @@ describe('interfaces', () => {
kind: ConvertedSyntaxKind.MethodDeclaration,
modifiers: [{kind: ConvertedSyntaxKind.AbstractModifier}],
name: 'f',
optional: false,
type: {kind: ConvertedSyntaxKind.KeywordType, typeName: 'number'},
parameters: []
}]
}]
}));
});

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,
Expand All @@ -227,6 +250,7 @@ describe('interfaces', () => {
kind: ConvertedSyntaxKind.PropertyDeclaration,
modifiers: [{kind: ConvertedSyntaxKind.ReadonlyModifier}],
name: 'a',
optional: false,
type: {kind: ConvertedSyntaxKind.KeywordType, typeName: 'number'}
}]
}]
Expand Down
24 changes: 24 additions & 0 deletions test/json/merge_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: []
}
Expand All @@ -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: []
},
Expand Down Expand Up @@ -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: []
}
Expand All @@ -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: []
},
Expand Down Expand Up @@ -197,6 +209,7 @@ declare var X: {
{
kind: ConvertedSyntaxKind.PropertyDeclaration,
name: 'n',
optional: false,
type: {kind: ConvertedSyntaxKind.KeywordType, typeName: 'number'},
},
]
Expand All @@ -209,6 +222,7 @@ declare var X: {
{
kind: ConvertedSyntaxKind.PropertyDeclaration,
name: 'n',
optional: false,
type: {kind: ConvertedSyntaxKind.KeywordType, typeName: 'number'},
},
{
Expand All @@ -227,6 +241,7 @@ declare var X: {
kind: ConvertedSyntaxKind.PropertyDeclaration,
modifiers: [{kind: ConvertedSyntaxKind.StaticModifier}],
name: 'm',
optional: false,
type: {kind: ConvertedSyntaxKind.KeywordType, typeName: 'string'},
},
]
Expand Down Expand Up @@ -262,6 +277,7 @@ declare var X: {
{
kind: ConvertedSyntaxKind.PropertyDeclaration,
name: 'n',
optional: false,
type: {kind: ConvertedSyntaxKind.KeywordType, typeName: 'number'},
},
]
Expand All @@ -274,6 +290,7 @@ declare var X: {
{
kind: ConvertedSyntaxKind.PropertyDeclaration,
name: 'n',
optional: false,
type: {kind: ConvertedSyntaxKind.KeywordType, typeName: 'number'},
},
{
Expand All @@ -291,6 +308,7 @@ declare var X: {
{
kind: ConvertedSyntaxKind.PropertyDeclaration,
name: 'm',
optional: false,
type: {kind: ConvertedSyntaxKind.KeywordType, typeName: 'string'},
},
]
Expand Down Expand Up @@ -322,23 +340,27 @@ 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: []
},
{
kind: ConvertedSyntaxKind.PropertyDeclaration,
modifiers: [{kind: ConvertedSyntaxKind.StaticModifier}],
name: 'd',
optional: false,
type: {kind: ConvertedSyntaxKind.KeywordType, typeName: 'number'},
}
]
Expand Down Expand Up @@ -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'},
// },
// ]
Expand Down

0 comments on commit 26668bb

Please sign in to comment.