Skip to content

fix: warn on implicit children snipett shadowing a prop #11633

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

Closed
wants to merge 3 commits into from
Closed
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/empty-worms-leave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte": patch
---

fix: warn on implicit children snipett shadowing a prop
6 changes: 5 additions & 1 deletion packages/svelte/messages/compile-errors/template.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,13 @@

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

## snippet_shadowing_children_prop

> The component content is implicitly passed as a `children` snippet which conflicts with the `children` prop set as an attribute.

## snippet_shadowing_prop

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

## style_directive_invalid_modifier

Expand Down
13 changes: 11 additions & 2 deletions packages/svelte/src/compiler/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -1077,13 +1077,22 @@ export function snippet_invalid_rest_parameter(node) {
}

/**
* This snippet is shadowing the prop `%prop%` with the same name
* The component content is implicitly passed as a `children` snippet which conflicts with the `children` prop set as an attribute.
* @param {null | number | NodeLike} node
* @returns {never}
*/
export function snippet_shadowing_children_prop(node) {
e(node, "snippet_shadowing_children_prop", "The component content is implicitly passed as a `children` snippet which conflicts with the `children` prop set as an attribute.");
}

/**
* This snippet is conflicting with 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`);
e(node, "snippet_shadowing_prop", `This snippet is conflicting with the prop \`${prop}\` with the same name`);
}

/**
Expand Down
11 changes: 11 additions & 0 deletions packages/svelte/src/compiler/phases/2-analyze/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ import { a11y_validators } from './a11y.js';
* @param {import('zimmerframe').Context<import('#compiler').SvelteNode, import('./types.js').AnalysisState>} context
*/
function validate_component(node, context) {
const has_implicit_children = node.fragment.nodes.length > 0;
if (
has_implicit_children &&
node.attributes.some(
(attribute) =>
(attribute.type === 'Attribute' || attribute.type === 'BindDirective') &&
attribute.name === 'children'
)
) {
e.snippet_shadowing_children_prop(node);
}
for (const attribute of node.attributes) {
if (
attribute.type !== 'Attribute' &&
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"code": "snippet_shadowing_children_prop",
"message": "The component content is implicitly passed as a `children` snippet which conflicts with the `children` prop set as an attribute.",
"start": {
"column": 0,
"line": 5
},
"end": {
"column": 12,
"line": 7
}
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script>
import Component from "./Component.svelte";
</script>

<Component children="">
title
</Component>
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[
{
"code": "snippet_shadowing_prop",
"message": "This snippet is shadowing the prop `title` with the same name",
"message": "This snippet is conflicting with the prop `title` with the same name",
"start": {
"column": 1,
"line": 6
Expand Down
Loading