-
-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: improve scoping of snippet declarations acting as slot properties #645
Conversation
🦋 Changeset detectedLatest commit: 2a10c10 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
commit: |
read: true, | ||
}); | ||
(reference as any).svelteSnippetReference = true; | ||
const defIds = variable.defs.map((d) => d.name); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Line number 305, I don't think we need to handle <svelte:boundary>
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think <svelte:boundary>
needs to be handled because it has a failed slot.
Currently, no-shadow is reported:
<script lang="ts">
let failed = false;
</script>
<svelte:boundary>
<MyComponent />
{#snippet failed()}
<div>oops! try again</div>
{/snippet}
</svelte:boundary>
Am I misunderstanding something?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As you say, only the failed
slot can be used in an actual <svelte:boundary>
.
However, I think parsers and scope analyzers should avoid handling by name whenever possible.
I think checking for these is done by lint rules and type checking.
Also, new slots may appear in the future.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the REPL above, hello
snippet is also rendered, right?
So <svelte:boundary>
can use any {@snippet}
IIUC.
So I guess snippets other than failed
should still be handled by the scope manager.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the confusion comes from the fact that the {#snippet}
syntax is both a declaration and an expression.
First of all, any {#snippet}
that isn't specified directly on a component or <svelte:boundary>
is always a declaration. It is excluded by conditions L300-L306.
When specified on a component, {#snippet}
is both a declaration and an expression.
It's like the following is done at once:
function mySnippet() {
}
const componentProps = { mySnippet }
In terms of the parser processing, {#snippet}
is generated by a function declaration in virtual code, so it has a variable. Then, by adding a reference as an expression at the variable position in L323, it behaves as both a declaration and an expression.
The purpose of this PR change is to remove the declarative behavior from {#snippet}
that is only used as a component prop, that is, to treat {#snippet}
as an expression in this case.
The image is as follows:
const componentProps = { mySnippet() {} }
In terms of parser processing, if {#snippet}
is only declared and not used anywhere else, the declared variables of the snippet are removed so that it behaves as if it were just an anonymous expression.
The following hello
snippet only behaves as an expression, so the variables are not added in the scope manager.
<svelte:boundary>
{#snippet hello(name)}
<p>hello {name}!</p>
{/snippet}
</svelte:boundary>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(日本語ですみません🙇)
ありがとうございます! 恐らくここまで同じ理解をできていると思います!
<svelte:boundary>
を使用する場合、failed
スニペットは <svelte:boundary>
のために特別に用意されたスニペットなので、他のどこからも参照されていなかったとしてもそれは有効です。なので、このPRで対処されるべきです。
一方、<svelte:boundary>
直下で宣言されるそれ以外のスニペットは、他の部分で (例えばコンポーネントルートで) 宣言するスニペットと同じように動作するので、このPRでの特別対応からは除外した方がいいんじゃないか、という確認でした🙋 (つまり、以下の例では、 hello はただの declaration
であるがどこからも使用されていないスニペットである)
<svelte:boundary>
{#snippet hello(name)}
<p>hello {name}!</p>
{/snippet}
</svelte:boundary>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
つまり、以下の例では、 hello はただの declaration であるがどこからも使用されていないスニペットである
はい。実際そうなのですが、それは<svelte:boundary>
の実装の話だと思っていまして、パーサーではスニペット名で分岐せず「式のみ」として記述されたスニペットとして扱うのが良いと思っています。
今後<svelte:boundary>
に新しいスニペットが追加されるたびに名前で分岐入れるのは厳しいですし、<svelte:boundary>
で使えないスニペットが「式のみ」にならないのなら、カスタムコンポーネントが持っていないスニペットが「式のみ」になるのも一貫性がないので、今のPRの動きが妥当なところかなと思ってます 😃
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ありがとうございます! ようやく完全に理解できました!
related to sveltejs/eslint-plugin-svelte#992, sveltejs/eslint-plugin-svelte#995