Skip to content

Infer optional record fields in object types #547

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 12 additions & 1 deletion lib/flow_doctrine.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

var namedTypes = {
'NumberTypeAnnotation': 'number',
'BooleanTypeAnnotation': 'boolean',
Expand All @@ -18,10 +20,19 @@ var literalTypes = {
};

function propertyToField(property) {
var type = flowDoctrine(property.value);
if (property.optional) {
// Doctrine does not support optional fields but it does have something called optional types
// (which makes no sense, but let's play along).
type = {
type: 'OptionalType',
expression: type
};
}
return {
type: 'FieldType',
key: property.key.name,
value: flowDoctrine(property.value)
value: type
};
}

Expand Down
9 changes: 8 additions & 1 deletion lib/infer/properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,18 @@ function inferProperties() {
}

function propertyToDoc(property, prefix) {
var type = flowDoctrine(property.value);
if (property.optional) {
type = {
type: 'OptionalType',
expression: type
};
}
var newProperty = {
title: 'property',
name: prefixedName(property.key.name, prefix),
lineNumber: property.loc.start.line,
type: flowDoctrine(property.value)
type: type
};
return newProperty;
}
Expand Down
5 changes: 5 additions & 0 deletions test/fixture/optional-record-field-type.input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/** */
type Record = {
opt?: number,
req: string,
};
89 changes: 89 additions & 0 deletions test/fixture/optional-record-field-type.output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
[
{
"description": "",
"tags": [],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 6
}
},
"context": {
"loc": {
"start": {
"line": 2,
"column": 0
},
"end": {
"line": 5,
"column": 2
}
}
},
"errors": [],
"name": "Record",
"kind": "typedef",
"properties": [
{
"title": "property",
"name": "opt",
"lineNumber": 3,
"type": {
"type": "OptionalType",
"expression": {
"type": "NameExpression",
"name": "number"
}
}
},
{
"title": "property",
"name": "req",
"lineNumber": 4,
"type": {
"type": "NameExpression",
"name": "string"
}
}
],
"type": {
"type": "RecordType",
"fields": [
{
"type": "FieldType",
"key": "opt",
"value": {
"type": "OptionalType",
"expression": {
"type": "NameExpression",
"name": "number"
}
}
},
{
"type": "FieldType",
"key": "req",
"value": {
"type": "NameExpression",
"name": "string"
}
}
]
},
"members": {
"instance": [],
"static": []
},
"path": [
{
"name": "Record",
"kind": "typedef"
}
],
"namespace": "Record"
}
]
8 changes: 8 additions & 0 deletions test/fixture/optional-record-field-type.output.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->

# Record

**Properties**

- `opt` **\[[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)]**
- `req` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)**
118 changes: 118 additions & 0 deletions test/fixture/optional-record-field-type.output.md.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
{
"type": "root",
"children": [
{
"type": "html",
"value": "<!-- Generated by documentation.js. Update this documentation by updating the source code. -->"
},
{
"depth": 1,
"type": "heading",
"children": [
{
"type": "text",
"value": "Record"
}
]
},
{
"type": "strong",
"children": [
{
"type": "text",
"value": "Properties"
}
]
},
{
"ordered": false,
"type": "list",
"children": [
{
"type": "listItem",
"children": [
{
"type": "paragraph",
"children": [
{
"type": "inlineCode",
"value": "opt"
},
{
"type": "text",
"value": " "
},
{
"type": "strong",
"children": [
{
"type": "text",
"value": "["
},
{
"href": "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number",
"url": "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number",
"type": "link",
"children": [
{
"type": "text",
"value": "number"
}
]
},
{
"type": "text",
"value": "]"
}
]
},
{
"type": "text",
"value": " "
}
]
}
]
},
{
"type": "listItem",
"children": [
{
"type": "paragraph",
"children": [
{
"type": "inlineCode",
"value": "req"
},
{
"type": "text",
"value": " "
},
{
"type": "strong",
"children": [
{
"href": "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String",
"url": "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String",
"type": "link",
"children": [
{
"type": "text",
"value": "string"
}
]
}
]
},
{
"type": "text",
"value": " "
}
]
}
]
}
]
}
]
}