react-docgen-typescript
Advanced tools
Comparing version
@@ -5,3 +5,3 @@ "use strict"; | ||
var buildFilter_1 = require("../buildFilter"); | ||
function createProp(name, required, defaultValue, description, type) { | ||
function createProp(name, required, defaultValue, description, type, parent) { | ||
if (required === void 0) { required = false; } | ||
@@ -14,2 +14,3 @@ if (description === void 0) { description = ''; } | ||
name: name, | ||
parent: parent, | ||
required: required, | ||
@@ -103,4 +104,30 @@ type: type | ||
}); | ||
it('should be possible to filter by interface in which prop was declared.', function () { | ||
var stringType = { name: 'string' }; | ||
var htmlAttributesInterface = { | ||
fileName: 'node_modules/@types/react/index.d.ts', | ||
name: 'HTMLAttributes' | ||
}; | ||
var excludedType = { name: 'ExcludedType', fileName: 'src/types.ts' }; | ||
var prop1 = createProp('foo', false, undefined, 'foo description', stringType); | ||
var prop2 = createProp('bar', false, undefined, 'bar description', stringType, excludedType); | ||
var prop3 = createProp('onFocus', false, undefined, 'onFocus description', stringType, htmlAttributesInterface); | ||
var opts = { | ||
propFilter: function (prop, component) { | ||
if (prop.parent == null) { | ||
return true; | ||
} | ||
if (prop.parent.fileName.indexOf('@types/react/index.d.ts') > -1) { | ||
return false; | ||
} | ||
return prop.parent.name !== 'ExcludedType'; | ||
} | ||
}; | ||
var filterFn = buildFilter_1.buildFilter(opts); | ||
chai_1.expect([prop1, prop2, prop3].filter(function (prop) { | ||
return filterFn(prop, { name: 'SomeComponent' }); | ||
})).to.eql([prop1]); | ||
}); | ||
}); | ||
}); | ||
//# sourceMappingURL=buildFilter.js.map |
@@ -514,2 +514,17 @@ "use strict"; | ||
}); | ||
it('should allow filtering by parent interface', function () { | ||
var propFilter = function (prop, component) { | ||
if (prop.parent == null) { | ||
return true; | ||
} | ||
return (prop.parent.fileName.indexOf('@types/react') < 0 && | ||
prop.parent.name !== 'HTMLAttributes'); | ||
}; | ||
testUtils_1.check('ColumnWithHtmlAttributes', { | ||
Column: { | ||
prop1: { type: 'string', required: false }, | ||
prop2: { type: 'number' } | ||
} | ||
}, true, undefined, { propFilter: propFilter }); | ||
}); | ||
}); | ||
@@ -516,0 +531,0 @@ describe('skipPropsWithName', function () { |
@@ -18,2 +18,3 @@ import * as ts from 'typescript'; | ||
defaultValue: any; | ||
parent?: ParentType; | ||
} | ||
@@ -27,2 +28,6 @@ export interface Component { | ||
} | ||
export interface ParentType { | ||
name: string; | ||
fileName: string; | ||
} | ||
export declare type PropFilter = (props: PropItem, component: Component) => boolean; | ||
@@ -29,0 +34,0 @@ export interface ParserOptions { |
@@ -203,2 +203,3 @@ "use strict"; | ||
} | ||
var parent = getParentType(prop); | ||
result[propName] = { | ||
@@ -208,2 +209,3 @@ defaultValue: defaultValue, | ||
name: propName, | ||
parent: parent, | ||
required: !isOptional, | ||
@@ -474,2 +476,23 @@ type: { name: propTypeString } | ||
} | ||
function getParentType(prop) { | ||
var decalarations = prop.getDeclarations(); | ||
if (decalarations == null || decalarations.length === 0) { | ||
return undefined; | ||
} | ||
// Props can be declared only in one place | ||
var parent = decalarations[0].parent; | ||
if (!isInterfaceOrTypeAliasDeclaration(parent)) { | ||
return undefined; | ||
} | ||
var parentName = parent.name.text; | ||
var fileName = parent.getSourceFile().fileName; | ||
return { | ||
fileName: fileName, | ||
name: parentName | ||
}; | ||
} | ||
function isInterfaceOrTypeAliasDeclaration(node) { | ||
return (node.kind === ts.SyntaxKind.InterfaceDeclaration || | ||
node.kind === ts.SyntaxKind.TypeAliasDeclaration); | ||
} | ||
//# sourceMappingURL=parser.js.map |
{ | ||
"name": "react-docgen-typescript", | ||
"version": "1.6.3", | ||
"version": "1.7.0", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -163,2 +163,4 @@ # react-docgen-typescript | ||
**@rkostrzewski** *Rafał Kostrzewski* | ||
**@RoystonS** *Royston Shufflebotham* - complete parser rewrite that use typescript parser in much better way and overcome many issues of previous implementation | ||
@@ -165,0 +167,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
113084
5.02%1478
4.97%176
1.15%