Skip to content

Commit 16da8bb

Browse files
arvtmcw
authored andcommitted
Infer optional record fields in object types (#547)
This is done with Doctrine's OptionalType, which Doctrine is only using for parameter types as of now. Towards #512
1 parent 71922c7 commit 16da8bb

6 files changed

+240
-2
lines changed

lib/flow_doctrine.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
'use strict';
2+
13
var namedTypes = {
24
'NumberTypeAnnotation': 'number',
35
'BooleanTypeAnnotation': 'boolean',
@@ -18,10 +20,19 @@ var literalTypes = {
1820
};
1921

2022
function propertyToField(property) {
23+
var type = flowDoctrine(property.value);
24+
if (property.optional) {
25+
// Doctrine does not support optional fields but it does have something called optional types
26+
// (which makes no sense, but let's play along).
27+
type = {
28+
type: 'OptionalType',
29+
expression: type
30+
};
31+
}
2132
return {
2233
type: 'FieldType',
2334
key: property.key.name,
24-
value: flowDoctrine(property.value)
35+
value: type
2536
};
2637
}
2738

lib/infer/properties.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,18 @@ function inferProperties() {
2121
}
2222

2323
function propertyToDoc(property, prefix) {
24+
var type = flowDoctrine(property.value);
25+
if (property.optional) {
26+
type = {
27+
type: 'OptionalType',
28+
expression: type
29+
};
30+
}
2431
var newProperty = {
2532
title: 'property',
2633
name: prefixedName(property.key.name, prefix),
2734
lineNumber: property.loc.start.line,
28-
type: flowDoctrine(property.value)
35+
type: type
2936
};
3037
return newProperty;
3138
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/** */
2+
type Record = {
3+
opt?: number,
4+
req: string,
5+
};
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
[
2+
{
3+
"description": "",
4+
"tags": [],
5+
"loc": {
6+
"start": {
7+
"line": 1,
8+
"column": 0
9+
},
10+
"end": {
11+
"line": 1,
12+
"column": 6
13+
}
14+
},
15+
"context": {
16+
"loc": {
17+
"start": {
18+
"line": 2,
19+
"column": 0
20+
},
21+
"end": {
22+
"line": 5,
23+
"column": 2
24+
}
25+
}
26+
},
27+
"errors": [],
28+
"name": "Record",
29+
"kind": "typedef",
30+
"properties": [
31+
{
32+
"title": "property",
33+
"name": "opt",
34+
"lineNumber": 3,
35+
"type": {
36+
"type": "OptionalType",
37+
"expression": {
38+
"type": "NameExpression",
39+
"name": "number"
40+
}
41+
}
42+
},
43+
{
44+
"title": "property",
45+
"name": "req",
46+
"lineNumber": 4,
47+
"type": {
48+
"type": "NameExpression",
49+
"name": "string"
50+
}
51+
}
52+
],
53+
"type": {
54+
"type": "RecordType",
55+
"fields": [
56+
{
57+
"type": "FieldType",
58+
"key": "opt",
59+
"value": {
60+
"type": "OptionalType",
61+
"expression": {
62+
"type": "NameExpression",
63+
"name": "number"
64+
}
65+
}
66+
},
67+
{
68+
"type": "FieldType",
69+
"key": "req",
70+
"value": {
71+
"type": "NameExpression",
72+
"name": "string"
73+
}
74+
}
75+
]
76+
},
77+
"members": {
78+
"instance": [],
79+
"static": []
80+
},
81+
"path": [
82+
{
83+
"name": "Record",
84+
"kind": "typedef"
85+
}
86+
],
87+
"namespace": "Record"
88+
}
89+
]
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->
2+
3+
# Record
4+
5+
**Properties**
6+
7+
- `opt` **\[[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)]**
8+
- `req` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)**
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
{
2+
"type": "root",
3+
"children": [
4+
{
5+
"type": "html",
6+
"value": "<!-- Generated by documentation.js. Update this documentation by updating the source code. -->"
7+
},
8+
{
9+
"depth": 1,
10+
"type": "heading",
11+
"children": [
12+
{
13+
"type": "text",
14+
"value": "Record"
15+
}
16+
]
17+
},
18+
{
19+
"type": "strong",
20+
"children": [
21+
{
22+
"type": "text",
23+
"value": "Properties"
24+
}
25+
]
26+
},
27+
{
28+
"ordered": false,
29+
"type": "list",
30+
"children": [
31+
{
32+
"type": "listItem",
33+
"children": [
34+
{
35+
"type": "paragraph",
36+
"children": [
37+
{
38+
"type": "inlineCode",
39+
"value": "opt"
40+
},
41+
{
42+
"type": "text",
43+
"value": " "
44+
},
45+
{
46+
"type": "strong",
47+
"children": [
48+
{
49+
"type": "text",
50+
"value": "["
51+
},
52+
{
53+
"href": "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number",
54+
"url": "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number",
55+
"type": "link",
56+
"children": [
57+
{
58+
"type": "text",
59+
"value": "number"
60+
}
61+
]
62+
},
63+
{
64+
"type": "text",
65+
"value": "]"
66+
}
67+
]
68+
},
69+
{
70+
"type": "text",
71+
"value": " "
72+
}
73+
]
74+
}
75+
]
76+
},
77+
{
78+
"type": "listItem",
79+
"children": [
80+
{
81+
"type": "paragraph",
82+
"children": [
83+
{
84+
"type": "inlineCode",
85+
"value": "req"
86+
},
87+
{
88+
"type": "text",
89+
"value": " "
90+
},
91+
{
92+
"type": "strong",
93+
"children": [
94+
{
95+
"href": "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String",
96+
"url": "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String",
97+
"type": "link",
98+
"children": [
99+
{
100+
"type": "text",
101+
"value": "string"
102+
}
103+
]
104+
}
105+
]
106+
},
107+
{
108+
"type": "text",
109+
"value": " "
110+
}
111+
]
112+
}
113+
]
114+
}
115+
]
116+
}
117+
]
118+
}

0 commit comments

Comments
 (0)