Skip to content

Commit 3c74cc2

Browse files
authored
Handle alternative compiled forms of array destructured createTheme (#1054)
1 parent a0fd623 commit 3c74cc2

File tree

3 files changed

+103
-17
lines changed

3 files changed

+103
-17
lines changed

.changeset/popular-kings-rhyme.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-debug-ids': patch
3+
---
4+
5+
Handle alternative compiled forms of array destructured `createTheme`

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

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ describe('babel plugin', () => {
413413
`);
414414
});
415415

416-
it('should handle createTheme using destructuring when already compiled', () => {
416+
it('should handle createTheme using destructuring when already compiled scenario 1', () => {
417417
const source = `
418418
import { createTheme } from '@vanilla-extract/css';
419419
@@ -432,6 +432,42 @@ describe('babel plugin', () => {
432432
`);
433433
});
434434

435+
it('should handle createTheme using destructuring when already compiled scenario 2', () => {
436+
const source = `
437+
import { createTheme } from '@vanilla-extract/css';
438+
439+
var ref = _slicedToArray(createTheme({}), 2);
440+
export var myThemeClass = ref[0],
441+
vars = ref[1];
442+
`;
443+
444+
expect(transform(source)).toMatchInlineSnapshot(`
445+
import { createTheme } from '@vanilla-extract/css';
446+
var ref = _slicedToArray(createTheme({}, "myThemeClass"), 2);
447+
export var myThemeClass = ref[0],
448+
vars = ref[1];
449+
`);
450+
});
451+
452+
it('should handle createTheme using destructuring when already compiled scenario 3', () => {
453+
const source = `
454+
import { createTheme } from '@vanilla-extract/css';
455+
456+
var ref = _slicedToArray(createTheme({}), 2),
457+
myThemeClass = ref[0],
458+
vars = ref[1];
459+
export { myThemeClass, vars };
460+
`;
461+
462+
expect(transform(source)).toMatchInlineSnapshot(`
463+
import { createTheme } from '@vanilla-extract/css';
464+
var ref = _slicedToArray(createTheme({}, "myThemeClass"), 2),
465+
myThemeClass = ref[0],
466+
vars = ref[1];
467+
export { myThemeClass, vars };
468+
`);
469+
});
470+
435471
it('should handle createGlobalTheme', () => {
436472
const source = `
437473
import { createGlobalTheme } from '@vanilla-extract/css';

packages/babel-plugin-debug-ids/src/index.ts

Lines changed: 61 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -93,27 +93,72 @@ const getDebugId = (path: NodePath<t.CallExpression>) => {
9393
return;
9494
}
9595

96-
// Special case: Handle `export const [themeClass, vars] = createTheme({});`
97-
// when it's already been compiled into this:
96+
// Special case 1: Handle `export const [themeClass, vars] = createTheme({});`
97+
// when it's already been compiled into one of the following forms:
9898
//
9999
// var _createTheme = createTheme({}),
100100
// _createTheme2 = _slicedToArray(_createTheme, 2),
101101
// themeClass = _createTheme2[0],
102102
// vars = _createTheme2[1];
103-
if (
104-
t.isVariableDeclaration(firstRelevantParentPath.parent) &&
105-
firstRelevantParentPath.parent.declarations.length === 4
106-
) {
107-
const [themeDeclarator, , classNameDeclarator] =
108-
firstRelevantParentPath.parent.declarations;
109-
110-
if (
111-
t.isCallExpression(themeDeclarator.init) &&
112-
t.isIdentifier(themeDeclarator.init.callee, { name: 'createTheme' }) &&
113-
t.isVariableDeclarator(classNameDeclarator) &&
114-
t.isIdentifier(classNameDeclarator.id)
115-
) {
116-
return classNameDeclarator.id.name;
103+
if (t.isVariableDeclaration(firstRelevantParentPath.parent)) {
104+
if (firstRelevantParentPath.parent.declarations.length === 4) {
105+
const [themeDeclarator, , classNameDeclarator] =
106+
firstRelevantParentPath.parent.declarations;
107+
108+
if (
109+
t.isCallExpression(themeDeclarator.init) &&
110+
t.isIdentifier(themeDeclarator.init.callee, { name: 'createTheme' }) &&
111+
t.isIdentifier(classNameDeclarator.id)
112+
) {
113+
return classNameDeclarator.id.name;
114+
}
115+
}
116+
// alternative compiled form:
117+
//
118+
// var ref = _slicedToArray(createTheme({}), 2);
119+
// export var themeClass = ref[0],
120+
// vars = ref[1];
121+
else if (firstRelevantParentPath.parent.declarations.length === 1) {
122+
const [themeDeclarator] = firstRelevantParentPath.parent.declarations;
123+
const nextSibling =
124+
firstRelevantParentPath.parentPath?.getNextSibling().node;
125+
126+
if (
127+
t.isCallExpression(themeDeclarator.init) &&
128+
t.isCallExpression(themeDeclarator.init.arguments[0]) &&
129+
t.isIdentifier(themeDeclarator.init.arguments[0].callee, {
130+
name: 'createTheme',
131+
}) &&
132+
t.isExportNamedDeclaration(nextSibling) &&
133+
t.isVariableDeclaration(nextSibling.declaration) &&
134+
t.isVariableDeclarator(nextSibling.declaration.declarations[0]) &&
135+
t.isIdentifier(nextSibling.declaration.declarations[0].id)
136+
) {
137+
return nextSibling.declaration.declarations[0].id.name;
138+
}
139+
}
140+
// Special case 2: Handle `const [themeClass, vars] = createTheme({});
141+
// export { themeClass, vars };`
142+
// when compiled into the following:
143+
//
144+
// var ref = _slicedToArray(createTheme({}), 2),
145+
// myThemeClass = ref[0],
146+
// vars = ref[1];
147+
// export { themeClass, vars };
148+
else if (firstRelevantParentPath.parent.declarations.length === 3) {
149+
const [themeDeclarator, classNameDeclarator] =
150+
firstRelevantParentPath.parent.declarations;
151+
152+
if (
153+
t.isCallExpression(themeDeclarator.init) &&
154+
t.isCallExpression(themeDeclarator.init.arguments[0]) &&
155+
t.isIdentifier(themeDeclarator.init.arguments[0].callee, {
156+
name: 'createTheme',
157+
}) &&
158+
t.isIdentifier(classNameDeclarator.id)
159+
) {
160+
return classNameDeclarator.id.name;
161+
}
117162
}
118163
}
119164

0 commit comments

Comments
 (0)