Skip to content

Commit 94ea1cc

Browse files
ondratrabuehler
authored andcommitted
feat: Support for parsing optional properties (class & interface) (buehler#80)
Related to buehler#80.
1 parent 596e150 commit 94ea1cc

File tree

5 files changed

+14
-1
lines changed

5 files changed

+14
-1
lines changed

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.type ? `: ${property.type}` : ''};\n`;
19+
`${property.name}${property.optional ? '?' : ''}${property.type ? `: ${property.type}` : ''};\n`;
2020
}

src/declarations/PropertyDeclaration.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export class PropertyDeclaration implements ScopedDeclaration, TypedDeclaration
1414
public name: string,
1515
public visibility: DeclarationVisibility | undefined,
1616
public type: string | undefined,
17+
public optional: boolean,
1718
public start?: number,
1819
public end?: number,
1920
) { }

src/node-parser/class-parser.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ export function parseCtorParams(
8888
(o.name as Identifier).text,
8989
getNodeVisibility(o),
9090
getNodeType(o.type),
91+
!!o.questionToken,
9192
o.getStart(),
9293
o.getEnd(),
9394
),
@@ -137,6 +138,7 @@ export function parseClass(tsResource: Resource, node: ClassDeclaration): void {
137138
(o.name as Identifier).text,
138139
getNodeVisibility(o),
139140
getNodeType(o.type),
141+
!!o.questionToken,
140142
o.getStart(),
141143
o.getEnd(),
142144
),
@@ -148,6 +150,7 @@ export function parseClass(tsResource: Resource, node: ClassDeclaration): void {
148150
(o.name as Identifier).text,
149151
getNodeVisibility(o),
150152
getNodeType(o.type),
153+
!!o.questionToken,
151154
o.getStart(),
152155
o.getEnd(),
153156
),

src/node-parser/interface-parser.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export function parseInterface(resource: Resource, node: InterfaceDeclaration):
3737
(o.name as Identifier).text,
3838
DeclarationVisibility.Public,
3939
getNodeType(o.type),
40+
!!o.questionToken,
4041
o.getStart(),
4142
o.getEnd(),
4243
),

test/__snapshots__/TypescriptParser.spec.ts.snap

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ ClassDeclaration {
6565
PropertyDeclaration {
6666
"end": 166,
6767
"name": "param1",
68+
"optional": false,
6869
"start": 145,
6970
"type": "string",
7071
"visibility": 2,
@@ -172,20 +173,23 @@ ClassDeclaration {
172173
PropertyDeclaration {
173174
"end": 363,
174175
"name": "_property",
176+
"optional": false,
175177
"start": 337,
176178
"type": "string",
177179
"visibility": 0,
178180
},
179181
PropertyDeclaration {
180182
"end": 394,
181183
"name": "protect",
184+
"optional": false,
182185
"start": 368,
183186
"type": "string",
184187
"visibility": 1,
185188
},
186189
PropertyDeclaration {
187190
"end": 418,
188191
"name": "pub",
192+
"optional": false,
189193
"start": 399,
190194
"type": "string",
191195
"visibility": 2,
@@ -653,13 +657,15 @@ InterfaceDeclaration {
653657
PropertyDeclaration {
654658
"end": 55,
655659
"name": "property1",
660+
"optional": false,
656661
"start": 37,
657662
"type": "string",
658663
"visibility": 2,
659664
},
660665
PropertyDeclaration {
661666
"end": 78,
662667
"name": "property2",
668+
"optional": false,
663669
"start": 60,
664670
"type": "number",
665671
"visibility": 2,
@@ -743,13 +749,15 @@ InterfaceDeclaration {
743749
PropertyDeclaration {
744750
"end": 191,
745751
"name": "property1",
752+
"optional": false,
746753
"start": 173,
747754
"type": "string",
748755
"visibility": 2,
749756
},
750757
PropertyDeclaration {
751758
"end": 214,
752759
"name": "property2",
760+
"optional": false,
753761
"start": 196,
754762
"type": "number",
755763
"visibility": 2,

0 commit comments

Comments
 (0)