Merge pull request #1 from ZiuChen/copilot/fix-fb61cdb4-ea0a-43f9-8c78-3cf2eb834cfb

Fix: Show helpful error when using `new App()` syntax in Svelte 5
pull/16700/head
Copilot 5 days ago committed by GitHub
parent 942eaf027b
commit 97e0fd356f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -592,7 +592,8 @@ export function client_component(analysis, options) {
)
)
);
} else if (dev) {
} else {
// Always check for invalid constructor usage, not just in dev mode
component_block.body.unshift(b.stmt(b.call('$.check_target', b.id('new.target'))));
}

@ -0,0 +1,28 @@
import { test } from '../../test';
export default test({
mode: ['client'],
compileOptions: {
dev: false // Test the fix works in production mode (where the issue was most severe)
},
async test({ mod, assert }) {
// Try to instantiate the component using the old `new Component()` syntax
// This should now throw a helpful error message instead of the cryptic "nodes_start" error
try {
const ComponentClass = mod.default;
const app = new ComponentClass();
assert.fail('Expected error when calling new ComponentClass()');
} catch (error) {
// The main fix: should NOT get the cryptic nodes_start error anymore
assert.ok(!error.message.includes('nodes_start'), 'Should not get cryptic nodes_start error');
// Should get a helpful error message instead
const isHelpfulError = error.message.includes('no longer valid in Svelte 5') ||
error.message.includes('https://svelte.dev/e/component_api_invalid_new');
assert.ok(isHelpfulError, 'Should get a helpful error message or URL');
// Should be a proper Svelte error
assert.equal(error.name, 'Svelte error', 'Should be a Svelte error');
}
}
});

@ -0,0 +1,6 @@
<script>
let count = 0;
</script>
<h1>Hello {count}</h1>
<button on:click={() => count++}>Click me</button>
Loading…
Cancel
Save