Skip to content

Commit 93b40df

Browse files
authored
babel-plugin: Handle nested call expressions/sequence expressions (#243)
1 parent 58c9d25 commit 93b40df

File tree

3 files changed

+82
-9
lines changed

3 files changed

+82
-9
lines changed

.changeset/orange-turtles-leave.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@vanilla-extract/babel-plugin': patch
3+
---
4+
5+
Correctly insert debugId within nested call expressions and sequence expressions

packages/babel-plugin/src/index.test.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,4 +709,64 @@ describe('babel plugin', () => {
709709
__vanilla_filescope__.endFileScope();"
710710
`);
711711
});
712+
713+
it('should handle nested call expressions', () => {
714+
const source = `
715+
import { style } from '@vanilla-extract/css';
716+
717+
const one = instrument(style({
718+
zIndex: 1,
719+
}));
720+
721+
const two = instrument(instrument(style({
722+
zIndex: 2,
723+
})));
724+
725+
const three = instrument(instrument(instrument(style({
726+
zIndex: 3,
727+
}))));
728+
`;
729+
730+
expect(transform(source)).toMatchInlineSnapshot(`
731+
"import * as __vanilla_filescope__ from '@vanilla-extract/css/fileScope';
732+
733+
__vanilla_filescope__.setFileScope(\\"src/dir/mockFilename.css.ts\\", \\"@vanilla-extract/babel-plugin\\");
734+
735+
import { style } from '@vanilla-extract/css';
736+
const one = instrument(style({
737+
zIndex: 1
738+
}, \\"one\\"));
739+
const two = instrument(instrument(style({
740+
zIndex: 2
741+
}, \\"two\\")));
742+
const three = instrument(instrument(instrument(style({
743+
zIndex: 3
744+
}, \\"three\\"))));
745+
746+
__vanilla_filescope__.endFileScope();"
747+
`);
748+
});
749+
750+
it('should handle instrumentation via sequence expresions', () => {
751+
const source = `
752+
import { style } from '@vanilla-extract/css';
753+
754+
const one = (something++, style({
755+
zIndex: 1,
756+
}));
757+
`;
758+
759+
expect(transform(source)).toMatchInlineSnapshot(`
760+
"import * as __vanilla_filescope__ from '@vanilla-extract/css/fileScope';
761+
762+
__vanilla_filescope__.setFileScope(\\"src/dir/mockFilename.css.ts\\", \\"@vanilla-extract/babel-plugin\\");
763+
764+
import { style } from '@vanilla-extract/css';
765+
const one = (something++, style({
766+
zIndex: 1
767+
}, \\"one\\"));
768+
769+
__vanilla_filescope__.endFileScope();"
770+
`);
771+
});
712772
});

packages/babel-plugin/src/index.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,19 +72,27 @@ const extractName = (node: t.Node) => {
7272
};
7373

7474
const getDebugId = (path: NodePath<t.CallExpression>) => {
75-
const { parent } = path;
75+
const firstRelevantParentPath = path.findParent(
76+
({ node }) => !(t.isCallExpression(node) || t.isSequenceExpression(node)),
77+
);
78+
79+
if (!firstRelevantParentPath) {
80+
return;
81+
}
82+
83+
const relevantParent = firstRelevantParentPath.node;
7684

7785
if (
78-
t.isObjectProperty(parent) ||
79-
t.isReturnStatement(parent) ||
80-
t.isArrowFunctionExpression(parent) ||
81-
t.isArrayExpression(parent) ||
82-
t.isSpreadElement(parent)
86+
t.isObjectProperty(relevantParent) ||
87+
t.isReturnStatement(relevantParent) ||
88+
t.isArrowFunctionExpression(relevantParent) ||
89+
t.isArrayExpression(relevantParent) ||
90+
t.isSpreadElement(relevantParent)
8391
) {
8492
const names: Array<string> = [];
8593

86-
path.findParent(({ node: parentNode }) => {
87-
const name = extractName(parentNode);
94+
path.findParent(({ node }) => {
95+
const name = extractName(node);
8896
if (name) {
8997
names.unshift(name);
9098
}
@@ -94,7 +102,7 @@ const getDebugId = (path: NodePath<t.CallExpression>) => {
94102

95103
return names.join('_');
96104
} else {
97-
return extractName(parent);
105+
return extractName(relevantParent);
98106
}
99107
};
100108

0 commit comments

Comments
 (0)