From a1e043bd20a395f23966c7bdc18ccca6dc5db135 Mon Sep 17 00:00:00 2001 From: Vladimir Date: Tue, 27 Jun 2023 11:38:43 +0200 Subject: [PATCH] fix(browser): transform superclass identifier (#3681) --- packages/browser/src/node/esmInjector.ts | 5 ++++- test/core/test/injector-esm.test.ts | 20 ++++++++++++++++++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/packages/browser/src/node/esmInjector.ts b/packages/browser/src/node/esmInjector.ts index 80b96cb01214..6b2da38f6198 100644 --- a/packages/browser/src/node/esmInjector.ts +++ b/packages/browser/src/node/esmInjector.ts @@ -241,7 +241,10 @@ export function injectVitestModule(code: string, id: string, parse: (code: strin s.prependRight(topNode.start, `const ${id.name} = ${binding};\n`) } } - else if (parent.type !== 'ClassExpression') { + else if ( + // don't transform class name identifier + !(parent.type === 'ClassExpression' && id === parent.id) + ) { s.update(id.start, id.end, binding) } }, diff --git a/test/core/test/injector-esm.test.ts b/test/core/test/injector-esm.test.ts index 3a2b3eb34038..985e0d5e753c 100644 --- a/test/core/test/injector-esm.test.ts +++ b/test/core/test/injector-esm.test.ts @@ -936,10 +936,26 @@ function test() { test('avoid binding ClassExpression', () => { const result = injectSimpleCode( - 'import Foo, {Bar} from \'./foo\';console.log(Foo, Bar);const obj = {foo: class Foo{}, bar: class Bar{}}', + ` + import Foo, { Bar } from './foo'; + console.log(Foo, Bar); + const obj = { + foo: class Foo {}, + bar: class Bar {} + } + const Baz = class extends Foo {} + `, ) expect(result).toMatchInlineSnapshot(` "import { __vi_inject__ as __vi_esm_0__ } from './foo' - console.log(__vi_esm_0__.default, __vi_esm_0__.Bar);const obj = {foo: class Foo{}, bar: class Bar{}}" + + + console.log(__vi_esm_0__.default, __vi_esm_0__.Bar); + const obj = { + foo: class Foo {}, + bar: class Bar {} + } + const Baz = class extends __vi_esm_0__.default {} + " `) })