Skip to content

Commit 0c279a8

Browse files
fix(define): preserve non-optional property access past undefined member
When an object member defined via DefinePlugin resolves to undefined, accessing properties non-optionally past that member was unconditionally collapsed into undefined by expressionMemberChain. At runtime, reading a property off undefined must throw a TypeError. We now cut the chain at the undefined member using dropTrailingMembers when the access is not optional, emitting (undefined).a so the runtime TypeError is preserved natively. Fixes #22014
1 parent 15beefc commit 0c279a8

3 files changed

Lines changed: 33 additions & 4 deletions

File tree

‎lib/DefinePlugin.js‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1220,11 +1220,23 @@ class DefinePlugin {
12201220
};
12211221
parser.hooks.expressionMemberChain
12221222
.for(chainRoot)
1223-
.tap(PLUGIN_NAME, (expr, members) => {
1223+
.tap(PLUGIN_NAME, (expr, members, membersOptionals) => {
12241224
const deps = [key];
1225-
if (findUndefinedMemberAccess(members, deps) === -1) return;
1225+
const undefinedIndex = findUndefinedMemberAccess(members, deps);
1226+
if (undefinedIndex === -1) return;
12261227
for (const dep of deps) addValueDependency(dep);
1227-
return toConstantDependency(parser, "undefined")(expr);
1228+
if (
1229+
members.length - 1 === undefinedIndex ||
1230+
(membersOptionals && membersOptionals[undefinedIndex + 1])
1231+
) {
1232+
return toConstantDependency(parser, "undefined")(expr);
1233+
}
1234+
return toConstantDependency(
1235+
parser,
1236+
"undefined"
1237+
)(
1238+
dropTrailingMembers(expr, members.length - 1 - undefinedIndex)
1239+
);
12281240
});
12291241
// Cut the chain at the undefined member, so what follows still
12301242
// throws; an optional link there takes the call, arguments unevaluated.

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,22 @@ it("should keep a non-optional read after an unknown member throwing (issue 2182
8282
expect(() => OBJECT.SUB1.UNKNOWN["a"]?.()).toThrow();
8383
expect(() => OBJECT.SUB1.UNKNOWN.deep.method()).toThrow();
8484
});
85+
it("should keep a non-optional property access after an unknown member throwing (issue 22014)", function () {
86+
const a = function () { return OBJECT.SUB1.UNKNOWN.a; };
87+
const b = function () { return OBJECT.SUB1.UNKNOWN["a"]; };
88+
const c = function () { return OBJECT.SUB1.UNKNOWN.a.b; };
89+
const d = function () { return OBJECT.SUB1.UNKNOWN.a?.b; };
90+
expect(a.toString()).toBe("function () { return undefined.a; }");
91+
expect(b.toString()).toBe('function () { return undefined["a"]; }');
92+
expect(c.toString()).toBe("function () { return undefined.a.b; }");
93+
expect(d.toString()).toBe("function () { return undefined.a?.b; }");
94+
expect(() => OBJECT.SUB1.UNKNOWN.a).toThrow();
95+
expect(() => OBJECT.SUB1.UNKNOWN["a"]).toThrow();
96+
expect(() => OBJECT.SUB1.UNKNOWN.a.b).toThrow();
97+
expect(() => OBJECT.SUB1.UNKNOWN.a?.b).toThrow();
98+
expect(OBJECT.SUB1.UNKNOWN?.a).toBe(undefined);
99+
expect(OBJECT.SUB1.UNKNOWN).toBe(undefined);
100+
});
85101
it("should keep optional calls on defined members intact (issue 21822)", function () {
86102
expect(OBJECT.SUB1.a?.toFixed(2)).toBe("1.00");
87103
expect(STRING?.toUpperCase()).toBe("STRING");

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,8 @@ it("should replace unknown member access with undefined (issue 15559)", function
158158
expect(typeof OBJECT.UNKNOWN).toBe("undefined");
159159
const a = function () { return OBJECT.UNKNOWN.A; };
160160
const b = function () { return OBJECT.SUB1.UNKNOWN; };
161-
expect(a.toString()).toBe("function () { return undefined; }");
161+
expect(a.toString()).toBe("function () { return undefined.A; }");
162+
expect(() => OBJECT.UNKNOWN.A).toThrow();
162163
expect(b.toString()).toBe("function () { return undefined; }");
163164
expect(OBJECT.SUB1.a).toBe(1);
164165
expect(OBJECT.SUB2.a).toBe(1);

0 commit comments

Comments
 (0)