From 3cc4426bdee815dc1757865d5bc37f858a85984b Mon Sep 17 00:00:00 2001 From: Tom Macwright Date: Mon, 24 Apr 2017 21:22:18 -0400 Subject: [PATCH] fix(params): Parameters with default use = not ? Fixes #737 --- lib/infer/params.js | 11 +--- lib/parse.js | 3 + test/fixture/es6.output-toc.md | 10 ++-- test/fixture/es6.output.json | 65 ++++++++------------- test/fixture/es6.output.md | 10 ++-- test/fixture/es6.output.md.json | 47 --------------- test/fixture/html/nested.config-output.html | 6 +- test/fixture/html/nested.output.files | 6 +- test/fixture/nest_params.output.json | 7 +-- test/fixture/nest_params.output.md | 2 +- test/fixture/nest_params.output.md.json | 4 -- test/fixture/params.output.json | 35 ++++------- test/fixture/params.output.md | 10 ++-- test/fixture/params.output.md.json | 20 ------- test/lib/infer/params.js | 12 +--- 15 files changed, 67 insertions(+), 181 deletions(-) diff --git a/lib/infer/params.js b/lib/infer/params.js index dd92bad1b..9a9a2b492 100644 --- a/lib/infer/params.js +++ b/lib/infer/params.js @@ -105,10 +105,7 @@ function paramToDoc( default: generate(param.right, { compact: true }).code, - type: { - type: 'OptionalType', - expression: newAssignmentParam.type - } + type: newAssignmentParam.type }); // ObjectPattern case 'ObjectPattern': // { a } @@ -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; } diff --git a/lib/parse.js b/lib/parse.js index 653a0a9a8..60203fca6 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -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); diff --git a/test/fixture/es6.output-toc.md b/test/fixture/es6.output-toc.md index 7f7abc73f..3a8dc9a99 100644 --- a/test/fixture/es6.output-toc.md +++ b/test/fixture/es6.output-toc.md @@ -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 @@ -111,7 +111,7 @@ This tests our support of optional parameters in ES6 **Parameters** -- `foo` **any?** (optional, default `'bar'`) +- `foo` (optional, default `'bar'`) ## iAmProtected @@ -133,7 +133,7 @@ Regression check for #498 - `array1` **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<T>** - `array2` **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<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)** diff --git a/test/fixture/es6.output.json b/test/fixture/es6.output.json index 52d58f881..d90207223 100644 --- a/test/fixture/es6.output.json +++ b/test/fixture/es6.output.json @@ -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", @@ -2257,10 +2248,7 @@ "title": "param", "name": "foo", "lineNumber": 112, - "default": "'bar'", - "type": { - "type": "OptionalType" - } + "default": "'bar'" } ], "properties": [], @@ -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" diff --git a/test/fixture/es6.output.md b/test/fixture/es6.output.md index 09b3af17c..f43d25dd4 100644 --- a/test/fixture/es6.output.md +++ b/test/fixture/es6.output.md @@ -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 @@ -134,7 +134,7 @@ This tests our support of optional parameters in ES6 **Parameters** -- `foo` **any?** (optional, default `'bar'`) +- `foo` (optional, default `'bar'`) ## iAmProtected @@ -156,7 +156,7 @@ Regression check for #498 - `array1` **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<T>** - `array2` **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<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)** diff --git a/test/fixture/es6.output.md.json b/test/fixture/es6.output.md.json index a90a28b30..da94047e3 100644 --- a/test/fixture/es6.output.md.json +++ b/test/fixture/es6.output.md.json @@ -94,10 +94,6 @@ "value": "Object" } ] - }, - { - "type": "text", - "value": "?" } ] }, @@ -142,19 +138,6 @@ "type": "text", "value": " " }, - { - "type": "strong", - "children": [ - { - "type": "text", - "value": "any" - }, - { - "type": "text", - "value": "?" - } - ] - }, { "type": "text", "value": " " @@ -194,19 +177,6 @@ "type": "text", "value": " " }, - { - "type": "strong", - "children": [ - { - "type": "text", - "value": "any" - }, - { - "type": "text", - "value": "?" - } - ] - }, { "type": "text", "value": " " @@ -1807,19 +1777,6 @@ "type": "text", "value": " " }, - { - "type": "strong", - "children": [ - { - "type": "text", - "value": "any" - }, - { - "type": "text", - "value": "?" - } - ] - }, { "type": "text", "value": " " @@ -2199,10 +2156,6 @@ "value": "boolean" } ] - }, - { - "type": "text", - "value": "?" } ] }, diff --git a/test/fixture/html/nested.config-output.html b/test/fixture/html/nested.config-output.html index cce7ee832..e08471d15 100644 --- a/test/fixture/html/nested.config-output.html +++ b/test/fixture/html/nested.config-output.html @@ -457,7 +457,7 @@

- isBuffer(buf, size = 0) + isBuffer(buf, size)