Skip to content

Commit 3cc4426

Browse files
committed
fix(params): Parameters with default use = not ?
Fixes #737
1 parent 2c19809 commit 3cc4426

15 files changed

+67
-181
lines changed

lib/infer/params.js

+2-9
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,7 @@ function paramToDoc(
105105
default: generate(param.right, {
106106
compact: true
107107
}).code,
108-
type: {
109-
type: 'OptionalType',
110-
expression: newAssignmentParam.type
111-
}
108+
type: newAssignmentParam.type
112109
});
113110
// ObjectPattern <AssignmentProperty | RestElement>
114111
case 'ObjectPattern': // { a }
@@ -311,11 +308,7 @@ function combineTags(inferredTag, explicitTag) {
311308
var defaultValue;
312309
if (!explicitTag.type) {
313310
type = inferredTag.type;
314-
} else if (explicitTag.type.type !== 'OptionalType' && inferredTag.default) {
315-
type = {
316-
type: 'OptionalType',
317-
expression: explicitTag.type
318-
};
311+
} else if (!explicitTag.default && inferredTag.default) {
319312
defaultValue = inferredTag.default;
320313
}
321314

lib/parse.js

+3
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,9 @@ var flatteners = {
252252

253253
if (tag.default) {
254254
param.default = tag.default;
255+
if (param.type && param.type.type === 'OptionalType') {
256+
param.type = param.type.expression;
257+
}
255258
}
256259

257260
result.params.push(param);

test/fixture/es6.output-toc.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ have any parameter descriptions.
77

88
**Parameters**
99

10-
- `$0` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)?** (optional, default `{}`)
11-
- `$0.phoneNumbers` **any?** (optional, default `[]`)
12-
- `$0.emailAddresses` **any?** (optional, default `[]`)
10+
- `$0` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** (optional, default `{}`)
11+
- `$0.phoneNumbers` (optional, default `[]`)
12+
- `$0.emailAddresses` (optional, default `[]`)
1313
- `$0.params` **...any**
1414

1515
## destructure
@@ -111,7 +111,7 @@ This tests our support of optional parameters in ES6
111111

112112
**Parameters**
113113

114-
- `foo` **any?** (optional, default `'bar'`)
114+
- `foo` (optional, default `'bar'`)
115115

116116
## iAmProtected
117117

@@ -133,7 +133,7 @@ Regression check for #498
133133

134134
- `array1` **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)&lt;T>**
135135
- `array2` **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)&lt;T>**
136-
- `compareFunction` **function (a: T, b: T): [boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?** (optional, default `(a:T,b:T):boolean=>a===b`)
136+
- `compareFunction` **function (a: T, b: T): [boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** (optional, default `(a:T,b:T):boolean=>a===b`)
137137

138138
Returns **[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)**
139139

test/fixture/es6.output.json

+25-40
Original file line numberDiff line numberDiff line change
@@ -88,30 +88,21 @@
8888
"name": "$0",
8989
"anonymous": true,
9090
"type": {
91-
"type": "OptionalType",
92-
"expression": {
93-
"type": "NameExpression",
94-
"name": "Object"
95-
}
91+
"type": "NameExpression",
92+
"name": "Object"
9693
},
9794
"properties": [
9895
{
9996
"title": "param",
10097
"name": "$0.phoneNumbers",
10198
"lineNumber": 6,
102-
"default": "[]",
103-
"type": {
104-
"type": "OptionalType"
105-
}
99+
"default": "[]"
106100
},
107101
{
108102
"title": "param",
109103
"name": "$0.emailAddresses",
110104
"lineNumber": 6,
111-
"default": "[]",
112-
"type": {
113-
"type": "OptionalType"
114-
}
105+
"default": "[]"
115106
},
116107
{
117108
"title": "param",
@@ -2257,10 +2248,7 @@
22572248
"title": "param",
22582249
"name": "foo",
22592250
"lineNumber": 112,
2260-
"default": "'bar'",
2261-
"type": {
2262-
"type": "OptionalType"
2263-
}
2251+
"default": "'bar'"
22642252
}
22652253
],
22662254
"properties": [],
@@ -2724,31 +2712,28 @@
27242712
"name": "compareFunction",
27252713
"lineNumber": 151,
27262714
"type": {
2727-
"type": "OptionalType",
2728-
"expression": {
2729-
"type": "FunctionType",
2730-
"params": [
2731-
{
2732-
"type": "ParameterType",
2733-
"name": "a",
2734-
"expression": {
2735-
"type": "NameExpression",
2736-
"name": "T"
2737-
}
2738-
},
2739-
{
2740-
"type": "ParameterType",
2741-
"name": "b",
2742-
"expression": {
2743-
"type": "NameExpression",
2744-
"name": "T"
2745-
}
2715+
"type": "FunctionType",
2716+
"params": [
2717+
{
2718+
"type": "ParameterType",
2719+
"name": "a",
2720+
"expression": {
2721+
"type": "NameExpression",
2722+
"name": "T"
2723+
}
2724+
},
2725+
{
2726+
"type": "ParameterType",
2727+
"name": "b",
2728+
"expression": {
2729+
"type": "NameExpression",
2730+
"name": "T"
27462731
}
2747-
],
2748-
"result": {
2749-
"type": "NameExpression",
2750-
"name": "boolean"
27512732
}
2733+
],
2734+
"result": {
2735+
"type": "NameExpression",
2736+
"name": "boolean"
27522737
}
27532738
},
27542739
"default": "(a:T,b:T):boolean=>a===b"

test/fixture/es6.output.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ have any parameter descriptions.
3030

3131
**Parameters**
3232

33-
- `$0` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)?** (optional, default `{}`)
34-
- `$0.phoneNumbers` **any?** (optional, default `[]`)
35-
- `$0.emailAddresses` **any?** (optional, default `[]`)
33+
- `$0` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** (optional, default `{}`)
34+
- `$0.phoneNumbers` (optional, default `[]`)
35+
- `$0.emailAddresses` (optional, default `[]`)
3636
- `$0.params` **...any**
3737

3838
## destructure
@@ -134,7 +134,7 @@ This tests our support of optional parameters in ES6
134134

135135
**Parameters**
136136

137-
- `foo` **any?** (optional, default `'bar'`)
137+
- `foo` (optional, default `'bar'`)
138138

139139
## iAmProtected
140140

@@ -156,7 +156,7 @@ Regression check for #498
156156

157157
- `array1` **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)&lt;T>**
158158
- `array2` **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)&lt;T>**
159-
- `compareFunction` **function (a: T, b: T): [boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?** (optional, default `(a:T,b:T):boolean=>a===b`)
159+
- `compareFunction` **function (a: T, b: T): [boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** (optional, default `(a:T,b:T):boolean=>a===b`)
160160

161161
Returns **[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)**
162162

test/fixture/es6.output.md.json

-47
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,6 @@
9494
"value": "Object"
9595
}
9696
]
97-
},
98-
{
99-
"type": "text",
100-
"value": "?"
10197
}
10298
]
10399
},
@@ -142,19 +138,6 @@
142138
"type": "text",
143139
"value": " "
144140
},
145-
{
146-
"type": "strong",
147-
"children": [
148-
{
149-
"type": "text",
150-
"value": "any"
151-
},
152-
{
153-
"type": "text",
154-
"value": "?"
155-
}
156-
]
157-
},
158141
{
159142
"type": "text",
160143
"value": " "
@@ -194,19 +177,6 @@
194177
"type": "text",
195178
"value": " "
196179
},
197-
{
198-
"type": "strong",
199-
"children": [
200-
{
201-
"type": "text",
202-
"value": "any"
203-
},
204-
{
205-
"type": "text",
206-
"value": "?"
207-
}
208-
]
209-
},
210180
{
211181
"type": "text",
212182
"value": " "
@@ -1807,19 +1777,6 @@
18071777
"type": "text",
18081778
"value": " "
18091779
},
1810-
{
1811-
"type": "strong",
1812-
"children": [
1813-
{
1814-
"type": "text",
1815-
"value": "any"
1816-
},
1817-
{
1818-
"type": "text",
1819-
"value": "?"
1820-
}
1821-
]
1822-
},
18231780
{
18241781
"type": "text",
18251782
"value": " "
@@ -2199,10 +2156,6 @@
21992156
"value": "boolean"
22002157
}
22012158
]
2202-
},
2203-
{
2204-
"type": "text",
2205-
"value": "?"
22062159
}
22072160
]
22082161
},

test/fixture/html/nested.config-output.html

+3-3
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ <h3 class='fl m0' id='klass'>
457457
<div class="clearfix small pointer toggle-sibling">
458458
<div class="py1 contain">
459459
<a class='icon pin-right py1 dark-link caret-right'></a>
460-
<span class='code strong strong truncate'>isBuffer(buf, size = 0)</span>
460+
<span class='code strong strong truncate'>isBuffer(buf, size)</span>
461461
</div>
462462
</div>
463463
<div class="clearfix display-none toggle-target">
@@ -468,7 +468,7 @@ <h3 class='fl m0' id='klass'>
468468
<p>This method takes a Buffer object that will be linked to nodejs.org</p>
469469

470470

471-
<div class='pre p1 fill-light mt0'>isBuffer(buf: (<a href="https://nodejs.org/api/buffer.html">Buffer</a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String">string</a>), size: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number">number</a>?): <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean">boolean</a></div>
471+
<div class='pre p1 fill-light mt0'>isBuffer(buf: (<a href="https://nodejs.org/api/buffer.html">Buffer</a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String">string</a>), size: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number">number</a>): <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean">boolean</a></div>
472472

473473

474474

@@ -493,7 +493,7 @@ <h3 class='fl m0' id='klass'>
493493

494494
<div class='space-bottom0'>
495495
<div>
496-
<span class='code bold'>size</span> <code class='quiet'>(<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number">number</a>?
496+
<span class='code bold'>size</span> <code class='quiet'>(<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number">number</a>
497497
= <code>0</code>)</code>
498498
size
499499

test/fixture/html/nested.output.files

+3-3
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ the referenced class type</p>
419419
<div class="clearfix small pointer toggle-sibling">
420420
<div class="py1 contain">
421421
<a class='icon pin-right py1 dark-link caret-right'>▸</a>
422-
<span class='code strong strong truncate'>isBuffer(buf, size = 0)</span>
422+
<span class='code strong strong truncate'>isBuffer(buf, size)</span>
423423
</div>
424424
</div>
425425
<div class="clearfix display-none toggle-target">
@@ -430,7 +430,7 @@ the referenced class type</p>
430430
<p>This method takes a Buffer object that will be linked to nodejs.org</p>
431431

432432

433-
<div class='pre p1 fill-light mt0'>isBuffer(buf: (<a href="https://nodejs.org/api/buffer.html">Buffer</a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String">string</a>), size: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number">number</a>?): <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean">boolean</a></div>
433+
<div class='pre p1 fill-light mt0'>isBuffer(buf: (<a href="https://nodejs.org/api/buffer.html">Buffer</a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String">string</a>), size: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number">number</a>): <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean">boolean</a></div>
434434

435435

436436

@@ -455,7 +455,7 @@ the referenced class type</p>
455455

456456
<div class='space-bottom0'>
457457
<div>
458-
<span class='code bold'>size</span> <code class='quiet'>(<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number">number</a>?
458+
<span class='code bold'>size</span> <code class='quiet'>(<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number">number</a>
459459
= <code>0</code>)</code>
460460
size
461461

test/fixture/nest_params.output.json

+2-5
Original file line numberDiff line numberDiff line change
@@ -333,11 +333,8 @@
333333
}
334334
},
335335
"type": {
336-
"type": "OptionalType",
337-
"expression": {
338-
"type": "NameExpression",
339-
"name": "string"
340-
}
336+
"type": "NameExpression",
337+
"name": "string"
341338
},
342339
"default": "minion"
343340
}

test/fixture/nest_params.output.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
- `employees` **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)&lt;[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)>** The employees who are responsible for the project.
1313
- `employees[].name` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** The name of an employee.
1414
- `employees[].department` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** The employee's department.
15-
- `type` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)?** The employee's type. (optional, default `minion`)
15+
- `type` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** The employee's type. (optional, default `minion`)
1616

1717
## foo
1818

test/fixture/nest_params.output.md.json

-4
Original file line numberDiff line numberDiff line change
@@ -299,10 +299,6 @@
299299
"value": "string"
300300
}
301301
]
302-
},
303-
{
304-
"type": "text",
305-
"value": "?"
306302
}
307303
]
308304
},

0 commit comments

Comments
 (0)