Skip to content

Commit e696232

Browse files
committed
fix: fix eval order
1 parent dafcffc commit e696232

File tree

5 files changed

+35
-17
lines changed

5 files changed

+35
-17
lines changed

lib/index.js

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ const normalizeOptions = options => ({
1414
|| defaultWrappersConfig,
1515
});
1616

17-
const findContainer = (path, t) => (path.node.body
18-
? path : findContainer(path.parentPath, t));
19-
2017
const findCandidateNameForExpression = (path) => {
2118
let id;
2219
path.find((currentPath) => {
@@ -67,14 +64,26 @@ module.exports = function displayNamePlugin(babel, options = {}) {
6764
});
6865

6966
if (args && displayName) {
70-
const nodes = t.expressionStatement(
71-
t.assignmentExpression(
72-
'=',
73-
t.memberExpression(nameNodeId, t.identifier('displayName')),
74-
t.stringLiteral(displayName),
67+
const tempId = path.scope.generateUidIdentifier();
68+
path.replaceWith(
69+
t.callExpression(
70+
t.arrowFunctionExpression(
71+
[t.cloneNode(tempId)],
72+
t.blockStatement([
73+
t.expressionStatement(
74+
t.assignmentExpression(
75+
'=',
76+
t.memberExpression(t.cloneNode(tempId), t.identifier('displayName')),
77+
t.stringLiteral(displayName),
78+
),
79+
),
80+
t.returnStatement(t.cloneNode(tempId)),
81+
]),
82+
),
83+
[path.node],
7584
),
7685
);
77-
findContainer(path, t).pushContainer('body', nodes);
86+
path.skip();
7887
}
7988
};
8089

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
const React = require('react');
22

3-
const Name = React.memo(() => null);
4-
Name.displayName = "Memo(Name)";
3+
const Name = (_temp => {
4+
_temp.displayName = "Memo(Name)";
5+
return _temp;
6+
})(React.memo(() => null));
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
import { memo } from 'react';
2-
const Name = memo(() => null);
3-
Name.displayName = "Memo(Name)";
2+
3+
const Name = (_temp => {
4+
_temp.displayName = "Memo(Name)";
5+
return _temp;
6+
})(memo(() => null));

test/fixtures/normal/destructed-require/output.mjs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,7 @@ const {
22
memo
33
} = require('react');
44

5-
const Name = memo(() => null);
6-
Name.displayName = "Memo(Name)";
5+
const Name = (_temp => {
6+
_temp.displayName = "Memo(Name)";
7+
return _temp;
8+
})(memo(() => null));

test/fixtures/normal/inner-scope/output.mjs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ const {
33
} = require('react');
44

55
const Outer = () => {
6-
const Name = memo(() => null);
7-
Name.displayName = "Memo(Name)";
6+
const Name = (_temp => {
7+
_temp.displayName = "Memo(Name)";
8+
return _temp;
9+
})(memo(() => null));
810
};
911

0 commit comments

Comments
 (0)