Skip to content

Commit

Permalink
Fix bug related to non exported functions
Browse files Browse the repository at this point in the history
  • Loading branch information
bvic23 committed Sep 14, 2017
1 parent 53d2472 commit 5bb2510
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 5 deletions.
16 changes: 16 additions & 0 deletions lib/__fixtures__/class-and-function-output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* eslint-disable */
import { TouchableNativeFeedback, Text } from 'react-native';
import React, { Component, PropTypes } from 'react';

const renderItem = ({ item: { date } }) => <View>{date}</View>;

export default class Button extends Component {
render() {
return <TouchableNativeFeedback onPress={this.props.onPress}>
<Text style={{ color: 'green' }}>
{this.props.title}
</Text>
</TouchableNativeFeedback>;
}
}

17 changes: 17 additions & 0 deletions lib/__fixtures__/class-and-function.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* eslint-disable */
import { TouchableNativeFeedback, Text } from 'react-native';
import React, { Component, PropTypes } from 'react';

const renderItem = ({ item: { date } }) => <View>{date}</View>;

export default class Button extends Component {
render() {
return (
<TouchableNativeFeedback onPress={this.props.onPress}>
<Text style={{ color: 'green' }}>
{this.props.title}
</Text>
</TouchableNativeFeedback>
);
}
}
11 changes: 11 additions & 0 deletions lib/__tests__/plugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,14 @@ test('const with destructure in param, should change', () => {
// When
expect(l(code)).toEqual(l(target));
});

test('simple function with destructed param', () => {
// Given
const { source, target } = given('class-and-function');

// When
const code = transform(source);

// When
expect(l(code)).toEqual(l(target));
});
11 changes: 10 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ module.exports = function({ Plugin, types: t }) {
const arrowVisitor = function(state) {
return function(path) {
if (process.env.BABEL_ENV === 'production') return;

const filename = state.file.opts.filename;
const arrowFunction = path.node;
const arrowBody = arrowFunction.body;
Expand All @@ -102,8 +103,16 @@ module.exports = function({ Plugin, types: t }) {
arrowBody.type === 'BlockStatement' &&
lastStatement.type === 'ReturnStatement' &&
lastStatement.argument.type === 'JSXElement';
const isExportDefaultOrVariable = ['ExportDefaultDeclaration', 'VariableDeclarator'].includes(path.parentPath.type)
if (!(isJSXElement || isEmbeddedReturn)) return;
if (!['ExportDefaultDeclaration', 'VariableDeclarator'].includes(path.parentPath.type)) return;
if (!isExportDefaultOrVariable) return;

if (path.parent.id) {
const code = state.file.code;
const functionName = path.parent.id.name;
const isExportConst = path.parentPath.parentPath.parentPath.type === 'ExportNamedDeclaration';
if (!isExportConst && code.indexOf("export default " + functionName) === -1) return
}

const id = path.parentPath.node.id;
const className = id ? id.name : 'Comp';
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "babel-plugin-functional-hmr",
"version": "1.0.20",
"version": "1.0.21",
"description": "HMR for functional components in React Native",
"repository" : {
"type" : "git",
Expand All @@ -23,9 +23,9 @@
"hot-reload",
"hmr",
"functional-components"],
"author": "bvic23",
"license": "MIT",
"devDependencies": {
"author": "bvic23",
"license": "MIT",
"devDependencies": {
"babel-core": "^6.25.0",
"babel-eslint": "^7.2.3",
"babel-jest": "^20.0.3",
Expand Down

0 comments on commit 5bb2510

Please sign in to comment.