From e696232dc99849dce258623ba5cd6eba5c69e682 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=96=87=E5=AE=87=E7=A5=A5?= Date: Tue, 7 Jun 2022 03:44:02 +0800 Subject: [PATCH] fix: fix eval order --- lib/index.js | 27 ++++++++++++------- test/fixtures/normal/constant/output.mjs | 6 +++-- .../normal/destructed-import/output.mjs | 7 +++-- .../normal/destructed-require/output.mjs | 6 +++-- test/fixtures/normal/inner-scope/output.mjs | 6 +++-- 5 files changed, 35 insertions(+), 17 deletions(-) diff --git a/lib/index.js b/lib/index.js index 6c1baf6..ae96cbf 100644 --- a/lib/index.js +++ b/lib/index.js @@ -14,9 +14,6 @@ const normalizeOptions = options => ({ || defaultWrappersConfig, }); -const findContainer = (path, t) => (path.node.body - ? path : findContainer(path.parentPath, t)); - const findCandidateNameForExpression = (path) => { let id; path.find((currentPath) => { @@ -67,14 +64,26 @@ module.exports = function displayNamePlugin(babel, options = {}) { }); if (args && displayName) { - const nodes = t.expressionStatement( - t.assignmentExpression( - '=', - t.memberExpression(nameNodeId, t.identifier('displayName')), - t.stringLiteral(displayName), + const tempId = path.scope.generateUidIdentifier(); + path.replaceWith( + t.callExpression( + t.arrowFunctionExpression( + [t.cloneNode(tempId)], + t.blockStatement([ + t.expressionStatement( + t.assignmentExpression( + '=', + t.memberExpression(t.cloneNode(tempId), t.identifier('displayName')), + t.stringLiteral(displayName), + ), + ), + t.returnStatement(t.cloneNode(tempId)), + ]), + ), + [path.node], ), ); - findContainer(path, t).pushContainer('body', nodes); + path.skip(); } }; diff --git a/test/fixtures/normal/constant/output.mjs b/test/fixtures/normal/constant/output.mjs index 66fa9b6..f6aeede 100644 --- a/test/fixtures/normal/constant/output.mjs +++ b/test/fixtures/normal/constant/output.mjs @@ -1,4 +1,6 @@ const React = require('react'); -const Name = React.memo(() => null); -Name.displayName = "Memo(Name)"; +const Name = (_temp => { + _temp.displayName = "Memo(Name)"; + return _temp; +})(React.memo(() => null)); diff --git a/test/fixtures/normal/destructed-import/output.mjs b/test/fixtures/normal/destructed-import/output.mjs index a89d898..0c56a6d 100644 --- a/test/fixtures/normal/destructed-import/output.mjs +++ b/test/fixtures/normal/destructed-import/output.mjs @@ -1,3 +1,6 @@ import { memo } from 'react'; -const Name = memo(() => null); -Name.displayName = "Memo(Name)"; + +const Name = (_temp => { + _temp.displayName = "Memo(Name)"; + return _temp; +})(memo(() => null)); \ No newline at end of file diff --git a/test/fixtures/normal/destructed-require/output.mjs b/test/fixtures/normal/destructed-require/output.mjs index 024f288..1cfb8f5 100644 --- a/test/fixtures/normal/destructed-require/output.mjs +++ b/test/fixtures/normal/destructed-require/output.mjs @@ -2,5 +2,7 @@ const { memo } = require('react'); -const Name = memo(() => null); -Name.displayName = "Memo(Name)"; +const Name = (_temp => { + _temp.displayName = "Memo(Name)"; + return _temp; +})(memo(() => null)); \ No newline at end of file diff --git a/test/fixtures/normal/inner-scope/output.mjs b/test/fixtures/normal/inner-scope/output.mjs index 4bce045..5812931 100644 --- a/test/fixtures/normal/inner-scope/output.mjs +++ b/test/fixtures/normal/inner-scope/output.mjs @@ -3,7 +3,9 @@ const { } = require('react'); const Outer = () => { - const Name = memo(() => null); - Name.displayName = "Memo(Name)"; + const Name = (_temp => { + _temp.displayName = "Memo(Name)"; + return _temp; + })(memo(() => null)); };