Skip to content

Commit a1e043b

Browse files
authoredJun 27, 2023
fix(browser): transform superclass identifier (#3681)
1 parent 6d26b8c commit a1e043b

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed
 

‎packages/browser/src/node/esmInjector.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,10 @@ export function injectVitestModule(code: string, id: string, parse: (code: strin
241241
s.prependRight(topNode.start, `const ${id.name} = ${binding};\n`)
242242
}
243243
}
244-
else if (parent.type !== 'ClassExpression') {
244+
else if (
245+
// don't transform class name identifier
246+
!(parent.type === 'ClassExpression' && id === parent.id)
247+
) {
245248
s.update(id.start, id.end, binding)
246249
}
247250
},

‎test/core/test/injector-esm.test.ts

+18-2
Original file line numberDiff line numberDiff line change
@@ -936,10 +936,26 @@ function test() {
936936

937937
test('avoid binding ClassExpression', () => {
938938
const result = injectSimpleCode(
939-
'import Foo, {Bar} from \'./foo\';console.log(Foo, Bar);const obj = {foo: class Foo{}, bar: class Bar{}}',
939+
`
940+
import Foo, { Bar } from './foo';
941+
console.log(Foo, Bar);
942+
const obj = {
943+
foo: class Foo {},
944+
bar: class Bar {}
945+
}
946+
const Baz = class extends Foo {}
947+
`,
940948
)
941949
expect(result).toMatchInlineSnapshot(`
942950
"import { __vi_inject__ as __vi_esm_0__ } from './foo'
943-
console.log(__vi_esm_0__.default, __vi_esm_0__.Bar);const obj = {foo: class Foo{}, bar: class Bar{}}"
951+
952+
953+
console.log(__vi_esm_0__.default, __vi_esm_0__.Bar);
954+
const obj = {
955+
foo: class Foo {},
956+
bar: class Bar {}
957+
}
958+
const Baz = class extends __vi_esm_0__.default {}
959+
"
944960
`)
945961
})

0 commit comments

Comments
 (0)
Please sign in to comment.