Skip to content

Commit 84768c2

Browse files
committed
fix
1 parent f74cd15 commit 84768c2

1 file changed

Lines changed: 16 additions & 17 deletions

File tree

packages/javascript-kernel/src/executor.ts

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,19 +1024,15 @@ export class JavaScriptExecutor {
10241024
const declarationType = declaration.id.type;
10251025

10261026
if (declarationType === 'ObjectPattern') {
1027-
// Handle object destructuring: const { a, b } = obj
1027+
// Handle object destructuring: const { a, b } = obj or const { a: b } = obj
10281028
for (const prop of declaration.id.properties) {
1029-
const key = prop.key.name;
1030-
1031-
if (key === 'default') {
1032-
// Handle: const { default: defaultExport } = await import(url)
1033-
if (prop.value.type === 'Identifier') {
1034-
const value = prop.value.name;
1035-
extraCode.push(this._addToGlobalThisCode(value));
1036-
}
1037-
} else {
1038-
extraCode.push(this._addToGlobalThisCode(key));
1039-
}
1029+
// For { a: b }, key is 'a' but local variable is 'b'
1030+
// For { a }, key and value are both 'a'
1031+
const localName =
1032+
prop.value?.type === 'Identifier'
1033+
? prop.value.name
1034+
: prop.key.name;
1035+
extraCode.push(this._addToGlobalThisCode(localName));
10401036
}
10411037
} else if (declarationType === 'ArrayPattern') {
10421038
// Handle array destructuring: const [a, b] = arr
@@ -1228,11 +1224,14 @@ export class JavaScriptExecutor {
12281224
if (importedNames.length > 0) {
12291225
newCodeOfNode += 'const { ';
12301226
for (let j = 0; j < importedNames.length; j++) {
1231-
newCodeOfNode += importedNames[j];
1232-
codeAddToGlobalScope += this._addToGlobalThisCode(
1233-
localNames[j],
1234-
importedNames[j]
1235-
);
1227+
// Handle aliased imports: import { foo as bar } -> const { foo: bar }
1228+
if (importedNames[j] !== localNames[j]) {
1229+
newCodeOfNode += `${importedNames[j]}: ${localNames[j]}`;
1230+
} else {
1231+
newCodeOfNode += importedNames[j];
1232+
}
1233+
// Use local name for globalThis assignment since that's what's in scope
1234+
codeAddToGlobalScope += this._addToGlobalThisCode(localNames[j]);
12361235
if (j < importedNames.length - 1) {
12371236
newCodeOfNode += ', ';
12381237
}

0 commit comments

Comments
 (0)