Skip to content

Commit 23484d6

Browse files
trueadmRich-Harris
andauthored
breaking: prevent usage of arguments keyword in certain places (#12191)
* breaking: prevent usage of arguments keyword in certain places * build * build * Update packages/svelte/messages/compile-errors/script.md Co-authored-by: Rich Harris <[email protected]> * build * build * lint --------- Co-authored-by: Rich Harris <[email protected]>
1 parent 09479c4 commit 23484d6

File tree

6 files changed

+38
-0
lines changed

6 files changed

+38
-0
lines changed

.changeset/healthy-zebras-accept.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
breaking: prevent usage of arguments keyword in certain places

packages/svelte/messages/compile-errors/script.md

+4
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@
5050

5151
> Imports of `svelte/internal/*` are forbidden. It contains private runtime code which is subject to change without notice. If you're importing from `svelte/internal/*` to work around a limitation of Svelte, please open an issue at https://github.com/sveltejs/svelte and explain your use case
5252
53+
## invalid_arguments_usage
54+
55+
> The arguments keyword cannot be used within the template or at the top level of a component
56+
5357
## legacy_export_invalid
5458

5559
> Cannot use `export let` in runes mode — use `$props()` instead

packages/svelte/src/compiler/errors.js

+9
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,15 @@ export function import_svelte_internal_forbidden(node) {
213213
e(node, "import_svelte_internal_forbidden", "Imports of `svelte/internal/*` are forbidden. It contains private runtime code which is subject to change without notice. If you're importing from `svelte/internal/*` to work around a limitation of Svelte, please open an issue at https://github.com/sveltejs/svelte and explain your use case");
214214
}
215215

216+
/**
217+
* The arguments keyword cannot be used within the template or at the top level of a component
218+
* @param {null | number | NodeLike} node
219+
* @returns {never}
220+
*/
221+
export function invalid_arguments_usage(node) {
222+
e(node, "invalid_arguments_usage", "The arguments keyword cannot be used within the template or at the top level of a component");
223+
}
224+
216225
/**
217226
* Cannot use `export let` in runes mode — use `$props()` instead
218227
* @param {null | number | NodeLike} node

packages/svelte/src/compiler/phases/2-analyze/index.js

+8
Original file line numberDiff line numberDiff line change
@@ -1238,6 +1238,14 @@ const common_visitors = {
12381238
return;
12391239
}
12401240

1241+
// If we are using arguments outside of a function, then throw an error
1242+
if (
1243+
node.name === 'arguments' &&
1244+
context.path.every((n) => n.type !== 'FunctionDeclaration' && n.type !== 'FunctionExpression')
1245+
) {
1246+
e.invalid_arguments_usage(node);
1247+
}
1248+
12411249
const binding = context.state.scope.get(node.name);
12421250

12431251
// if no binding, means some global variable
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { test } from '../../test';
2+
3+
export default test({
4+
error: {
5+
code: 'invalid_arguments_usage',
6+
message:
7+
'The arguments keyword cannot be used within the template or at the top level of a component'
8+
}
9+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<script>
2+
arguments
3+
</script>

0 commit comments

Comments
 (0)