Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expect __docgenInfo to be before the HoC call #74

Merged
merged 6 commits into from Oct 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
@@ -1,4 +1,5 @@
node_modules
*.log
lib
*.DS_Store
*.DS_Store
.vscode
40 changes: 39 additions & 1 deletion src/index.js
Expand Up @@ -65,7 +65,14 @@ function injectReactDocgenInfo(path, state, code, t) {
t.memberExpression(t.identifier(exportName), t.identifier('__docgenInfo')),
docNode
));
program.pushContainer('body', docgenInfo);

const exportPath = program.get('body').find((node) => isExportCurrent(node, exportName, t));

if (exportPath) {
exportPath.insertBefore(docgenInfo);
} else {
program.pushContainer('body', docgenInfo);
}

injectDocgenGlobal(exportName, path, state, t);
});
Expand Down Expand Up @@ -151,3 +158,34 @@ function buildObjectExpression(obj, t){
return t.nullLiteral();
}
}

function getComponentFromHoC(path) {
if (path.isCallExpression()) {
return getComponentFromHoC(path.get('arguments.0'));
}
return path.isIdentifier() ? path.node.name : null;
}

function isExportCurrent(path, exportName, t) {
if (t.isExportDefaultDeclaration(path)) {
const decl = path.get('declaration');

const identifier = (
decl.isIdentifier() // export default MyComp
? decl.node.name
: getComponentFromHoC(decl) // export default withHoC(MyComp)
);

if (identifier === exportName) {
return true
}
}

if (t.isExportNamedDeclaration(path)) {
return path.get('specifiers').find((sp) => (
sp.node.exported.name === exportName
))
}

return false
}
6 changes: 3 additions & 3 deletions test/fixtures/case2/expected.js
Expand Up @@ -56,8 +56,6 @@ function (_React$Component) {
ErrorBox.propTypes = {
children: _react["default"].PropTypes.node.isRequired
};
var _default = ErrorBox;
exports["default"] = _default;
ErrorBox.__docgenInfo = {
"description": "",
"methods": [],
Expand All @@ -72,11 +70,13 @@ ErrorBox.__docgenInfo = {
}
}
};
var _default = ErrorBox;
exports["default"] = _default;

if (typeof STORYBOOK_REACT_CLASSES !== "undefined") {
STORYBOOK_REACT_CLASSES["test/fixtures/case2/actual.js"] = {
name: "ErrorBox",
docgenInfo: ErrorBox.__docgenInfo,
path: "test/fixtures/case2/actual.js"
};
}
}
21 changes: 10 additions & 11 deletions test/fixtures/case3/expected.js
Expand Up @@ -25,17 +25,6 @@ Button.propTypes = {
onClick: _react["default"].PropTypes.func,
style: _react["default"].PropTypes.object
};
var _default = Button;
exports["default"] = _default;
var A;
A = [1, 2, 2, 2];

function abc() {
var c = function cef() {
A = 'str';
};
}

Button.__docgenInfo = {
"description": "",
"methods": [],
Expand Down Expand Up @@ -68,6 +57,16 @@ Button.__docgenInfo = {
}
}
};
var _default = Button;
exports["default"] = _default;
var A;
A = [1, 2, 2, 2];

function abc() {
var c = function cef() {
A = 'str';
};
}

if (typeof STORYBOOK_REACT_CLASSES !== "undefined") {
STORYBOOK_REACT_CLASSES["test/fixtures/case3/actual.js"] = {
Expand Down
23 changes: 11 additions & 12 deletions test/fixtures/case4/expected.js
Expand Up @@ -25,17 +25,6 @@ Button.propTypes = {
onClick: _react["default"].PropTypes.func,
style: _react["default"].PropTypes.object
};
var _default = Button;
exports["default"] = _default;
var A;
A = [1, 2, 2, 2];

function abc() {
var c = function cef() {
A = 'str';
};
}

Button.__docgenInfo = {
"description": "",
"methods": [],
Expand Down Expand Up @@ -68,11 +57,21 @@ Button.__docgenInfo = {
}
}
};
var _default = Button;
exports["default"] = _default;
var A;
A = [1, 2, 2, 2];

function abc() {
var c = function cef() {
A = 'str';
};
}

if (typeof STORYBOOK_REACT_CLASSES !== "undefined") {
STORYBOOK_REACT_CLASSES["test/fixtures/case4/actual.js"] = {
name: "Button",
docgenInfo: Button.__docgenInfo,
path: "test/fixtures/case4/actual.js"
};
}
}
6 changes: 3 additions & 3 deletions test/fixtures/case5/expected.js
Expand Up @@ -21,8 +21,6 @@ var First = function First(_ref) {
First.propTypes = {
children: _react.PropTypes.node
};
var _default = First;
exports["default"] = _default;
First.__docgenInfo = {
"description": "",
"methods": [],
Expand All @@ -37,11 +35,13 @@ First.__docgenInfo = {
}
}
};
var _default = First;
exports["default"] = _default;

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"
};
}
}
4 changes: 2 additions & 2 deletions test/fixtures/example/expected.js
Expand Up @@ -64,13 +64,13 @@ function (_Component) {
return App;
}(_react.Component);

var _default = App;
exports["default"] = _default;
App.__docgenInfo = {
"description": "",
"methods": [],
"displayName": "App"
};
var _default = App;
exports["default"] = _default;

if (typeof STORYBOOK_REACT_CLASSES !== "undefined") {
STORYBOOK_REACT_CLASSES["test/fixtures/example/actual.js"] = {
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/flowType/expected.js
Expand Up @@ -66,8 +66,6 @@ function (_React$Component) {
return FlowTypeButton;
}(_react["default"].Component);

var _default = FlowTypeButton;
exports["default"] = _default;
FlowTypeButton.__docgenInfo = {
"description": "",
"methods": [{
Expand Down Expand Up @@ -107,6 +105,8 @@ FlowTypeButton.__docgenInfo = {
}
}
};
var _default = FlowTypeButton;
exports["default"] = _default;

if (typeof STORYBOOK_REACT_CLASSES !== "undefined") {
STORYBOOK_REACT_CLASSES["test/fixtures/flowType/actual.js"] = {
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/functionDeclaration/expected.js
Expand Up @@ -21,8 +21,6 @@ function FuncDeclaration(_ref) {
FuncDeclaration.propTypes = {
children: _react.PropTypes.node
};
var _default = FuncDeclaration;
exports["default"] = _default;
FuncDeclaration.__docgenInfo = {
"description": "",
"methods": [],
Expand All @@ -37,6 +35,8 @@ FuncDeclaration.__docgenInfo = {
}
}
};
var _default = FuncDeclaration;
exports["default"] = _default;

if (typeof STORYBOOK_REACT_CLASSES !== "undefined") {
STORYBOOK_REACT_CLASSES["test/fixtures/functionDeclaration/actual.js"] = {
Expand Down
8 changes: 4 additions & 4 deletions test/fixtures/hoc-function/expected.js
Expand Up @@ -30,10 +30,6 @@ TestComponent.propTypes = {
/** Called on click */
onClick: _propTypes["default"].func
};

var _default = (0, _reactRedux.connect)(_testSelector.mapStateToProps, _testSelector.mapDispatchToProps)(TestComponent);

exports["default"] = _default;
TestComponent.__docgenInfo = {
"description": "",
"methods": [],
Expand All @@ -56,6 +52,10 @@ TestComponent.__docgenInfo = {
}
};

var _default = (0, _reactRedux.connect)(_testSelector.mapStateToProps, _testSelector.mapDispatchToProps)(TestComponent);

exports["default"] = _default;

if (typeof STORYBOOK_REACT_CLASSES !== "undefined") {
STORYBOOK_REACT_CLASSES["test/fixtures/hoc-function/actual.js"] = {
name: "TestComponent",
Expand Down
11 changes: 11 additions & 0 deletions test/fixtures/hoc-multiple/actual.js
Expand Up @@ -17,3 +17,14 @@ Component.propTypes = {
}

export default withHoc()(deeperHoc(Component))

class CompA extends React.Component {
render() { return null }
}

CompA.propTypes = {
/** Fancy styles in here */
myProp: React.PropTypes.object,
}

export { CompA }
62 changes: 56 additions & 6 deletions test/fixtures/hoc-multiple/expected.js
Expand Up @@ -3,7 +3,7 @@
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
exports.CompA = exports["default"] = void 0;

function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

Expand Down Expand Up @@ -59,10 +59,6 @@ Component.propTypes = {
/** Fancy styles in here */
style: React.PropTypes.object
};

var _default = withHoc()(deeperHoc(Component));

exports["default"] = _default;
Component.__docgenInfo = {
"description": "Super tiny component",
"methods": [],
Expand Down Expand Up @@ -95,10 +91,64 @@ Component.__docgenInfo = {
}
};

var _default = withHoc()(deeperHoc(Component));

exports["default"] = _default;

var CompA =
/*#__PURE__*/
function (_React$Component2) {
_inherits(CompA, _React$Component2);

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

return _possibleConstructorReturn(this, _getPrototypeOf(CompA).apply(this, arguments));
}

_createClass(CompA, [{
key: "render",
value: function render() {
return null;
}
}]);

return CompA;
}(React.Component);

exports.CompA = CompA;
CompA.propTypes = {
/** Fancy styles in here */
myProp: React.PropTypes.object
};
CompA.__docgenInfo = {
"description": "",
"methods": [],
"displayName": "CompA",
"props": {
"myProp": {
"type": {
"name": "custom",
"raw": "React.PropTypes.object"
},
"required": false,
"description": "Fancy styles in here"
}
}
};

if (typeof STORYBOOK_REACT_CLASSES !== "undefined") {
STORYBOOK_REACT_CLASSES["test/fixtures/hoc-multiple/actual.js"] = {
name: "Component",
docgenInfo: Component.__docgenInfo,
path: "test/fixtures/hoc-multiple/actual.js"
};
}
}

if (typeof STORYBOOK_REACT_CLASSES !== "undefined") {
STORYBOOK_REACT_CLASSES["test/fixtures/hoc-multiple/actual.js"] = {
name: "CompA",
docgenInfo: CompA.__docgenInfo,
path: "test/fixtures/hoc-multiple/actual.js"
};
}
10 changes: 5 additions & 5 deletions test/fixtures/hoc/expected.js
Expand Up @@ -59,10 +59,6 @@ Component.propTypes = {
/** Fancy styles in here */
style: React.PropTypes.object
};

var _default = withHoc(Component);

exports["default"] = _default;
Component.__docgenInfo = {
"description": "Super tiny component",
"methods": [],
Expand Down Expand Up @@ -95,10 +91,14 @@ Component.__docgenInfo = {
}
};

var _default = withHoc(Component);

exports["default"] = _default;

if (typeof STORYBOOK_REACT_CLASSES !== "undefined") {
STORYBOOK_REACT_CLASSES["test/fixtures/hoc/actual.js"] = {
name: "Component",
docgenInfo: Component.__docgenInfo,
path: "test/fixtures/hoc/actual.js"
};
}
}