Skip to content

Commit 085c416

Browse files
committed
fix: only short-circuit when the link after the undefined member is optional
1 parent 2b0b49b commit 085c416

3 files changed

Lines changed: 11 additions & 8 deletions

File tree

‎.changeset/051-define-plugin-optional-member-call.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
"webpack": patch
33
---
44

5-
Keep the optional chain in `OBJ.MISSING?.method()` short-circuiting under `DefinePlugin`.
5+
Fix `DefinePlugin` dropping the optional chain in `OBJ.MISSING?.method()`.

‎lib/DefinePlugin.js‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,7 +1149,7 @@ class DefinePlugin {
11491149
const chainRoot = isMeta ? "import.meta" : chainParts[0];
11501150
const chainPrefix = chainParts.slice(isMeta ? 2 : 1);
11511151
/**
1152-
* Whether a member chain reads a property that is not defined.
1152+
* Finds the member of a chain that reads a property that is not defined.
11531153
* Walks the merged view so dotted keys of every instance count.
11541154
* Collects every define key consulted so the caller can record a
11551155
* value dependency on each (a sibling key like `OBJECT.SUB2` affects
@@ -1213,19 +1213,17 @@ class DefinePlugin {
12131213
});
12141214
// In a call, replace only the callee so the call itself is kept:
12151215
// `x.MISSING()` stays a (throwing) call and `x.MISSING?.()`
1216-
// short-circuits, with the object never inlined.
1216+
// short-circuits, with the object never inlined. An optional link
1217+
// right after the undefined member short-circuits the whole call.
12171218
parser.hooks.callMemberChain
12181219
.for(chainRoot)
12191220
.tap(PLUGIN_NAME, (expr, members, membersOptionals) => {
12201221
const deps = [key];
12211222
const undefinedIndex = findUndefinedMemberAccess(members, deps);
12221223
if (undefinedIndex === -1) return;
12231224
for (const dep of deps) addValueDependency(dep);
1224-
for (let i = undefinedIndex + 1; i < members.length; i++) {
1225-
// an optional link after it short-circuits the whole call
1226-
if (membersOptionals[i]) {
1227-
return toConstantDependency(parser, "undefined")(expr);
1228-
}
1225+
if (membersOptionals[undefinedIndex + 1]) {
1226+
return toConstantDependency(parser, "undefined")(expr);
12291227
}
12301228
toConstantDependency(
12311229
parser,

‎test/configCases/plugins/define-plugin-optional-chaining/index.js‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,16 @@ it("should short-circuit a call on a member read from an unknown member (issue 2
4141
const a = function () { return OBJECT.SUB1.UNKNOWN?.includes("foo"); };
4242
const b = function () { return OBJECT?.SUB1?.UNKNOWN?.toUpperCase(); };
4343
const c = function () { return NOT_DEFINED.SUB2.b?.["includes"]("foo"); };
44+
const d = function () { return OBJECT.SUB1.UNKNOWN?.a.b(); };
45+
const e = function () { return OBJECT.SUB1.UNKNOWN.a?.b(); };
4446
expect(a.toString()).toBe("function () { return undefined; }");
4547
expect(b.toString()).toBe("function () { return undefined; }");
4648
expect(c.toString()).toBe("function () { return undefined; }");
49+
expect(d.toString()).toBe("function () { return undefined; }");
50+
expect(e.toString()).toBe("function () { return undefined(); }");
4751
expect(OBJECT.SUB1.UNKNOWN?.includes("foo")).toBe(undefined);
4852
expect(NOT_DEFINED.SUB2.b?.["includes"]("foo")).toBe(undefined);
53+
expect(() => OBJECT.SUB1.UNKNOWN.a?.b()).toThrow();
4954
});
5055
it("should still substitute arguments of an unknown member call (issue 15559)", function () {
5156
const a = function () { return OBJECT.SUB1.UNKNOWN?.(STRING); };

0 commit comments

Comments
 (0)