Skip to content

Commit f291d3e

Browse files
fix(define): throw on a non-optional read past an undefined member
`expressionMemberChain` replaced the whole chain with `undefined`, so `OBJ.SUB.MISSING.a` evaluated to `undefined` where the source throws. The chain is now cut at the undefined member, as #21827 already does for calls; an optional link right after it still short-circuits the read. Closes #22014
1 parent ab04b83 commit f291d3e

5 files changed

Lines changed: 75 additions & 4 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+
Throw on a non-optional read past an undefined `DefinePlugin` member.

‎lib/DefinePlugin.js‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,13 +1218,26 @@ class DefinePlugin {
12181218
}
12191219
return -1;
12201220
};
1221+
// Cut the chain at the undefined member, so a read after it still
1222+
// throws; an optional link there short-circuits the whole chain.
12211223
parser.hooks.expressionMemberChain
12221224
.for(chainRoot)
1223-
.tap(PLUGIN_NAME, (expr, members) => {
1225+
.tap(PLUGIN_NAME, (expr, members, membersOptionals) => {
12241226
const deps = [key];
1225-
if (findUndefinedMemberAccess(members, deps) === -1) return;
1227+
const undefinedIndex = findUndefinedMemberAccess(members, deps);
1228+
if (undefinedIndex === -1) return;
12261229
for (const dep of deps) addValueDependency(dep);
1227-
return toConstantDependency(parser, "undefined")(expr);
1230+
return toConstantDependency(
1231+
parser,
1232+
"undefined"
1233+
)(
1234+
membersOptionals[undefinedIndex + 1]
1235+
? expr
1236+
: dropTrailingMembers(
1237+
expr,
1238+
members.length - 1 - undefinedIndex
1239+
)
1240+
);
12281241
});
12291242
// Cut the chain at the undefined member, so what follows still
12301243
// throws; an optional link there takes the call, arguments unevaluated.

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,17 @@ 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 read past an unknown import.meta member as the source would (issue 22014)", function () {
16+
const a = function () { return import.meta.config.MISSING.deep; };
17+
const b = function () { return import.meta.config.MISSING?.deep; };
18+
const c = function () { return import.meta.config.MISSING.deep?.a; };
19+
expect(a.toString()).toBe("function () { return undefined.deep; }");
20+
expect(b.toString()).toBe("function () { return undefined; }");
21+
expect(c.toString()).toBe("function () { return undefined.deep?.a; }");
22+
expect(import.meta.config.MISSING?.deep).toBe(undefined);
23+
expect(a).toThrow(TypeError);
24+
expect(c).toThrow(TypeError);
25+
});
1526
it("should short-circuit a call reached through an unknown import.meta member (issue 21822)", function () {
1627
const a = function () { return import.meta.config.MISSING?.includes("token"); };
1728
const b = function () { return import.meta.config?.MISSING?.deep.method(); };

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,31 @@ 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 short-circuit a read reached through an unknown member (issue 22014)", function () {
86+
const a = function () { return OBJECT.SUB1.UNKNOWN?.a; };
87+
const b = function () { return OBJECT.SUB1.UNKNOWN?.a.b; };
88+
const c = function () { return OBJECT?.SUB1?.UNKNOWN?.["a"]; };
89+
const d = function () { return process.env.MISSING?.length; };
90+
expect(a.toString()).toBe("function () { return undefined; }");
91+
expect(b.toString()).toBe("function () { return undefined; }");
92+
expect(c.toString()).toBe("function () { return undefined; }");
93+
expect(d.toString()).toBe("function () { return undefined; }");
94+
expect(OBJECT.SUB1.UNKNOWN?.a).toBe(undefined);
95+
expect(OBJECT.SUB1.UNKNOWN?.a.b).toBe(undefined);
96+
expect(OBJECT?.SUB1?.UNKNOWN?.["a"]).toBe(undefined);
97+
expect(process.env.MISSING?.length).toBe(undefined);
98+
});
99+
it("should keep a non-optional read past an unknown member throwing (issue 22014)", function () {
100+
const a = function () { return OBJECT.SUB1.UNKNOWN.a?.b; };
101+
const b = function () { return OBJECT.SUB1?.UNKNOWN.a; };
102+
const c = function () { return process.env.MISSING.a?.b; };
103+
expect(a.toString()).toBe("function () { return undefined.a?.b; }");
104+
expect(b.toString()).toBe("function () { return undefined.a; }");
105+
expect(c.toString()).toBe("function () { return undefined.a?.b; }");
106+
expect(a).toThrow(TypeError);
107+
expect(b).toThrow(TypeError);
108+
expect(c).toThrow(TypeError);
109+
});
85110
it("should keep optional calls on defined members intact (issue 21822)", function () {
86111
expect(OBJECT.SUB1.a?.toFixed(2)).toBe("1.00");
87112
expect(STRING?.toUpperCase()).toBe("STRING");

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ it("should define OBJECT.SUB.STRING", function () {
156156
});
157157
it("should replace unknown member access with undefined (issue 15559)", function () {
158158
expect(typeof OBJECT.UNKNOWN).toBe("undefined");
159-
const a = function () { return OBJECT.UNKNOWN.A; };
159+
const a = function () { return OBJECT.UNKNOWN; };
160160
const b = function () { return OBJECT.SUB1.UNKNOWN; };
161161
expect(a.toString()).toBe("function () { return undefined; }");
162162
expect(b.toString()).toBe("function () { return undefined; }");
@@ -176,6 +176,23 @@ it("should resolve unknown member access on a dotted object key (issue 15559)",
176176
expect(a.toString()).toBe("function () { return undefined; }");
177177
expect(b.toString()).toBe("function () { return undefined; }");
178178
});
179+
it("should keep a non-optional read past an unknown member throwing (issue 22014)", function () {
180+
const a = function () { return OBJECT.UNKNOWN.A; };
181+
const b = function () { return OBJECT.SUB1.UNKNOWN.a; };
182+
const c = function () { return OBJECT.SUB1.UNKNOWN["a"]; };
183+
const d = function () { return OBJECT.SUB1.UNKNOWN.a.b; };
184+
const e = function () { return NOT_DEFINED.SUB2.b.deep; };
185+
expect(a.toString()).toBe("function () { return undefined.A; }");
186+
expect(b.toString()).toBe("function () { return undefined.a; }");
187+
expect(c.toString()).toBe('function () { return undefined["a"]; }');
188+
expect(d.toString()).toBe("function () { return undefined.a.b; }");
189+
expect(e.toString()).toBe("function () { return undefined.deep; }");
190+
expect(a).toThrow(TypeError);
191+
expect(b).toThrow(TypeError);
192+
expect(c).toThrow(TypeError);
193+
expect(d).toThrow(TypeError);
194+
expect(e).toThrow(TypeError);
195+
});
179196
it("should not inline the object when calling an unknown member (issue 15559)", function () {
180197
const a = function () { return OBJECT.SUB1.UNKNOWN(); };
181198
const b = function () { return (OBJECT.SUB1.UNKNOWN)(); };

0 commit comments

Comments
 (0)