This repository has been archived by the owner on Feb 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
246 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
stage: 0, | ||
loose: true, | ||
optional: ["runtime"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
var fs = require('fs'); | ||
var t = require('../').t; | ||
var toObject = require('tcomb-doc').toObject; | ||
var parseComments = require('get-comments'); | ||
var parseJSDocs = require('doctrine').parse; | ||
|
||
// (path: t.String) => { description: t.String, tags: Array<Tag> } | ||
function getComments(path) { | ||
var source = fs.readFileSync(path, 'utf8'); | ||
var comments = parseComments(source, true); | ||
var values = comments.map(function (comment) { | ||
return comment.value; | ||
}); | ||
return parseJSDocs(values.join('\n'), { unwrap: true }); | ||
} | ||
|
||
// (comments: t.Object) => { component, props: {} } | ||
function getDescriptions(comments) { | ||
var ret = { | ||
component: comments.description, | ||
props: {} | ||
}; | ||
comments.tags.forEach(function (tag) { | ||
if (tag.name) { | ||
ret.props[tag.name] = tag.description; | ||
} | ||
}); | ||
return ret; | ||
} | ||
|
||
// (exports: t.Object) => ReactComponent | ||
function getComponent(defaultExport) { | ||
if (defaultExport['default']) { // eslint-disable-line dot-notation | ||
defaultExport = defaultExport['default']; // eslint-disable-line dot-notation | ||
} | ||
return defaultExport.propTypes ? defaultExport : null; | ||
} | ||
|
||
// (component: ReactComponent) => t.String | ||
function getComponentName(component) { | ||
return component.name; | ||
} | ||
|
||
// (component: ReactComponent) => TcombType | ||
function getPropsType(component) { | ||
var propTypes = component.propTypes; | ||
var props = {}; | ||
Object.keys(propTypes).forEach(function (k) { | ||
if (k !== '__strict__' && k !== '__subtype__') { | ||
props[k] = propTypes[k].tcomb; | ||
} | ||
}); | ||
if (propTypes.hasOwnProperty('__subtype__')) { | ||
return t.refinement(t.struct(props), propTypes.__subtype__.predicate); | ||
} | ||
return t.struct(props); | ||
} | ||
|
||
// (component: ReactComponent) => t.Object | ||
function getDefaultProps(component) { | ||
return component.defaultProps || {}; | ||
} | ||
|
||
// (path: t.String) => { name, description, props } | ||
function parse(path) { | ||
if (t.Array.is(path)) { | ||
return path.map(parse).filter(Boolean); | ||
} | ||
var component = getComponent(require(path)); | ||
if (component) { | ||
var comments = getComments(path); | ||
var descriptions = getDescriptions(comments); | ||
var type = toObject(getPropsType(component)); | ||
var props = type.kind === 'refinement' ? type.type.props : type.props; | ||
var defaultProps = getDefaultProps(component); | ||
var name = getComponentName(component); | ||
for (var prop in props) { | ||
if (props.hasOwnProperty(prop)) { | ||
if (defaultProps.hasOwnProperty(prop)) { | ||
props[prop].defaultValue = defaultProps[prop]; | ||
} | ||
if (descriptions.props.hasOwnProperty(prop)) { | ||
props[prop].description = descriptions.props[prop]; | ||
} | ||
} | ||
} | ||
return { | ||
name: name, | ||
description: descriptions.component, | ||
props: props | ||
}; | ||
} | ||
} | ||
|
||
module.exports = parse; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
var t = require('../').t; | ||
|
||
function getProps(props) { | ||
return Object.keys(props).map(function (k) { | ||
var prop = props[k]; | ||
return '- `' + k + ': ' + prop.name + '` ' + (prop.required ? '' : '(optional' + (t.Nil.is(prop.defaultValue) ? '' : ', default: `' + JSON.stringify(prop.defaultValue) + '`') + ')') + ' ' + ( prop.description || ''); | ||
}).join('\n'); | ||
} | ||
|
||
function toMarkdown(json) { | ||
if (t.Array.is(json)) { | ||
return json.map(toMarkdown).join('\n'); | ||
} | ||
return '# ' + json.name + '\n' + (json.description ? '\n' + json.description + '\n' : '') + '\n**Props**\n\n' + getProps(json.props) + '\n'; | ||
} | ||
|
||
module.exports = toMarkdown; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import React from 'react' | ||
import { props, t } from '../../../../.' | ||
import User from './User' | ||
|
||
/** | ||
* Component description here | ||
* name and surname must be both nil or both specified | ||
* @param name - name description | ||
* @param surname - surname description | ||
*/ | ||
|
||
const Props = t.refinement(t.struct({ | ||
name: t.maybe(User.meta.props.name), | ||
surname: t.maybe(User.meta.props.surname) | ||
}), (x) => t.Nil.is(x.name) === t.Nil.is(x.surname)) | ||
|
||
@props(Props) | ||
export default class Card extends React.Component { | ||
|
||
static defaultProps = { | ||
name: 'Giulio', | ||
surname: 'Canti' | ||
} | ||
|
||
render() { | ||
return ( | ||
<div> | ||
<p>{this.props.name}</p> | ||
<p>{this.props.surname}</p> | ||
</div> | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { t } from '../../../../.' | ||
|
||
export default t.struct({ | ||
name: t.String, | ||
surname: t.String | ||
}, 'User') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"name": "Card", | ||
"description": "Component description here\nname and surname must be both nil or both specified", | ||
"props": { | ||
"name": { | ||
"kind": "irreducible", | ||
"name": "String", | ||
"required": false, | ||
"defaultValue": "Giulio", | ||
"description": "name description" | ||
}, | ||
"surname": { | ||
"kind": "irreducible", | ||
"name": "String", | ||
"required": false, | ||
"defaultValue": "Canti", | ||
"description": "surname description" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Card | ||
|
||
Component description here | ||
name and surname must be both nil or both specified | ||
|
||
**Props** | ||
|
||
- `name: String` (optional, default: `"Giulio"`) name description | ||
- `surname: String` (optional, default: `"Canti"`) surname description |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters