sveltejs / svelte

Cybernetically enhanced web apps
https://svelte.dev
MIT License
76.98k stars 4k forks source link

Add error when a {#snippet} hide a props #11603

Closed adiguba closed 2 weeks ago

adiguba commented 2 weeks ago

Describe the problem

Snippet are passed as props, and they can therefore hide props of the same name.

Ex:

<Comp title="Title using props">
    {#snippet title()}
        Title using <b>snippet</b>
    {/snippet}
</Comp>

will be compiled to something like this :

        var title = ($$anchor) => {
            var fragment_4 = root_2();
            var b_1 = $.sibling($.first_child(fragment_4, true));

            $.append($$anchor, fragment_4);
        };

        Comp(node_2, { title: "Title using props", title });

Note that there are two attributes "title", and the snippet is hiding the props.

Exemple : REPL

Describe the proposed solution

Currently, Svelte generates an error when using the same attribute twice :

<Comp title="a" title="b"></Comp><!-- ERROR : Attributes need to be unique -->

I think it should be the same when a snippet collides with a prop :

<Comp title="prop">
    {#snippet title()} <!-- ERROR : Attribute and snippet need to be unique -->
        <b>snippet</b>
    {/snippet}
</Comp>

Importance

nice to have

brunnerh commented 2 weeks ago

I think this still needs some special handling for children.

<Comp children="Children using props">
    Children using <b>snippet</b>
</Comp>

REPL

paoloricciuti commented 2 weeks ago

I think this still needs some special handling for children.

<Comp children="Children using props">
  Children using <b>snippet</b>
</Comp>

REPL

Ugh i purposefully left out children but i think you might be right...having an error here doesn't seem a bad idea