Skip to content

Commit e8267f4

Browse files
committed
fix: allow using typescript in customElement.extend option
1 parent add0c6a commit e8267f4

File tree

5 files changed

+43
-3
lines changed

5 files changed

+43
-3
lines changed

.changeset/olive-pandas-trade.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: allow using typescript in `customElement.extend` option

packages/svelte/src/compiler/phases/1-parse/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ export class Parser {
143143
if (options_index !== -1) {
144144
const options = /** @type {AST.SvelteOptionsRaw} */ (this.root.fragment.nodes[options_index]);
145145
this.root.fragment.nodes.splice(options_index, 1);
146-
this.root.options = read_options(options);
146+
this.root.options = read_options(options, this.ts);
147147

148148
disallow_children(options);
149149

packages/svelte/src/compiler/phases/1-parse/read/options.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
/** @import { AST } from '#compiler' */
33
import { NAMESPACE_MATHML, NAMESPACE_SVG } from '../../../../constants.js';
44
import * as e from '../../../errors.js';
5+
import { remove_typescript_nodes } from '../remove_typescript_nodes.js';
56

67
/**
78
* @param {AST.SvelteOptionsRaw} node
9+
* @param {boolean} ts
810
* @returns {AST.Root['options']}
911
*/
10-
export default function read_options(node) {
12+
export default function read_options(node, ts) {
1113
/** @type {AST.SvelteOptions} */
1214
const component_options = {
1315
start: node.start,
@@ -142,7 +144,7 @@ export default function read_options(node) {
142144

143145
const extend = properties.find(([name]) => name === 'extend')?.[1];
144146
if (extend) {
145-
ce.extend = extend;
147+
ce.extend = ts ? remove_typescript_nodes(extend) : extend;
146148
}
147149

148150
component_options.customElement = ce;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { test } from '../../assert';
2+
const tick = () => Promise.resolve();
3+
4+
export default test({
5+
async test({ assert, target }) {
6+
target.innerHTML = '<custom-element name="world"></custom-element>';
7+
await tick();
8+
/** @type {any} */
9+
const el = target.querySelector('custom-element');
10+
11+
assert.htmlEqual(
12+
el.shadowRoot.innerHTML,
13+
`
14+
<p>name: world</p>
15+
`
16+
);
17+
assert.equal(el.test, `test`);
18+
}
19+
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<svelte:options customElement={{
2+
tag: "custom-element",
3+
extend: (customClass)=>{
4+
return class extends customClass{
5+
public test: string = "test";
6+
}
7+
},
8+
}}/>
9+
10+
<script lang="ts">
11+
let { name } = $props();
12+
</script>
13+
14+
<p>name: {name}</p>

0 commit comments

Comments
 (0)