react-docgen-typescript
Advanced tools
Comparing version
@@ -367,5 +367,2 @@ "use strict"; | ||
}); | ||
it('should parse referenced props', function () { | ||
testUtils_1.check('StatelessWithReferencedDefaultProps', expectation); | ||
}); | ||
it('should parse props with shorthands', function () { | ||
@@ -395,5 +392,8 @@ testUtils_1.check('StatelessShorthandDefaultProps', { | ||
}); | ||
it('supports spread props', function () { | ||
testUtils_1.check('StatelessWithSpreadDefaultProps', expectation); | ||
it('supports destructuring', function () { | ||
testUtils_1.check('StatelessWithDestructuredProps', expectation); | ||
}); | ||
it('supports destructuring for arrow functions', function () { | ||
testUtils_1.check('StatelessWithDestructuredPropsArrow', expectation); | ||
}); | ||
}); | ||
@@ -669,3 +669,3 @@ it('should parse functional component component defined as function', function () { | ||
name: 'myParam', | ||
type: { name: 'number' }, | ||
type: { name: 'number' } | ||
}, | ||
@@ -675,3 +675,3 @@ { | ||
name: 'mySecondParam?', | ||
type: { name: 'string' }, | ||
type: { name: 'string' } | ||
} | ||
@@ -703,3 +703,3 @@ ]); | ||
var methods = parsed.methods; | ||
chai_1.assert.equal(Boolean(methods.find((function (method) { return method.name === 'myPrivateFunction'; }))), false); | ||
chai_1.assert.equal(Boolean(methods.find(function (method) { return method.name === 'myPrivateFunction'; })), false); | ||
}); | ||
@@ -737,4 +737,4 @@ }); | ||
Header: { | ||
content: { type: 'string', required: true, description: '' }, | ||
}, | ||
content: { type: 'string', required: true, description: '' } | ||
} | ||
}, true, ''); | ||
@@ -741,0 +741,0 @@ }); |
@@ -84,8 +84,10 @@ "use strict"; | ||
var expectedDefaultValue = expectedProp.defaultValue; | ||
var actualDefaultValue = prop.defaultValue | ||
? prop.defaultValue.value | ||
: prop.defaultValue; | ||
if (expectedDefaultValue && | ||
prop.defaultValue && | ||
expectedDefaultValue !== prop.defaultValue.value) { | ||
expectedDefaultValue !== actualDefaultValue) { | ||
errors.push( | ||
// tslint:disable-next-line:max-line-length | ||
"Property '" + compName + "." + expectedPropName + "' defaultValue is different - expected: " + expectedDefaultValue + ", actual: " + prop.defaultValue.value); | ||
"Property '" + compName + "." + expectedPropName + "' defaultValue is different - expected: " + expectedDefaultValue + ", actual: " + actualDefaultValue); | ||
} | ||
@@ -92,0 +94,0 @@ } |
@@ -110,7 +110,8 @@ import * as ts from 'typescript'; | ||
getFullJsDocComment(symbol: ts.Symbol): JSDoc; | ||
getFunctionStatement(statement: ts.Statement): ts.ArrowFunction | ts.FunctionExpression | ts.FunctionDeclaration | undefined; | ||
extractDefaultPropsFromComponent(symbol: ts.Symbol, source: ts.SourceFile): {}; | ||
getLiteralValueFromPropertyAssignment(property: ts.PropertyAssignment): string | null; | ||
getPropMap(properties: ts.NodeArray<ts.PropertyAssignment>): StringIndexedObject<string>; | ||
getLiteralValueFromPropertyAssignment(property: ts.PropertyAssignment | ts.BindingElement): string | null; | ||
getPropMap(properties: ts.NodeArray<ts.PropertyAssignment | ts.BindingElement>): StringIndexedObject<string>; | ||
} | ||
export declare function getDefaultExportForFile(source: ts.SourceFile): string; | ||
export {}; |
"use strict"; | ||
var __assign = (this && this.__assign) || function () { | ||
__assign = Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -354,2 +365,15 @@ var fs = require("fs"); | ||
}; | ||
Parser.prototype.getFunctionStatement = function (statement) { | ||
if (ts.isFunctionDeclaration(statement)) { | ||
return statement; | ||
} | ||
if (ts.isVariableStatement(statement)) { | ||
var initializer = statement.declarationList.declarations[0].initializer; | ||
if (initializer && | ||
(ts.isArrowFunction(initializer) || | ||
ts.isFunctionExpression(initializer))) { | ||
return initializer; | ||
} | ||
} | ||
}; | ||
Parser.prototype.extractDefaultPropsFromComponent = function (symbol, source) { | ||
@@ -366,49 +390,56 @@ var _this = this; | ||
// expression statement used in a React.StatelessComponent | ||
possibleStatements = source.statements.filter(function (stmt) { | ||
return ts.isExpressionStatement(stmt); | ||
}); | ||
possibleStatements = source.statements.filter(function (stmt) { return ts.isExpressionStatement(stmt) || ts.isVariableStatement(stmt); }); | ||
} | ||
if (!possibleStatements.length) { | ||
return {}; | ||
} | ||
var statement = possibleStatements[0]; | ||
if (statementIsClassDeclaration(statement) && statement.members.length) { | ||
var possibleDefaultProps = statement.members.filter(function (member) { return member.name && getPropertyName(member.name) === 'defaultProps'; }); | ||
if (!possibleDefaultProps.length) { | ||
return {}; | ||
} | ||
var defaultProps = possibleDefaultProps[0]; | ||
var initializer = defaultProps.initializer; | ||
var properties = initializer.properties; | ||
while (ts.isIdentifier(initializer)) { | ||
var defaultPropsReference = this.checker.getSymbolAtLocation(initializer); | ||
if (defaultPropsReference) { | ||
var declarations = defaultPropsReference.getDeclarations(); | ||
if (declarations) { | ||
initializer = declarations[0] | ||
.initializer; | ||
properties = initializer.properties; | ||
return possibleStatements.reduce(function (res, statement) { | ||
if (statementIsClassDeclaration(statement) && statement.members.length) { | ||
var possibleDefaultProps = statement.members.filter(function (member) { | ||
return member.name && getPropertyName(member.name) === 'defaultProps'; | ||
}); | ||
if (!possibleDefaultProps.length) { | ||
return res; | ||
} | ||
var defaultProps = possibleDefaultProps[0]; | ||
var initializer = defaultProps.initializer; | ||
var properties = initializer.properties; | ||
while (ts.isIdentifier(initializer)) { | ||
var defaultPropsReference = _this.checker.getSymbolAtLocation(initializer); | ||
if (defaultPropsReference) { | ||
var declarations = defaultPropsReference.getDeclarations(); | ||
if (declarations) { | ||
initializer = declarations[0] | ||
.initializer; | ||
properties = initializer | ||
.properties; | ||
} | ||
} | ||
} | ||
var propMap = {}; | ||
if (properties) { | ||
propMap = _this.getPropMap(properties); | ||
} | ||
return __assign({}, res, propMap); | ||
} | ||
var propMap = {}; | ||
if (properties) { | ||
propMap = this.getPropMap(properties); | ||
else if (statementIsStatelessWithDefaultProps(statement)) { | ||
var propMap_1 = {}; | ||
statement.getChildren().forEach(function (child) { | ||
var right = child.right; | ||
if (right) { | ||
var properties = right.properties; | ||
if (properties) { | ||
propMap_1 = _this.getPropMap(properties); | ||
} | ||
} | ||
}); | ||
return __assign({}, res, propMap_1); | ||
} | ||
return propMap; | ||
} | ||
else if (statementIsStateless(statement)) { | ||
var propMap_1 = {}; | ||
statement.getChildren().forEach(function (child) { | ||
var right = child.right; | ||
if (right) { | ||
var properties = right.properties; | ||
if (properties) { | ||
propMap_1 = _this.getPropMap(properties); | ||
} | ||
var functionStatement = _this.getFunctionStatement(statement); | ||
// Extracting default values from props destructuring | ||
if (functionStatement && functionStatement.parameters.length) { | ||
var name = functionStatement.parameters[0].name; | ||
if (ts.isObjectBindingPattern(name)) { | ||
return __assign({}, res, _this.getPropMap(name.elements)); | ||
} | ||
}); | ||
return propMap_1; | ||
} | ||
return {}; | ||
} | ||
return res; | ||
}, {}); | ||
}; | ||
@@ -479,3 +510,3 @@ Parser.prototype.getLiteralValueFromPropertyAssignment = function (property) { | ||
} | ||
function statementIsStateless(statement) { | ||
function statementIsStatelessWithDefaultProps(statement) { | ||
var children = statement.getChildren(); | ||
@@ -482,0 +513,0 @@ for (var _i = 0, children_1 = children; _i < children_1.length; _i++) { |
{ | ||
"name": "react-docgen-typescript", | ||
"version": "1.12.5", | ||
"version": "1.13.0", | ||
"description": "", | ||
@@ -34,3 +34,3 @@ "homepage": "https://github.com/styleguidist/react-docgen-typescript/", | ||
"husky": "^0.14.3", | ||
"lint-staged": "^7.2.0", | ||
"lint-staged": "^7.3.0", | ||
"mocha": "^5.2.0", | ||
@@ -50,4 +50,4 @@ "prettier": "^1.10.2", | ||
"type": "git", | ||
"url": "https://github.com/styleguidist/react-docgen-typescript.git" | ||
"url": "https://github.com/styleguidist/react-docgen-typescript.git" | ||
} | ||
} |
@@ -48,2 +48,15 @@ # react-docgen-typescript | ||
In case you do not want to print out all the HTML props, because your component is typed like this: | ||
```typescript | ||
const MyComponent: React.FC<React.HTMLAttributes<HTMLDivElement>> = ()... | ||
``` | ||
you can use this workaround inside `propFilter`: | ||
```typescript | ||
if (prop.parent) { | ||
return !prop.parent.fileName.includes('node_modules') | ||
} | ||
return true | ||
``` | ||
Note: `children` without a doc comment will not be documented. | ||
@@ -50,0 +63,0 @@ |
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
144751
2.08%1910
1.81%167
8.44%