Skip to content

feat: error when snippet shadow a prop #11631

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 4 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/warm-cherries-shake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte": patch
---

feat: error when snippet shadow a prop
4 changes: 4 additions & 0 deletions packages/svelte/messages/compile-errors/template.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,10 @@

> snippets do not support rest parameters; use an array instead

## snippet_shadowing_prop

> This snippet is shadowing the prop `%prop%` with the same name

## style_directive_invalid_modifier

> `style:` directive can only use the `important` modifier
Expand Down
10 changes: 10 additions & 0 deletions packages/svelte/src/compiler/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -1076,6 +1076,16 @@ export function snippet_invalid_rest_parameter(node) {
e(node, "snippet_invalid_rest_parameter", "snippets do not support rest parameters; use an array instead");
}

/**
* This snippet is shadowing the prop `%prop%` with the same name
* @param {null | number | NodeLike} node
* @param {string} prop
* @returns {never}
*/
export function snippet_shadowing_prop(node, prop) {
e(node, "snippet_shadowing_prop", `This snippet is shadowing the prop \`${prop}\` with the same name`);
}

/**
* `style:` directive can only use the `important` modifier
* @param {null | number | NodeLike} node
Expand Down
16 changes: 14 additions & 2 deletions packages/svelte/src/compiler/phases/2-analyze/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -624,11 +624,23 @@ const validation = {

context.next({ ...context.state, parent_element: null });

if (node.expression.name !== 'children') return;

const { path } = context;
const parent = path.at(-2);
if (!parent) return;

if (
parent.type === 'Component' &&
parent.attributes.some(
(attribute) =>
(attribute.type === 'Attribute' || attribute.type === 'BindDirective') &&
attribute.name === node.expression.name
)
) {
e.snippet_shadowing_prop(node, node.expression.name);
}

if (node.expression.name !== 'children') return;

if (
parent.type === 'Component' ||
parent.type === 'SvelteComponent' ||
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"code": "snippet_shadowing_prop",
"message": "This snippet is shadowing the prop `title` with the same name",
"start": {
"column": 1,
"line": 6
},
"end": {
"column": 11,
"line": 8
}
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<script>
import Component from "./Component.svelte";
</script>

<Component title="">
{#snippet title()}
title
{/snippet}
</Component>
Loading