react-docgen-typescript
Advanced tools
Comparing version
@@ -366,2 +366,14 @@ "use strict"; | ||
}); | ||
it('should parse react stateless component default props when declared as a normal function inside forwardRef', function () { | ||
testUtils_1.check('ForwardRefDefaultValues', { | ||
ForwardRefDefaultValues: { | ||
myProp: { | ||
defaultValue: "I'm default", | ||
description: 'myProp description', | ||
type: 'string', | ||
required: false | ||
} | ||
} | ||
}, false, 'ForwardRefDefaultValues description'); | ||
}); | ||
it('should parse react stateless component with external intersection props', function () { | ||
@@ -401,5 +413,5 @@ testUtils_1.check('StatelessIntersectionExternalProps', { | ||
}); | ||
it('should parse react stateless component with generic intersection + union overlap props', function () { | ||
testUtils_1.check('ComplexGenericUnionIntersection', { | ||
ComplexGenericUnionIntersection: { | ||
it('should parse react stateless component with generic intersection + union overlap props - simple', function () { | ||
testUtils_1.check('SimpleGenericUnionIntersection', { | ||
SimpleGenericUnionIntersection: { | ||
as: { type: 'T', description: '' }, | ||
@@ -420,2 +432,28 @@ foo: { | ||
}); | ||
it('should parse react stateless component with generic intersection + union overlap props', function () { | ||
testUtils_1.check('ComplexGenericUnionIntersection', { | ||
ComplexGenericUnionIntersection: { | ||
as: { | ||
type: 'E', | ||
required: false, | ||
description: 'Render the component as another component' | ||
}, | ||
align: { | ||
description: 'The flex "align" property', | ||
required: false, | ||
type: '"stretch" | "center" | "flex-start" | "flex-end"' | ||
}, | ||
justify: { | ||
description: "Use flex 'center' | 'flex-start' | 'flex-end' | 'stretch' with\na gap between each child.\nUse flex 'space-between' | 'space-around' | 'space-evenly' and\nflex will space the children.", | ||
required: false, | ||
type: '"stretch" | "center" | "flex-start" | "flex-end" | "space-between" | "space-around" | "space-evenly"' | ||
}, | ||
gap: { | ||
description: 'The space between children\nYou cannot use gap when using a "space" justify property', | ||
required: false, | ||
type: 'ReactText' | ||
} | ||
} | ||
}); | ||
}); | ||
it('should parse react stateful component with intersection props', function () { | ||
@@ -745,3 +783,2 @@ testUtils_1.check('StatefulIntersectionProps', { | ||
sampleBoolean: { type: 'boolean' }, | ||
sampleComplexUnion: { type: 'number | "string1" | "string2"' }, | ||
sampleEnum: { | ||
@@ -756,8 +793,3 @@ raw: 'sampleEnum', | ||
}, | ||
sampleString: { type: 'string' }, | ||
sampleStringUnion: { | ||
raw: '"string1" | "string2"', | ||
type: 'enum', | ||
value: [{ value: '"string1"' }, { value: '"string2"' }] | ||
} | ||
sampleString: { type: 'string' } | ||
} | ||
@@ -771,4 +803,4 @@ }, true, null, { | ||
it('extracts all values from union', function () { | ||
testUtils_1.check('ExtractLiteralValuesFromEnum', { | ||
ExtractLiteralValuesFromEnum: { | ||
testUtils_1.check('ExtractLiteralValuesFromUnion', { | ||
ExtractLiteralValuesFromUnion: { | ||
sampleComplexUnion: { | ||
@@ -788,2 +820,33 @@ raw: 'number | "string1" | "string2"', | ||
}); | ||
it('extracts numbers from a union', function () { | ||
testUtils_1.check('ExtractLiteralValuesFromUnion', { | ||
ExtractLiteralValuesFromUnion: { | ||
sampleNumberUnion: { | ||
raw: '1 | 2 | 3', | ||
type: 'enum', | ||
value: [{ value: '1' }, { value: '2' }, { value: '3' }] | ||
} | ||
} | ||
}, false, null, { | ||
shouldExtractValuesFromUnion: true | ||
}); | ||
}); | ||
it('extracts numbers and strings from a mixed union', function () { | ||
testUtils_1.check('ExtractLiteralValuesFromUnion', { | ||
ExtractLiteralValuesFromUnion: { | ||
sampleMixedUnion: { | ||
raw: '"string1" | "string2" | 1 | 2', | ||
type: 'enum', | ||
value: [ | ||
{ value: '"string1"' }, | ||
{ value: '"string2"' }, | ||
{ value: '1' }, | ||
{ value: '2' } | ||
] | ||
} | ||
} | ||
}, false, null, { | ||
shouldExtractValuesFromUnion: true | ||
}); | ||
}); | ||
}); | ||
@@ -790,0 +853,0 @@ describe('Returning not string default props ', function () { |
@@ -111,2 +111,3 @@ import * as ts from 'typescript'; | ||
getReturnDescription(symbol: ts.Symbol): string | null; | ||
private getValuesFromUnionType; | ||
getDocgenType(propType: ts.Type, isRequired: boolean): PropItemType; | ||
@@ -113,0 +114,0 @@ getPropsInfo(propsObj: ts.Symbol, defaultProps?: StringIndexedObject<string>): Props; |
@@ -135,3 +135,5 @@ "use strict"; | ||
} | ||
else if (type.symbol && (ts.isPropertyAccessExpression(declaration) || ts.isPropertyDeclaration(declaration))) { | ||
else if (type.symbol && | ||
(ts.isPropertyAccessExpression(declaration) || | ||
ts.isPropertyDeclaration(declaration))) { | ||
commentSource = type.symbol; | ||
@@ -153,3 +155,3 @@ } | ||
if (propsType) { | ||
var defaultProps = this.extractDefaultPropsFromComponent(exp, source); | ||
var defaultProps = this.extractDefaultPropsFromComponent(commentSource, commentSource.valueDeclaration.getSourceFile()); | ||
var props = this.getPropsInfo(propsType, defaultProps); | ||
@@ -309,18 +311,34 @@ for (var _i = 0, _a = Object.keys(props); _i < _a.length; _i++) { | ||
}; | ||
Parser.prototype.getValuesFromUnionType = function (type) { | ||
if (type.isStringLiteral()) | ||
return "\"" + type.value + "\""; | ||
if (type.isNumberLiteral()) | ||
return "" + type.value; | ||
return this.checker.typeToString(type); | ||
}; | ||
Parser.prototype.getDocgenType = function (propType, isRequired) { | ||
var _this = this; | ||
var propTypeString = this.checker.typeToString(propType); | ||
if (propType.isUnion() && | ||
(this.shouldExtractValuesFromUnion || | ||
(this.shouldExtractLiteralValuesFromEnum && | ||
propType.types.every(function (type) { return type.isStringLiteral(); })))) { | ||
return { | ||
name: 'enum', | ||
raw: propTypeString, | ||
value: propType.types.map(function (type) { return ({ | ||
value: type.isStringLiteral() | ||
? "\"" + type.value + "\"" | ||
: _this.checker.typeToString(type) | ||
}); }) | ||
}; | ||
if (propType.isUnion()) { | ||
if (this.shouldExtractValuesFromUnion) { | ||
return { | ||
name: 'enum', | ||
raw: propTypeString, | ||
value: propType.types.map(function (type) { return ({ | ||
value: _this.getValuesFromUnionType(type) | ||
}); }) | ||
}; | ||
} | ||
if (this.shouldExtractLiteralValuesFromEnum && | ||
propType.types.every(function (type) { return type.isStringLiteral(); })) { | ||
return { | ||
name: 'enum', | ||
raw: propTypeString, | ||
value: propType.types.map(function (type) { return ({ | ||
value: type.isStringLiteral() | ||
? "\"" + type.value + "\"" | ||
: _this.checker.typeToString(type) | ||
}); }) | ||
}; | ||
} | ||
} | ||
@@ -340,6 +358,13 @@ if (this.shouldRemoveUndefinedFromOptional && !isRequired) { | ||
var baseProps = propsType.getProperties(); | ||
var propertiesOfProps = propsType.isUnionOrIntersection() | ||
? // Using internal typescript API to get all properties | ||
this.checker.getAllPossiblePropertiesOfTypes(propsType.types) | ||
: baseProps; | ||
var propertiesOfProps = baseProps; | ||
if (propsType.isUnionOrIntersection()) { | ||
// Using internal typescript API to get all properties | ||
propertiesOfProps = this.checker.getAllPossiblePropertiesOfTypes(propsType.types); | ||
if (!propertiesOfProps.length) { | ||
propertiesOfProps = this | ||
.checker.getAllPossiblePropertiesOfTypes(propsType.types.reduce( | ||
// @ts-ignore | ||
function (all, t) { return all.concat((t.types || [])); }, [])); | ||
} | ||
} | ||
var result = {}; | ||
@@ -440,2 +465,9 @@ propertiesOfProps.forEach(function (prop) { | ||
statement.declarationList.declarations[0].initializer; | ||
// Look at forwardRef function argument | ||
if (initializer && ts.isCallExpression(initializer)) { | ||
var symbol_1 = this.checker.getSymbolAtLocation(initializer.expression); | ||
if (!symbol_1 || symbol_1.getName() !== 'forwardRef') | ||
return; | ||
initializer = initializer.arguments[0]; | ||
} | ||
if (initializer && | ||
@@ -516,4 +548,4 @@ (ts.isArrowFunction(initializer) || | ||
if (ts.isShorthandPropertyAssignment(property)) { | ||
var symbol_1 = this.checker.getShorthandAssignmentValueSymbol(property); | ||
var decl = symbol_1 && symbol_1.valueDeclaration; | ||
var symbol_2 = this.checker.getShorthandAssignmentValueSymbol(property); | ||
var decl = symbol_2 && symbol_2.valueDeclaration; | ||
if (decl && decl.initializer) { | ||
@@ -744,3 +776,3 @@ initializer = decl.initializer; | ||
.filter(function (sourceFile) { | ||
return typeof sourceFile !== "undefined"; | ||
return typeof sourceFile !== 'undefined'; | ||
}) | ||
@@ -781,9 +813,19 @@ .reduce(function (docs, sourceFile) { | ||
}); | ||
return docs.concat(componentDocs.filter(function (comp, index, comps) { | ||
// Remove any duplicates (for HOC where the names are the same) | ||
var componentDocsNoDuplicates = componentDocs.reduce(function (prevVal, comp) { | ||
var duplicate = prevVal.find(function (compDoc) { | ||
return compDoc.displayName === comp.displayName; | ||
}); | ||
if (duplicate) | ||
return prevVal; | ||
return prevVal.concat([comp]); | ||
}, []); | ||
var filteredComponentDocs = componentDocsNoDuplicates.filter(function (comp, index, comps) { | ||
return comps | ||
.slice(index + 1) | ||
.every(function (innerComp) { return innerComp.displayName !== comp.displayName; }); | ||
})); | ||
}); | ||
return docs.concat(filteredComponentDocs); | ||
}, []); | ||
} | ||
//# sourceMappingURL=parser.js.map |
{ | ||
"name": "react-docgen-typescript", | ||
"version": "1.18.0", | ||
"version": "1.20.0", | ||
"description": "", | ||
@@ -18,3 +18,3 @@ "homepage": "https://github.com/styleguidist/react-docgen-typescript/", | ||
"print:sample1": "npm run tsc && node ./lib/print.js ./src/__tests__/data/ColumnHigherOrderComponent.tsx simple", | ||
"lint": "tslint -c tslint.json 'src/**/*.{ts,tsx}'", | ||
"lint": "eslint -c lint.json 'src/**/*.{ts,tsx}'", | ||
"lint:fix": "npm run lint -- --fix", | ||
@@ -38,3 +38,5 @@ "prettier:base": "prettier --parser typescript --single-quote --trailing-comma none", | ||
"chai": "^4.1.2", | ||
"eslint": "^7.5.0", | ||
"husky": "^0.14.3", | ||
"install": "^0.13.0", | ||
"lint-staged": "^7.3.0", | ||
@@ -47,3 +49,2 @@ "lodash": "^4.17.15", | ||
"source-map-support": "^0.5.6", | ||
"tslint": "^5.11.0", | ||
"typescript": "3.1.6" | ||
@@ -58,3 +59,4 @@ }, | ||
"url": "https://github.com/styleguidist/react-docgen-typescript.git" | ||
} | ||
}, | ||
"dependencies": {} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
177494
2.44%2328
1.84%19
5.56%33
-26.67%