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

Commit 379dcaf

Browse files
soda0289JamesHenry
authored andcommitted
Fix: Only set optional property on certain node property (fixes #289) (#292)
1 parent 89f8561 commit 379dcaf

5 files changed

+706
-9
lines changed

lib/convert.js

+25-9
Original file line numberDiff line numberDiff line change
@@ -326,15 +326,6 @@ module.exports = function convert(config) {
326326
type: AST_NODE_TYPES.Identifier,
327327
name: nodeUtils.unescapeIdentifier(node.text)
328328
});
329-
if (node.parent.questionToken && (
330-
SyntaxKind.Parameter === node.parent.kind ||
331-
SyntaxKind.PropertyDeclaration === node.parent.kind ||
332-
SyntaxKind.PropertySignature === node.parent.kind ||
333-
SyntaxKind.MethodDeclaration === node.parent.kind ||
334-
SyntaxKind.MethodSignature === node.parent.kind
335-
)) {
336-
result.optional = true;
337-
}
338329
break;
339330

340331
case SyntaxKind.WithStatement:
@@ -675,6 +666,14 @@ module.exports = function convert(config) {
675666
decorators: convertDecorators(node.decorators),
676667
typeAnnotation: (node.type) ? convertTypeAnnotation(node.type) : null
677668
});
669+
670+
if (node.name.kind === SyntaxKind.Identifier && node.questionToken) {
671+
result.key.optional = true;
672+
}
673+
674+
if (result.key.type === AST_NODE_TYPES.Literal && node.questionToken) {
675+
result.optional = true;
676+
}
678677
break;
679678
}
680679

@@ -753,6 +752,10 @@ module.exports = function convert(config) {
753752

754753
}
755754

755+
if (result.key.type === AST_NODE_TYPES.Identifier && node.questionToken) {
756+
result.key.optional = true;
757+
}
758+
756759
if (node.kind === SyntaxKind.GetAccessor) {
757760
result.kind = "get";
758761
} else if (node.kind === SyntaxKind.SetAccessor) {
@@ -1097,6 +1100,10 @@ module.exports = function convert(config) {
10971100
});
10981101
}
10991102

1103+
if (node.questionToken) {
1104+
parameter.optional = true;
1105+
}
1106+
11001107
if (node.modifiers) {
11011108
return {
11021109
type: AST_NODE_TYPES.TSParameterProperty,
@@ -1764,6 +1771,15 @@ module.exports = function convert(config) {
17641771
});
17651772
break;
17661773

1774+
case SyntaxKind.PropertySignature:
1775+
case SyntaxKind.MethodSignature:
1776+
deeplyCopy();
1777+
1778+
if (node.questionToken) {
1779+
result.name.optional = true;
1780+
}
1781+
break;
1782+
17671783
default:
17681784
deeplyCopy();
17691785
}

0 commit comments

Comments
 (0)