Skip to content

Commit 2b0b49b

Browse files
committed
fix: short-circuit an optional call on an undefined DefinePlugin member
1 parent 3b81288 commit 2b0b49b

3 files changed

Lines changed: 34 additions & 12 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+
Keep the optional chain in `OBJ.MISSING?.method()` short-circuiting under `DefinePlugin`.

‎lib/DefinePlugin.js‎

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,12 +1157,12 @@ class DefinePlugin {
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,21 +1193,21 @@ 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
});
@@ -1216,10 +1216,17 @@ class DefinePlugin {
12161216
// short-circuits, with the object never inlined.
12171217
parser.hooks.callMemberChain
12181218
.for(chainRoot)
1219-
.tap(PLUGIN_NAME, (expr, members) => {
1219+
.tap(PLUGIN_NAME, (expr, members, membersOptionals) => {
12201220
const deps = [key];
1221-
if (!isUndefinedMemberAccess(members, deps)) return;
1221+
const undefinedIndex = findUndefinedMemberAccess(members, deps);
1222+
if (undefinedIndex === -1) return;
12221223
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+
}
1229+
}
12231230
toConstantDependency(
12241231
parser,
12251232
"undefined"

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ 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+
expect(a.toString()).toBe("function () { return undefined; }");
45+
expect(b.toString()).toBe("function () { return undefined; }");
46+
expect(c.toString()).toBe("function () { return undefined; }");
47+
expect(OBJECT.SUB1.UNKNOWN?.includes("foo")).toBe(undefined);
48+
expect(NOT_DEFINED.SUB2.b?.["includes"]("foo")).toBe(undefined);
49+
});
4050
it("should still substitute arguments of an unknown member call (issue 15559)", function () {
4151
const a = function () { return OBJECT.SUB1.UNKNOWN?.(STRING); };
4252
expect(a.toString()).toBe('function () { return undefined?.("string"); }');

0 commit comments

Comments
 (0)