Skip to content

Commit a5a829e

Browse files
fix(parser): answer an absent export from the spec namespace (#22092)
* fix(parser): answer an absent export from the spec namespace A static reference to a name the module does not export read and wrote the exports object, so every name `Object.prototype` carries — `__proto__`, `toString`, `constructor` — showed through where the spec reports undefined, and a write quietly created a property instead of failing. Route those names through the namespace, which answers both as the spec does. Unskips `module-code/namespace/internals/{get-str-not-found,set}.js` and `module-code/export-expname-binding-index.js`. * test(library): import LibraryType from the declarations that export it `LibraryType` is exported from `declarations/WebpackOptions.d.ts`, not re-exported from the package root, so `lint:types-test` fails on `main` with TS2694. Same one-line change as #22093, ported here to unblock this PR's `lint` rather than wait on that PR to land.
1 parent 5a41278 commit a5a829e

7 files changed

Lines changed: 71 additions & 8 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+
Answer a name a module does not export from the spec namespace.

‎lib/RuntimeTemplate.js‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2571,6 +2571,18 @@ class RuntimeTemplate {
25712571

25722572
if (exportName.length > 0) {
25732573
const exportsInfo = moduleGraph.getExportsInfo(module);
2574+
// A name the module does not export is the namespace's to answer — reading
2575+
// it is undefined and writing it fails — not the exports object's.
2576+
if (
2577+
!isModuleDeferred &&
2578+
usesSpecNamespaceObject(module) &&
2579+
exportsInfo.isExportProvided(exportName) === false
2580+
) {
2581+
runtimeRequirements.add(RuntimeGlobals.specNamespaceObject);
2582+
const ns = `${RuntimeGlobals.specNamespaceObject}(${importVar})`;
2583+
2584+
return `${ns}${propertyAccess(exportName)}`;
2585+
}
25742586
// in some case the exported item is renamed (get this by getUsedName). for example,
25752587
// x.default might be emitted as x.Z (default is renamed to Z)
25762588
const used = exportsInfo.getUsedName(exportName, runtime);

‎test/configCases/library/script-type-keeps-script-output/webpack.config.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ function expectScriptOutput() {
2020
}
2121

2222
/**
23-
* @param {import("../../../../").LibraryType} type library type declared on `output`
23+
* @param {import("../../../../declarations/WebpackOptions").LibraryType} type library type declared on `output`
2424
* @returns {Configuration} configuration
2525
*/
2626
const outputLibrary = (type) => ({
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import * as ns from "./m.js";
2+
3+
it("should read a name the module does not export as undefined", () => {
4+
// A name that is not an export is answered by the namespace, so nothing
5+
// inherited from `Object.prototype` can show through it.
6+
expect(ns.__proto__).toBe(undefined);
7+
expect(ns.toString).toBe(undefined);
8+
expect(ns.constructor).toBe(undefined);
9+
expect(ns.hasOwnProperty).toBe(undefined);
10+
expect(ns.valueOf).toBe(undefined);
11+
expect(ns.notAnExport).toBe(undefined);
12+
expect(ns.renamedLocal).toBe(undefined);
13+
});
14+
15+
it("should refuse a write to a name the module does not export", () => {
16+
expect(() => {
17+
ns.notAnExport = 1;
18+
}).toThrow(TypeError);
19+
expect(() => {
20+
ns.renamedLocal = 1;
21+
}).toThrow(TypeError);
22+
expect(ns.notAnExport).toBe(undefined);
23+
});
24+
25+
it("should let a delete of a name the module does not export succeed", () => {
26+
expect(delete ns.notAnExport).toBe(true);
27+
});
28+
29+
it("should still hand out the names the module does export", () => {
30+
expect(ns.present).toBe(1);
31+
expect(ns.renamed).toBe(2);
32+
expect(Object.getOwnPropertyNames(ns)).toEqual(["present", "renamed"]);
33+
expect(() => {
34+
ns.present = 9;
35+
}).toThrow(TypeError);
36+
});
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
var renamedLocal = 2;
2+
3+
export var present = 1;
4+
export { renamedLocal as renamed };
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"use strict";
2+
3+
/** @type {import("../../../../").Configuration} */
4+
module.exports = {
5+
module: {
6+
rules: [
7+
{ test: /m\.js$/, parser: { specNamespaceObject: true } },
8+
// Reading a name the module does not export is the subject here, and
9+
// webpack warns about each one, so leave that reporting to its own case.
10+
{ test: /index\.js$/, parser: { exportsPresence: false } }
11+
]
12+
}
13+
};

‎test/test262.spectest.js‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -915,13 +915,6 @@ const linkErrorsAtBuildTime = new Set([
915915
]);
916916

917917
const knownBugs = [
918-
// A static export reference is read directly rather than through the
919-
// namespace, so the proxy never sees the delete or the strict assignment.
920-
"module-code/export-expname-binding-index.js",
921-
"module-code/namespace/internals/set.js",
922-
// `ns.__proto__` reaches the exports object instead of reporting undefined
923-
// for a name the module does not export.
924-
"module-code/namespace/internals/get-str-not-found.js",
925918
// Expected error because we use `Promise` to load modules, but this test overrides global `Promise`
926919
"expressions/dynamic-import/returns-promise.js",
927920

0 commit comments

Comments
 (0)