Skip to content

Commit

Permalink
fix: clone callee when wrapping referenced objects
Browse files Browse the repository at this point in the history
Fixes #20
  • Loading branch information
merceyz committed Jun 18, 2021
1 parent 572b929 commit 16eef18
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 1 deletion.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
},
"devDependencies": {
"@babel/core": "^7.8.4",
"@babel/plugin-transform-modules-commonjs": "^7.8.3",
"@babel/plugin-transform-template-literals": "^7.8.3",
"@babel/plugin-transform-typescript": "^7.8.3",
"@babel/preset-env": "^7.8.4",
Expand Down
5 changes: 4 additions & 1 deletion src/visitors/referencedObjects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ export const referencedObjects: VisitorFunction = ({ expression, pushToQueue })
) {
const init = binding.path.get('init');

init.replaceWith(t.callExpression(expression.node.callee, [binding.path.node.init]));
init.replaceWith(
t.callExpression(t.cloneNode(expression.node.callee), [binding.path.node.init])
);

pushToQueue(init as babel.NodePath<t.CallExpression>);
}
}
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
plugins: [require.resolve('@babel/plugin-transform-modules-commonjs')],
};
7 changes: 7 additions & 0 deletions test/fixtures/referenced-object/transformed-callee/code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import clsx from 'clsx';

const stateClasses = {
checked: isChecked,
};

clsx(stateClasses);
10 changes: 10 additions & 0 deletions test/fixtures/referenced-object/transformed-callee/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';

var _clsx = _interopRequireDefault(require('clsx'));

function _interopRequireDefault(obj) {
return obj && obj.__esModule ? obj : { default: obj };
}

const stateClasses = (0, _clsx.default)(isChecked && 'checked');
(0, _clsx.default)(stateClasses);
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1950,6 +1950,7 @@ __metadata:
dependencies:
"@babel/core": ^7.8.4
"@babel/generator": ^7.6.2
"@babel/plugin-transform-modules-commonjs": ^7.8.3
"@babel/plugin-transform-template-literals": ^7.8.3
"@babel/plugin-transform-typescript": ^7.8.3
"@babel/preset-env": ^7.8.4
Expand Down

0 comments on commit 16eef18

Please sign in to comment.