Repository: styleguidist/react-styleguidist
Pull request #1671 (2020-08-27 12:53:41)

Fix Warning in ArgumentRenderer #1667
Resolved

- Warning in ArgumentRenderer #1667

I checked the behavior of the component of the original issue after I changed the `name` to optional and found that the following error occurred.

```
DoctrineError {name: "DoctrineError", message: "Unknown type undefined"}
```

What I have found in my research is that in the above cases, `type.type` in the `returns` is lost in `getProps` in this case.
`returns` should have `NameExpression` in `type.type` in my opinion.

```js
// I saw this shape.
{ 
  description: 'return a Boolean Value', 
  type: { name: 'boolean' } 
}

// but it should be this shape.
{
  title: 'returns',
  description: 'return a Boolean Value',
  type: { type: 'NameExpression', name: 'boolean' }
}
```

codecov[bot]: # [Codecov](https://codecov.io/gh/styleguidist/react-styleguidist/pull/1671?src=pr&el=h1) Report
> Merging [#1671](https://codecov.io/gh/styleguidist/react-styleguidist/pull/1671?src=pr&el=desc) into [master](https://codecov.io/gh/styleguidist/react-styleguidist/commit/be90f3aa688e5d9f188686c7261d964157a338b6?el=desc) will **not change** coverage.
> The diff coverage is `100.00%`.

| [Impacted Files](https://codecov.io/gh/styleguidist/react-styleguidist/pull/1671?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [...lient/rsg-components/Argument/ArgumentRenderer.tsx](https://codecov.io/gh/styleguidist/react-styleguidist/pull/1671/diff?src=pr&el=tree#diff-c3JjL2NsaWVudC9yc2ctY29tcG9uZW50cy9Bcmd1bWVudC9Bcmd1bWVudFJlbmRlcmVyLnRzeA==) | `100.00% <ø> (ø)` | |
| [src/loaders/utils/getProps.ts](https://codecov.io/gh/styleguidist/react-styleguidist/pull/1671/diff?src=pr&el=tree#diff-c3JjL2xvYWRlcnMvdXRpbHMvZ2V0UHJvcHMudHM=) | `98.57% <100.00%> (ø)` | |

mitsuruog: @sapegin please review me.

Patch:
diff --git a/src/client/rsg-components/Argument/ArgumentRenderer.tsx b/src/client/rsg-components/Argument/ArgumentRenderer.tsx
index 9cfbcdd1..f3dfc513 100644
--- a/src/client/rsg-components/Argument/ArgumentRenderer.tsx
+++ b/src/client/rsg-components/Argument/ArgumentRenderer.tsx
@@ -15,7 +15,7 @@ export const styles = ({ space }: Rsg.Theme) => ({
 });
 
 export interface ArgumentProps {
-	name: string;
+	name?: string;
 	type?: any;
 	default?: string;
 	description?: string;
@@ -70,7 +70,7 @@ export const ArgumentRenderer: React.FunctionComponent<ArgumentPropsWithClasses>
 
 ArgumentRenderer.propTypes = {
 	classes: PropTypes.objectOf(PropTypes.string.isRequired).isRequired,
-	name: PropTypes.string.isRequired,
+	name: PropTypes.string,
 	type: PropTypes.object,
 	default: PropTypes.string,
 	description: PropTypes.string,
diff --git a/src/loaders/utils/getProps.ts b/src/loaders/utils/getProps.ts
index 16900576..c83e915b 100644
--- a/src/loaders/utils/getProps.ts
+++ b/src/loaders/utils/getProps.ts
@@ -94,7 +94,15 @@ export default function getProps(doc: DocumentationObject, filepath?: string): R
 			allTags as TagProps,
 			JS_DOC_METHOD_RETURN_TAG_SYNONYMS
 		) as TagParamObject[];
-		const returns = method.returns || returnTags[0];
+		const returns = method.returns
+			? {
+					...method.returns,
+					type: {
+						type: 'NameExpression',
+						...method.returns.type,
+					},
+			  }
+			: returnTags[0];
 
 		if (returns) {
 			method.returns = returns;


Test patch:
diff --git a/src/loaders/utils/__tests__/getProps.spec.ts b/src/loaders/utils/__tests__/getProps.spec.ts
index e08ecf73..9330fe1a 100644
--- a/src/loaders/utils/__tests__/getProps.spec.ts
+++ b/src/loaders/utils/__tests__/getProps.spec.ts
@@ -225,6 +225,40 @@ it("should not crash when using doctrine to parse a default prop that isn't in t
 	expect(result).toMatchSnapshot();
 });
 
+it('should not crash when using doctrine to parse a return method that does not have type in it', () => {
+	const result = getProps(
+		{
+			displayName: 'Button',
+			methods: [
+				{
+					docblock: `
+Public Method
+
+@public
+@returns {Boolean} return a Boolean Value
+`,
+					returns: {
+						description: 'return a Boolean Value',
+						type: { name: 'boolean' },
+					},
+				},
+			] as any,
+		},
+		__filename
+	);
+
+	// @ts-ignore
+	expect(result.methods[0].returns).toEqual(
+		expect.objectContaining({
+			description: 'return a Boolean Value',
+			type: {
+				name: 'boolean',
+				type: 'NameExpression',
+			},
+		})
+	);
+});
+
 it('should guess a displayName for components that react-docgen was not able to recognize', () => {
 	const result = getProps(
 		{