Skip to content

Commit

Permalink
feat: add isOptional to methods as well (#84)
Browse files Browse the repository at this point in the history
Add tests for isOptional properties and methods (#80) and
the same flag to methods.
  • Loading branch information
buehler committed Jul 31, 2018
1 parent 94ea1cc commit e550f7c
Show file tree
Hide file tree
Showing 13 changed files with 251 additions and 16 deletions.
1 change: 1 addition & 0 deletions jest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"transform": {
"^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
},
"testURL": "http://localhost/",
"testMatch": [
"**/test/**/*.spec.ts"
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ export function generatePropertyDeclaration(
): string {
return `${Array(tabSize + 1).join(' ')}` +
`${property.visibility !== undefined ? getVisibilityText(property.visibility) + ' ' : ''}` +
`${property.name}${property.optional ? '?' : ''}${property.type ? `: ${property.type}` : ''};\n`;
`${property.name}${property.isOptional ? '?' : ''}${property.type ? `: ${property.type}` : ''};\n`;
}
17 changes: 17 additions & 0 deletions src/declarations/Declaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,20 @@ export interface AbstractDeclaration extends Declaration {
*/
isAbstract: boolean;
}

/**
* Interface for possible optional declarations. Contains information if the element is optional or not.
*
* @export
* @interface OptionalDeclaration
* @extends {Declaration}
*/
export interface OptionalDeclaration extends Declaration {
/**
* Defines if the declaration is optional or not.
*
* @type {boolean}
* @memberof OptionalDeclaration
*/
isOptional: boolean;
}
17 changes: 15 additions & 2 deletions src/declarations/MethodDeclaration.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { AbstractDeclaration, CallableDeclaration, ScopedDeclaration, TypedDeclaration } from './Declaration';
import {
AbstractDeclaration,
CallableDeclaration,
OptionalDeclaration,
ScopedDeclaration,
TypedDeclaration,
} from './Declaration';
import { DeclarationVisibility } from './DeclarationVisibility';
import { ParameterDeclaration } from './ParameterDeclaration';
import { VariableDeclaration } from './VariableDeclaration';
Expand All @@ -13,7 +19,13 @@ import { VariableDeclaration } from './VariableDeclaration';
* @implements {ScopedDeclaration}
* @implements {TypedDeclaration}
*/
export class MethodDeclaration implements AbstractDeclaration, CallableDeclaration, ScopedDeclaration, TypedDeclaration {
export class MethodDeclaration implements
AbstractDeclaration,
CallableDeclaration,
OptionalDeclaration,
ScopedDeclaration,
TypedDeclaration {

public parameters: ParameterDeclaration[] = [];
public variables: VariableDeclaration[] = [];

Expand All @@ -22,6 +34,7 @@ export class MethodDeclaration implements AbstractDeclaration, CallableDeclarati
public isAbstract: boolean,
public visibility: DeclarationVisibility | undefined,
public type: string | undefined,
public isOptional: boolean,
public start?: number,
public end?: number,
) { }
Expand Down
6 changes: 3 additions & 3 deletions src/declarations/PropertyDeclaration.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ScopedDeclaration, TypedDeclaration } from './Declaration';
import { OptionalDeclaration, ScopedDeclaration, TypedDeclaration } from './Declaration';
import { DeclarationVisibility } from './DeclarationVisibility';

/**
Expand All @@ -9,12 +9,12 @@ import { DeclarationVisibility } from './DeclarationVisibility';
* @implements {ScopedDeclaration}
* @implements {TypedDeclaration}
*/
export class PropertyDeclaration implements ScopedDeclaration, TypedDeclaration {
export class PropertyDeclaration implements OptionalDeclaration, ScopedDeclaration, TypedDeclaration {
constructor(
public name: string,
public visibility: DeclarationVisibility | undefined,
public type: string | undefined,
public optional: boolean,
public isOptional: boolean,
public start?: number,
public end?: number,
) { }
Expand Down
1 change: 1 addition & 0 deletions src/node-parser/class-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ export function parseClass(tsResource: Resource, node: ClassDeclaration): void {
o.modifiers !== undefined && o.modifiers.some(m => m.kind === SyntaxKind.AbstractKeyword),
getNodeVisibility(o),
getNodeType(o.type),
!!o.questionToken,
o.getStart(),
o.getEnd(),
);
Expand Down
1 change: 1 addition & 0 deletions src/node-parser/interface-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export function parseInterface(resource: Resource, node: InterfaceDeclaration):
true,
DeclarationVisibility.Public,
getNodeType(o.type),
!!o.questionToken,
o.getStart(),
o.getEnd(),
);
Expand Down
22 changes: 20 additions & 2 deletions test/TypescriptParser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ describe('TypescriptParser', () => {
});

it('should parse a file', () => {
expect(parsed.declarations).toHaveLength(4);
expect(parsed.declarations).toHaveLength(6);
});

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

it('should parse optional properties', () => {
const parsedInterface = parsed.declarations[4] as InterfaceDeclaration;

expect(parsedInterface.properties).toMatchSnapshot();
});

it('should parse optional functions', () => {
const parsedInterface = parsed.declarations[5] as InterfaceDeclaration;

expect(parsedInterface).toMatchSnapshot();
});

});

describe('Classes', () => {
Expand All @@ -448,7 +460,7 @@ describe('TypescriptParser', () => {
});

it('should parse a file', () => {
expect(parsed.declarations).toHaveLength(8);
expect(parsed.declarations).toHaveLength(9);
});

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

it('should parse optional class properties', () => {
const parsedClass = parsed.declarations[8] as ClassDeclaration;

expect(parsedClass.properties).toMatchSnapshot();
});

});

describe('Modules', () => {
Expand Down

0 comments on commit e550f7c

Please sign in to comment.