Skip to content
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

Merged
merged 2 commits into from
Jan 15, 2025

Conversation

ota-meshi
Copy link
Member

@ota-meshi ota-meshi commented Jan 14, 2025

Copy link

changeset-bot bot commented Jan 14, 2025

🦋 Changeset detected

Latest commit: 2a10c10

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
svelte-eslint-parser Minor

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

Copy link

pkg-pr-new bot commented Jan 14, 2025

Open in Stackblitz

npm i https://pkg.pr.new/svelte-eslint-parser@645

commit: 2a10c10

read: true,
});
(reference as any).svelteSnippetReference = true;
const defIds = variable.defs.map((d) => d.name);
Copy link
Member

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>.

Copy link
Member Author

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>

Demo

Am I misunderstanding something?

Copy link
Member Author

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.

Copy link
Member

@baseballyama baseballyama Jan 15, 2025

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.

Copy link
Member

@baseballyama baseballyama Jan 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Going back to the topic, I shared my opinion that the following case should not involve changes to the scope manager. What are your thoughts on this?

<svelte:boundary>
	{#snippet hello(name)}
		<p>hello {name}!</p>
	{/snippet}
</svelte:boundary>

Original (My opinion is keeping this scope manager)
image

This PR
image

Copy link
Member Author

@ota-meshi ota-meshi Jan 15, 2025

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>

Copy link
Member

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>

Copy link
Member Author

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の動きが妥当なところかなと思ってます 😃

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ありがとうございます! ようやく完全に理解できました!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants