Skip to content

Cleanup literal types #522

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 4, 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
8 changes: 4 additions & 4 deletions lib/flow_doctrine.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ var oneToOne = {
};

var literalTypes = {
'BooleanLiteralTypeAnnotation': 'BooleanLiteral',
'NumericLiteralTypeAnnotation': 'NumberLiteral',
'StringLiteralTypeAnnotation': 'StringLiteral'
'BooleanLiteralTypeAnnotation': 'BooleanLiteralType',
'NumericLiteralTypeAnnotation': 'NumericLiteralType',
'StringLiteralTypeAnnotation': 'StringLiteralType'
};

function propertyToField(property) {
Expand Down Expand Up @@ -127,7 +127,7 @@ function flowDoctrine(type) {
if (type.type in literalTypes) {
return {
type: literalTypes[type.type],
name: type.value
value: type.value
};
}
}
Expand Down
8 changes: 6 additions & 2 deletions lib/output/util/format_type.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ function link(text, getHref, description) {
return u('link', {
href: href,
url: href
}, [u('text', description || text)]);
}, [t(description || text)]);
}
return u('text', text);
return t(text);
}

/**
Expand Down Expand Up @@ -169,6 +169,10 @@ function formatType(getHref, node) {
return decorate(formatType(getHref, node.expression), '!', node.prefix);
case Syntax.NullableType:
return decorate(formatType(getHref, node.expression), '?', node.prefix);
case Syntax.StringLiteralType:
return [u('inlineCode', JSON.stringify(node.value))];
case Syntax.NumericLiteralType:
return [u('inlineCode', String(node.value))];

default:
throw new Error('Unknown type ' + node.type);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"concat-stream": "^1.5.0",
"debounce": "^1.0.0",
"disparity": "^2.0.0",
"doctrine": "^1.1.0",
"doctrine": "^1.3.0",
"events": "^1.1.0",
"extend": "^3.0.0",
"get-comments": "^1.0.1",
Expand Down
7 changes: 7 additions & 0 deletions test/fixture/literal_types.input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* @param {'a' | "b" | '' | 0 | 42 | 3.14} x
*/
function f(x) {}

/** */
function g(x: 'a' | "b" | '' | 0 | 42 | 3.14) {}
189 changes: 189 additions & 0 deletions test/fixture/literal_types.output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
[
{
"description": "",
"tags": [
{
"title": "param",
"description": null,
"lineNumber": 1,
"type": {
"type": "UnionType",
"elements": [
{
"type": "StringLiteralType",
"value": "a"
},
{
"type": "StringLiteralType",
"value": "b"
},
{
"type": "StringLiteralType",
"value": ""
},
{
"type": "NumericLiteralType",
"value": 0
},
{
"type": "NumericLiteralType",
"value": 42
},
{
"type": "NumericLiteralType",
"value": 3.14
}
]
},
"name": "x"
}
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 3
}
},
"context": {
"loc": {
"start": {
"line": 4,
"column": 0
},
"end": {
"line": 4,
"column": 16
}
}
},
"errors": [],
"params": [
{
"name": "x",
"lineNumber": 1,
"type": {
"type": "UnionType",
"elements": [
{
"type": "StringLiteralType",
"value": "a"
},
{
"type": "StringLiteralType",
"value": "b"
},
{
"type": "StringLiteralType",
"value": ""
},
{
"type": "NumericLiteralType",
"value": 0
},
{
"type": "NumericLiteralType",
"value": 42
},
{
"type": "NumericLiteralType",
"value": 3.14
}
]
}
}
],
"name": "f",
"kind": "function",
"members": {
"instance": [],
"static": []
},
"path": [
{
"name": "f",
"kind": "function"
}
],
"namespace": "f"
},
{
"description": "",
"tags": [],
"loc": {
"start": {
"line": 6,
"column": 0
},
"end": {
"line": 6,
"column": 6
}
},
"context": {
"loc": {
"start": {
"line": 7,
"column": 0
},
"end": {
"line": 7,
"column": 48
}
}
},
"errors": [],
"name": "g",
"kind": "function",
"params": [
{
"title": "param",
"name": "x",
"lineNumber": 7,
"type": {
"type": "UnionType",
"elements": [
{
"type": "StringLiteralType",
"value": "a"
},
{
"type": "StringLiteralType",
"value": "b"
},
{
"type": "StringLiteralType",
"value": ""
},
{
"type": "NumericLiteralType",
"value": 0
},
{
"type": "NumericLiteralType",
"value": 42
},
{
"type": "NumericLiteralType",
"value": 3.14
}
]
}
}
],
"members": {
"instance": [],
"static": []
},
"path": [
{
"name": "g",
"kind": "function"
}
],
"namespace": "g"
}
]
13 changes: 13 additions & 0 deletions test/fixture/literal_types.output.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->

# f

**Parameters**

- `x` **(`"a"` \| `"b"` \| `""` \| `0` \| `42` \| `3.14`)**

# g

**Parameters**

- `x` **(`"a"` \| `"b"` \| `""` \| `0` \| `42` \| `3.14`)**
Loading