react-docgen-typescript
Advanced tools
Comparing version
@@ -316,5 +316,29 @@ "use strict"; | ||
}); | ||
// it('supports spread props', () => { | ||
// check('StatelessWithSpreadDefaultProps', expectation); | ||
// }); | ||
it('should parse props with shorthands', function () { | ||
testUtils_1.check('StatelessShorthandDefaultProps', { | ||
StatelessShorthandDefaultProps: { | ||
onCallback: { | ||
defaultValue: null, | ||
description: 'onCallback description', | ||
required: false, | ||
type: '() => void' | ||
}, | ||
regularProp: { | ||
defaultValue: 'foo', | ||
description: 'regularProp description', | ||
required: true, | ||
type: 'string' | ||
}, | ||
shorthandProp: { | ||
defaultValue: '123', | ||
description: 'shorthandProp description', | ||
required: false, | ||
type: 'number' | ||
} | ||
} | ||
}); | ||
}); | ||
it('supports spread props', function () { | ||
testUtils_1.check('StatelessWithSpreadDefaultProps', expectation); | ||
}); | ||
}); | ||
@@ -321,0 +345,0 @@ it('should parse functional component component defined as function', function () { |
@@ -12,3 +12,3 @@ import { ComponentDoc, ParserOptions } from '../parser'; | ||
description?: string; | ||
defaultValue?: string; | ||
defaultValue?: string | null; | ||
} | ||
@@ -15,0 +15,0 @@ export declare function fixturePath(componentName: string): string; |
@@ -293,3 +293,3 @@ "use strict"; | ||
if (properties) { | ||
propMap = getPropMap(properties); | ||
propMap = this.getPropMap(properties); | ||
} | ||
@@ -305,3 +305,3 @@ return propMap; | ||
if (properties) { | ||
propMap_1 = getPropMap(properties); | ||
propMap_1 = _this.getPropMap(properties); | ||
} | ||
@@ -314,2 +314,58 @@ } | ||
}; | ||
Parser.prototype.getLiteralValueFromPropertyAssignment = function (property) { | ||
var initializer = property.initializer; | ||
// Shorthand properties, so inflect their actual value | ||
if (!initializer) { | ||
if (ts.isShorthandPropertyAssignment(property)) { | ||
var symbol = this.checker.getShorthandAssignmentValueSymbol(property); | ||
var decl = symbol && symbol.valueDeclaration; | ||
if (decl && decl.initializer) { | ||
initializer = decl.initializer; | ||
} | ||
} | ||
} | ||
if (!initializer) { | ||
return null; | ||
} | ||
// Literal values | ||
switch (initializer.kind) { | ||
case ts.SyntaxKind.FalseKeyword: | ||
return 'false'; | ||
case ts.SyntaxKind.TrueKeyword: | ||
return 'true'; | ||
case ts.SyntaxKind.StringLiteral: | ||
return initializer.text.trim(); | ||
case ts.SyntaxKind.PrefixUnaryExpression: | ||
return initializer.getFullText().trim(); | ||
case ts.SyntaxKind.NumericLiteral: | ||
return "" + initializer.text; | ||
case ts.SyntaxKind.NullKeyword: | ||
return 'null'; | ||
case ts.SyntaxKind.Identifier: | ||
// can potentially find other identifiers in the source and map those in the future | ||
return initializer.text === 'undefined' | ||
? 'undefined' | ||
: null; | ||
case ts.SyntaxKind.ObjectLiteralExpression: | ||
// return the source text for an object literal | ||
return initializer.getText(); | ||
default: | ||
return null; | ||
} | ||
}; | ||
Parser.prototype.getPropMap = function (properties) { | ||
var _this = this; | ||
var propMap = properties.reduce(function (acc, property) { | ||
if (ts.isSpreadAssignment(property) || !property.name) { | ||
return acc; | ||
} | ||
var literalValue = _this.getLiteralValueFromPropertyAssignment(property); | ||
var propertyName = getPropertyName(property.name); | ||
if (typeof literalValue === 'string' && propertyName !== null) { | ||
acc[propertyName] = literalValue; | ||
} | ||
return acc; | ||
}, {}); | ||
return propMap; | ||
}; | ||
return Parser; | ||
@@ -334,16 +390,2 @@ }()); | ||
} | ||
function getPropMap(properties) { | ||
var propMap = properties.reduce(function (acc, property) { | ||
if (ts.isSpreadAssignment(property) || !property.name) { | ||
return acc; | ||
} | ||
var literalValue = getLiteralValueFromPropertyAssignment(property); | ||
var propertyName = getPropertyName(property.name); | ||
if (typeof literalValue === 'string' && propertyName !== null) { | ||
acc[propertyName] = literalValue; | ||
} | ||
return acc; | ||
}, {}); | ||
return propMap; | ||
} | ||
function getPropertyName(name) { | ||
@@ -361,29 +403,2 @@ switch (name.kind) { | ||
} | ||
function getLiteralValueFromPropertyAssignment(property) { | ||
var initializer = property.initializer; | ||
switch (initializer.kind) { | ||
case ts.SyntaxKind.FalseKeyword: | ||
return 'false'; | ||
case ts.SyntaxKind.TrueKeyword: | ||
return 'true'; | ||
case ts.SyntaxKind.StringLiteral: | ||
return initializer.text.trim(); | ||
case ts.SyntaxKind.PrefixUnaryExpression: | ||
return initializer.getFullText().trim(); | ||
case ts.SyntaxKind.NumericLiteral: | ||
return "" + initializer.text; | ||
case ts.SyntaxKind.NullKeyword: | ||
return 'null'; | ||
case ts.SyntaxKind.Identifier: | ||
// can potentially find other identifiers in the source and map those in the future | ||
return initializer.text === 'undefined' | ||
? 'undefined' | ||
: null; | ||
case ts.SyntaxKind.ObjectLiteralExpression: | ||
// return the source text for an object literal | ||
return initializer.getText(); | ||
default: | ||
return null; | ||
} | ||
} | ||
function formatTag(tag) { | ||
@@ -390,0 +405,0 @@ var result = '@' + tag.name; |
{ | ||
"name": "react-docgen-typescript", | ||
"version": "1.6.0", | ||
"version": "1.6.1", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
105531
2.65%1381
2.91%