Don't duplicate the base class when using constantSuper (#13303)

This commit is contained in:
Nicolò Ribaudo
2021-05-15 00:02:46 +02:00
committed by GitHub
parent 3195883923
commit 989aac2a4c
10 changed files with 77 additions and 33 deletions

View File

@@ -672,7 +672,7 @@ const thisContextVisitor = traverse.visitors.merge([
function replaceThisContext(
path,
ref,
superRef,
getSuperRef,
file,
isStaticBlock,
constantSuper,
@@ -682,9 +682,9 @@ function replaceThisContext(
const replacer = new ReplaceSupers({
methodPath: path,
constantSuper,
superRef,
file,
refToPreserve: ref,
getSuperRef,
getObjectRef() {
state.needsClassRef = true;
return isStaticBlock || path.node.static
@@ -710,11 +710,20 @@ export function buildFieldsInitNodes(
constantSuper,
) {
let needsClassRef = false;
let injectSuperRef;
const staticNodes = [];
const instanceNodes = [];
// These nodes are pure and can be moved to the closest statement position
const pureStaticNodes = [];
const getSuperRef = t.isIdentifier(superRef)
? () => superRef
: () => {
injectSuperRef ??=
props[0].scope.generateUidIdentifierBasedOnNode(superRef);
return injectSuperRef;
};
for (const prop of props) {
ts.assertFieldTransformed(prop);
@@ -730,7 +739,7 @@ export function buildFieldsInitNodes(
const replaced = replaceThisContext(
prop,
ref,
superRef,
getSuperRef,
state,
isStaticBlock,
constantSuper,
@@ -865,6 +874,14 @@ export function buildFieldsInitNodes(
prop.remove();
}
if (injectSuperRef) {
path.scope.push({ id: t.cloneNode(injectSuperRef) });
path.set(
"superClass",
t.assignmentExpression("=", injectSuperRef, path.node.superClass),
);
}
if (!needsClassRef) return path;
if (path.isClassExpression()) {