From 707b91ce50f88b8f185d613c456bc1c4d600b287 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Tue, 5 Mar 2024 12:12:22 -0500 Subject: [PATCH] fix: skip decorators and key when replacing super within a class method --- .../babel-helper-replace-supers/src/index.ts | 15 +++++++-- .../exec.js | 33 ------------------- .../exec.js | 33 +++++++++++++++++++ 3 files changed, 46 insertions(+), 35 deletions(-) delete mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2023-11-misc/.decorator-evaluation-yield-super-private-all-private/exec.js diff --git a/packages/babel-helper-replace-supers/src/index.ts b/packages/babel-helper-replace-supers/src/index.ts index d30e9e60f989..568bedd76102 100644 --- a/packages/babel-helper-replace-supers/src/index.ts +++ b/packages/babel-helper-replace-supers/src/index.ts @@ -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(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(methodPath, visitor, { file: this.file, scope: this.methodPath.scope, isDerivedConstructor: this.isDerivedConstructor, diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-11-misc/.decorator-evaluation-yield-super-private-all-private/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-11-misc/.decorator-evaluation-yield-super-private-all-private/exec.js deleted file mode 100644 index 12046fcc0847..000000000000 --- a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-11-misc/.decorator-evaluation-yield-super-private-all-private/exec.js +++ /dev/null @@ -1,33 +0,0 @@ -{ - let B; - - class CaptureFactory { - static *id(fn) { - yield fn; - } - } - - function dummy() {} - - class A extends CaptureFactory { - static #X = "A#X"; - static *[Symbol.iterator] () { - B = @dummy - class B { - 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"); -} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-11-misc/decorator-evaluation-yield-private-super-property/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-11-misc/decorator-evaluation-yield-private-super-property/exec.js index 8bb4769085bc..4766b6546da4 100644 --- a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-11-misc/decorator-evaluation-yield-private-super-property/exec.js +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-11-misc/decorator-evaluation-yield-private-super-property/exec.js @@ -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"); +}