From c45a3e68e1fe7d14de51e7a521b51dad97179c9b Mon Sep 17 00:00:00 2001 From: atanasster Date: Mon, 17 May 2021 15:15:26 +0300 Subject: [PATCH] fix: clean quotes from react-docgen props info --- .vscode/launch.json | 14 +++++++++++ core/core/src/controls-smart.ts | 2 +- .../react-docgen/src/transform-props.ts | 24 +++++++++++++++++-- .../test/__snapshots__/flow-type.test.ts.snap | 4 ++-- .../__snapshots__/prop-types.test.ts.snap | 4 ++-- .../__snapshots__/typescript.test.ts.snap | 4 ++-- 6 files changed, 43 insertions(+), 9 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 1efaf814b..81ea276ef 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -92,6 +92,20 @@ "program": "${workspaceFolder}/node_modules/jest/bin/jest" } }, + { + "type": "node", + "request": "launch", + "name": "jest react-docgen", + "program": "${workspaceFolder}/node_modules/.bin/jest", + "cwd": "${workspaceFolder}/props-info/react-docgen", + "args": ["flow-type"], + "console": "integratedTerminal", + "internalConsoleOptions": "neverOpen", + "disableOptimisticBPs": true, + "windows": { + "program": "${workspaceFolder}/node_modules/jest/bin/jest" + } + }, { "type": "node", "request": "launch", diff --git a/core/core/src/controls-smart.ts b/core/core/src/controls-smart.ts index 778604a1e..ddb1d0c30 100644 --- a/core/core/src/controls-smart.ts +++ b/core/core/src/controls-smart.ts @@ -2,7 +2,7 @@ import { ComponentControl, ComponentControls, ControlTypes } from './controls'; import { PropType, PropTypes } from './components'; -const cleanQuotes = (txt?: string) => +export const cleanQuotes = (txt?: string): string | undefined => typeof txt === 'string' ? txt.replace(/['"]+/g, '') : txt; const handledTypes = [ diff --git a/props-info/react-docgen/src/transform-props.ts b/props-info/react-docgen/src/transform-props.ts index 8086be173..8e6847db9 100644 --- a/props-info/react-docgen/src/transform-props.ts +++ b/props-info/react-docgen/src/transform-props.ts @@ -1,4 +1,9 @@ -import { PropTypes, PropType, TypeInformation } from '@component-controls/core'; +import { + PropTypes, + PropType, + TypeInformation, + cleanQuotes, +} from '@component-controls/core'; import { RdPropInfo, RdValue, @@ -59,7 +64,7 @@ const propTypeToCCType = (dgType: RdPropType): TypeInformation => { ? dgType.value.map(({ value }: any) => { return { name: typeof value, - value, + value: cleanQuotes(value), }; }) : undefined, @@ -144,6 +149,21 @@ const tsTypeToCCType = (dgType: RdTypescriptType): TypeInformation => { value: dgType.elements.map((element: any) => tsTypeToCCType(element)), }; break; + case 'union': + type = { + ...dgType, + elements: dgType.elements.map((element: any) => { + const clean = + typeof element === 'object' && element.value + ? { + ...element, + value: cleanQuotes(element.value), + } + : element; + return clean; + }), + } as TypeInformation; + break; case 'signature': if (dgType.signature?.arguments) { type = { diff --git a/props-info/react-docgen/test/__snapshots__/flow-type.test.ts.snap b/props-info/react-docgen/test/__snapshots__/flow-type.test.ts.snap index f7bf75ff0..3c76d4d30 100644 --- a/props-info/react-docgen/test/__snapshots__/flow-type.test.ts.snap +++ b/props-info/react-docgen/test/__snapshots__/flow-type.test.ts.snap @@ -38,11 +38,11 @@ Object { "elements": Array [ Object { "name": "literal", - "value": "'string'", + "value": "string", }, Object { "name": "literal", - "value": "'otherstring'", + "value": "otherstring", }, Object { "name": "number", diff --git a/props-info/react-docgen/test/__snapshots__/prop-types.test.ts.snap b/props-info/react-docgen/test/__snapshots__/prop-types.test.ts.snap index 6d788e733..6899c7bd9 100644 --- a/props-info/react-docgen/test/__snapshots__/prop-types.test.ts.snap +++ b/props-info/react-docgen/test/__snapshots__/prop-types.test.ts.snap @@ -51,11 +51,11 @@ it as an enum.", "value": Array [ Object { "name": "string", - "value": "'News'", + "value": "News", }, Object { "name": "string", - "value": "'Photos'", + "value": "Photos", }, ], }, diff --git a/props-info/react-docgen/test/__snapshots__/typescript.test.ts.snap b/props-info/react-docgen/test/__snapshots__/typescript.test.ts.snap index 39f54f4d9..1de529f7e 100644 --- a/props-info/react-docgen/test/__snapshots__/typescript.test.ts.snap +++ b/props-info/react-docgen/test/__snapshots__/typescript.test.ts.snap @@ -56,11 +56,11 @@ it as an enum.", "elements": Array [ Object { "name": "literal", - "value": "'News'", + "value": "News", }, Object { "name": "literal", - "value": "'Photos'", + "value": "Photos", }, ], "name": "union",