From 732dbf7fa9b0166f25405d8e0694aa540ea9898d Mon Sep 17 00:00:00 2001 From: Simon H <5968653+dummdidumm@users.noreply.github.com> Date: Wed, 21 Aug 2024 21:15:22 +0200 Subject: [PATCH] breaking: deprecate `context="module"` in favor of `module` (#12948) * breaking: deprecate `context="module"` in favor of `module` Also reserve a few attributes, which we may or may not use in the future closes #12637 * fix tests * one more * add svelte package to the root so eslint and prettier can use it * tweak messages * warn on unknown attributes * regenerate --------- Co-authored-by: Rich Harris --- .changeset/quick-eagles-sit.md | 5 + .../01-component-fundamentals.md | 6 +- package.json | 1 + .../svelte/messages/compile-errors/script.md | 4 +- .../messages/compile-errors/template.md | 10 +- .../messages/compile-warnings/template.md | 8 ++ packages/svelte/src/compiler/errors.js | 32 +++++-- packages/svelte/src/compiler/migrate/index.js | 14 ++- .../compiler/phases/1-parse/read/script.js | 72 +++++++++----- .../src/compiler/phases/2-analyze/index.js | 2 +- .../svelte/src/compiler/types/template.d.ts | 4 +- packages/svelte/src/compiler/warnings.js | 18 ++++ .../samples/export-state-module/_config.js | 2 +- .../samples/export-state-module/main.svelte | 2 +- .../samples/runes-export-let/main.svelte | 2 +- .../store-autosub-context-module/_config.js | 4 +- .../store-autosub-context-module/main.svelte | 2 +- .../script-context-module/input.svelte | 3 + .../script-context-module/output.svelte | 3 + .../samples/options/input.svelte | 2 +- .../parser-modern/samples/options/output.json | 58 +++++------- .../samples/component-namespace/Widget.svelte | 8 +- .../component-namespaced/components.svelte | 8 +- .../component-slot-fallback-2/store.svelte | 46 ++++----- .../samples/context-api/Tabs.svelte | 2 +- .../main.svelte | 4 +- .../Foo.svelte | 6 +- .../samples/module-context-export/Foo.svelte | 2 +- .../main.svelte | 2 +- .../samples/module-context/main.svelte | 4 +- .../samples/preload/main.svelte | 4 +- .../main.svelte | 2 +- .../main.svelte | 2 +- .../samples/store-imported-module/main.svelte | 2 +- .../class-state-derived-unowned/main.svelte | 2 +- .../Component.svelte | 2 +- .../derived-unowned-2/Component.svelte | 11 +-- .../samples/derived-unowned-5/main.svelte | 2 +- .../samples/derived-unowned-8/main.svelte | 6 +- .../samples/derived-unowned/Component.svelte | 4 +- .../samples/effect-dependencies/main.svelte | 10 +- .../samples/element-is-attribute/main.svelte | 2 +- .../event-attribute-not-hoistable/main.svelte | 2 +- .../samples/inspect-derived-2/main.svelte | 2 +- .../samples/module-context-export/main.svelte | 2 +- .../non-local-mutation-global-2/child.svelte | 4 +- .../proxied-state-property-access/main.svelte | 2 +- .../runes-in-module-context/main.svelte | 2 +- .../samples/typescript/main.svelte | 2 +- .../samples/two-scripts/input.svelte | 2 +- .../illegal-variable-declaration/errors.json | 2 +- .../illegal-variable-declaration/input.svelte | 2 +- .../samples/inline-new-class-5/input.svelte | 8 +- .../input.svelte | 2 +- .../errors.json | 2 +- .../errors.json | 2 +- .../input.svelte | 4 +- .../input.svelte | 2 +- .../reactive-module-variable-2/input.svelte | 2 +- .../reactive-module-variable/input.svelte | 2 +- .../input.svelte | 3 + .../warnings.json | 14 +++ .../script-invalid-context/errors.json | 2 +- .../input.svelte | 1 + .../warnings.json | 14 +++ .../script-unknown-attribute/input.svelte | 1 + .../script-unknown-attribute/warnings.json | 14 +++ .../samples/silence-warnings-2/input.svelte | 2 +- .../samples/silence-warnings/input.svelte | 2 +- .../unreferenced-variables/input.svelte | 2 +- packages/svelte/types/index.d.ts | 4 +- pnpm-lock.yaml | 94 +++---------------- .../src/lib/CodeMirror.svelte | 2 +- .../content/03-appendix/03-deprecations.md | 11 +++ 74 files changed, 339 insertions(+), 258 deletions(-) create mode 100644 .changeset/quick-eagles-sit.md create mode 100644 packages/svelte/tests/migrate/samples/script-context-module/input.svelte create mode 100644 packages/svelte/tests/migrate/samples/script-context-module/output.svelte create mode 100644 packages/svelte/tests/validator/samples/script-context-module-deprecated/input.svelte create mode 100644 packages/svelte/tests/validator/samples/script-context-module-deprecated/warnings.json create mode 100644 packages/svelte/tests/validator/samples/script-invalid-spread-attribute/input.svelte create mode 100644 packages/svelte/tests/validator/samples/script-invalid-spread-attribute/warnings.json create mode 100644 packages/svelte/tests/validator/samples/script-unknown-attribute/input.svelte create mode 100644 packages/svelte/tests/validator/samples/script-unknown-attribute/warnings.json diff --git a/.changeset/quick-eagles-sit.md b/.changeset/quick-eagles-sit.md new file mode 100644 index 0000000000..613ea2bd05 --- /dev/null +++ b/.changeset/quick-eagles-sit.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +breaking: deprecate `context="module"` in favor of `module` diff --git a/documentation/docs/02-template-syntax/01-component-fundamentals.md b/documentation/docs/02-template-syntax/01-component-fundamentals.md index debe4828d2..fc81bb24bd 100644 --- a/documentation/docs/02-template-syntax/01-component-fundamentals.md +++ b/documentation/docs/02-template-syntax/01-component-fundamentals.md @@ -161,16 +161,16 @@ If you'd like to react to changes to a prop, use the `$derived` or `$effect` run For more information on reactivity, read the documentation around runes. -## <script context="module"> +## <script module> -A ` diff --git a/packages/svelte/tests/compiler-errors/samples/store-autosub-context-module/_config.js b/packages/svelte/tests/compiler-errors/samples/store-autosub-context-module/_config.js index a48923bd4a..e8ec74c5f0 100644 --- a/packages/svelte/tests/compiler-errors/samples/store-autosub-context-module/_config.js +++ b/packages/svelte/tests/compiler-errors/samples/store-autosub-context-module/_config.js @@ -3,7 +3,7 @@ import { test } from '../../test'; export default test({ error: { code: 'store_invalid_subscription', - message: 'Cannot reference store value inside ` diff --git a/packages/svelte/tests/migrate/samples/script-context-module/output.svelte b/packages/svelte/tests/migrate/samples/script-context-module/output.svelte new file mode 100644 index 0000000000..e508049d5d --- /dev/null +++ b/packages/svelte/tests/migrate/samples/script-context-module/output.svelte @@ -0,0 +1,3 @@ + diff --git a/packages/svelte/tests/parser-modern/samples/options/input.svelte b/packages/svelte/tests/parser-modern/samples/options/input.svelte index 9e8253c50c..b7ee13b5e5 100644 --- a/packages/svelte/tests/parser-modern/samples/options/input.svelte +++ b/packages/svelte/tests/parser-modern/samples/options/input.svelte @@ -1,6 +1,6 @@ - \ No newline at end of file + export const Widget = { Tooltip }; + diff --git a/packages/svelte/tests/runtime-legacy/samples/component-namespaced/components.svelte b/packages/svelte/tests/runtime-legacy/samples/component-namespaced/components.svelte index 5b9a6c5167..3672036a53 100644 --- a/packages/svelte/tests/runtime-legacy/samples/component-namespaced/components.svelte +++ b/packages/svelte/tests/runtime-legacy/samples/component-namespaced/components.svelte @@ -1,5 +1,5 @@ - \ No newline at end of file + export const Components = { Foo }; + diff --git a/packages/svelte/tests/runtime-legacy/samples/component-slot-fallback-2/store.svelte b/packages/svelte/tests/runtime-legacy/samples/component-slot-fallback-2/store.svelte index e377aaf314..ef5dc7e91a 100644 --- a/packages/svelte/tests/runtime-legacy/samples/component-slot-fallback-2/store.svelte +++ b/packages/svelte/tests/runtime-legacy/samples/component-slot-fallback-2/store.svelte @@ -1,23 +1,23 @@ - \ No newline at end of file + diff --git a/packages/svelte/tests/runtime-legacy/samples/context-api/Tabs.svelte b/packages/svelte/tests/runtime-legacy/samples/context-api/Tabs.svelte index 6692bb31e9..79cc4e11b1 100644 --- a/packages/svelte/tests/runtime-legacy/samples/context-api/Tabs.svelte +++ b/packages/svelte/tests/runtime-legacy/samples/context-api/Tabs.svelte @@ -1,6 +1,6 @@ - diff --git a/packages/svelte/tests/runtime-legacy/samples/deconflict-component-name-with-module-global/main.svelte b/packages/svelte/tests/runtime-legacy/samples/deconflict-component-name-with-module-global/main.svelte index a14f3e7a1e..9fac585775 100644 --- a/packages/svelte/tests/runtime-legacy/samples/deconflict-component-name-with-module-global/main.svelte +++ b/packages/svelte/tests/runtime-legacy/samples/deconflict-component-name-with-module-global/main.svelte @@ -1,5 +1,5 @@ - -

{set.has('x')}

\ No newline at end of file +

{set.has('x')}

diff --git a/packages/svelte/tests/runtime-legacy/samples/module-context-export-referenced-in-template/Foo.svelte b/packages/svelte/tests/runtime-legacy/samples/module-context-export-referenced-in-template/Foo.svelte index a0f3258fd6..4750e6253b 100644 --- a/packages/svelte/tests/runtime-legacy/samples/module-context-export-referenced-in-template/Foo.svelte +++ b/packages/svelte/tests/runtime-legacy/samples/module-context-export-referenced-in-template/Foo.svelte @@ -1,5 +1,7 @@ - diff --git a/packages/svelte/tests/runtime-legacy/samples/module-context-export/Foo.svelte b/packages/svelte/tests/runtime-legacy/samples/module-context-export/Foo.svelte index c992ceef52..86f110f481 100644 --- a/packages/svelte/tests/runtime-legacy/samples/module-context-export/Foo.svelte +++ b/packages/svelte/tests/runtime-legacy/samples/module-context-export/Foo.svelte @@ -1,4 +1,4 @@ - diff --git a/packages/svelte/tests/runtime-legacy/samples/module-context/main.svelte b/packages/svelte/tests/runtime-legacy/samples/module-context/main.svelte index 851d98bea3..2e1ae663fb 100644 --- a/packages/svelte/tests/runtime-legacy/samples/module-context/main.svelte +++ b/packages/svelte/tests/runtime-legacy/samples/module-context/main.svelte @@ -1,5 +1,5 @@ - -

{foo}

\ No newline at end of file +

{foo}

diff --git a/packages/svelte/tests/runtime-legacy/samples/preload/main.svelte b/packages/svelte/tests/runtime-legacy/samples/preload/main.svelte index f7f2adff18..b285d423fc 100644 --- a/packages/svelte/tests/runtime-legacy/samples/preload/main.svelte +++ b/packages/svelte/tests/runtime-legacy/samples/preload/main.svelte @@ -1,7 +1,7 @@ - \ No newline at end of file + diff --git a/packages/svelte/tests/runtime-legacy/samples/reactive-import-statement-module/main.svelte b/packages/svelte/tests/runtime-legacy/samples/reactive-import-statement-module/main.svelte index beb2c406d1..18db8a7a31 100644 --- a/packages/svelte/tests/runtime-legacy/samples/reactive-import-statement-module/main.svelte +++ b/packages/svelte/tests/runtime-legacy/samples/reactive-import-statement-module/main.svelte @@ -1,4 +1,4 @@ - diff --git a/packages/svelte/tests/runtime-legacy/samples/store-imported-module/main.svelte b/packages/svelte/tests/runtime-legacy/samples/store-imported-module/main.svelte index 8710e3d1c6..35986d43d5 100644 --- a/packages/svelte/tests/runtime-legacy/samples/store-imported-module/main.svelte +++ b/packages/svelte/tests/runtime-legacy/samples/store-imported-module/main.svelte @@ -1,4 +1,4 @@ - diff --git a/packages/svelte/tests/runtime-runes/samples/class-state-derived-unowned/main.svelte b/packages/svelte/tests/runtime-runes/samples/class-state-derived-unowned/main.svelte index baa1b4aa86..898df1d66c 100644 --- a/packages/svelte/tests/runtime-runes/samples/class-state-derived-unowned/main.svelte +++ b/packages/svelte/tests/runtime-runes/samples/class-state-derived-unowned/main.svelte @@ -1,4 +1,4 @@ - diff --git a/packages/svelte/tests/runtime-runes/samples/derived-unowned-5/main.svelte b/packages/svelte/tests/runtime-runes/samples/derived-unowned-5/main.svelte index 3889869461..beecbacb69 100644 --- a/packages/svelte/tests/runtime-runes/samples/derived-unowned-5/main.svelte +++ b/packages/svelte/tests/runtime-runes/samples/derived-unowned-5/main.svelte @@ -1,4 +1,4 @@ - diff --git a/packages/svelte/tests/runtime-runes/samples/derived-unowned/Component.svelte b/packages/svelte/tests/runtime-runes/samples/derived-unowned/Component.svelte index 8946c01d50..1790f088c0 100644 --- a/packages/svelte/tests/runtime-runes/samples/derived-unowned/Component.svelte +++ b/packages/svelte/tests/runtime-runes/samples/derived-unowned/Component.svelte @@ -1,4 +1,4 @@ - diff --git a/packages/svelte/tests/runtime-runes/samples/effect-dependencies/main.svelte b/packages/svelte/tests/runtime-runes/samples/effect-dependencies/main.svelte index 75f5a0cd6e..770666289c 100644 --- a/packages/svelte/tests/runtime-runes/samples/effect-dependencies/main.svelte +++ b/packages/svelte/tests/runtime-runes/samples/effect-dependencies/main.svelte @@ -1,8 +1,8 @@ -
- - + +
{#if things.tab === 'A'} - A + A {:else} B {#each things.list as item} diff --git a/packages/svelte/tests/runtime-runes/samples/element-is-attribute/main.svelte b/packages/svelte/tests/runtime-runes/samples/element-is-attribute/main.svelte index 76aca5fa32..15d2d521e4 100644 --- a/packages/svelte/tests/runtime-runes/samples/element-is-attribute/main.svelte +++ b/packages/svelte/tests/runtime-runes/samples/element-is-attribute/main.svelte @@ -1,4 +1,4 @@ - diff --git a/packages/svelte/tests/runtime-runes/samples/non-local-mutation-global-2/child.svelte b/packages/svelte/tests/runtime-runes/samples/non-local-mutation-global-2/child.svelte index 5efb0d11ab..13de753647 100644 --- a/packages/svelte/tests/runtime-runes/samples/non-local-mutation-global-2/child.svelte +++ b/packages/svelte/tests/runtime-runes/samples/non-local-mutation-global-2/child.svelte @@ -1,4 +1,4 @@ - diff --git a/packages/svelte/tests/validator/samples/illegal-variable-declaration/errors.json b/packages/svelte/tests/validator/samples/illegal-variable-declaration/errors.json index e0002aaa74..850880e744 100644 --- a/packages/svelte/tests/validator/samples/illegal-variable-declaration/errors.json +++ b/packages/svelte/tests/validator/samples/illegal-variable-declaration/errors.json @@ -1,7 +1,7 @@ [ { "code": "declaration_duplicate_module_import", - "message": "Cannot declare same variable name which is imported inside ` diff --git a/packages/svelte/tests/validator/samples/module-script-reactive-declaration/input.svelte b/packages/svelte/tests/validator/samples/module-script-reactive-declaration/input.svelte index 9df1cd54dd..2673ab2f9a 100644 --- a/packages/svelte/tests/validator/samples/module-script-reactive-declaration/input.svelte +++ b/packages/svelte/tests/validator/samples/module-script-reactive-declaration/input.svelte @@ -1,4 +1,4 @@ - - \ No newline at end of file diff --git a/packages/svelte/tests/validator/samples/reactive-module-const-variable/input.svelte b/packages/svelte/tests/validator/samples/reactive-module-const-variable/input.svelte index c89cc1c1d0..82fb3aba9b 100644 --- a/packages/svelte/tests/validator/samples/reactive-module-const-variable/input.svelte +++ b/packages/svelte/tests/validator/samples/reactive-module-const-variable/input.svelte @@ -1,4 +1,4 @@ - diff --git a/packages/svelte/tests/validator/samples/reactive-module-variable-2/input.svelte b/packages/svelte/tests/validator/samples/reactive-module-variable-2/input.svelte index d36d9cf210..5209154b6f 100644 --- a/packages/svelte/tests/validator/samples/reactive-module-variable-2/input.svelte +++ b/packages/svelte/tests/validator/samples/reactive-module-variable-2/input.svelte @@ -1,4 +1,4 @@ - diff --git a/packages/svelte/tests/validator/samples/script-context-module-deprecated/warnings.json b/packages/svelte/tests/validator/samples/script-context-module-deprecated/warnings.json new file mode 100644 index 0000000000..739e2b2295 --- /dev/null +++ b/packages/svelte/tests/validator/samples/script-context-module-deprecated/warnings.json @@ -0,0 +1,14 @@ +[ + { + "code": "script_context_deprecated", + "message": "`context=\"module\"` is deprecated, use the `module` attribute instead", + "start": { + "column": 8, + "line": 1 + }, + "end": { + "column": 24, + "line": 1 + } + } +] diff --git a/packages/svelte/tests/validator/samples/script-invalid-context/errors.json b/packages/svelte/tests/validator/samples/script-invalid-context/errors.json index 3698b92386..16072ef2cb 100644 --- a/packages/svelte/tests/validator/samples/script-invalid-context/errors.json +++ b/packages/svelte/tests/validator/samples/script-invalid-context/errors.json @@ -8,7 +8,7 @@ }, "end": { "line": 1, - "column": 8 + "column": 22 } } ] diff --git a/packages/svelte/tests/validator/samples/script-invalid-spread-attribute/input.svelte b/packages/svelte/tests/validator/samples/script-invalid-spread-attribute/input.svelte new file mode 100644 index 0000000000..5a28597de7 --- /dev/null +++ b/packages/svelte/tests/validator/samples/script-invalid-spread-attribute/input.svelte @@ -0,0 +1 @@ + diff --git a/packages/svelte/tests/validator/samples/script-invalid-spread-attribute/warnings.json b/packages/svelte/tests/validator/samples/script-invalid-spread-attribute/warnings.json new file mode 100644 index 0000000000..c6748f711d --- /dev/null +++ b/packages/svelte/tests/validator/samples/script-invalid-spread-attribute/warnings.json @@ -0,0 +1,14 @@ +[ + { + "code": "script_unknown_attribute", + "message": "Unrecognized attribute — should be one of `generics`, `lang` or `module`. If this exists for a preprocessor, ensure that the preprocessor removes it", + "start": { + "column": 8, + "line": 1 + }, + "end": { + "column": 18, + "line": 1 + } + } +] diff --git a/packages/svelte/tests/validator/samples/script-unknown-attribute/input.svelte b/packages/svelte/tests/validator/samples/script-unknown-attribute/input.svelte new file mode 100644 index 0000000000..2c991e62c1 --- /dev/null +++ b/packages/svelte/tests/validator/samples/script-unknown-attribute/input.svelte @@ -0,0 +1 @@ + diff --git a/packages/svelte/tests/validator/samples/script-unknown-attribute/warnings.json b/packages/svelte/tests/validator/samples/script-unknown-attribute/warnings.json new file mode 100644 index 0000000000..fdcad269a6 --- /dev/null +++ b/packages/svelte/tests/validator/samples/script-unknown-attribute/warnings.json @@ -0,0 +1,14 @@ +[ + { + "code": "script_unknown_attribute", + "message": "Unrecognized attribute — should be one of `generics`, `lang` or `module`. If this exists for a preprocessor, ensure that the preprocessor removes it", + "start": { + "column": 8, + "line": 1 + }, + "end": { + "column": 14, + "line": 1 + } + } +] diff --git a/packages/svelte/tests/validator/samples/silence-warnings-2/input.svelte b/packages/svelte/tests/validator/samples/silence-warnings-2/input.svelte index 275165ffd7..251ea12051 100644 --- a/packages/svelte/tests/validator/samples/silence-warnings-2/input.svelte +++ b/packages/svelte/tests/validator/samples/silence-warnings-2/input.svelte @@ -1,4 +1,4 @@ - diff --git a/packages/svelte/tests/validator/samples/silence-warnings/input.svelte b/packages/svelte/tests/validator/samples/silence-warnings/input.svelte index 4520aed9c1..a8760fdd19 100644 --- a/packages/svelte/tests/validator/samples/silence-warnings/input.svelte +++ b/packages/svelte/tests/validator/samples/silence-warnings/input.svelte @@ -1,4 +1,4 @@ - diff --git a/packages/svelte/tests/validator/samples/unreferenced-variables/input.svelte b/packages/svelte/tests/validator/samples/unreferenced-variables/input.svelte index cebd861985..caac6d8bf9 100644 --- a/packages/svelte/tests/validator/samples/unreferenced-variables/input.svelte +++ b/packages/svelte/tests/validator/samples/unreferenced-variables/input.svelte @@ -1,4 +1,4 @@ - diff --git a/sites/svelte-5-preview/src/routes/docs/content/03-appendix/03-deprecations.md b/sites/svelte-5-preview/src/routes/docs/content/03-appendix/03-deprecations.md index e4fdcc86b3..0c45bdaab2 100644 --- a/sites/svelte-5-preview/src/routes/docs/content/03-appendix/03-deprecations.md +++ b/sites/svelte-5-preview/src/routes/docs/content/03-appendix/03-deprecations.md @@ -117,3 +117,14 @@ A derived value may be used in other contexts: ## `immutable` The `immutable` compiler option is deprecated. Use runes mode instead, where all state is immutable (which means that assigning to `object.property` won't cause updates for anything that is observing `object` itself, or a different property of it). + +## `context="module"` + +`context="module"` is deprecated, use the new `module` attribute instead: + +```diff +- +```