-
-
Notifications
You must be signed in to change notification settings - Fork 24
feat: improve scoping of snippet declarations acting as slot properties #645
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"svelte-eslint-parser": minor | ||
--- | ||
|
||
feat: improve scoping of snippet declarations acting as slot properties |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
tests/fixtures/integrations/snippet-scope/snippet-shadow-scope-input.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<script lang="ts"> | ||
import Foo from './Foo.svelte'; | ||
</script> | ||
|
||
<div> | ||
<!-- The snippet is not used. --> | ||
{#snippet children()} | ||
<th>fruit</th> | ||
<th>qty</th> | ||
<th>price</th> | ||
<th>total</th> | ||
{/snippet} | ||
</div> | ||
|
||
<Foo> | ||
<!-- The snippet is used and does not add any variables. --> | ||
{#snippet children(arg)} | ||
<p>{arg}</p> | ||
{/snippet} | ||
{#snippet c()} | ||
<Foo> | ||
<!-- The snippet is used and does not add any variables. --> | ||
{#snippet children(arg)} | ||
<p>{arg}</p> | ||
{/snippet} | ||
</Foo> | ||
{/snippet} | ||
</Foo> | ||
|
||
<!-- The snippet is used and add a variable. --> | ||
{#snippet bar(arg)} | ||
<p>{arg}</p> | ||
{/snippet} | ||
<Foo children={bar}> | ||
{#snippet c()} | ||
<!-- The snippet is used and add a variable. --> | ||
{#snippet bar(arg)} | ||
<p>{arg}</p> | ||
{/snippet} | ||
<Foo children={bar}> | ||
</Foo> | ||
{/snippet} | ||
</Foo> |
23 changes: 23 additions & 0 deletions
23
tests/fixtures/integrations/snippet-scope/snippet-shadow-scope-output.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
[ | ||
{ | ||
"ruleId": "no-unused-vars", | ||
"code": "children", | ||
"line": 7, | ||
"column": 12, | ||
"message": "'children' is defined but never used." | ||
}, | ||
{ | ||
"ruleId": "no-shadow", | ||
"code": "bar", | ||
"line": 37, | ||
"column": 13, | ||
"message": "'bar' is already declared in the upper scope on line 31 column 11." | ||
}, | ||
{ | ||
"ruleId": "@typescript-eslint/no-shadow", | ||
"code": "bar", | ||
"line": 37, | ||
"column": 13, | ||
"message": "'bar' is already declared in the upper scope on line 31 column 11." | ||
} | ||
] |
5 changes: 5 additions & 0 deletions
5
tests/fixtures/integrations/snippet-scope/snippet-shadow-scope-requirements.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"parse": { | ||
"svelte": ">=5.0.0-0" | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
tests/fixtures/integrations/snippet-scope/snippet-shadow-scope-setup.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import type { Linter } from "eslint"; | ||
import { generateParserOptions } from "../../../src/parser/test-utils.js"; | ||
import { rules } from "@typescript-eslint/eslint-plugin"; | ||
import * as parser from "../../../../src/index.js"; | ||
import globals from "globals"; | ||
|
||
export function getConfig(): Linter.Config { | ||
return { | ||
plugins: { | ||
"@typescript-eslint": { | ||
rules: rules as any, | ||
}, | ||
}, | ||
languageOptions: { | ||
parser, | ||
parserOptions: { | ||
...generateParserOptions(), | ||
svelteFeatures: { runes: true }, | ||
}, | ||
globals: { | ||
...globals.browser, | ||
...globals.es2021, | ||
}, | ||
}, | ||
rules: { | ||
"no-shadow": "error", | ||
"@typescript-eslint/no-shadow": "error", | ||
"no-undef": "error", | ||
"no-unused-vars": "error", | ||
}, | ||
}; | ||
} |
4 changes: 2 additions & 2 deletions
4
tests/fixtures/integrations/snippet-scope/ts-snippet-hoist-scope-setup.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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:
Demo
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.
Ah I forgot
failed
. But I think onlyfailed
we can ignore.https://svelte.dev/playground/hello-world?version=5.17.5#H4sIAAAAAAAAE3WS0W7bMAxFf4VRB9gBnLjPrmOsGLBf2IZlD4rNLEJlSpDoJIagfx-suGvTJg8ChEvykOJVECR7FJWo_RE1Y7UzA3XSjY0oxF5p9KL6HQSPdkqaBFG8ljxbu75UiULspMdbemuIkdinFq1TlpstbVn11jiG71q-jN9Mbw0hMeyd6SFbl9fyDMuetlSXbwz6NPIErj8gy6ROJzx4UtYiw14qjV2OzhlXgEOPvIwpjevdwGwIDLVatS-bkKKxMcb6BbAbQf6ViurykpfgoZzBE2MaYbFawQ8ESdBhq6VDMHxAB3OaB6m9gQM6hNWquZ7tgFqbfNrk_5Fsk0QIkxoXdWlvtg1fHVKHbkZkUqsWs4lSlzfMZTyzqNgNGIs7Bt-04drreyl3bNfI0JvBI2zgi2fJmAc4V_BYwFjBI8TlPZNPijpzmhiGEqE3R9yEHJewaSBcVpUC6zNsANetVkj88-l9ZHwX-ZUiMW6pvDSyTZjr43m-jXFeNn36F3nq-_oWGrSO6YWng2Q8ooPRDNCZAjpDGUOqAj4oDxfS9u0PXZvxJ_4Dpk4LsJQDAAA=
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.
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?
Original (My opinion is keeping this scope manager)

This 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.
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:
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:
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.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
であるがどこからも使用されていないスニペットである)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>
の実装の話だと思っていまして、パーサーではスニペット名で分岐せず「式のみ」として記述されたスニペットとして扱うのが良いと思っています。今後
<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.
ありがとうございます! ようやく完全に理解できました!