Skip to content

Commit

Permalink
fix: skip decorators and key when replacing super within a class method
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed Mar 5, 2024
1 parent 2cd5bcc commit 707b91c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 35 deletions.
15 changes: 13 additions & 2 deletions packages/babel-helper-replace-supers/src/index.ts
Expand Up @@ -419,16 +419,27 @@ export default class ReplaceSupers {
}

replace() {
const { methodPath } = this;
// https://github.com/babel/babel/issues/11994
if (this.opts.refToPreserve) {
this.methodPath.traverse(unshadowSuperBindingVisitor, {
methodPath.traverse(unshadowSuperBindingVisitor, {
refName: this.opts.refToPreserve.name,
});
}

const handler = this.constantSuper ? looseHandlers : specHandlers;

memberExpressionToFunctions<ReplaceState>(this.methodPath, visitor, {
// todo: this should have been handled by the environmentVisitor,
// consider add visitSelf support for the path.traverse
visitor.shouldSkip = path => {
if (path.parentPath === methodPath) {
if (path.parentKey === "decorators" || path.parentKey === "key") {
return true;
}
}
};

memberExpressionToFunctions<ReplaceState>(methodPath, visitor, {
file: this.file,
scope: this.methodPath.scope,
isDerivedConstructor: this.isDerivedConstructor,
Expand Down

This file was deleted.

Expand Up @@ -64,3 +64,36 @@

expect([...A].map(fn => fn(B)).join(",")).toBe("B#X,B#X,B#X,B#X,B#X,B#X,B#X,B#X,B#X,B#X");
}

{
let B;

class CaptureFactory {
static *id(fn) {
yield fn;
}
}

function dummy() {}

class A extends CaptureFactory {
static #X = "A#X";
static *[Symbol.iterator] () {
B = class {
static #X = "B#X";
@(yield* super.id(_ => _.#X), dummy) #method () {}
@(yield* super.id(_ => _.#X), dummy) static #Method () {}
@(yield* super.id(_ => _.#X), dummy) get #getter () {}
@(yield* super.id(_ => _.#X), dummy) static get #Getter () {}
@(yield* super.id(_ => _.#X), dummy) set #setter (v) {}
@(yield* super.id(_ => _.#X), dummy) static set #Setter (v) {}
@(yield* super.id(_ => _.#X), dummy) #property;
@(yield* super.id(_ => _.#X), dummy) static #Property;
@(yield* super.id(_ => _.#X), dummy) accessor #accessor;
@(yield* super.id(_ => _.#X), dummy) static accessor #Accessor;
}
}
}

expect([...A].map(fn => fn(B)).join(",")).toBe("B#X,B#X,B#X,B#X,B#X,B#X,B#X,B#X,B#X,B#X");
}

0 comments on commit 707b91c

Please sign in to comment.