From 2195c66d755277c06a4a157d3dda76ba25aaee5e Mon Sep 17 00:00:00 2001 From: Bryce Osterhaus Date: Wed, 30 Oct 2019 12:27:09 -0700 Subject: [PATCH 1/3] add failing fixture --- src/__tests__/fixtures/component_29.tsx | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 src/__tests__/fixtures/component_29.tsx diff --git a/src/__tests__/fixtures/component_29.tsx b/src/__tests__/fixtures/component_29.tsx new file mode 100644 index 00000000000..ecdac2609ea --- /dev/null +++ b/src/__tests__/fixtures/component_29.tsx @@ -0,0 +1,24 @@ +import React from 'react'; + +export const STRING_VALS = [ + 'one', + 'two', + 'three' +]; + +interface IProps { + /** + * String value of a number + */ + value?: typeof STRING_VALS[number]; +} + +const MyComponent = (props: IProps) => { + return ( +
+ {props.value} +
+ ); +} + +export default MyComponent; From 6a989076e6e4e36690faead85a71e28cdf817950 Mon Sep 17 00:00:00 2001 From: Bryce Osterhaus Date: Wed, 30 Oct 2019 12:30:37 -0700 Subject: [PATCH 2/3] check for value before using methods --- src/__tests__/__snapshots__/main-test.ts.snap | 18 ++++++++++++++++++ .../{component_29.tsx => component_41.tsx} | 0 src/utils/getTSType.ts | 5 +++-- 3 files changed, 21 insertions(+), 2 deletions(-) rename src/__tests__/fixtures/{component_29.tsx => component_41.tsx} (100%) diff --git a/src/__tests__/__snapshots__/main-test.ts.snap b/src/__tests__/__snapshots__/main-test.ts.snap index 99041fe6d4f..b1182a11bc8 100644 --- a/src/__tests__/__snapshots__/main-test.ts.snap +++ b/src/__tests__/__snapshots__/main-test.ts.snap @@ -1801,6 +1801,24 @@ Object { } `; +exports[`main fixtures processes component "component_41.tsx" without errors 1`] = ` +Object { + "description": "", + "displayName": "MyComponent", + "methods": Array [], + "props": Object { + "value": Object { + "description": "String value of a number", + "required": false, + "tsType": Object { + "name": "STRING_VALS[undefined]", + "raw": "typeof STRING_VALS[number]", + }, + }, + }, +} +`; + exports[`main fixtures processes component "flow-export-type.js" without errors 1`] = ` Object { "description": "This is a Flow class component", diff --git a/src/__tests__/fixtures/component_29.tsx b/src/__tests__/fixtures/component_41.tsx similarity index 100% rename from src/__tests__/fixtures/component_29.tsx rename to src/__tests__/fixtures/component_41.tsx diff --git a/src/utils/getTSType.ts b/src/utils/getTSType.ts index bb24791d285..953696aaa9c 100644 --- a/src/utils/getTSType.ts +++ b/src/utils/getTSType.ts @@ -391,12 +391,13 @@ function handleTSIndexedAccessType( // We only get the signature if the objectType is a type (vs interface) if (!objectType.signature) return { - name: `${objectType.name}[${indexType.value.toString()}]`, + name: `${objectType.name}[${indexType.value && + indexType.value.toString()}]`, raw: printValue(path), }; const resolvedType = objectType.signature.properties.find(p => { // indexType.value = "'foo'" - return p.key === indexType.value.replace(/['"]+/g, ''); + return indexType.value && p.key === indexType.value.replace(/['"]+/g, ''); }); if (!resolvedType) { return { name: 'unknown' }; From 8a7f5e25363951e8fbbe82b5f36dfea33ed3254c Mon Sep 17 00:00:00 2001 From: Daniel Tschinder <231804+danez@users.noreply.github.com> Date: Sat, 12 Dec 2020 19:25:44 +0000 Subject: [PATCH 3/3] use name if value not present --- src/__tests__/__snapshots__/main-test.ts.snap | 2 +- src/utils/__tests__/getTSType-test.ts | 20 +++++++++++++++++++ src/utils/getTSType.ts | 5 +++-- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/__tests__/__snapshots__/main-test.ts.snap b/src/__tests__/__snapshots__/main-test.ts.snap index b1182a11bc8..01fdcf44668 100644 --- a/src/__tests__/__snapshots__/main-test.ts.snap +++ b/src/__tests__/__snapshots__/main-test.ts.snap @@ -1811,7 +1811,7 @@ Object { "description": "String value of a number", "required": false, "tsType": Object { - "name": "STRING_VALS[undefined]", + "name": "STRING_VALS[number]", "raw": "typeof STRING_VALS[number]", }, }, diff --git a/src/utils/__tests__/getTSType-test.ts b/src/utils/__tests__/getTSType-test.ts index 8b239bca3c9..887e0cc397f 100644 --- a/src/utils/__tests__/getTSType-test.ts +++ b/src/utils/__tests__/getTSType-test.ts @@ -753,6 +753,26 @@ describe('getTSType', () => { }); }); + it('resolves indexed access of array', () => { + const typePath = statement(` + var x: typeof STRING_VALS[number]; + + const STRING_VALS = [ + 'one', + 'two', + 'three' + ]; + `) + .get('declarations', 0) + .get('id') + .get('typeAnnotation') + .get('typeAnnotation'); + expect(getTSType(typePath, null, noopImporter)).toEqual({ + name: 'STRING_VALS[number]', + raw: 'typeof STRING_VALS[number]', + }); + }); + it('can resolve indexed access to imported type', () => { const typePath = statement(` var x: A["x"] = 2; diff --git a/src/utils/getTSType.ts b/src/utils/getTSType.ts index 953696aaa9c..d3856661782 100644 --- a/src/utils/getTSType.ts +++ b/src/utils/getTSType.ts @@ -391,8 +391,9 @@ function handleTSIndexedAccessType( // We only get the signature if the objectType is a type (vs interface) if (!objectType.signature) return { - name: `${objectType.name}[${indexType.value && - indexType.value.toString()}]`, + name: `${objectType.name}[${ + indexType.value ? indexType.value.toString() : indexType.name + }]`, raw: printValue(path), }; const resolvedType = objectType.signature.properties.find(p => {