Skip to content

Commit

Permalink
fix: error on incorrect attributes for svelte:body (#13084)
Browse files Browse the repository at this point in the history
* fix: error on spreading attributes for svelte:body

* tweak
  • Loading branch information
trueadm authored Aug 30, 2024
1 parent 2af9b19 commit cf6b64c
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/eleven-icons-sniff.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: error on incorrect attributes for svelte:body
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 @@ -274,6 +274,10 @@ HTML restricts where certain elements can appear. In case of a violation the bro

> A component can have a single top-level `<style>` element
## svelte_body_illegal_attribute

> `<svelte:body>` does not support non-event attributes or spread attributes
## svelte_component_invalid_this

> Invalid component definition — must be an `{expression}`
Expand Down
9 changes: 9 additions & 0 deletions packages/svelte/src/compiler/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -1183,6 +1183,15 @@ export function style_duplicate(node) {
e(node, "style_duplicate", "A component can have a single top-level `<style>` element");
}

/**
* `<svelte:body>` does not support non-event attributes or spread attributes
* @param {null | number | NodeLike} node
* @returns {never}
*/
export function svelte_body_illegal_attribute(node) {
e(node, "svelte_body_illegal_attribute", "`<svelte:body>` does not support non-event attributes or spread attributes");
}

/**
* Invalid component definition — must be an `{expression}`
* @param {null | number | NodeLike} node
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/** @import { SvelteBody } from '#compiler' */
/** @import { Context } from '../types' */
import * as e from '../../../errors.js';
import { is_event_attribute } from '../../../utils/ast.js';
import { disallow_children } from './shared/special-element.js';

/**
Expand All @@ -8,5 +10,13 @@ import { disallow_children } from './shared/special-element.js';
*/
export function SvelteBody(node, context) {
disallow_children(node);
for (const attribute of node.attributes) {
if (
attribute.type === 'SpreadAttribute' ||
(attribute.type === 'Attribute' && !is_event_attribute(attribute))
) {
e.svelte_body_illegal_attribute(attribute);
}
}
context.next();
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/** @import { Expression } from 'estree' */
/** @import { SvelteBody, SvelteDocument, SvelteWindow } from '#compiler' */
/** @import { ComponentContext } from '../../types' */
import { is_event_attribute } from '../../../../../utils/ast.js';
import * as b from '../../../../../utils/builders.js';

/**
Expand Down

0 comments on commit cf6b64c

Please sign in to comment.