Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for parsing optional properties (class & interface) #80

Merged
merged 2 commits into from
Jul 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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