Skip to content

Commit

Permalink
feat: Support for parsing optional properties (class & interface) (#80)
Browse files Browse the repository at this point in the history
Related to #80.
  • Loading branch information
ondratra authored and buehler committed Jul 30, 2018
1 parent 596e150 commit 94ea1cc
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 1 deletion.
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.type ? `: ${property.type}` : ''};\n`;
`${property.name}${property.optional ? '?' : ''}${property.type ? `: ${property.type}` : ''};\n`;
}
1 change: 1 addition & 0 deletions src/declarations/PropertyDeclaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export class PropertyDeclaration implements ScopedDeclaration, TypedDeclaration
public name: string,
public visibility: DeclarationVisibility | undefined,
public type: string | undefined,
public optional: boolean,
public start?: number,
public end?: number,
) { }
Expand Down
3 changes: 3 additions & 0 deletions src/node-parser/class-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export function parseCtorParams(
(o.name as Identifier).text,
getNodeVisibility(o),
getNodeType(o.type),
!!o.questionToken,
o.getStart(),
o.getEnd(),
),
Expand Down Expand Up @@ -137,6 +138,7 @@ export function parseClass(tsResource: Resource, node: ClassDeclaration): void {
(o.name as Identifier).text,
getNodeVisibility(o),
getNodeType(o.type),
!!o.questionToken,
o.getStart(),
o.getEnd(),
),
Expand All @@ -148,6 +150,7 @@ export function parseClass(tsResource: Resource, node: ClassDeclaration): void {
(o.name as Identifier).text,
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 @@ -37,6 +37,7 @@ export function parseInterface(resource: Resource, node: InterfaceDeclaration):
(o.name as Identifier).text,
DeclarationVisibility.Public,
getNodeType(o.type),
!!o.questionToken,
o.getStart(),
o.getEnd(),
),
Expand Down
8 changes: 8 additions & 0 deletions test/__snapshots__/TypescriptParser.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ ClassDeclaration {
PropertyDeclaration {
"end": 166,
"name": "param1",
"optional": false,
"start": 145,
"type": "string",
"visibility": 2,
Expand Down Expand Up @@ -172,20 +173,23 @@ ClassDeclaration {
PropertyDeclaration {
"end": 363,
"name": "_property",
"optional": false,
"start": 337,
"type": "string",
"visibility": 0,
},
PropertyDeclaration {
"end": 394,
"name": "protect",
"optional": false,
"start": 368,
"type": "string",
"visibility": 1,
},
PropertyDeclaration {
"end": 418,
"name": "pub",
"optional": false,
"start": 399,
"type": "string",
"visibility": 2,
Expand Down Expand Up @@ -653,13 +657,15 @@ InterfaceDeclaration {
PropertyDeclaration {
"end": 55,
"name": "property1",
"optional": false,
"start": 37,
"type": "string",
"visibility": 2,
},
PropertyDeclaration {
"end": 78,
"name": "property2",
"optional": false,
"start": 60,
"type": "number",
"visibility": 2,
Expand Down Expand Up @@ -743,13 +749,15 @@ InterfaceDeclaration {
PropertyDeclaration {
"end": 191,
"name": "property1",
"optional": false,
"start": 173,
"type": "string",
"visibility": 2,
},
PropertyDeclaration {
"end": 214,
"name": "property2",
"optional": false,
"start": 196,
"type": "number",
"visibility": 2,
Expand Down

0 comments on commit 94ea1cc

Please sign in to comment.