Skip to content

Commit d0c6f37

Browse files
committed
Cleanup literal types
This updates Doctrine to 1.3 to pick up its support for literal types (doctrine still does not support boolean literals types). Towards documentationjs#373
1 parent 0a97e0e commit d0c6f37

7 files changed

+437
-7
lines changed

lib/flow_doctrine.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ var oneToOne = {
1212
};
1313

1414
var literalTypes = {
15-
'BooleanLiteralTypeAnnotation': 'BooleanLiteral',
16-
'NumericLiteralTypeAnnotation': 'NumberLiteral',
17-
'StringLiteralTypeAnnotation': 'StringLiteral'
15+
'BooleanLiteralTypeAnnotation': 'BooleanLiteralType',
16+
'NumericLiteralTypeAnnotation': 'NumericLiteralType',
17+
'StringLiteralTypeAnnotation': 'StringLiteralType'
1818
};
1919

2020
function propertyToField(property) {
@@ -127,7 +127,7 @@ function flowDoctrine(type) {
127127
if (type.type in literalTypes) {
128128
return {
129129
type: literalTypes[type.type],
130-
name: type.value
130+
value: type.value
131131
};
132132
}
133133
}

lib/output/util/format_type.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ function link(text, getHref, description) {
3333
return u('link', {
3434
href: href,
3535
url: href
36-
}, [u('text', description || text)]);
36+
}, [t(description || text)]);
3737
}
38-
return u('text', text);
38+
return t(text);
3939
}
4040

4141
/**
@@ -169,6 +169,10 @@ function formatType(getHref, node) {
169169
return decorate(formatType(getHref, node.expression), '!', node.prefix);
170170
case Syntax.NullableType:
171171
return decorate(formatType(getHref, node.expression), '?', node.prefix);
172+
case Syntax.StringLiteralType:
173+
return [u('inlineCode', JSON.stringify(node.value))];
174+
case Syntax.NumericLiteralType:
175+
return [u('inlineCode', String(node.value))];
172176

173177
default:
174178
throw new Error('Unknown type ' + node.type);

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"concat-stream": "^1.5.0",
2323
"debounce": "^1.0.0",
2424
"disparity": "^2.0.0",
25-
"doctrine": "^1.1.0",
25+
"doctrine": "^1.3.0",
2626
"events": "^1.1.0",
2727
"extend": "^3.0.0",
2828
"get-comments": "^1.0.1",

test/fixture/literal_types.input.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
* @param {'a' | "b" | '' | 0 | 42 | 3.14} x
3+
*/
4+
function f(x) {}
5+
6+
/** */
7+
function g(x: 'a' | "b" | '' | 0 | 42 | 3.14) {}
+189
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
[
2+
{
3+
"description": "",
4+
"tags": [
5+
{
6+
"title": "param",
7+
"description": null,
8+
"lineNumber": 1,
9+
"type": {
10+
"type": "UnionType",
11+
"elements": [
12+
{
13+
"type": "StringLiteralType",
14+
"value": "a"
15+
},
16+
{
17+
"type": "StringLiteralType",
18+
"value": "b"
19+
},
20+
{
21+
"type": "StringLiteralType",
22+
"value": ""
23+
},
24+
{
25+
"type": "NumericLiteralType",
26+
"value": 0
27+
},
28+
{
29+
"type": "NumericLiteralType",
30+
"value": 42
31+
},
32+
{
33+
"type": "NumericLiteralType",
34+
"value": 3.14
35+
}
36+
]
37+
},
38+
"name": "x"
39+
}
40+
],
41+
"loc": {
42+
"start": {
43+
"line": 1,
44+
"column": 0
45+
},
46+
"end": {
47+
"line": 3,
48+
"column": 3
49+
}
50+
},
51+
"context": {
52+
"loc": {
53+
"start": {
54+
"line": 4,
55+
"column": 0
56+
},
57+
"end": {
58+
"line": 4,
59+
"column": 16
60+
}
61+
}
62+
},
63+
"errors": [],
64+
"params": [
65+
{
66+
"name": "x",
67+
"lineNumber": 1,
68+
"type": {
69+
"type": "UnionType",
70+
"elements": [
71+
{
72+
"type": "StringLiteralType",
73+
"value": "a"
74+
},
75+
{
76+
"type": "StringLiteralType",
77+
"value": "b"
78+
},
79+
{
80+
"type": "StringLiteralType",
81+
"value": ""
82+
},
83+
{
84+
"type": "NumericLiteralType",
85+
"value": 0
86+
},
87+
{
88+
"type": "NumericLiteralType",
89+
"value": 42
90+
},
91+
{
92+
"type": "NumericLiteralType",
93+
"value": 3.14
94+
}
95+
]
96+
}
97+
}
98+
],
99+
"name": "f",
100+
"kind": "function",
101+
"members": {
102+
"instance": [],
103+
"static": []
104+
},
105+
"path": [
106+
{
107+
"name": "f",
108+
"kind": "function"
109+
}
110+
],
111+
"namespace": "f"
112+
},
113+
{
114+
"description": "",
115+
"tags": [],
116+
"loc": {
117+
"start": {
118+
"line": 6,
119+
"column": 0
120+
},
121+
"end": {
122+
"line": 6,
123+
"column": 6
124+
}
125+
},
126+
"context": {
127+
"loc": {
128+
"start": {
129+
"line": 7,
130+
"column": 0
131+
},
132+
"end": {
133+
"line": 7,
134+
"column": 48
135+
}
136+
}
137+
},
138+
"errors": [],
139+
"name": "g",
140+
"kind": "function",
141+
"params": [
142+
{
143+
"title": "param",
144+
"name": "x",
145+
"lineNumber": 7,
146+
"type": {
147+
"type": "UnionType",
148+
"elements": [
149+
{
150+
"type": "StringLiteralType",
151+
"value": "a"
152+
},
153+
{
154+
"type": "StringLiteralType",
155+
"value": "b"
156+
},
157+
{
158+
"type": "StringLiteralType",
159+
"value": ""
160+
},
161+
{
162+
"type": "NumericLiteralType",
163+
"value": 0
164+
},
165+
{
166+
"type": "NumericLiteralType",
167+
"value": 42
168+
},
169+
{
170+
"type": "NumericLiteralType",
171+
"value": 3.14
172+
}
173+
]
174+
}
175+
}
176+
],
177+
"members": {
178+
"instance": [],
179+
"static": []
180+
},
181+
"path": [
182+
{
183+
"name": "g",
184+
"kind": "function"
185+
}
186+
],
187+
"namespace": "g"
188+
}
189+
]

test/fixture/literal_types.output.md

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->
2+
3+
# f
4+
5+
**Parameters**
6+
7+
- `x` **(`"a"` \| `"b"` \| `""` \| `0` \| `42` \| `3.14`)**
8+
9+
# g
10+
11+
**Parameters**
12+
13+
- `x` **(`"a"` \| `"b"` \| `""` \| `0` \| `42` \| `3.14`)**

0 commit comments

Comments
 (0)