Skip to content

Commit

Permalink
fix: disallow invalid attributes for <svelte:window> and `<svelte:d…
Browse files Browse the repository at this point in the history
…ocument>` (#14228)
  • Loading branch information
trueadm authored Nov 14, 2024
1 parent 312dd51 commit 7fd3774
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/fast-ligers-repeat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: disallow invalid attributes for `<svelte:window>` and `<svelte:document>`
6 changes: 6 additions & 0 deletions documentation/docs/98-reference/.generated/compile-errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,12 @@ Expected whitespace
`$host()` can only be used inside custom element component instances
```

### illegal_element_attribute

```
`<%name%>` does not support non-event attributes or spread attributes
```

### import_svelte_internal_forbidden

```
Expand Down
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 @@ -172,6 +172,10 @@

> Expected whitespace
## illegal_element_attribute

> `<%name%>` does not support non-event attributes or spread attributes
## js_parse_error

> %message%
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 @@ -984,6 +984,16 @@ export function expected_whitespace(node) {
e(node, "expected_whitespace", "Expected whitespace");
}

/**
* `<%name%>` does not support non-event attributes or spread attributes
* @param {null | number | NodeLike} node
* @param {string} name
* @returns {never}
*/
export function illegal_element_attribute(node, name) {
e(node, "illegal_element_attribute", `\`<${name}>\` does not support non-event attributes or spread attributes`);
}

/**
* %message%
* @param {null | number | NodeLike} node
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
/** @import { AST } from '#compiler' */
/** @import { Context } from '../types' */
import { disallow_children } from './shared/special-element.js';
import * as e from '../../../errors.js';
import { is_event_attribute } from '../../../utils/ast.js';

/**
* @param {AST.SvelteDocument} node
* @param {Context} context
*/
export function SvelteDocument(node, context) {
disallow_children(node);

for (const attribute of node.attributes) {
if (
attribute.type === 'SpreadAttribute' ||
(attribute.type === 'Attribute' && !is_event_attribute(attribute))
) {
e.illegal_element_attribute(attribute, 'svelte:document');
}
}

context.next();
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
/** @import { AST } from '#compiler' */
/** @import { Context } from '../types' */
import { disallow_children } from './shared/special-element.js';
import * as e from '../../../errors.js';
import { is_event_attribute } from '../../../utils/ast.js';

/**
* @param {AST.SvelteWindow} node
* @param {Context} context
*/
export function SvelteWindow(node, context) {
disallow_children(node);

for (const attribute of node.attributes) {
if (
attribute.type === 'SpreadAttribute' ||
(attribute.type === 'Attribute' && !is_event_attribute(attribute))
) {
e.illegal_element_attribute(attribute, 'svelte:window');
}
}

context.next();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"code": "illegal_element_attribute",
"message": "`<svelte:document>` does not support non-event attributes or spread attributes",
"start": {
"line": 4,
"column": 17
},
"end": {
"line": 4,
"column": 23
}
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<script>
let a = {};
</script>
<svelte:document {...a}></svelte:document>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"code": "illegal_element_attribute",
"message": "`<svelte:window>` does not support non-event attributes or spread attributes",
"start": {
"line": 4,
"column": 15
},
"end": {
"line": 4,
"column": 21
}
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<script>
let a = {};
</script>
<svelte:window {...a}></svelte:window>

0 comments on commit 7fd3774

Please sign in to comment.