Skip to content

Commit

Permalink
now only inject docgen info to exported components
Browse files Browse the repository at this point in the history
  • Loading branch information
madushan1000 committed Nov 1, 2016
1 parent 94ff6e5 commit 2234134
Show file tree
Hide file tree
Showing 12 changed files with 354 additions and 19 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
"main": "lib/index.js",
"devDependencies": {
"babel-cli": "6.x.x",
"babel-preset-es2015": "6.x.x",
"babel-preset-stage-0": "6.x.x",
"babel-preset-es2015": "6.18.0",
"babel-preset-stage-0": "6.16.0",
"mocha": "2.x.x"
},
"scripts": {
Expand All @@ -25,7 +25,7 @@
"babel-plugin"
],
"dependencies": {
"babel-preset-react": "6.x.x",
"babel-preset-react": "6.16.0",
"babel-types": "^6.16.0",
"lodash": "4.x.x",
"react-docgen": "^2.12.1"
Expand Down
20 changes: 15 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,25 @@ export default function ({types: t}) {

function isExported(path, className, t){
const types = [
'ExportDeclaration',
'ExportNamedDeclaration',
'ExportDefaultDeclaration'
'ExportDefaultDeclaration',
'ExportNamedDeclaration'
];

if(path.parentPath.node &&
types.some(type => {return path.parentPath.node.type === type;})) {
return true;
}

const program = path.scope.getProgramParent().path;
return program.get('body').some(path=>{
if(types.some(type=> type == path.node.type)) {
return program.get('body').some(path => {
if(path.node.type === 'ExportNamedDeclaration' &&
path.node.specifiers &&
path.node.specifiers.length) {
return className === path.node.specifiers[0].exported.name;
} else if(path.node.type === 'ExportDefaultDeclaration') {
return className === path.node.declaration.name;
}
return false;
});
}

Expand Down
6 changes: 3 additions & 3 deletions test/fixtures/case1/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@ var CalendarDay = function (_React$Component) {
value: function render() {
var _this3 = this;

var _props = this.props;
var day = _props.day;
var modifiers = _props.modifiers;
var _props = this.props,
day = _props.day,
modifiers = _props.modifiers;


return _react2.default.createElement(
Expand Down
8 changes: 4 additions & 4 deletions test/fixtures/case3/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ var _react2 = _interopRequireDefault(_react);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var Button = function Button(_ref) {
var children = _ref.children;
var onClick = _ref.onClick;
var _ref$style = _ref.style;
var style = _ref$style === undefined ? {} : _ref$style;
var children = _ref.children,
onClick = _ref.onClick,
_ref$style = _ref.style,
style = _ref$style === undefined ? {} : _ref$style;
return _react2.default.createElement(
'button',
{
Expand Down
8 changes: 4 additions & 4 deletions test/fixtures/case4/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ var _react2 = _interopRequireDefault(_react);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var Button = function Button(_ref) {
var children = _ref.children;
var onClick = _ref.onClick;
var _ref$style = _ref.style;
var style = _ref$style === undefined ? {} : _ref$style;
var children = _ref.children,
onClick = _ref.onClick,
_ref$style = _ref.style,
style = _ref$style === undefined ? {} : _ref$style;

return _react2.default.createElement(
'button',
Expand Down
55 changes: 55 additions & 0 deletions test/fixtures/case5/expected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
'use strict';

Object.defineProperty(exports, "__esModule", {
value: true
});

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var Second = function Second() {
return _react2.default.createElement(
'div',
null,
'Sample'
);
};

var First = function First(_ref) {
var children = _ref.children;
return _react2.default.createElement(
'div',
null,
children,
_react2.default.createElement(Second, null)
);
};

First.propTypes = {
children: _react.PropTypes.node
};

exports.default = First;
First.__docgenInfo = {
description: '',
props: {
children: {
type: {
name: 'node'
},
required: false,
description: ''
}
}
};

if (typeof STORYBOOK_REACT_CLASSES !== 'undefined') {
STORYBOOK_REACT_CLASSES['test/fixtures/case5/actual.js'] = {
name: 'First',
docgenInfo: First.__docgenInfo,
path: 'test/fixtures/case5/actual.js'
};
}
17 changes: 17 additions & 0 deletions test/fixtures/case6/actual.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import React from 'react';
export function hoc (Inner) {
const C1 = (props) => <ActualC1 {...props} />
const C2 = (props) => <ActualC2 {...props} />
return {
C1,
C2
}
}

class ActualC1 extends React.Component {
render() { return <div/> }
}

class ActualC2 extends React.Component {
render() { return <div/> }
}
72 changes: 72 additions & 0 deletions test/fixtures/case6/expected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
'use strict';

Object.defineProperty(exports, "__esModule", {
value: true
});

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

exports.hoc = hoc;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

function hoc(Inner) {
var C1 = function C1(props) {
return _react2.default.createElement(ActualC1, props);
};
var C2 = function C2(props) {
return _react2.default.createElement(ActualC2, props);
};
return {
C1: C1,
C2: C2
};
}

var ActualC1 = function (_React$Component) {
_inherits(ActualC1, _React$Component);

function ActualC1() {
_classCallCheck(this, ActualC1);

return _possibleConstructorReturn(this, (ActualC1.__proto__ || Object.getPrototypeOf(ActualC1)).apply(this, arguments));
}

_createClass(ActualC1, [{
key: 'render',
value: function render() {
return _react2.default.createElement('div', null);
}
}]);

return ActualC1;
}(_react2.default.Component);

var ActualC2 = function (_React$Component2) {
_inherits(ActualC2, _React$Component2);

function ActualC2() {
_classCallCheck(this, ActualC2);

return _possibleConstructorReturn(this, (ActualC2.__proto__ || Object.getPrototypeOf(ActualC2)).apply(this, arguments));
}

_createClass(ActualC2, [{
key: 'render',
value: function render() {
return _react2.default.createElement('div', null);
}
}]);

return ActualC2;
}(_react2.default.Component);
18 changes: 18 additions & 0 deletions test/fixtures/case7/actual.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import React, { PropTypes } from 'react'

const Second = () => (
<div>Sample</div>
)

const First = ({ children }) => (
<div>
{ children }
<Second />
</div>
)

First.propTypes = {
children: PropTypes.node
}

export {First}
56 changes: 56 additions & 0 deletions test/fixtures/case7/expected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
'use strict';

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.First = undefined;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var Second = function Second() {
return _react2.default.createElement(
'div',
null,
'Sample'
);
};

var First = function First(_ref) {
var children = _ref.children;
return _react2.default.createElement(
'div',
null,
children,
_react2.default.createElement(Second, null)
);
};

First.propTypes = {
children: _react.PropTypes.node
};

exports.First = First;
First.__docgenInfo = {
description: '',
props: {
children: {
type: {
name: 'node'
},
required: false,
description: ''
}
}
};

if (typeof STORYBOOK_REACT_CLASSES !== 'undefined') {
STORYBOOK_REACT_CLASSES['test/fixtures/case7/actual.js'] = {
name: 'First',
docgenInfo: First.__docgenInfo,
path: 'test/fixtures/case7/actual.js'
};
}
26 changes: 26 additions & 0 deletions test/fixtures/case8/actual.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import React from 'react';

export const wrapComopnent = (Component) => {
const WrappedComponent = props => (
<Wrapper>
<Component/>
</Wrapper>
);
return WrappedComponent;
};

export class Wrapper extends React.Component {
render() {
const { children } = this.props;

return (
<div className="error-box">
{children}
</div>
);
}
}

Wrapper.propTypes = {
children: React.PropTypes.node.isRequired,
};
Loading

0 comments on commit 2234134

Please sign in to comment.