Skip to content
This repository was archived by the owner on Jun 22, 2022. It is now read-only.

Commit 1bd01dc

Browse files
committed
Fix incorrect mocking inside template literal
1 parent 23bb4ee commit 1bd01dc

File tree

3 files changed

+97
-1
lines changed

3 files changed

+97
-1
lines changed

src/__tests__/__snapshots__/babel.spec.ts.snap

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,24 @@ exports[`For component like source Does not import inside in tagged template 1`]
145145
"import A from \\"a\\";
146146
import styled from \\"styled\\";
147147
import theme from \\"../theme\\";
148+
import { someFunc, someVal } from \\"./some\\";
148149
const StyledA = styled(A)\`
149150
color: \${theme.color.black};
150151
\`;
152+
const StyledB = styled(\\"div\\")\`
153+
height: \${theme.elements.someHeight};
154+
width: \${theme.elements.someWidth};
155+
\`;
156+
const StyledC = styled(\\"div\\")\`\${theme.someFunc()}\`;
157+
const StyledD = styled(\\"div\\")\`\${someFunc()}\`;
158+
const StyledE = styled(\\"div\\")\`\${() => someFunc()}\`;
159+
const StyledF = styled(\\"div\\")\`\${someVal}\`;
151160
React.createElement(StyledA, null);
161+
React.createElement(StyledB, null);
162+
React.createElement(StyledC, null);
163+
React.createElement(StyledD, null);
164+
React.createElement(StyledE, null);
165+
React.createElement(StyledF, null);
152166
export const Mocks = [{
153167
identifier: \\"A\\",
154168
path: \\"a\\",
@@ -170,14 +184,46 @@ var _styled = _interopRequireDefault(require(\\"styled\\"));
170184
171185
var _theme = _interopRequireDefault(require(\\"../theme\\"));
172186
187+
var _some = require(\\"./some\\");
188+
173189
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
174190
175191
const StyledA =
176192
/*#__PURE__*/
177193
(0, _styled.default)(_a.default, {
178194
target: \\"e1h80wsp0\\"
179195
})(\\"color:\\", _theme.default.color.black, \\";\\");
196+
const StyledB =
197+
/*#__PURE__*/
198+
(0, _styled.default)(\\"div\\", {
199+
target: \\"e1h80wsp1\\"
200+
})(\\"height:\\", _theme.default.elements.someHeight, \\";width:\\", _theme.default.elements.someWidth, \\";\\");
201+
const StyledC =
202+
/*#__PURE__*/
203+
(0, _styled.default)(\\"div\\", {
204+
target: \\"e1h80wsp2\\"
205+
})(_theme.default.someFunc());
206+
const StyledD =
207+
/*#__PURE__*/
208+
(0, _styled.default)(\\"div\\", {
209+
target: \\"e1h80wsp3\\"
210+
})((0, _some.someFunc)());
211+
const StyledE =
212+
/*#__PURE__*/
213+
(0, _styled.default)(\\"div\\", {
214+
target: \\"e1h80wsp4\\"
215+
})(() => (0, _some.someFunc)());
216+
const StyledF =
217+
/*#__PURE__*/
218+
(0, _styled.default)(\\"div\\", {
219+
target: \\"e1h80wsp5\\"
220+
})(_some.someVal);
180221
React.createElement(StyledA, null);
222+
React.createElement(StyledB, null);
223+
React.createElement(StyledC, null);
224+
React.createElement(StyledD, null);
225+
React.createElement(StyledE, null);
226+
React.createElement(StyledF, null);
181227
const Mocks = [{
182228
identifier: \\"A\\",
183229
path: \\"a\\",
@@ -298,14 +344,48 @@ var _styled = _interopRequireDefault(require(\\"styled\\"));
298344
299345
var _theme = _interopRequireDefault(require(\\"../theme\\"));
300346
347+
var _some = require(\\"./some\\");
348+
301349
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
302350
303351
var StyledA =
304352
/*#__PURE__*/
305353
(0, _styled.default)(_a.default, {
306354
target: \\"e1h80wsp0\\"
307355
})(\\"color:\\", _theme.default.color.black, \\";\\");
356+
var StyledB =
357+
/*#__PURE__*/
358+
(0, _styled.default)(\\"div\\", {
359+
target: \\"e1h80wsp1\\"
360+
})(\\"height:\\", _theme.default.elements.someHeight, \\";width:\\", _theme.default.elements.someWidth, \\";\\");
361+
var StyledC =
362+
/*#__PURE__*/
363+
(0, _styled.default)(\\"div\\", {
364+
target: \\"e1h80wsp2\\"
365+
})(_theme.default.someFunc());
366+
var StyledD =
367+
/*#__PURE__*/
368+
(0, _styled.default)(\\"div\\", {
369+
target: \\"e1h80wsp3\\"
370+
})((0, _some.someFunc)());
371+
var StyledE =
372+
/*#__PURE__*/
373+
(0, _styled.default)(\\"div\\", {
374+
target: \\"e1h80wsp4\\"
375+
})(function () {
376+
return (0, _some.someFunc)();
377+
});
378+
var StyledF =
379+
/*#__PURE__*/
380+
(0, _styled.default)(\\"div\\", {
381+
target: \\"e1h80wsp5\\"
382+
})(_some.someVal);
308383
React.createElement(StyledA, null);
384+
React.createElement(StyledB, null);
385+
React.createElement(StyledC, null);
386+
React.createElement(StyledD, null);
387+
React.createElement(StyledE, null);
388+
React.createElement(StyledF, null);
309389
const Mocks = [{
310390
identifier: \\"A\\",
311391
path: \\"a\\",

src/__tests__/babel.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,29 @@ describe("For component like source", () => {
149149
import A from "a";
150150
import styled from "styled";
151151
import theme from "../theme";
152+
import { someFunc, someVal } from "./some";
152153
153154
const StyledA = styled(A)\`
154155
color: \${theme.color.black};
155156
\`;
156157
158+
const StyledB = styled("div") \`
159+
height: \${theme.elements.someHeight};
160+
width: \${theme.elements.someWidth};
161+
\`;
162+
163+
const StyledC = styled("div") \`\${theme.someFunc()}\`;
164+
const StyledD = styled("div") \`\${someFunc()}\`;
165+
const StyledE = styled("div") \`\${() => someFunc()}\`;
166+
const StyledF = styled("div") \`\${someVal}\`;
167+
157168
158169
<StyledA />;
170+
<StyledB />;
171+
<StyledC />;
172+
<StyledD />;
173+
<StyledE />;
174+
<StyledF />;
159175
`;
160176

161177
expect(transform(source, { babelrc: false, plugins: [plugin], presets: ["@babel/react"] }).code).toMatchSnapshot();

src/babel.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ export default function plugin({ types: t, template: tmpl }: typeof b, options:
263263
[identifierNode, identifierBinding] = processHocLikeCallExpression(callExp.callee, binding.path.scope);
264264
}
265265
// otherwise process normally
266-
if (!identifierNode) {
266+
if (!identifierNode && !t.isCallExpression(callExp.callee)) {
267267
[identifierNode, identifierBinding] = processHocLikeCallExpression(callExp, binding.path.scope);
268268
}
269269
}

0 commit comments

Comments
 (0)