From 045cf843d13d72c113229dba810b314ac5083c5e Mon Sep 17 00:00:00 2001 From: Naor Matania Date: Tue, 20 Mar 2018 06:32:55 -0700 Subject: [PATCH] Revert D7305051: Work around babel7 issue with duplicate declarations Differential Revision: D7305051 Original commit changeset: ac9a0130dc61 fbshipit-source-id: 6489a2385477be5c37e63387bf6367dd3ae97be1 --- .../babel7-regression/actual.js.es6 | 9 --- .../__fixtures__/babel7-regression/babel.json | 7 -- .../babel7-regression/expected.js.es6 | 37 --------- .../babel-plugin-react-transform-test.js.snap | 78 +++++-------------- .../lib/index.js | 25 +----- 5 files changed, 20 insertions(+), 136 deletions(-) delete mode 100644 packages/metro-babel7-plugin-react-transform/__tests__/__fixtures__/babel7-regression/actual.js.es6 delete mode 100644 packages/metro-babel7-plugin-react-transform/__tests__/__fixtures__/babel7-regression/babel.json delete mode 100644 packages/metro-babel7-plugin-react-transform/__tests__/__fixtures__/babel7-regression/expected.js.es6 diff --git a/packages/metro-babel7-plugin-react-transform/__tests__/__fixtures__/babel7-regression/actual.js.es6 b/packages/metro-babel7-plugin-react-transform/__tests__/__fixtures__/babel7-regression/actual.js.es6 deleted file mode 100644 index 708d32ee..00000000 --- a/packages/metro-babel7-plugin-react-transform/__tests__/__fixtures__/babel7-regression/actual.js.es6 +++ /dev/null @@ -1,9 +0,0 @@ -class Foo { - f() { - class Bar extends React.Component { - render() {} - } - - foo(Bar); - } -} diff --git a/packages/metro-babel7-plugin-react-transform/__tests__/__fixtures__/babel7-regression/babel.json b/packages/metro-babel7-plugin-react-transform/__tests__/__fixtures__/babel7-regression/babel.json deleted file mode 100644 index 06659d70..00000000 --- a/packages/metro-babel7-plugin-react-transform/__tests__/__fixtures__/babel7-regression/babel.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "transforms": [{ - "transform": "react-transform-hmr/lib/index.js", - "imports": ["react"], - "locals": ["module"] - }] -} diff --git a/packages/metro-babel7-plugin-react-transform/__tests__/__fixtures__/babel7-regression/expected.js.es6 b/packages/metro-babel7-plugin-react-transform/__tests__/__fixtures__/babel7-regression/expected.js.es6 deleted file mode 100644 index 15473dd3..00000000 --- a/packages/metro-babel7-plugin-react-transform/__tests__/__fixtures__/babel7-regression/expected.js.es6 +++ /dev/null @@ -1,37 +0,0 @@ -var _transformLib = _interopRequireDefault(require(\\"transform-lib\\")).default; - -const _components = { - Bar: { - displayName: \\"Bar\\", - isInFunction: true - } -}; - -const _transformLib2 = _transformLib({ - filename: \\"unknown\\", - components: _components, - locals: [], - imports: [] -}); - -function _wrapComponent(id) { - return function (Component) { - return _transformLib2(Component, id); - }; -} - -function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } - -class Foo { - f() { - const Bar = _wrapComponent(\\"Bar\\")(function () { - return class Bar extends React.Component { - render() {} - - }; - }()); - - foo(Bar); - } - -} diff --git a/packages/metro-babel7-plugin-react-transform/__tests__/__snapshots__/babel-plugin-react-transform-test.js.snap b/packages/metro-babel7-plugin-react-transform/__tests__/__snapshots__/babel-plugin-react-transform-test.js.snap index 9db26621..30c05d1b 100644 --- a/packages/metro-babel7-plugin-react-transform/__tests__/__snapshots__/babel-plugin-react-transform-test.js.snap +++ b/packages/metro-babel7-plugin-react-transform/__tests__/__snapshots__/babel-plugin-react-transform-test.js.snap @@ -1,40 +1,5 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`finds React components should babel7 regression 1`] = ` -"import _react from \\"react\\"; -import _reactTransformHmrLibIndexJs from \\"react-transform-hmr/lib/index.js\\"; -const _components = { - Bar: { - displayName: \\"Bar\\", - isInFunction: true - } -}; - -const _reactTransformHmrLibIndexJs2 = _reactTransformHmrLibIndexJs({ - filename: \\"unknown\\", - components: _components, - locals: [module], - imports: [_react] -}); - -function _wrapComponent(id) { - return function (Component) { - return _reactTransformHmrLibIndexJs2(Component, id); - }; -} - -class Foo { - f() { - class Bar extends React.Component { - render() {} - } - - Bar = _wrapComponent(\\"Bar\\")(Bar); - foo(Bar); - } -}" -`; - exports[`finds React components should code call expression with render method 1`] = ` "factory({ render() {} @@ -106,10 +71,10 @@ function _wrapComponent(id) { } import React, { Component } from 'react'; -class Foo extends Component { + +const Foo = _wrapComponent('Foo')(class Foo extends Component { render() {} -} -Foo = _wrapComponent('Foo')(Foo);" +});" `; exports[`finds React components should code class extends purecomponent with render method 1`] = ` @@ -134,10 +99,10 @@ function _wrapComponent(id) { } import React, { PureComponent } from 'react'; -class Foo extends PureComponent { + +const Foo = _wrapComponent('Foo')(class Foo extends PureComponent { render() {} -} -Foo = _wrapComponent('Foo')(Foo);" +});" `; exports[`finds React components should code class extends react component 1`] = `"class Foo extends React.Component {}"`; @@ -163,10 +128,9 @@ function _wrapComponent(id) { }; } -class Foo extends React.Component { +const Foo = _wrapComponent(\\"Foo\\")(class Foo extends React.Component { render() {} -} -Foo = _wrapComponent(\\"Foo\\")(Foo);" +});" `; exports[`finds React components should code class with render method 1`] = ` @@ -429,14 +393,13 @@ function _wrapComponent(id) { }; } -class Foo extends BooComponent { +const Foo = _wrapComponent(\\"Foo\\")(class Foo extends BooComponent { render() {} -} -Foo = _wrapComponent(\\"Foo\\")(Foo); -class Bar extends CustomComponent { +}); + +const Bar = _wrapComponent(\\"Bar\\")(class Bar extends CustomComponent { render() {} -} -Bar = _wrapComponent(\\"Bar\\")(Bar);" +});" `; exports[`finds React components should options multiple transforms 1`] = `"class Foo extends React.Component {}"`; @@ -470,10 +433,9 @@ function _wrapComponent(id) { }; } -class Foo extends React.Component { +const Foo = _wrapComponent(\\"Foo\\")(class Foo extends React.Component { render() {} -} -Foo = _wrapComponent(\\"Foo\\")(Foo);" +});" `; exports[`finds React components should options with imports 1`] = `"class Foo extends React.Component {}"`; @@ -501,10 +463,9 @@ function _wrapComponent(id) { }; } -class Foo extends React.Component { +const Foo = _wrapComponent(\\"Foo\\")(class Foo extends React.Component { render() {} -} -Foo = _wrapComponent(\\"Foo\\")(Foo);" +});" `; exports[`finds React components should options with locals 1`] = `"class Foo extends React.Component {}"`; @@ -530,8 +491,7 @@ function _wrapComponent(id) { }; } -class Foo extends React.Component { +const Foo = _wrapComponent(\\"Foo\\")(class Foo extends React.Component { render() {} -} -Foo = _wrapComponent(\\"Foo\\")(Foo);" +});" `; diff --git a/packages/metro-babel7-plugin-react-transform/lib/index.js b/packages/metro-babel7-plugin-react-transform/lib/index.js index c6ac456c..c7fcfdd2 100644 --- a/packages/metro-babel7-plugin-react-transform/lib/index.js +++ b/packages/metro-babel7-plugin-react-transform/lib/index.js @@ -125,29 +125,6 @@ module.exports = { // Can't wrap ClassDeclarations const isStatement = t.isStatement(path.node); - const isExport = t.isExportDefaultDeclaration(path.parent); - - if (isStatement && !isExport) { - // class decl - // need to work around Babel 7 detecting duplicate decls here - - path.insertAfter( - t.expressionStatement( - t.assignmentExpression( - '=', - t.identifier(componentId), - wrapComponent( - t.identifier(componentId), - componentId, - this.wrapperFunctionId, - ), - ), - ), - ); - - return; - } - const expression = t.toExpression(path.node); // wrapperFunction("componentId")(node) @@ -166,7 +143,7 @@ module.exports = { ]); } - if (isExport) { + if (t.isExportDefaultDeclaration(path.parent)) { path.parentPath.insertBefore(wrapped); path.parent.declaration = constId; } else {