Skip to content
This repository was archived by the owner on Jan 19, 2019. It is now read-only.

Commit 67984de

Browse files
committed
Fix: Only set optional property on certain node property (fixes #289)
1 parent 525a544 commit 67984de

File tree

3 files changed

+344
-9
lines changed

3 files changed

+344
-9
lines changed

lib/convert.js

+21-9
Original file line numberDiff line numberDiff line change
@@ -342,15 +342,6 @@ module.exports = function convert(config) {
342342
type: AST_NODE_TYPES.Identifier,
343343
name: nodeUtils.unescapeIdentifier(node.text)
344344
});
345-
if (node.parent.questionToken && (
346-
SyntaxKind.Parameter === node.parent.kind ||
347-
SyntaxKind.PropertyDeclaration === node.parent.kind ||
348-
SyntaxKind.PropertySignature === node.parent.kind ||
349-
SyntaxKind.MethodDeclaration === node.parent.kind ||
350-
SyntaxKind.MethodSignature === node.parent.kind
351-
)) {
352-
result.optional = true;
353-
}
354345
break;
355346

356347
case SyntaxKind.WithStatement:
@@ -691,6 +682,10 @@ module.exports = function convert(config) {
691682
decorators: convertDecorators(node.decorators),
692683
typeAnnotation: (node.type) ? convertTypeAnnotation(node.type) : null
693684
});
685+
686+
if (node.name.kind === SyntaxKind.Identifier && node.questionToken) {
687+
result.key.optional = true;
688+
}
694689
break;
695690
}
696691

@@ -769,6 +764,10 @@ module.exports = function convert(config) {
769764

770765
}
771766

767+
if (result.key.type === AST_NODE_TYPES.Identifier && node.questionToken) {
768+
result.key.optional = true;
769+
}
770+
772771
if (node.kind === SyntaxKind.GetAccessor) {
773772
result.kind = "get";
774773
} else if (node.kind === SyntaxKind.SetAccessor) {
@@ -1113,6 +1112,10 @@ module.exports = function convert(config) {
11131112
});
11141113
}
11151114

1115+
if (node.questionToken) {
1116+
parameter.optional = true;
1117+
}
1118+
11161119
if (node.modifiers) {
11171120
return {
11181121
type: AST_NODE_TYPES.TSParameterProperty,
@@ -1767,6 +1770,15 @@ module.exports = function convert(config) {
17671770
});
17681771
break;
17691772

1773+
case SyntaxKind.PropertySignature:
1774+
case SyntaxKind.MethodSignature:
1775+
deeplyCopy();
1776+
1777+
if (node.questionToken) {
1778+
result.name.optional = true;
1779+
}
1780+
break;
1781+
17701782
default:
17711783
deeplyCopy();
17721784
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,319 @@
1+
module.exports = {
2+
"type": "Program",
3+
"range": [
4+
0,
5+
39
6+
],
7+
"loc": {
8+
"start": {
9+
"line": 1,
10+
"column": 0
11+
},
12+
"end": {
13+
"line": 3,
14+
"column": 1
15+
}
16+
},
17+
"body": [
18+
{
19+
"type": "ClassDeclaration",
20+
"range": [
21+
0,
22+
39
23+
],
24+
"loc": {
25+
"start": {
26+
"line": 1,
27+
"column": 0
28+
},
29+
"end": {
30+
"line": 3,
31+
"column": 1
32+
}
33+
},
34+
"id": {
35+
"type": "Identifier",
36+
"range": [
37+
6,
38+
7
39+
],
40+
"loc": {
41+
"start": {
42+
"line": 1,
43+
"column": 6
44+
},
45+
"end": {
46+
"line": 1,
47+
"column": 7
48+
}
49+
},
50+
"name": "X"
51+
},
52+
"body": {
53+
"type": "ClassBody",
54+
"body": [
55+
{
56+
"type": "ClassProperty",
57+
"range": [
58+
12,
59+
37
60+
],
61+
"loc": {
62+
"start": {
63+
"line": 2,
64+
"column": 2
65+
},
66+
"end": {
67+
"line": 2,
68+
"column": 27
69+
}
70+
},
71+
"key": {
72+
"type": "Identifier",
73+
"range": [
74+
20,
75+
23
76+
],
77+
"loc": {
78+
"start": {
79+
"line": 2,
80+
"column": 10
81+
},
82+
"end": {
83+
"line": 2,
84+
"column": 13
85+
}
86+
},
87+
"name": "foo",
88+
"optional": true
89+
},
90+
"value": {
91+
"type": "Identifier",
92+
"range": [
93+
27,
94+
36
95+
],
96+
"loc": {
97+
"start": {
98+
"line": 2,
99+
"column": 17
100+
},
101+
"end": {
102+
"line": 2,
103+
"column": 26
104+
}
105+
},
106+
"name": "undefined"
107+
},
108+
"computed": false,
109+
"static": false,
110+
"accessibility": "private",
111+
"decorators": [],
112+
"typeAnnotation": null
113+
}
114+
],
115+
"range": [
116+
8,
117+
39
118+
],
119+
"loc": {
120+
"start": {
121+
"line": 1,
122+
"column": 8
123+
},
124+
"end": {
125+
"line": 3,
126+
"column": 1
127+
}
128+
}
129+
},
130+
"superClass": null,
131+
"implements": [],
132+
"decorators": []
133+
}
134+
],
135+
"sourceType": "script",
136+
"tokens": [
137+
{
138+
"type": "Keyword",
139+
"value": "class",
140+
"range": [
141+
0,
142+
5
143+
],
144+
"loc": {
145+
"start": {
146+
"line": 1,
147+
"column": 0
148+
},
149+
"end": {
150+
"line": 1,
151+
"column": 5
152+
}
153+
}
154+
},
155+
{
156+
"type": "Identifier",
157+
"value": "X",
158+
"range": [
159+
6,
160+
7
161+
],
162+
"loc": {
163+
"start": {
164+
"line": 1,
165+
"column": 6
166+
},
167+
"end": {
168+
"line": 1,
169+
"column": 7
170+
}
171+
}
172+
},
173+
{
174+
"type": "Punctuator",
175+
"value": "{",
176+
"range": [
177+
8,
178+
9
179+
],
180+
"loc": {
181+
"start": {
182+
"line": 1,
183+
"column": 8
184+
},
185+
"end": {
186+
"line": 1,
187+
"column": 9
188+
}
189+
}
190+
},
191+
{
192+
"type": "Keyword",
193+
"value": "private",
194+
"range": [
195+
12,
196+
19
197+
],
198+
"loc": {
199+
"start": {
200+
"line": 2,
201+
"column": 2
202+
},
203+
"end": {
204+
"line": 2,
205+
"column": 9
206+
}
207+
}
208+
},
209+
{
210+
"type": "Identifier",
211+
"value": "foo",
212+
"range": [
213+
20,
214+
23
215+
],
216+
"loc": {
217+
"start": {
218+
"line": 2,
219+
"column": 10
220+
},
221+
"end": {
222+
"line": 2,
223+
"column": 13
224+
}
225+
}
226+
},
227+
{
228+
"type": "Punctuator",
229+
"value": "?",
230+
"range": [
231+
23,
232+
24
233+
],
234+
"loc": {
235+
"start": {
236+
"line": 2,
237+
"column": 13
238+
},
239+
"end": {
240+
"line": 2,
241+
"column": 14
242+
}
243+
}
244+
},
245+
{
246+
"type": "Punctuator",
247+
"value": "=",
248+
"range": [
249+
25,
250+
26
251+
],
252+
"loc": {
253+
"start": {
254+
"line": 2,
255+
"column": 15
256+
},
257+
"end": {
258+
"line": 2,
259+
"column": 16
260+
}
261+
}
262+
},
263+
{
264+
"type": "Keyword",
265+
"value": "undefined",
266+
"range": [
267+
27,
268+
36
269+
],
270+
"loc": {
271+
"start": {
272+
"line": 2,
273+
"column": 17
274+
},
275+
"end": {
276+
"line": 2,
277+
"column": 26
278+
}
279+
}
280+
},
281+
{
282+
"type": "Punctuator",
283+
"value": ";",
284+
"range": [
285+
36,
286+
37
287+
],
288+
"loc": {
289+
"start": {
290+
"line": 2,
291+
"column": 26
292+
},
293+
"end": {
294+
"line": 2,
295+
"column": 27
296+
}
297+
}
298+
},
299+
{
300+
"type": "Punctuator",
301+
"value": "}",
302+
"range": [
303+
38,
304+
39
305+
],
306+
"loc": {
307+
"start": {
308+
"line": 3,
309+
"column": 0
310+
},
311+
"end": {
312+
"line": 3,
313+
"column": 1
314+
}
315+
}
316+
}
317+
]
318+
};
319+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class X {
2+
private foo? = undefined;
3+
}
4+

0 commit comments

Comments
 (0)