Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(browser): transform superclass identifier #3681

Merged
merged 1 commit into from Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/browser/src/node/esmInjector.ts
Expand Up @@ -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)
}
},
Expand Down
20 changes: 18 additions & 2 deletions test/core/test/injector-esm.test.ts
Expand Up @@ -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 {}
"
`)
})