fix: allow using typescript in `customElement.extend` option

pull/16001/head
paoloricciuti 4 months ago
parent add0c6a987
commit e8267f494f

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: allow using typescript in `customElement.extend` option

@ -143,7 +143,7 @@ export class Parser {
if (options_index !== -1) { if (options_index !== -1) {
const options = /** @type {AST.SvelteOptionsRaw} */ (this.root.fragment.nodes[options_index]); const options = /** @type {AST.SvelteOptionsRaw} */ (this.root.fragment.nodes[options_index]);
this.root.fragment.nodes.splice(options_index, 1); this.root.fragment.nodes.splice(options_index, 1);
this.root.options = read_options(options); this.root.options = read_options(options, this.ts);
disallow_children(options); disallow_children(options);

@ -2,12 +2,14 @@
/** @import { AST } from '#compiler' */ /** @import { AST } from '#compiler' */
import { NAMESPACE_MATHML, NAMESPACE_SVG } from '../../../../constants.js'; import { NAMESPACE_MATHML, NAMESPACE_SVG } from '../../../../constants.js';
import * as e from '../../../errors.js'; import * as e from '../../../errors.js';
import { remove_typescript_nodes } from '../remove_typescript_nodes.js';
/** /**
* @param {AST.SvelteOptionsRaw} node * @param {AST.SvelteOptionsRaw} node
* @param {boolean} ts
* @returns {AST.Root['options']} * @returns {AST.Root['options']}
*/ */
export default function read_options(node) { export default function read_options(node, ts) {
/** @type {AST.SvelteOptions} */ /** @type {AST.SvelteOptions} */
const component_options = { const component_options = {
start: node.start, start: node.start,
@ -142,7 +144,7 @@ export default function read_options(node) {
const extend = properties.find(([name]) => name === 'extend')?.[1]; const extend = properties.find(([name]) => name === 'extend')?.[1];
if (extend) { if (extend) {
ce.extend = extend; ce.extend = ts ? remove_typescript_nodes(extend) : extend;
} }
component_options.customElement = ce; component_options.customElement = ce;

@ -0,0 +1,19 @@
import { test } from '../../assert';
const tick = () => Promise.resolve();
export default test({
async test({ assert, target }) {
target.innerHTML = '<custom-element name="world"></custom-element>';
await tick();
/** @type {any} */
const el = target.querySelector('custom-element');
assert.htmlEqual(
el.shadowRoot.innerHTML,
`
<p>name: world</p>
`
);
assert.equal(el.test, `test`);
}
});

@ -0,0 +1,14 @@
<svelte:options customElement={{
tag: "custom-element",
extend: (customClass)=>{
return class extends customClass{
public test: string = "test";
}
},
}}/>
<script lang="ts">
let { name } = $props();
</script>
<p>name: {name}</p>
Loading…
Cancel
Save