From c9a5fbebd982368e42813e10b849a3afdf684313 Mon Sep 17 00:00:00 2001 From: Bill Collins Date: Mon, 17 Jul 2023 13:53:24 +0100 Subject: [PATCH 1/2] Add failing test demonstrating failure to detect default on renamed prop --- src/__tests__/data/StatelessWithDefaultProps.tsx | 5 ++++- src/__tests__/data/StatelessWithDefaultPropsTypescript3.tsx | 5 ++++- src/__tests__/data/StatelessWithDestructuredProps.tsx | 5 ++++- src/__tests__/data/StatelessWithDestructuredPropsArrow.tsx | 5 ++++- src/__tests__/parser.ts | 5 +++++ 5 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/__tests__/data/StatelessWithDefaultProps.tsx b/src/__tests__/data/StatelessWithDefaultProps.tsx index e6da283e..8fb42fd3 100644 --- a/src/__tests__/data/StatelessWithDefaultProps.tsx +++ b/src/__tests__/data/StatelessWithDefaultProps.tsx @@ -28,6 +28,8 @@ export interface StatelessWithDefaultPropsProps { sampleUndefined?: any; /** sampleNumber description */ sampleNumber?: number; + /** sampleRenamed description */ + sampleRenamed?: string; } /** StatelessWithDefaultProps description */ @@ -44,5 +46,6 @@ StatelessWithDefaultProps.defaultProps = { sampleObject: { a: '1', b: 2, c: true, d: false, e: undefined, f: null, g: { a: '1' } }, sampleString: 'hello', sampleTrue: true, - sampleUndefined: undefined + sampleUndefined: undefined, + sampleRenamed: 'world' }; diff --git a/src/__tests__/data/StatelessWithDefaultPropsTypescript3.tsx b/src/__tests__/data/StatelessWithDefaultPropsTypescript3.tsx index db23e30b..6b3193a8 100644 --- a/src/__tests__/data/StatelessWithDefaultPropsTypescript3.tsx +++ b/src/__tests__/data/StatelessWithDefaultPropsTypescript3.tsx @@ -28,6 +28,8 @@ export interface StatelessWithDefaultPropsProps { sampleUndefined: any; /** sampleNumber description */ sampleNumber: number; + /** sampleRenamed description */ + sampleRenamed?: string; } /** StatelessWithDefaultProps description */ @@ -44,5 +46,6 @@ StatelessWithDefaultProps.defaultProps = { sampleObject: { a: '1', b: 2, c: true, d: false, e: undefined, f: null, g: { a: '1' } }, sampleString: 'hello', sampleTrue: true, - sampleUndefined: undefined + sampleUndefined: undefined, + sampleRenamed: 'world' }; diff --git a/src/__tests__/data/StatelessWithDestructuredProps.tsx b/src/__tests__/data/StatelessWithDestructuredProps.tsx index 6ca03962..c606bd06 100644 --- a/src/__tests__/data/StatelessWithDestructuredProps.tsx +++ b/src/__tests__/data/StatelessWithDestructuredProps.tsx @@ -28,6 +28,8 @@ export interface StatelessWithDefaultPropsProps { sampleUndefined?: any; /** sampleNumber description */ sampleNumber?: number; + /** sampleRenamed description */ + sampleRenamed?: string; } /** StatelessWithDefaultProps description */ @@ -40,7 +42,8 @@ export function StatelessWithDefaultProps({ sampleObject = { a: '1', b: 2, c: true, d: false, e: undefined, f: null, g: { a: '1' } }, sampleString = 'hello', sampleTrue = true, - sampleUndefined + sampleUndefined, + sampleRenamed: renamedSample = 'world' }: StatelessWithDefaultPropsProps) { return
test
; } diff --git a/src/__tests__/data/StatelessWithDestructuredPropsArrow.tsx b/src/__tests__/data/StatelessWithDestructuredPropsArrow.tsx index d370d0b9..8e0a517a 100644 --- a/src/__tests__/data/StatelessWithDestructuredPropsArrow.tsx +++ b/src/__tests__/data/StatelessWithDestructuredPropsArrow.tsx @@ -28,6 +28,8 @@ export interface StatelessWithDefaultPropsProps { sampleUndefined?: any; /** sampleNumber description */ sampleNumber?: number; + /** sampleRenamed description */ + sampleRenamed?: string; } /** StatelessWithDefaultProps description */ @@ -40,5 +42,6 @@ export const StatelessWithDefaultProps: React.StatelessComponent
test
; diff --git a/src/__tests__/parser.ts b/src/__tests__/parser.ts index fab7b73e..79610ab7 100644 --- a/src/__tests__/parser.ts +++ b/src/__tests__/parser.ts @@ -672,6 +672,11 @@ describe('parser', () => { defaultValue: undefined, required: false, type: 'any' + }, + sampleRenamed: { + defaultValue: 'world', + required: false, + type: 'string' } } }; From 7136ce3f5c7acf39ac260cdcd0c2c66f3fc4fcb1 Mon Sep 17 00:00:00 2001 From: Bill Collins Date: Mon, 17 Jul 2023 13:58:57 +0100 Subject: [PATCH 2/2] Support extracting defaults from renamed properties --- src/parser.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/parser.ts b/src/parser.ts index 47a86e5d..083979c4 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -1100,19 +1100,18 @@ export class Parser { properties: ts.NodeArray ): StringIndexedObject { return properties.reduce((acc, property) => { - if (ts.isSpreadAssignment(property) || !property.name) { + const propertyName = getPropertyName(ts.isBindingElement(property) ? (property.propertyName || property.name) : property.name); + if (ts.isSpreadAssignment(property) || !propertyName) { return acc; } const literalValue = this.getLiteralValueFromPropertyAssignment(property); - const propertyName = getPropertyName(property.name); if ( (typeof literalValue === 'string' || typeof literalValue === 'number' || typeof literalValue === 'boolean' || - literalValue === null) && - propertyName !== null + literalValue === null) ) { acc[propertyName] = literalValue; }