Skip to content

Commit 7300663

Browse files
dotcspvasek
authored andcommitted
Fix issue with negative numbers as default values (#69)
1 parent a225f45 commit 7300663

File tree

3 files changed

+6
-0
lines changed

3 files changed

+6
-0
lines changed

src/__tests__/data/ComponentWithDefaultProps.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ export interface IComponentWithDefaultPropsProps {
1919
sampleNull?: null;
2020
/** sampleUndefined description */
2121
sampleUndefined?: any;
22+
/** sampleNumber description */
23+
sampleNumber?: number;
2224
}
2325

2426
/** ComponentWithDefaultProps description */
@@ -29,6 +31,7 @@ export class ComponentWithDefaultProps extends React.Component<
2931
static defaultProps: Partial<IComponentWithDefaultPropsProps> = {
3032
sampleFalse: false,
3133
sampleNull: null,
34+
sampleNumber: -1,
3235
// prettier-ignore
3336
sampleObject: { a: '1', b: 2, c: true, d: false, e: undefined, f: null, g: { a: '1' } },
3437
sampleString: 'hello',

src/__tests__/parser.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ describe('parser', () => {
190190
type: 'boolean'
191191
},
192192
sampleNull: { type: 'null', required: false, defaultValue: 'null' },
193+
sampleNumber: { type: 'number', required: false, defaultValue: '-1' },
193194
sampleObject: {
194195
defaultValue: `{ a: '1', b: 2, c: true, d: false, e: undefined, f: null, g: { a: '1' } }`,
195196
required: false,

src/parser.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,8 @@ function getLiteralValueFromPropertyAssignment(
449449
return 'true';
450450
case ts.SyntaxKind.StringLiteral:
451451
return (initializer as ts.StringLiteral).text.trim();
452+
case ts.SyntaxKind.PrefixUnaryExpression:
453+
return initializer.getFullText().trim();
452454
case ts.SyntaxKind.NumericLiteral:
453455
return `${(initializer as ts.NumericLiteral).text}`;
454456
case ts.SyntaxKind.NullKeyword:

0 commit comments

Comments
 (0)