Skip to content

Commit

Permalink
fix(params): Parameters with default use = not ?
Browse files Browse the repository at this point in the history
Fixes #737
  • Loading branch information
tmcw committed Apr 26, 2017
1 parent 2c19809 commit 3cc4426
Show file tree
Hide file tree
Showing 15 changed files with 67 additions and 181 deletions.
11 changes: 2 additions & 9 deletions lib/infer/params.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,7 @@ function paramToDoc(
default: generate(param.right, {
compact: true
}).code,
type: {
type: 'OptionalType',
expression: newAssignmentParam.type
}
type: newAssignmentParam.type
});
// ObjectPattern <AssignmentProperty | RestElement>
case 'ObjectPattern': // { a }
Expand Down Expand Up @@ -311,11 +308,7 @@ function combineTags(inferredTag, explicitTag) {
var defaultValue;
if (!explicitTag.type) {
type = inferredTag.type;
} else if (explicitTag.type.type !== 'OptionalType' && inferredTag.default) {
type = {
type: 'OptionalType',
expression: explicitTag.type
};
} else if (!explicitTag.default && inferredTag.default) {
defaultValue = inferredTag.default;
}

Expand Down
3 changes: 3 additions & 0 deletions lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,9 @@ var flatteners = {

if (tag.default) {
param.default = tag.default;
if (param.type && param.type.type === 'OptionalType') {
param.type = param.type.expression;
}
}

result.params.push(param);
Expand Down
10 changes: 5 additions & 5 deletions test/fixture/es6.output-toc.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ have any parameter descriptions.

**Parameters**

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

## destructure
Expand Down Expand Up @@ -111,7 +111,7 @@ This tests our support of optional parameters in ES6

**Parameters**

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

## iAmProtected

Expand All @@ -133,7 +133,7 @@ Regression check for #498

- `array1` **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)&lt;T>**
- `array2` **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)&lt;T>**
- `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`)
- `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`)

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

Expand Down
65 changes: 25 additions & 40 deletions test/fixture/es6.output.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,30 +88,21 @@
"name": "$0",
"anonymous": true,
"type": {
"type": "OptionalType",
"expression": {
"type": "NameExpression",
"name": "Object"
}
"type": "NameExpression",
"name": "Object"
},
"properties": [
{
"title": "param",
"name": "$0.phoneNumbers",
"lineNumber": 6,
"default": "[]",
"type": {
"type": "OptionalType"
}
"default": "[]"
},
{
"title": "param",
"name": "$0.emailAddresses",
"lineNumber": 6,
"default": "[]",
"type": {
"type": "OptionalType"
}
"default": "[]"
},
{
"title": "param",
Expand Down Expand Up @@ -2257,10 +2248,7 @@
"title": "param",
"name": "foo",
"lineNumber": 112,
"default": "'bar'",
"type": {
"type": "OptionalType"
}
"default": "'bar'"
}
],
"properties": [],
Expand Down Expand Up @@ -2724,31 +2712,28 @@
"name": "compareFunction",
"lineNumber": 151,
"type": {
"type": "OptionalType",
"expression": {
"type": "FunctionType",
"params": [
{
"type": "ParameterType",
"name": "a",
"expression": {
"type": "NameExpression",
"name": "T"
}
},
{
"type": "ParameterType",
"name": "b",
"expression": {
"type": "NameExpression",
"name": "T"
}
"type": "FunctionType",
"params": [
{
"type": "ParameterType",
"name": "a",
"expression": {
"type": "NameExpression",
"name": "T"
}
},
{
"type": "ParameterType",
"name": "b",
"expression": {
"type": "NameExpression",
"name": "T"
}
],
"result": {
"type": "NameExpression",
"name": "boolean"
}
],
"result": {
"type": "NameExpression",
"name": "boolean"
}
},
"default": "(a:T,b:T):boolean=>a===b"
Expand Down
10 changes: 5 additions & 5 deletions test/fixture/es6.output.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ have any parameter descriptions.

**Parameters**

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

## destructure
Expand Down Expand Up @@ -134,7 +134,7 @@ This tests our support of optional parameters in ES6

**Parameters**

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

## iAmProtected

Expand All @@ -156,7 +156,7 @@ Regression check for #498

- `array1` **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)&lt;T>**
- `array2` **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)&lt;T>**
- `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`)
- `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`)

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

Expand Down
47 changes: 0 additions & 47 deletions test/fixture/es6.output.md.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,6 @@
"value": "Object"
}
]
},
{
"type": "text",
"value": "?"
}
]
},
Expand Down Expand Up @@ -142,19 +138,6 @@
"type": "text",
"value": " "
},
{
"type": "strong",
"children": [
{
"type": "text",
"value": "any"
},
{
"type": "text",
"value": "?"
}
]
},
{
"type": "text",
"value": " "
Expand Down Expand Up @@ -194,19 +177,6 @@
"type": "text",
"value": " "
},
{
"type": "strong",
"children": [
{
"type": "text",
"value": "any"
},
{
"type": "text",
"value": "?"
}
]
},
{
"type": "text",
"value": " "
Expand Down Expand Up @@ -1807,19 +1777,6 @@
"type": "text",
"value": " "
},
{
"type": "strong",
"children": [
{
"type": "text",
"value": "any"
},
{
"type": "text",
"value": "?"
}
]
},
{
"type": "text",
"value": " "
Expand Down Expand Up @@ -2199,10 +2156,6 @@
"value": "boolean"
}
]
},
{
"type": "text",
"value": "?"
}
]
},
Expand Down
6 changes: 3 additions & 3 deletions test/fixture/html/nested.config-output.html
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ <h3 class='fl m0' id='klass'>
<div class="clearfix small pointer toggle-sibling">
<div class="py1 contain">
<a class='icon pin-right py1 dark-link caret-right'></a>
<span class='code strong strong truncate'>isBuffer(buf, size = 0)</span>
<span class='code strong strong truncate'>isBuffer(buf, size)</span>
</div>
</div>
<div class="clearfix display-none toggle-target">
Expand All @@ -468,7 +468,7 @@ <h3 class='fl m0' id='klass'>
<p>This method takes a Buffer object that will be linked to nodejs.org</p>


<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>
<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>



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

<div class='space-bottom0'>
<div>
<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>?
<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>
= <code>0</code>)</code>
size

Expand Down
6 changes: 3 additions & 3 deletions test/fixture/html/nested.output.files
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ the referenced class type</p>
<div class="clearfix small pointer toggle-sibling">
<div class="py1 contain">
<a class='icon pin-right py1 dark-link caret-right'>▸</a>
<span class='code strong strong truncate'>isBuffer(buf, size = 0)</span>
<span class='code strong strong truncate'>isBuffer(buf, size)</span>
</div>
</div>
<div class="clearfix display-none toggle-target">
Expand All @@ -430,7 +430,7 @@ the referenced class type</p>
<p>This method takes a Buffer object that will be linked to nodejs.org</p>


<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>
<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>



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

<div class='space-bottom0'>
<div>
<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>?
<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>
= <code>0</code>)</code>
size

Expand Down
7 changes: 2 additions & 5 deletions test/fixture/nest_params.output.json
Original file line number Diff line number Diff line change
Expand Up @@ -333,11 +333,8 @@
}
},
"type": {
"type": "OptionalType",
"expression": {
"type": "NameExpression",
"name": "string"
}
"type": "NameExpression",
"name": "string"
},
"default": "minion"
}
Expand Down
2 changes: 1 addition & 1 deletion test/fixture/nest_params.output.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
- `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.
- `employees[].name` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** The name of an employee.
- `employees[].department` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** The employee's department.
- `type` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)?** The employee's type. (optional, default `minion`)
- `type` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** The employee's type. (optional, default `minion`)

## foo

Expand Down
4 changes: 0 additions & 4 deletions test/fixture/nest_params.output.md.json
Original file line number Diff line number Diff line change
Expand Up @@ -299,10 +299,6 @@
"value": "string"
}
]
},
{
"type": "text",
"value": "?"
}
]
},
Expand Down
Loading

0 comments on commit 3cc4426

Please sign in to comment.