Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support extracting defaults from renamed destructuring #507

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/__tests__/data/StatelessWithDefaultProps.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export interface StatelessWithDefaultPropsProps {
sampleUndefined?: any;
/** sampleNumber description */
sampleNumber?: number;
/** sampleRenamed description */
sampleRenamed?: string;
}

/** StatelessWithDefaultProps description */
Expand All @@ -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'
};
5 changes: 4 additions & 1 deletion src/__tests__/data/StatelessWithDefaultPropsTypescript3.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export interface StatelessWithDefaultPropsProps {
sampleUndefined: any;
/** sampleNumber description */
sampleNumber: number;
/** sampleRenamed description */
sampleRenamed?: string;
}

/** StatelessWithDefaultProps description */
Expand All @@ -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'
};
5 changes: 4 additions & 1 deletion src/__tests__/data/StatelessWithDestructuredProps.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export interface StatelessWithDefaultPropsProps {
sampleUndefined?: any;
/** sampleNumber description */
sampleNumber?: number;
/** sampleRenamed description */
sampleRenamed?: string;
}

/** StatelessWithDefaultProps description */
Expand All @@ -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 <div>test</div>;
}
5 changes: 4 additions & 1 deletion src/__tests__/data/StatelessWithDestructuredPropsArrow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export interface StatelessWithDefaultPropsProps {
sampleUndefined?: any;
/** sampleNumber description */
sampleNumber?: number;
/** sampleRenamed description */
sampleRenamed?: string;
}

/** StatelessWithDefaultProps description */
Expand All @@ -40,5 +42,6 @@ export const StatelessWithDefaultProps: React.StatelessComponent<StatelessWithDe
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'
}) => <div>test</div>;
5 changes: 5 additions & 0 deletions src/__tests__/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,11 @@ describe('parser', () => {
defaultValue: undefined,
required: false,
type: 'any'
},
sampleRenamed: {
defaultValue: 'world',
required: false,
type: 'string'
}
}
};
Expand Down
7 changes: 3 additions & 4 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1100,19 +1100,18 @@ export class Parser {
properties: ts.NodeArray<ts.PropertyAssignment | ts.BindingElement>
): StringIndexedObject<string | boolean | number | null> {
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;
}
Expand Down