react-docgen-typescript
Advanced tools
Comparing version
@@ -222,8 +222,8 @@ "use strict"; | ||
sampleFalse: { | ||
defaultValue: 'false', | ||
defaultValue: false, | ||
required: false, | ||
type: 'boolean' | ||
}, | ||
sampleNull: { type: 'null', required: false, defaultValue: 'null' }, | ||
sampleNumber: { type: 'number', required: false, defaultValue: '-1' }, | ||
sampleNull: { type: 'null', required: false, defaultValue: null }, | ||
sampleNumber: { type: 'number', required: false, defaultValue: -1 }, | ||
sampleObject: { | ||
@@ -239,3 +239,3 @@ defaultValue: "{ a: '1', b: 2, c: true, d: false, e: undefined, f: null, g: { a: '1' } }", | ||
}, | ||
sampleTrue: { type: 'boolean', required: false, defaultValue: 'true' }, | ||
sampleTrue: { type: 'boolean', required: false, defaultValue: true }, | ||
sampleUndefined: { | ||
@@ -342,8 +342,8 @@ defaultValue: 'undefined', | ||
sampleFalse: { | ||
defaultValue: 'false', | ||
defaultValue: false, | ||
required: false, | ||
type: 'boolean' | ||
}, | ||
sampleNull: { type: 'null', required: false, defaultValue: 'null' }, | ||
sampleNumber: { type: 'number', required: false, defaultValue: '-1' }, | ||
sampleNull: { type: 'null', required: false, defaultValue: null }, | ||
sampleNumber: { type: 'number', required: false, defaultValue: -1 }, | ||
sampleObject: { | ||
@@ -359,5 +359,5 @@ defaultValue: "{ a: '1', b: 2, c: true, d: false, e: undefined, f: null, g: { a: '1' } }", | ||
}, | ||
sampleTrue: { type: 'boolean', required: false, defaultValue: 'true' }, | ||
sampleTrue: { type: 'boolean', required: false, defaultValue: true }, | ||
sampleUndefined: { | ||
defaultValue: 'undefined', | ||
defaultValue: undefined, | ||
required: false, | ||
@@ -387,3 +387,3 @@ type: 'any' | ||
shorthandProp: { | ||
defaultValue: '123', | ||
defaultValue: 123, | ||
description: 'shorthandProp description', | ||
@@ -638,2 +638,42 @@ required: false, | ||
}); | ||
describe('Returning not string default props ', function () { | ||
it('returns not string defaultProps', function () { | ||
testUtils_1.check('StatelessWithDefaultPropsAsString', { | ||
StatelessWithDefaultPropsAsString: { | ||
sampleFalse: { | ||
defaultValue: 'false', | ||
required: false, | ||
type: 'boolean' | ||
}, | ||
sampleNumberWithPrefix: { | ||
type: 'number', | ||
required: false, | ||
defaultValue: '-1' | ||
}, | ||
sampleNumber: { | ||
type: 'number', | ||
required: false, | ||
defaultValue: '1' | ||
}, | ||
sampleTrue: { | ||
type: 'boolean', | ||
required: false, | ||
defaultValue: 'true' | ||
}, | ||
sampleNull: { | ||
type: 'null', | ||
required: false, | ||
defaultValue: 'null' | ||
}, | ||
sampleUndefined: { | ||
type: 'undefined', | ||
required: false, | ||
defaultValue: 'undefined' | ||
} | ||
} | ||
}, true, null, { | ||
savePropValueAsString: true | ||
}); | ||
}); | ||
}); | ||
}); | ||
@@ -640,0 +680,0 @@ describe('withCustomConfig', function () { |
@@ -12,3 +12,3 @@ import { ComponentDoc, ParserOptions } from '../parser'; | ||
description?: string; | ||
defaultValue?: string | null; | ||
defaultValue?: string | number | boolean | null | undefined; | ||
parent?: { | ||
@@ -15,0 +15,0 @@ name: string; |
@@ -58,2 +58,3 @@ import * as ts from 'typescript'; | ||
shouldExtractLiteralValuesFromEnum?: boolean; | ||
savePropValueAsString?: boolean; | ||
} | ||
@@ -95,2 +96,3 @@ export interface StaticPropFilter { | ||
private shouldExtractLiteralValuesFromEnum; | ||
private savePropValueAsString; | ||
constructor(program: ts.Program, opts: ParserOptions); | ||
@@ -118,6 +120,6 @@ getComponentInfo(exp: ts.Symbol, source: ts.SourceFile, componentNameResolver?: ComponentNameResolver): ComponentDoc | null; | ||
extractDefaultPropsFromComponent(symbol: ts.Symbol, source: ts.SourceFile): {}; | ||
getLiteralValueFromPropertyAssignment(property: ts.PropertyAssignment | ts.BindingElement): string | null; | ||
getPropMap(properties: ts.NodeArray<ts.PropertyAssignment | ts.BindingElement>): StringIndexedObject<string>; | ||
getLiteralValueFromPropertyAssignment(property: ts.PropertyAssignment | ts.BindingElement): string | boolean | number | null | undefined; | ||
getPropMap(properties: ts.NodeArray<ts.PropertyAssignment | ts.BindingElement>): StringIndexedObject<string | boolean | number | null>; | ||
} | ||
export declare function getDefaultExportForFile(source: ts.SourceFile): string; | ||
export {}; |
@@ -86,5 +86,7 @@ "use strict"; | ||
function Parser(program, opts) { | ||
var savePropValueAsString = opts.savePropValueAsString, shouldExtractLiteralValuesFromEnum = opts.shouldExtractLiteralValuesFromEnum; | ||
this.checker = program.getTypeChecker(); | ||
this.propFilter = buildFilter_1.buildFilter(opts); | ||
this.shouldExtractLiteralValuesFromEnum = Boolean(opts.shouldExtractLiteralValuesFromEnum); | ||
this.shouldExtractLiteralValuesFromEnum = Boolean(shouldExtractLiteralValuesFromEnum); | ||
this.savePropValueAsString = Boolean(savePropValueAsString); | ||
} | ||
@@ -476,3 +478,3 @@ Parser.prototype.getComponentInfo = function (exp, source, componentNameResolver) { | ||
if (!initializer) { | ||
return null; | ||
return undefined; | ||
} | ||
@@ -484,13 +486,17 @@ // Literal values | ||
case ts.SyntaxKind.FalseKeyword: | ||
return 'false'; | ||
return this.savePropValueAsString ? 'false' : false; | ||
case ts.SyntaxKind.TrueKeyword: | ||
return 'true'; | ||
return this.savePropValueAsString ? 'true' : true; | ||
case ts.SyntaxKind.StringLiteral: | ||
return initializer.text.trim(); | ||
case ts.SyntaxKind.PrefixUnaryExpression: | ||
return initializer.getFullText().trim(); | ||
return this.savePropValueAsString | ||
? initializer.getFullText().trim() | ||
: Number(initializer.getFullText()); | ||
case ts.SyntaxKind.NumericLiteral: | ||
return "" + initializer.text; | ||
return this.savePropValueAsString | ||
? "" + initializer.text | ||
: Number(initializer.text); | ||
case ts.SyntaxKind.NullKeyword: | ||
return 'null'; | ||
return this.savePropValueAsString ? 'null' : null; | ||
case ts.SyntaxKind.Identifier: | ||
@@ -516,3 +522,7 @@ // can potentially find other identifiers in the source and map those in the future | ||
var propertyName = getPropertyName(property.name); | ||
if (typeof literalValue === 'string' && propertyName !== null) { | ||
if ((typeof literalValue === 'string' || | ||
typeof literalValue === 'number' || | ||
typeof literalValue === 'boolean' || | ||
literalValue === null) && | ||
propertyName !== null) { | ||
acc[propertyName] = literalValue; | ||
@@ -519,0 +529,0 @@ } |
{ | ||
"name": "react-docgen-typescript", | ||
"version": "1.14.1", | ||
"version": "1.15.0", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/styleguidist/react-docgen-typescript/", |
@@ -45,3 +45,3 @@ # react-docgen-typescript | ||
```typescript | ||
(props: PropItem, component: Component) => boolean | ||
(prop: PropItem, component: Component) => boolean | ||
``` | ||
@@ -48,0 +48,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
154103
2.48%2027
2.63%