Skip to content

Commit 7d9cd93

Browse files
fix: throw on a non-optional read past an undefined DefinePlugin member
A call whose callee chain reads an undefined member of a defined object had its whole callee replaced, so `OBJ.MISSING.a?.()` was emitted as `undefined?.()` and returned undefined where the source throws at `.a`. The chain is now replaced only up to the undefined member, leaving the members after it to read off `undefined` as they would natively. Also covers the short-circuit and throwing paths for the `import.meta` chain root and for `process.env`, the shape #21822 was reported against. Refs #21822
1 parent 18f7ee7 commit 7d9cd93

5 files changed

Lines changed: 86 additions & 19 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-
Fix `DefinePlugin` dropping the optional chain in `OBJ.MISSING?.method()`.
5+
Fix `DefinePlugin` optional chains reading an undefined object member.

‎lib/DefinePlugin.js‎

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const {
2323
const createHash = require("./util/createHash");
2424
const createHooksRegistry = require("./util/createHooksRegistry");
2525

26-
/** @import { Expression } from "estree" */
26+
/** @import { Expression, MemberExpression } from "estree" */
2727
/** @import Compiler from "./Compiler" */
2828
/**
2929
* @import {
@@ -164,6 +164,21 @@ const isObjectDefinition = (code) =>
164164
!(code instanceof RuntimeValue) &&
165165
!(code instanceof RegExp);
166166

167+
/**
168+
* Walks a member chain up, dropping its last `count` members.
169+
* @param {Expression} node member expression
170+
* @param {number} count members to drop
171+
* @returns {Expression} the chain without them
172+
*/
173+
const dropTrailingMembers = (node, count) => {
174+
while (count--) {
175+
node = /** @type {Expression} */ (
176+
/** @type {MemberExpression} */ (node).object
177+
);
178+
}
179+
return node;
180+
};
181+
167182
/** @typedef {Set<string> | null} ObjKeys */
168183
/** @typedef {boolean | undefined | null} AsiSafe */
169184

@@ -1211,10 +1226,13 @@ class DefinePlugin {
12111226
for (const dep of deps) addValueDependency(dep);
12121227
return toConstantDependency(parser, "undefined")(expr);
12131228
});
1214-
// In a call, replace only the callee so the call itself is kept:
1215-
// `x.MISSING()` stays a (throwing) call and `x.MISSING?.()`
1216-
// short-circuits, with the object never inlined. An optional link
1217-
// right after the undefined member short-circuits the whole call.
1229+
// In a call, replace the chain only up to the undefined member,
1230+
// so the call itself is kept and the object is never inlined:
1231+
// `x.MISSING()` stays a (throwing) call, and members left after
1232+
// the undefined one still read off `undefined` and still throw
1233+
// (`x.MISSING.a?.()`). An optional link right after the undefined
1234+
// member short-circuits the whole call instead — its arguments
1235+
// are never evaluated, so they go with it.
12181236
parser.hooks.callMemberChain
12191237
.for(chainRoot)
12201238
.tap(PLUGIN_NAME, (expr, members, membersOptionals) => {
@@ -1228,7 +1246,12 @@ class DefinePlugin {
12281246
toConstantDependency(
12291247
parser,
12301248
"undefined"
1231-
)(/** @type {Expression} */ (expr.callee));
1249+
)(
1250+
dropTrailingMembers(
1251+
/** @type {Expression} */ (expr.callee),
1252+
members.length - 1 - undefinedIndex
1253+
)
1254+
);
12321255
parser.walkExpressions(expr.arguments);
12331256
return true;
12341257
});

‎test/configCases/plugins/define-plugin-import-meta/index.js‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,15 @@ it("should resolve unknown import.meta member access with undefined (issue 15559
1212
expect(import.meta.config?.MISSING).toBe(undefined);
1313
expect(import.meta.config.MISSING?.()).toBe(undefined);
1414
});
15+
it("should short-circuit a call reached through an unknown import.meta member (issue 21822)", function () {
16+
const a = function () { return import.meta.config.MISSING?.includes("token"); };
17+
const b = function () { return import.meta.config?.MISSING?.deep.method(); };
18+
const c = function () { return import.meta.config.MISSING.deep?.method(); };
19+
expect(a.toString()).toBe("function () { return undefined; }");
20+
expect(b.toString()).toBe("function () { return undefined; }");
21+
expect(c.toString()).toBe("function () { return undefined.deep?.method(); }");
22+
expect(import.meta.config.MISSING?.includes("token")).toBe(undefined);
23+
expect(import.meta.config?.MISSING?.deep.method()).toBe(undefined);
24+
expect(import.meta.config.TOKEN?.toUpperCase()).toBe("TOKEN");
25+
expect(() => import.meta.config.MISSING.deep?.method()).toThrow();
26+
});

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

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,53 @@ 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(); };
40+
it("should still substitute arguments of an unknown member call (issue 15559)", function () {
41+
const a = function () { return OBJECT.SUB1.UNKNOWN?.(STRING); };
42+
expect(a.toString()).toBe('function () { return undefined?.("string"); }');
43+
expect(OBJECT.SUB1.UNKNOWN?.(STRING)).toBe(undefined);
44+
});
45+
it("should short-circuit a call reached through an unknown member (issue 21822)", function () {
46+
const a = function () { return process.env.MISSING?.includes("foo"); };
47+
const b = function () { return OBJECT.SUB1.UNKNOWN?.includes("foo"); };
48+
const c = function () { return OBJECT?.SUB1?.UNKNOWN?.toUpperCase(); };
49+
const d = function () { return NOT_DEFINED.SUB2.b?.["includes"]("foo"); };
50+
const e = function () { return OBJECT.SUB1.UNKNOWN?.a.b(); };
51+
const f = function () { return OBJECT.SUB1.UNKNOWN?.a?.b(); };
4652
expect(a.toString()).toBe("function () { return undefined; }");
4753
expect(b.toString()).toBe("function () { return undefined; }");
4854
expect(c.toString()).toBe("function () { return undefined; }");
4955
expect(d.toString()).toBe("function () { return undefined; }");
50-
expect(e.toString()).toBe("function () { return undefined(); }");
56+
expect(e.toString()).toBe("function () { return undefined; }");
57+
expect(f.toString()).toBe("function () { return undefined; }");
58+
expect(process.env.MISSING?.includes("foo")).toBe(undefined);
5159
expect(OBJECT.SUB1.UNKNOWN?.includes("foo")).toBe(undefined);
60+
expect(OBJECT?.SUB1?.UNKNOWN?.toUpperCase()).toBe(undefined);
5261
expect(NOT_DEFINED.SUB2.b?.["includes"]("foo")).toBe(undefined);
62+
expect(OBJECT.SUB1.UNKNOWN?.a.b()).toBe(undefined);
63+
});
64+
it("should not evaluate the arguments of a short-circuited call (issue 21822)", function () {
65+
let calls = 0;
66+
const arg = function () { calls++; return STRING; };
67+
expect(OBJECT.SUB1.UNKNOWN?.includes(arg())).toBe(undefined);
68+
expect(calls).toBe(0);
69+
});
70+
it("should keep a non-optional read after an unknown member throwing (issue 21822)", function () {
71+
const a = function () { return OBJECT.SUB1.UNKNOWN.a?.(); };
72+
const b = function () { return OBJECT.SUB1.UNKNOWN.a?.b(); };
73+
const c = function () { return OBJECT.SUB1.UNKNOWN["a"]?.(); };
74+
const d = function () { return OBJECT.SUB1.UNKNOWN.deep.method(); };
75+
expect(a.toString()).toBe("function () { return undefined.a?.(); }");
76+
expect(b.toString()).toBe("function () { return undefined.a?.b(); }");
77+
expect(c.toString()).toBe('function () { return undefined["a"]?.(); }');
78+
expect(d.toString()).toBe("function () { return undefined.deep.method(); }");
79+
expect(() => OBJECT.SUB1.UNKNOWN.a?.()).toThrow();
5380
expect(() => OBJECT.SUB1.UNKNOWN.a?.b()).toThrow();
81+
expect(() => OBJECT.SUB1.UNKNOWN["a"]?.()).toThrow();
82+
expect(() => OBJECT.SUB1.UNKNOWN.deep.method()).toThrow();
5483
});
55-
it("should still substitute arguments of an unknown member call (issue 15559)", function () {
56-
const a = function () { return OBJECT.SUB1.UNKNOWN?.(STRING); };
57-
expect(a.toString()).toBe('function () { return undefined?.("string"); }');
58-
expect(OBJECT.SUB1.UNKNOWN?.(STRING)).toBe(undefined);
84+
it("should keep optional calls on defined members intact (issue 21822)", function () {
85+
expect(OBJECT.SUB1.a?.toFixed(2)).toBe("1.00");
86+
expect(STRING?.toUpperCase()).toBe("STRING");
87+
expect(process.env.NODE_ENV?.toUpperCase()).toBe("PRODUCTION");
88+
expect(OBJECT.SUB1?.a).toBe(1);
5989
});

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ module.exports = {
99
OBJECT: { SUB1: { a: 1 } },
1010
"OBJECT.SUB2": { a: 1 },
1111
"NOT_DEFINED.SUB2": { a: 1 },
12-
STRING: JSON.stringify("string")
12+
STRING: JSON.stringify("string"),
13+
// the shape issue 21822 was reported against
14+
"process.env": { NODE_ENV: JSON.stringify("production") }
1315
})
1416
]
1517
};

0 commit comments

Comments
 (0)