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

Add support for higher order components with any arbitrary depth. #32

Merged
merged 5 commits into from
Aug 11, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@ function isExported(path, className, t){
'ExportNamedDeclaration'
];

function findMostRightHandArgument(args = []) {
const arg = args[0]
if (t.isIdentifier(arg)) {
return arg.name
} else if(t.isCallExpression(arg)) {
return findMostRightHandArgument(arg.arguments)
}
}

if(path.parentPath.node &&
types.some(type => {return path.parentPath.node.type === type;})) {
return true;
Expand All @@ -93,7 +102,12 @@ function isExported(path, className, t){
path.node.specifiers.length) {
return className === path.node.specifiers[0].exported.name;
} else if(path.node.type === 'ExportDefaultDeclaration') {
return className === path.node.declaration.name;
const decl = path.node.declaration
if (t.isCallExpression(decl)) {
return className === findMostRightHandArgument(decl.arguments);
} else {
return className === path.node.declaration.name;
}
}
return false;
});
Expand Down
11 changes: 11 additions & 0 deletions test/fixtures/hoc/actual.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Component extends React.Component {
render() { return null }
}

Component.propTypes = {
children: React.PropTypes.string.isRequired,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add some comments in here like /* comment */ like the other files so we can make sure those are extracted properly?

onClick: React.PropTypes.func,
style: React.PropTypes.object,
}

export default withHoc()(deeperHoc(Component))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would you mind adding another test with just 1 hoc? it's a slightly different test case and I don't want this to regress in case someone refactors this.

77 changes: 77 additions & 0 deletions test/fixtures/hoc/expected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
"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; }; }();

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; }

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

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

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

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

return Component;
}(React.Component);

Component.propTypes = {
children: React.PropTypes.string.isRequired,
onClick: React.PropTypes.func,
style: React.PropTypes.object
};

exports.default = withHoc()(deeperHoc(Component));
Component.__docgenInfo = {
"description": "",
"props": {
"children": {
"type": {
"name": "custom",
"raw": "React.PropTypes.string.isRequired"
},
"required": false,
"description": ""
},
"onClick": {
"type": {
"name": "custom",
"raw": "React.PropTypes.func"
},
"required": false,
"description": ""
},
"style": {
"type": {
"name": "custom",
"raw": "React.PropTypes.object"
},
"required": false,
"description": ""
}
}
};

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"
};
}