Skip to content

Commit 84fd011

Browse files
fix: name an anonymous export default class before its static initializers (#21910)
* fix: name an anonymous `export default class` before its static initializers A static initializer reads the class name during ClassDefinitionEvaluation, so the fix-up emitted after the class expression was already too late and the name read back as `__WEBPACK_DEFAULT_EXPORT__`. The class is now created as a property of that name instead, which also drops the runtime helper. * chore: format CHANGELOG.md `fmt:check` is red on `main` since the last release regenerated the file; ported here so this branch is green, and a no-op once `main` carries it. * test: skip the class-static-initializer case below Node 12 The fixture observes the name from a class static field, so Node 10 cannot parse the bundle the harness executes. The fix itself needs no new syntax.
1 parent b6ccff1 commit 84fd011

8 files changed

Lines changed: 66 additions & 10 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+
Name an anonymous `export default class` before its static initializers run.

‎lib/dependencies/HarmonyExportDependencyParserPlugin.js‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,14 +218,19 @@ module.exports = class HarmonyExportDependencyParserPlugin {
218218
: undefined,
219219
constValue
220220
);
221+
const isAnonymousClass =
222+
(node.type === "ClassDeclaration" || node.type === "ClassExpression") &&
223+
!node.id;
221224
dep.isAnonymousDefault =
222225
this.options.anonymousDefaultExportName !== false &&
223226
(node.type === "ArrowFunctionExpression" ||
227+
isAnonymousClass ||
224228
((node.type === "FunctionDeclaration" ||
225-
node.type === "FunctionExpression" ||
226-
node.type === "ClassDeclaration" ||
227-
node.type === "ClassExpression") &&
229+
node.type === "FunctionExpression") &&
228230
!node.id));
231+
// A class observes its own name from a static initializer, which runs
232+
// before the name could be patched afterwards, so it is named in place.
233+
dep.isAnonymousDefaultClass = dep.isAnonymousDefault && isAnonymousClass;
229234
dep.setLocWithIndex(parser.getLocation(statement), -1);
230235
parser.state.current.addDependency(dep);
231236
getInnerGraphUtils(parser.state.compilation).addVariableUsage(

‎lib/dependencies/HarmonyExportExpressionDependency.js‎

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ const DEFAULT_EXPORT_NAME = "default";
2626
/** @import { InlinedValue } from "../optimize/InlineExports" */
2727
/** @import { Range } from "../javascript/JavascriptParser" */
2828
/** @typedef {string | { id?: string | undefined, range: Range, prefix: string, suffix: string }} DeclarationId */
29-
/** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext<[Range, Range, string, DeclarationId | undefined, boolean]>} ObjectDeserializerContext */
30-
/** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext<[Range, Range, string, DeclarationId | undefined, boolean]>} ObjectSerializerContext */
29+
/** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext<[Range, Range, string, DeclarationId | undefined, boolean, boolean]>} ObjectDeserializerContext */
30+
/** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext<[Range, Range, string, DeclarationId | undefined, boolean, boolean]>} ObjectSerializerContext */
3131
/** @import { ExportMap } from "./HarmonyExportInitFragment" */
3232
/**
3333
* @import {
@@ -54,6 +54,8 @@ class HarmonyExportExpressionDependency extends NullDependency {
5454
this.constValue = constValue;
5555
/** @type {boolean} */
5656
this.isAnonymousDefault = false;
57+
/** @type {boolean} */
58+
this.isAnonymousDefaultClass = false;
5759
}
5860

5961
get type() {
@@ -126,6 +128,7 @@ class HarmonyExportExpressionDependency extends NullDependency {
126128
.write(this.prefix)
127129
.write(this.declarationId)
128130
.write(this.isAnonymousDefault)
131+
.write(this.isAnonymousDefaultClass)
129132
.write(this.constValue);
130133
super.serialize(context);
131134
}
@@ -145,8 +148,10 @@ class HarmonyExportExpressionDependency extends NullDependency {
145148
const c4 = c3.rest;
146149
this.isAnonymousDefault = c4.read();
147150
const c5 = c4.rest;
148-
this.constValue = c5.read();
149-
super.deserialize(c5.rest);
151+
this.isAnonymousDefaultClass = c5.read();
152+
const c6 = c5.rest;
153+
this.constValue = c6.read();
154+
super.deserialize(c6.rest);
150155
}
151156
}
152157

@@ -308,6 +313,22 @@ HarmonyExportExpressionDependency.Template = class HarmonyExportDependencyTempla
308313
}
309314

310315
if (dep.range) {
316+
// `export default class {}` names the class "default", and a static
317+
// initializer reads that name before any later fix-up could run, so
318+
// the class is created as a property of that name instead.
319+
if (dep.isAnonymousDefaultClass) {
320+
source.replace(
321+
dep.rangeStatement[0],
322+
dep.range[0] - 1,
323+
`${content}{ ${DEFAULT_EXPORT_NAME}: (${dep.prefix}`
324+
);
325+
source.replace(
326+
dep.range[1],
327+
dep.rangeStatement[1] - 0.5,
328+
`) }.${DEFAULT_EXPORT_NAME};`
329+
);
330+
return;
331+
}
311332
source.replace(
312333
dep.rangeStatement[0],
313334
dep.range[0] - 1,
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export let nameDuringDefinition;
2+
3+
export default class {
4+
static observed = (nameDuringDefinition = this.name);
5+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import Thing, { nameDuringDefinition } from "./Thing";
2+
3+
it("should name an anonymous default class before its static initializers run", () => {
4+
// A static initializer reads the name during ClassDefinitionEvaluation, so a
5+
// fix-up applied after the class expression is too late to be observed.
6+
expect(nameDuringDefinition).toBe("default");
7+
expect(Thing.name).toBe("default");
8+
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"use strict";
2+
3+
// The fixture observes the name from a class static field, which Node 10
4+
// cannot parse when the harness executes the bundle.
5+
module.exports = function filter() {
6+
const major = Number(process.versions.node.split(".")[0]);
7+
return major >= 12;
8+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"use strict";
2+
3+
/** @type {import("../../../../").Configuration} */
4+
module.exports = {
5+
mode: "development",
6+
devtool: false
7+
};

‎test/test262.spectest.js‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -920,9 +920,6 @@ const knownBugs = [
920920
// Potential improvement for enumerate
921921
"module-code/namespace/internals/enumerate-binding-uninit.js",
922922

923-
// Replacing `export default` will remove `default` name by spec, need to `static name = "default";` if doesn't exist
924-
"expressions/class/elements/class-name-static-initializer-default-export.js",
925-
926923
// Tests use `$262.evalScript`/`Object.preventExtensions(this)` to declare
927924
// or collide global bindings; webpack wraps each module so `this` is not
928925
// the realm's global object and there is no Script Record context.

0 commit comments

Comments
 (0)