From 23484d68751dea941e17fd577f9a477d88f2bf24 Mon Sep 17 00:00:00 2001 From: Dominic Gannaway Date: Wed, 26 Jun 2024 15:09:10 +0100 Subject: [PATCH] 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 * build * build * lint --------- Co-authored-by: Rich Harris --- .changeset/healthy-zebras-accept.md | 5 +++++ packages/svelte/messages/compile-errors/script.md | 4 ++++ packages/svelte/src/compiler/errors.js | 9 +++++++++ packages/svelte/src/compiler/phases/2-analyze/index.js | 8 ++++++++ .../samples/invalid-arguments-usage/_config.js | 9 +++++++++ .../samples/invalid-arguments-usage/main.svelte | 3 +++ 6 files changed, 38 insertions(+) create mode 100644 .changeset/healthy-zebras-accept.md create mode 100644 packages/svelte/tests/compiler-errors/samples/invalid-arguments-usage/_config.js create mode 100644 packages/svelte/tests/compiler-errors/samples/invalid-arguments-usage/main.svelte diff --git a/.changeset/healthy-zebras-accept.md b/.changeset/healthy-zebras-accept.md new file mode 100644 index 0000000000..994789b976 --- /dev/null +++ b/.changeset/healthy-zebras-accept.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +breaking: prevent usage of arguments keyword in certain places diff --git a/packages/svelte/messages/compile-errors/script.md b/packages/svelte/messages/compile-errors/script.md index cfa92ce0ad..57fa1ed700 100644 --- a/packages/svelte/messages/compile-errors/script.md +++ b/packages/svelte/messages/compile-errors/script.md @@ -50,6 +50,10 @@ > 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 +## invalid_arguments_usage + +> The arguments keyword cannot be used within the template or at the top level of a component + ## legacy_export_invalid > Cannot use `export let` in runes mode — use `$props()` instead diff --git a/packages/svelte/src/compiler/errors.js b/packages/svelte/src/compiler/errors.js index ee8afd8f5d..d6d3d51d7c 100644 --- a/packages/svelte/src/compiler/errors.js +++ b/packages/svelte/src/compiler/errors.js @@ -213,6 +213,15 @@ export function import_svelte_internal_forbidden(node) { 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"); } +/** + * The arguments keyword cannot be used within the template or at the top level of a component + * @param {null | number | NodeLike} node + * @returns {never} + */ +export function invalid_arguments_usage(node) { + e(node, "invalid_arguments_usage", "The arguments keyword cannot be used within the template or at the top level of a component"); +} + /** * Cannot use `export let` in runes mode — use `$props()` instead * @param {null | number | NodeLike} node diff --git a/packages/svelte/src/compiler/phases/2-analyze/index.js b/packages/svelte/src/compiler/phases/2-analyze/index.js index ad39beb4eb..2e46d18c6f 100644 --- a/packages/svelte/src/compiler/phases/2-analyze/index.js +++ b/packages/svelte/src/compiler/phases/2-analyze/index.js @@ -1238,6 +1238,14 @@ const common_visitors = { return; } + // If we are using arguments outside of a function, then throw an error + if ( + node.name === 'arguments' && + context.path.every((n) => n.type !== 'FunctionDeclaration' && n.type !== 'FunctionExpression') + ) { + e.invalid_arguments_usage(node); + } + const binding = context.state.scope.get(node.name); // if no binding, means some global variable diff --git a/packages/svelte/tests/compiler-errors/samples/invalid-arguments-usage/_config.js b/packages/svelte/tests/compiler-errors/samples/invalid-arguments-usage/_config.js new file mode 100644 index 0000000000..8a559e46ec --- /dev/null +++ b/packages/svelte/tests/compiler-errors/samples/invalid-arguments-usage/_config.js @@ -0,0 +1,9 @@ +import { test } from '../../test'; + +export default test({ + error: { + code: 'invalid_arguments_usage', + message: + 'The arguments keyword cannot be used within the template or at the top level of a component' + } +}); diff --git a/packages/svelte/tests/compiler-errors/samples/invalid-arguments-usage/main.svelte b/packages/svelte/tests/compiler-errors/samples/invalid-arguments-usage/main.svelte new file mode 100644 index 0000000000..3869f90ff6 --- /dev/null +++ b/packages/svelte/tests/compiler-errors/samples/invalid-arguments-usage/main.svelte @@ -0,0 +1,3 @@ +