Skip to content

Commit 3d08642

Browse files
authored
[babel-plugin] fix named export detection for turbopack (#1394)
1 parent cfe4cdd commit 3d08642

File tree

1 file changed

+35
-34
lines changed

1 file changed

+35
-34
lines changed

packages/@stylexjs/babel-plugin/src/utils/ast-helpers.js

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -211,62 +211,63 @@ export function getProgramStatement(path: NodePath<>): NodePath<> {
211211
* - Direct named exports: `export const x = ...`
212212
* - Locally declared named exports: `const x = ...; export { x }`
213213
*
214-
* Default exports and re-exports from other files (e.g., `export { x } from './other'`) are NOT allowed.
214+
* Default exports, aliasing and re-exports from other files (e.g., `export { x } from './other'`) are NOT allowed.
215215
*/
216216
export function isVariableNamedExported(
217217
path: NodePath<t.VariableDeclarator>,
218218
): boolean {
219-
const declaration = path.parentPath;
220219
const idPath = path.get('id');
221-
if (
222-
!declaration ||
223-
!declaration.isVariableDeclaration() ||
224-
declaration.node.kind !== 'const' ||
225-
!idPath.isIdentifier()
226-
) {
227-
return false;
228-
}
220+
if (!idPath.isIdentifier()) return false;
229221

230222
const variableName = idPath.node.name;
231223

232-
if (
233-
declaration.parentPath?.isExportNamedDeclaration() &&
234-
declaration.parentPath.node.source == null
235-
) {
236-
return true;
237-
}
224+
const binding = path.scope.getBinding(variableName);
225+
if (!binding) return false;
238226

239227
const programPath = getProgramPath(path);
240228
if (programPath == null) {
241229
return false;
242230
}
243231

244-
let result = false;
232+
let exported = false;
245233

246234
programPath.traverse({
247235
ExportNamedDeclaration(p) {
248236
const node = p.node;
249-
if (node.source != null) {
250-
return;
251-
}
252-
if (node.declaration != null) {
237+
238+
if (node.source != null) return;
239+
240+
if (node.declaration) {
241+
if (
242+
node.declaration.type === 'VariableDeclaration' &&
243+
node.declaration.declarations.some(
244+
(d) =>
245+
d.id.type === 'Identifier' &&
246+
p.scope.getBinding(d.id.name) === binding,
247+
)
248+
) {
249+
exported = true;
250+
p.stop();
251+
}
253252
return;
254253
}
255-
if (node.specifiers == null || node.specifiers.length === 0) {
256-
return;
254+
255+
for (const s of node.specifiers ?? []) {
256+
if (s.type !== 'ExportSpecifier') continue;
257+
if (s.local.type !== 'Identifier') continue;
258+
if (s.exported.type !== 'Identifier') continue;
259+
if (s.exportKind === 'type') continue;
260+
if (s.local.name !== s.exported.name) continue;
261+
262+
const localBinding = p.scope.getBinding(s.local.name);
263+
if (localBinding === binding) {
264+
exported = true;
265+
p.stop();
266+
return;
267+
}
257268
}
258-
result =
259-
result ||
260-
node.specifiers.some(
261-
(s) =>
262-
s.type === 'ExportSpecifier' &&
263-
s.local.type === 'Identifier' &&
264-
s.exported.type === 'Identifier' &&
265-
s.local.name === variableName &&
266-
s.exported.name === variableName,
267-
);
268269
},
269270
});
270271

271-
return result;
272+
return exported;
272273
}

0 commit comments

Comments
 (0)