mirror of https://github.com/sveltejs/svelte
This adds a `parent` property to the `__svelte_meta` properties that are added to elements at dev time. This property represents the closest non-element parent the element is related to. For example for `{#if ...}<div>foo</div>{/if}` the `parent` of the div would be the line/column of the if block. The parent is recursive and goes upwards (through component boundaries) until the root component is reached, which has no parent. part of #11389pull/16255/head
parent
32481bece6
commit
338500085a
@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
'svelte': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
feat: add parent hierarchy to \_\_svelte_meta objects at dev time
|
@ -0,0 +1,82 @@
|
|||||||
|
import { tick } from 'svelte';
|
||||||
|
import { test } from '../../test';
|
||||||
|
|
||||||
|
export default test({
|
||||||
|
mode: ['client', 'hydrate'],
|
||||||
|
compileOptions: {
|
||||||
|
dev: true
|
||||||
|
},
|
||||||
|
|
||||||
|
html: `<p>no parent</p><p>if</p><p>each</p><p>loading</p><p>key</p><p>hi</p><p>hi</p>`,
|
||||||
|
|
||||||
|
async test({ target, assert }) {
|
||||||
|
await tick();
|
||||||
|
const [main, if_, each, await_, key, child1, child2] = target.querySelectorAll('p');
|
||||||
|
|
||||||
|
// @ts-expect-error
|
||||||
|
assert.deepEqual(main.__svelte_meta.parent, null);
|
||||||
|
|
||||||
|
// @ts-expect-error
|
||||||
|
assert.deepEqual(if_.__svelte_meta.parent, {
|
||||||
|
file: 'main.svelte',
|
||||||
|
type: 'if',
|
||||||
|
line: 10,
|
||||||
|
column: 0,
|
||||||
|
parent: null
|
||||||
|
});
|
||||||
|
|
||||||
|
// @ts-expect-error
|
||||||
|
assert.deepEqual(each.__svelte_meta.parent, {
|
||||||
|
file: 'main.svelte',
|
||||||
|
type: 'each',
|
||||||
|
line: 14,
|
||||||
|
column: 0,
|
||||||
|
parent: null
|
||||||
|
});
|
||||||
|
|
||||||
|
// @ts-expect-error
|
||||||
|
assert.deepEqual(await_.__svelte_meta.parent, {
|
||||||
|
file: 'main.svelte',
|
||||||
|
type: 'await',
|
||||||
|
line: 18,
|
||||||
|
column: 0,
|
||||||
|
parent: null
|
||||||
|
});
|
||||||
|
|
||||||
|
// @ts-expect-error
|
||||||
|
assert.deepEqual(key.__svelte_meta.parent, {
|
||||||
|
file: 'main.svelte',
|
||||||
|
type: 'key',
|
||||||
|
line: 24,
|
||||||
|
column: 0,
|
||||||
|
parent: null
|
||||||
|
});
|
||||||
|
|
||||||
|
// @ts-expect-error
|
||||||
|
assert.deepEqual(child1.__svelte_meta.parent, {
|
||||||
|
file: 'main.svelte',
|
||||||
|
type: 'component',
|
||||||
|
componentTag: 'Child',
|
||||||
|
line: 28,
|
||||||
|
column: 0,
|
||||||
|
parent: null
|
||||||
|
});
|
||||||
|
|
||||||
|
// @ts-expect-error
|
||||||
|
assert.deepEqual(child2.__svelte_meta.parent, {
|
||||||
|
file: 'main.svelte',
|
||||||
|
type: 'component',
|
||||||
|
componentTag: 'Child',
|
||||||
|
line: 31,
|
||||||
|
column: 1,
|
||||||
|
parent: {
|
||||||
|
file: 'main.svelte',
|
||||||
|
type: 'component',
|
||||||
|
componentTag: 'Passthrough',
|
||||||
|
line: 30,
|
||||||
|
column: 0,
|
||||||
|
parent: null
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
@ -0,0 +1 @@
|
|||||||
|
<p>hi</p>
|
@ -0,0 +1,32 @@
|
|||||||
|
<script>
|
||||||
|
import Child from "./child.svelte";
|
||||||
|
import Passthrough from "./passthrough.svelte";
|
||||||
|
|
||||||
|
let key = 'test';
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<p>no parent</p>
|
||||||
|
|
||||||
|
{#if true}
|
||||||
|
<p>if</p>
|
||||||
|
{/if}
|
||||||
|
|
||||||
|
{#each [1]}
|
||||||
|
<p>each</p>
|
||||||
|
{/each}
|
||||||
|
|
||||||
|
{#await Promise.resolve()}
|
||||||
|
<p>loading</p>
|
||||||
|
{:then}
|
||||||
|
<p>await</p>
|
||||||
|
{/await}
|
||||||
|
|
||||||
|
{#key key}
|
||||||
|
<p>key</p>
|
||||||
|
{/key}
|
||||||
|
|
||||||
|
<Child />
|
||||||
|
|
||||||
|
<Passthrough>
|
||||||
|
<Child />
|
||||||
|
</Passthrough>
|
@ -0,0 +1,5 @@
|
|||||||
|
<script>
|
||||||
|
let { children } = $props();
|
||||||
|
</script>
|
||||||
|
|
||||||
|
{@render children()}
|
Loading…
Reference in new issue