Skip to content

Commit 18f7ee7

Browse files
authored
fix: short-circuit an optional call on an undefined DefinePlugin member (#21823)
* fix: short-circuit an optional call on an undefined DefinePlugin member * fix: only short-circuit when the link after the undefined member is optional --------- Co-authored-by: lazerg <[email protected]>
1 parent 038c301 commit 18f7ee7

3 files changed

Lines changed: 39 additions & 14 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": patch
3+
---
4+
5+
Fix `DefinePlugin` dropping the optional chain in `OBJ.MISSING?.method()`.

‎lib/DefinePlugin.js‎

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,20 +1149,20 @@ 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
11561156
* the result even though `OBJECT` registered the handler).
11571157
* Inherited members (e.g. `toString`) stay defined.
11581158
* @param {Members} members chain members (after the root)
11591159
* @param {string[]} deps consulted define keys (mutated)
1160-
* @returns {boolean} true when the access resolves to `undefined`
1160+
* @returns {number} index of the member resolving to `undefined`, or -1
11611161
*/
1162-
const isUndefinedMemberAccess = (members, deps) => {
1163-
if (members.length <= chainPrefix.length) return false;
1162+
const findUndefinedMemberAccess = (members, deps) => {
1163+
if (members.length <= chainPrefix.length) return -1;
11641164
for (let i = 0; i < chainPrefix.length; i++) {
1165-
if (members[i] !== chainPrefix[i]) return false;
1165+
if (members[i] !== chainPrefix[i]) return -1;
11661166
}
11671167
/** @type {MergedDefinitionNode | undefined} */
11681168
let value = definition;
@@ -1175,9 +1175,9 @@ class DefinePlugin {
11751175
deps.push(nextPath);
11761176
value = value.get(member);
11771177
} else if (member in EMPTY_OBJECT) {
1178-
return false;
1178+
return -1;
11791179
} else {
1180-
return true;
1180+
return i;
11811181
}
11821182
} else if (isObjectDefinition(value)) {
11831183
// non-expandable object leaf (e.g. an array)
@@ -1193,33 +1193,38 @@ class DefinePlugin {
11931193
deps.push(nextPath);
11941194
value = mergedDefinitions.definitions[nextPath];
11951195
} else {
1196-
return true;
1196+
return i;
11971197
}
11981198
} else {
11991199
// a leaf with members left is a real property access on a value
1200-
return false;
1200+
return -1;
12011201
}
12021202
path = nextPath;
12031203
}
1204-
return false;
1204+
return -1;
12051205
};
12061206
parser.hooks.expressionMemberChain
12071207
.for(chainRoot)
12081208
.tap(PLUGIN_NAME, (expr, members) => {
12091209
const deps = [key];
1210-
if (!isUndefinedMemberAccess(members, deps)) return;
1210+
if (findUndefinedMemberAccess(members, deps) === -1) return;
12111211
for (const dep of deps) addValueDependency(dep);
12121212
return toConstantDependency(parser, "undefined")(expr);
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)
1219-
.tap(PLUGIN_NAME, (expr, members) => {
1220+
.tap(PLUGIN_NAME, (expr, members, membersOptionals) => {
12201221
const deps = [key];
1221-
if (!isUndefinedMemberAccess(members, deps)) return;
1222+
const undefinedIndex = findUndefinedMemberAccess(members, deps);
1223+
if (undefinedIndex === -1) return;
12221224
for (const dep of deps) addValueDependency(dep);
1225+
if (membersOptionals[undefinedIndex + 1]) {
1226+
return toConstantDependency(parser, "undefined")(expr);
1227+
}
12231228
toConstantDependency(
12241229
parser,
12251230
"undefined"

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,21 @@ it("should resolve unknown member calls with optional members without inlining t
3737
expect(c.toString()).toBe("function () { return undefined(); }");
3838
expect(() => OBJECT.SUB1?.UNKNOWN()).toThrow();
3939
});
40+
it("should short-circuit a call on a member read from an unknown member (issue 21822)", function () {
41+
const a = function () { return OBJECT.SUB1.UNKNOWN?.includes("foo"); };
42+
const b = function () { return OBJECT?.SUB1?.UNKNOWN?.toUpperCase(); };
43+
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(); };
46+
expect(a.toString()).toBe("function () { return undefined; }");
47+
expect(b.toString()).toBe("function () { return undefined; }");
48+
expect(c.toString()).toBe("function () { return undefined; }");
49+
expect(d.toString()).toBe("function () { return undefined; }");
50+
expect(e.toString()).toBe("function () { return undefined(); }");
51+
expect(OBJECT.SUB1.UNKNOWN?.includes("foo")).toBe(undefined);
52+
expect(NOT_DEFINED.SUB2.b?.["includes"]("foo")).toBe(undefined);
53+
expect(() => OBJECT.SUB1.UNKNOWN.a?.b()).toThrow();
54+
});
4055
it("should still substitute arguments of an unknown member call (issue 15559)", function () {
4156
const a = function () { return OBJECT.SUB1.UNKNOWN?.(STRING); };
4257
expect(a.toString()).toBe('function () { return undefined?.("string"); }');

0 commit comments

Comments
 (0)