Skip to content
This repository has been archived by the owner on Apr 9, 2020. It is now read-only.

Commit

Permalink
Adding support in for module pattern, adding Function Expressions to …
Browse files Browse the repository at this point in the history
…the current visitors added for pure components.
  • Loading branch information
Josh Black committed Oct 6, 2015
1 parent 9ff3d61 commit e2aa5fa
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 10 deletions.
24 changes: 19 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ export default function ({ Plugin, types: t }) {
if (node.id) {
return node.id.name;
}
if (t.isFunctionExpression && t.isProperty(parent) && parent.key) {
return parent.key.name;
}
if (t.isExportDefaultDeclaration(parent) &&
(t.isArrowFunctionExpression(node) || t.isFunctionExpression(node))) {
return file.opts.basename;
Expand Down Expand Up @@ -297,7 +300,7 @@ export default function ({ Plugin, types: t }) {
}
},

'FunctionDeclaration|ArrowFunctionExpression': {
'FunctionDeclaration|FunctionExpression|ArrowFunctionExpression': {
enter(node, parent, scope, file) {
if (!this.state[depthKey]) {
this.state[depthKey] = 0;
Expand All @@ -312,16 +315,27 @@ export default function ({ Plugin, types: t }) {
return;
}

// No-op for `render` methods in this visitor
if (parent.key && parent.key.name === 'render') {
return;
}

delete this.state[foundJSXKey];

const wrapReactComponentId = this.state[wrapComponentIdKey];
const uniqueId = addComponentRecord(node, parent, scope, file, this.state);
const bindingId = node.id;

if (t.isArrowFunctionExpression(node) &&
(t.isVariableDeclarator(parent) ||
t.isExportDefaultDeclaration(parent))
) {
if (t.isArrowFunctionExpression(node)) {
if (t.isVariableDeclarator(parent) || t.isExportDefaultDeclaration(parent)) {
return t.callExpression(
t.callExpression(wrapReactComponentId, [t.literal(uniqueId)]),
[node]
);
}
}

if (t.isFunctionExpression(node)) {
return t.callExpression(
t.callExpression(wrapReactComponentId, [t.literal(uniqueId)]),
[node]
Expand Down
11 changes: 10 additions & 1 deletion test/fixtures/pure-function/actual.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';

function DefaultFunction() {
function FunctionDeclaration() {
return <div />;
}

Expand All @@ -12,6 +12,15 @@ function DefaultExportFunction() {
return <div />;
}

const ModulePattern = {
NoProps() {
return <div />;
},
WithProps(props) {
return <div {...props} />;
}
};

export { NamedExportFunction };
export default DefaultExportFunction;
export default function () {
Expand Down
25 changes: 21 additions & 4 deletions test/fixtures/pure-function/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ Object.defineProperty(exports, '__esModule', {
var _react2 = _interopRequireDefault(_react);

var _components = {
_$DefaultFunction: {
displayName: 'DefaultFunction',
_$FunctionDeclaration: {
displayName: 'FunctionDeclaration',
isFunction: true
},
_$NamedExportFunction: {
Expand All @@ -29,6 +29,14 @@ var _components = {
displayName: 'DefaultExportFunction',
isFunction: true
},
_$NoProps: {
displayName: 'NoProps',
isFunction: true
},
_$WithProps: {
displayName: 'WithProps',
isFunction: true
},
_$actual: {
displayName: 'actual',
isFunction: true
Expand Down Expand Up @@ -57,10 +65,11 @@ function _wrapComponent(uniqueId) {

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

function DefaultFunction() {
function FunctionDeclaration() {
return _react2['default'].createElement('div', null);
}

FunctionDeclaration = _wrapComponent('_$FunctionDeclaration')(FunctionDeclaration)
function NamedExportFunction() {
return _react2['default'].createElement('div', null);
}
Expand All @@ -69,8 +78,16 @@ exports.NamedExportFunction = NamedExportFunction = _wrapComponent('_$NamedExpor
function DefaultExportFunction() {
return _react2['default'].createElement('div', null);
}

DefaultExportFunction = _wrapComponent('_$DefaultExportFunction')(DefaultExportFunction)
var ModulePattern = {
NoProps: _wrapComponent('_$NoProps')(function () {
return _react2['default'].createElement('div', null);
}),
WithProps: _wrapComponent('_$WithProps')(function (props) {
return _react2['default'].createElement('div', props);
})
};

exports.NamedExportFunction = NamedExportFunction;
exports['default'] = DefaultExportFunction;
exports['default'] = _wrapComponent('_$actual')(function () {
Expand Down

1 comment on commit e2aa5fa

@joshblack
Copy link
Collaborator

Choose a reason for hiding this comment

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

Added in support for the module pattern (including function expressions). This change introduced a regression with the factory function declaration introduced in the classic and modern tests that I'm trying to debug now.

Please sign in to comment.