From d7d9abfe4e0e95a755e1199a90a4c8d8137fc32e Mon Sep 17 00:00:00 2001 From: Lucas Victor Date: Fri, 20 Aug 2021 17:47:40 -0300 Subject: [PATCH] Implemented new treatment for old export declaration (module.exports) in ReactForwardedRefOrMemo components. (#1) * Implemented new treatment for old export declaration (module.exports) in ReactForwardedRefOrMemo components * fix test of moduleExportsInMemoOrForwardRefComponent * removed displayName of module.exports and fix version of babel * fix to remove displayName in module.exports case * changed fix to ignore MemberExpression node type Co-authored-by: Lucas Victor --- index.js | 3 ++- package.json | 5 +++-- .../expected.js | 15 +++++++++++++++ .../reactModuleExportsInMemoForwardRef/input.js | 15 +++++++++++++++ 4 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 test/fixtures/reactModuleExportsInMemoForwardRef/expected.js create mode 100644 test/fixtures/reactModuleExportsInMemoForwardRef/input.js diff --git a/index.js b/index.js index 94c06de..ff8a81b 100644 --- a/index.js +++ b/index.js @@ -60,7 +60,8 @@ function transform (babel) { } var id = path.node.arguments[0].id || findCandidateNameForExpression(path); - if (id) { + + if (id && id.type !== 'MemberExpression') { setDisplayNameUsingClosure(path.get('arguments.0'), id, babel.types); } } diff --git a/package.json b/package.json index aefbecc..35ef217 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@geekie/babel-plugin-add-react-displayname", - "version": "0.0.5", + "version": "0.1.1", "description": "Automatically add displayName to all your components", "main": "index.js", "dependencies": {}, @@ -22,7 +22,8 @@ }, "author": "Ron Cohen", "contributors": [ - "Rodrigo Almeida" + "Rodrigo Almeida", + "Lucas Victor" ], "license": "MIT", "bugs": { diff --git a/test/fixtures/reactModuleExportsInMemoForwardRef/expected.js b/test/fixtures/reactModuleExportsInMemoForwardRef/expected.js new file mode 100644 index 0000000..7908602 --- /dev/null +++ b/test/fixtures/reactModuleExportsInMemoForwardRef/expected.js @@ -0,0 +1,15 @@ +import React from "react"; + +let TestComponent = class TestComponent extends React.Component { + constructor(props) { + super(props); + } + + render() { + return React.createElement("div", null); + } +}; +TestComponent.displayName = "TestComponent"; + + +module.exports = React.forwardRef((props, state) => React.createElement(TestComponent, null)); diff --git a/test/fixtures/reactModuleExportsInMemoForwardRef/input.js b/test/fixtures/reactModuleExportsInMemoForwardRef/input.js new file mode 100644 index 0000000..869ae5a --- /dev/null +++ b/test/fixtures/reactModuleExportsInMemoForwardRef/input.js @@ -0,0 +1,15 @@ +import React from "react"; + +class TestComponent extends React.Component { + constructor(props) { + super(props); + } + + render() { + return ( +
+ ) + } +} + +module.exports = React.forwardRef((props, state) => );