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

Fix: Only set optional property on certain node property (fixes #289) #292

Merged
merged 2 commits into from
May 28, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
30 changes: 21 additions & 9 deletions lib/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,15 +342,6 @@ module.exports = function convert(config) {
type: AST_NODE_TYPES.Identifier,
name: nodeUtils.unescapeIdentifier(node.text)
});
if (node.parent.questionToken && (
SyntaxKind.Parameter === node.parent.kind ||
SyntaxKind.PropertyDeclaration === node.parent.kind ||
SyntaxKind.PropertySignature === node.parent.kind ||
SyntaxKind.MethodDeclaration === node.parent.kind ||
SyntaxKind.MethodSignature === node.parent.kind
)) {
result.optional = true;
}
break;

case SyntaxKind.WithStatement:
Expand Down Expand Up @@ -691,6 +682,10 @@ module.exports = function convert(config) {
decorators: convertDecorators(node.decorators),
typeAnnotation: (node.type) ? convertTypeAnnotation(node.type) : null
});

if (node.name.kind === SyntaxKind.Identifier && node.questionToken) {
result.key.optional = true;
}
break;
}

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

}

if (result.key.type === AST_NODE_TYPES.Identifier && node.questionToken) {
result.key.optional = true;
}

if (node.kind === SyntaxKind.GetAccessor) {
result.kind = "get";
} else if (node.kind === SyntaxKind.SetAccessor) {
Expand Down Expand Up @@ -1113,6 +1112,10 @@ module.exports = function convert(config) {
});
}

if (node.questionToken) {
parameter.optional = true;
}

if (node.modifiers) {
return {
type: AST_NODE_TYPES.TSParameterProperty,
Expand Down Expand Up @@ -1767,6 +1770,15 @@ module.exports = function convert(config) {
});
break;

case SyntaxKind.PropertySignature:
case SyntaxKind.MethodSignature:
deeplyCopy();

if (node.questionToken) {
result.name.optional = true;
}
break;

default:
deeplyCopy();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,319 @@
module.exports = {
"type": "Program",
"range": [
0,
39
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"body": [
{
"type": "ClassDeclaration",
"range": [
0,
39
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"id": {
"type": "Identifier",
"range": [
6,
7
],
"loc": {
"start": {
"line": 1,
"column": 6
},
"end": {
"line": 1,
"column": 7
}
},
"name": "X"
},
"body": {
"type": "ClassBody",
"body": [
{
"type": "ClassProperty",
"range": [
12,
37
],
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 2,
"column": 27
}
},
"key": {
"type": "Identifier",
"range": [
20,
23
],
"loc": {
"start": {
"line": 2,
"column": 10
},
"end": {
"line": 2,
"column": 13
}
},
"name": "foo",
"optional": true
},
"value": {
"type": "Identifier",
"range": [
27,
36
],
"loc": {
"start": {
"line": 2,
"column": 17
},
"end": {
"line": 2,
"column": 26
}
},
"name": "undefined"
},
"computed": false,
"static": false,
"accessibility": "private",
"decorators": [],
"typeAnnotation": null
}
],
"range": [
8,
39
],
"loc": {
"start": {
"line": 1,
"column": 8
},
"end": {
"line": 3,
"column": 1
}
}
},
"superClass": null,
"implements": [],
"decorators": []
}
],
"sourceType": "script",
"tokens": [
{
"type": "Keyword",
"value": "class",
"range": [
0,
5
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 5
}
}
},
{
"type": "Identifier",
"value": "X",
"range": [
6,
7
],
"loc": {
"start": {
"line": 1,
"column": 6
},
"end": {
"line": 1,
"column": 7
}
}
},
{
"type": "Punctuator",
"value": "{",
"range": [
8,
9
],
"loc": {
"start": {
"line": 1,
"column": 8
},
"end": {
"line": 1,
"column": 9
}
}
},
{
"type": "Keyword",
"value": "private",
"range": [
12,
19
],
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 2,
"column": 9
}
}
},
{
"type": "Identifier",
"value": "foo",
"range": [
20,
23
],
"loc": {
"start": {
"line": 2,
"column": 10
},
"end": {
"line": 2,
"column": 13
}
}
},
{
"type": "Punctuator",
"value": "?",
"range": [
23,
24
],
"loc": {
"start": {
"line": 2,
"column": 13
},
"end": {
"line": 2,
"column": 14
}
}
},
{
"type": "Punctuator",
"value": "=",
"range": [
25,
26
],
"loc": {
"start": {
"line": 2,
"column": 15
},
"end": {
"line": 2,
"column": 16
}
}
},
{
"type": "Keyword",
"value": "undefined",
"range": [
27,
36
],
"loc": {
"start": {
"line": 2,
"column": 17
},
"end": {
"line": 2,
"column": 26
}
}
},
{
"type": "Punctuator",
"value": ";",
"range": [
36,
37
],
"loc": {
"start": {
"line": 2,
"column": 26
},
"end": {
"line": 2,
"column": 27
}
}
},
{
"type": "Punctuator",
"value": "}",
"range": [
38,
39
],
"loc": {
"start": {
"line": 3,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
}
}
]
};

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class X {
private foo? = undefined;
}