Skip to content

Commit e550f7c

Browse files
authored
feat: add isOptional to methods as well (#84)
Add tests for isOptional properties and methods (#80) and the same flag to methods.
1 parent 94ea1cc commit e550f7c

File tree

13 files changed

+251
-16
lines changed

13 files changed

+251
-16
lines changed

jest.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"transform": {
44
"^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
55
},
6+
"testURL": "http://localhost/",
67
"testMatch": [
78
"**/test/**/*.spec.ts"
89
],

src/code-generators/typescript-generators/propertyDeclaration.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@ export function generatePropertyDeclaration(
1616
): string {
1717
return `${Array(tabSize + 1).join(' ')}` +
1818
`${property.visibility !== undefined ? getVisibilityText(property.visibility) + ' ' : ''}` +
19-
`${property.name}${property.optional ? '?' : ''}${property.type ? `: ${property.type}` : ''};\n`;
19+
`${property.name}${property.isOptional ? '?' : ''}${property.type ? `: ${property.type}` : ''};\n`;
2020
}

src/declarations/Declaration.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,3 +175,20 @@ export interface AbstractDeclaration extends Declaration {
175175
*/
176176
isAbstract: boolean;
177177
}
178+
179+
/**
180+
* Interface for possible optional declarations. Contains information if the element is optional or not.
181+
*
182+
* @export
183+
* @interface OptionalDeclaration
184+
* @extends {Declaration}
185+
*/
186+
export interface OptionalDeclaration extends Declaration {
187+
/**
188+
* Defines if the declaration is optional or not.
189+
*
190+
* @type {boolean}
191+
* @memberof OptionalDeclaration
192+
*/
193+
isOptional: boolean;
194+
}

src/declarations/MethodDeclaration.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import { AbstractDeclaration, CallableDeclaration, ScopedDeclaration, TypedDeclaration } from './Declaration';
1+
import {
2+
AbstractDeclaration,
3+
CallableDeclaration,
4+
OptionalDeclaration,
5+
ScopedDeclaration,
6+
TypedDeclaration,
7+
} from './Declaration';
28
import { DeclarationVisibility } from './DeclarationVisibility';
39
import { ParameterDeclaration } from './ParameterDeclaration';
410
import { VariableDeclaration } from './VariableDeclaration';
@@ -13,7 +19,13 @@ import { VariableDeclaration } from './VariableDeclaration';
1319
* @implements {ScopedDeclaration}
1420
* @implements {TypedDeclaration}
1521
*/
16-
export class MethodDeclaration implements AbstractDeclaration, CallableDeclaration, ScopedDeclaration, TypedDeclaration {
22+
export class MethodDeclaration implements
23+
AbstractDeclaration,
24+
CallableDeclaration,
25+
OptionalDeclaration,
26+
ScopedDeclaration,
27+
TypedDeclaration {
28+
1729
public parameters: ParameterDeclaration[] = [];
1830
public variables: VariableDeclaration[] = [];
1931

@@ -22,6 +34,7 @@ export class MethodDeclaration implements AbstractDeclaration, CallableDeclarati
2234
public isAbstract: boolean,
2335
public visibility: DeclarationVisibility | undefined,
2436
public type: string | undefined,
37+
public isOptional: boolean,
2538
public start?: number,
2639
public end?: number,
2740
) { }

src/declarations/PropertyDeclaration.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ScopedDeclaration, TypedDeclaration } from './Declaration';
1+
import { OptionalDeclaration, ScopedDeclaration, TypedDeclaration } from './Declaration';
22
import { DeclarationVisibility } from './DeclarationVisibility';
33

44
/**
@@ -9,12 +9,12 @@ import { DeclarationVisibility } from './DeclarationVisibility';
99
* @implements {ScopedDeclaration}
1010
* @implements {TypedDeclaration}
1111
*/
12-
export class PropertyDeclaration implements ScopedDeclaration, TypedDeclaration {
12+
export class PropertyDeclaration implements OptionalDeclaration, ScopedDeclaration, TypedDeclaration {
1313
constructor(
1414
public name: string,
1515
public visibility: DeclarationVisibility | undefined,
1616
public type: string | undefined,
17-
public optional: boolean,
17+
public isOptional: boolean,
1818
public start?: number,
1919
public end?: number,
2020
) { }

src/node-parser/class-parser.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ export function parseClass(tsResource: Resource, node: ClassDeclaration): void {
196196
o.modifiers !== undefined && o.modifiers.some(m => m.kind === SyntaxKind.AbstractKeyword),
197197
getNodeVisibility(o),
198198
getNodeType(o.type),
199+
!!o.questionToken,
199200
o.getStart(),
200201
o.getEnd(),
201202
);

src/node-parser/interface-parser.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export function parseInterface(resource: Resource, node: InterfaceDeclaration):
4848
true,
4949
DeclarationVisibility.Public,
5050
getNodeType(o.type),
51+
!!o.questionToken,
5152
o.getStart(),
5253
o.getEnd(),
5354
);

test/TypescriptParser.spec.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ describe('TypescriptParser', () => {
395395
});
396396

397397
it('should parse a file', () => {
398-
expect(parsed.declarations).toHaveLength(4);
398+
expect(parsed.declarations).toHaveLength(6);
399399
});
400400

401401
it('should parse a non exported interface', () => {
@@ -436,6 +436,18 @@ describe('TypescriptParser', () => {
436436
expect(parsedInterface.typeParameters).toContain('TError');
437437
});
438438

439+
it('should parse optional properties', () => {
440+
const parsedInterface = parsed.declarations[4] as InterfaceDeclaration;
441+
442+
expect(parsedInterface.properties).toMatchSnapshot();
443+
});
444+
445+
it('should parse optional functions', () => {
446+
const parsedInterface = parsed.declarations[5] as InterfaceDeclaration;
447+
448+
expect(parsedInterface).toMatchSnapshot();
449+
});
450+
439451
});
440452

441453
describe('Classes', () => {
@@ -448,7 +460,7 @@ describe('TypescriptParser', () => {
448460
});
449461

450462
it('should parse a file', () => {
451-
expect(parsed.declarations).toHaveLength(8);
463+
expect(parsed.declarations).toHaveLength(9);
452464
});
453465

454466
it('should parse an abstract class', () => {
@@ -529,6 +541,12 @@ describe('TypescriptParser', () => {
529541
expect(parsedClass.ctor).toMatchSnapshot();
530542
});
531543

544+
it('should parse optional class properties', () => {
545+
const parsedClass = parsed.declarations[8] as ClassDeclaration;
546+
547+
expect(parsedClass.properties).toMatchSnapshot();
548+
});
549+
532550
});
533551

534552
describe('Modules', () => {

0 commit comments

Comments
 (0)