mirror of
https://github.com/sveltejs/svelte.git
synced 2024-11-22 12:27:29 +01:00
parent
eedb59355f
commit
4285e6d814
5
.changeset/wet-wombats-repeat.md
Normal file
5
.changeset/wet-wombats-repeat.md
Normal file
@ -0,0 +1,5 @@
|
||||
---
|
||||
"svelte": patch
|
||||
---
|
||||
|
||||
fix: prevent snippet children conflict
|
@ -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} */
|
||||
|
@ -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) {
|
||||
|
@ -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';
|
||||
|
@ -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]
|
||||
}
|
||||
});
|
@ -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>
|
Loading…
Reference in New Issue
Block a user