fix: destructuring namespace import with default (#19526)

This commit is contained in:
Alexander Akait 2025-05-14 17:28:01 +03:00 committed by GitHub
parent c6ae84152e
commit a66acdc81e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 57 additions and 2 deletions

View File

@ -2852,8 +2852,15 @@ class JavascriptParser extends Parser {
for (let i = 0; i < properties.length; i++) {
const property = properties[i];
if (property.type !== "Property") return;
if (property.shorthand && property.value.type === "Identifier") {
this.scope.inShorthand = property.value.name;
if (property.shorthand) {
if (property.value.type === "Identifier") {
this.scope.inShorthand = property.value.name;
} else if (
property.value.type === "AssignmentPattern" &&
property.value.left.type === "Identifier"
) {
this.scope.inShorthand = property.value.left.name;
}
}
const key = property.key;
if (key.type === "Identifier") {

View File

@ -0,0 +1,41 @@
import * as namespace from "./module.js";
it("should work with destructuring", function() {
{
const { foo } = namespace;
expect(foo).toBe("bar");
}
{
let foo;
({ foo } = namespace);
expect(foo).toBe("bar");
}
{
let foo;
({ foo = 'foo' } = namespace);
expect(foo).toBe("bar");
}
{
const { foo = 'foo' } = namespace;
expect(foo).toBe("bar");
}
{
const strings = [];
({ foo : strings[0] } = namespace);
expect(strings[0]).toBe("bar");
}
{
const { foo: otherFoo = 'foo' } = namespace;
expect(otherFoo).toBe("bar");
}
{
const { bar = 'foo' } = namespace;
expect(bar).toBe("foo");
}
});

View File

@ -0,0 +1 @@
export const foo = 'bar';

View File

@ -0,0 +1,6 @@
/** @type {import("../../../../").Configuration} */
module.exports = {
optimization: {
concatenateModules: true
}
};