0
0
mirror of https://github.com/sveltejs/svelte.git synced 2024-11-22 12:27:29 +01:00

fix: prevent snippet children conflict (#10700)

closes #10385
This commit is contained in:
Simon H 2024-03-05 13:51:46 +01:00 committed by GitHub
parent eedb59355f
commit 4285e6d814
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 62 additions and 3 deletions

View File

@ -0,0 +1,5 @@
---
"svelte": patch
---
fix: prevent snippet children conflict

View File

@ -296,7 +296,9 @@ const slots = {
/** @param {string} name @param {string} component */
'duplicate-slot-name': (name, component) => `Duplicate slot name '${name}' in <${component}>`,
'invalid-default-slot-content': () =>
`Found default slot content alongside an explicit slot="default"`
`Found default slot content alongside an explicit slot="default"`,
'conflicting-children-snippet': () =>
`Cannot use explicit children snippet at the same time as implicit children content. Remove either the non-whitespace content or the children snippet block`
};
/** @satisfies {Errors} */

View File

@ -521,6 +521,24 @@ const validation = {
);
}
},
SnippetBlock(node, { path }) {
if (node.expression.name !== 'children') return;
const parent = path.at(-2);
if (!parent) return;
if (
parent.type === 'Component' ||
parent.type === 'SvelteComponent' ||
parent.type === 'SvelteSelf'
) {
if (
parent.fragment.nodes.some(
(node) => node.type !== 'SnippetBlock' && (node.type !== 'Text' || node.data.trim())
)
) {
error(node, 'conflicting-children-snippet');
}
}
},
SvelteHead(node) {
const attribute = node.attributes[0];
if (attribute) {

View File

@ -1,8 +1,7 @@
import {
regex_ends_with_whitespaces,
regex_not_whitespace,
regex_starts_with_whitespaces,
regex_whitespaces_strict
regex_starts_with_whitespaces
} from '../patterns.js';
import * as b from '../../utils/builders.js';
import { walk } from 'zimmerframe';

View File

@ -0,0 +1,10 @@
import { test } from '../../test';
export default test({
error: {
code: 'conflicting-children-snippet',
message:
'Cannot use explicit children snippet at the same time as implicit children content. Remove either the non-whitespace content or the children snippet block',
position: [320, 353]
}
});

View File

@ -0,0 +1,25 @@
<!-- ok -->
<Button>
hello
</Button>
<Button>
{#snippet children()}hi{/snippet}
</Button>
<Button>
hello
{#snippet foo()}x{/snippet}
</Button>
<Button>
{#snippet children()}hi{/snippet}
{#snippet foo()}x{/snippet}
</Button>
<div>
hello
{#snippet children()}hi{/snippet}
</div>
<!-- invalid -->
<Button>
hello
{#snippet children()}hi{/snippet}
</Button>