From 6bd20c66099fa782d5f3edb9a7306dcc8c579c08 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Thu, 19 Sep 2024 14:24:47 -0400 Subject: [PATCH] feat: deprecate `` in runes mode --- .changeset/old-spoons-pull.md | 5 +++++ .../svelte/messages/compile-warnings/template.md | 4 ++++ .../phases/2-analyze/visitors/SvelteSelf.js | 12 ++++++++++++ packages/svelte/src/compiler/warnings.js | 13 ++++++++++++- .../samples/attribute-quoted/input.svelte | 1 + .../samples/attribute-quoted/warnings.json | 8 ++++---- .../samples/svelte-self-deprecated/input.svelte | 10 ++++++++++ .../samples/svelte-self-deprecated/warnings.json | 14 ++++++++++++++ 8 files changed, 62 insertions(+), 5 deletions(-) create mode 100644 .changeset/old-spoons-pull.md create mode 100644 packages/svelte/tests/validator/samples/svelte-self-deprecated/input.svelte create mode 100644 packages/svelte/tests/validator/samples/svelte-self-deprecated/warnings.json diff --git a/.changeset/old-spoons-pull.md b/.changeset/old-spoons-pull.md new file mode 100644 index 000000000000..15de7e6e6c08 --- /dev/null +++ b/.changeset/old-spoons-pull.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +feat: deprecate `` in runes mode diff --git a/packages/svelte/messages/compile-warnings/template.md b/packages/svelte/messages/compile-warnings/template.md index 194bd1087da9..5e198c2d5522 100644 --- a/packages/svelte/messages/compile-warnings/template.md +++ b/packages/svelte/messages/compile-warnings/template.md @@ -97,3 +97,7 @@ A derived value may be used in other contexts: ## svelte_element_invalid_this > `this` should be an `{expression}`. Using a string attribute value will cause an error in future versions of Svelte + +## svelte_self_deprecated + +> `` is deprecated — use self-imports (e.g. `import %name% from './%basename%'`) instead diff --git a/packages/svelte/src/compiler/phases/2-analyze/visitors/SvelteSelf.js b/packages/svelte/src/compiler/phases/2-analyze/visitors/SvelteSelf.js index 18696e0c552a..4023560b570b 100644 --- a/packages/svelte/src/compiler/phases/2-analyze/visitors/SvelteSelf.js +++ b/packages/svelte/src/compiler/phases/2-analyze/visitors/SvelteSelf.js @@ -2,6 +2,8 @@ /** @import { Context } from '../types' */ import { visit_component } from './shared/component.js'; import * as e from '../../../errors.js'; +import * as w from '../../../warnings.js'; +import { filename } from '../../../state.js'; /** * @param {AST.SvelteSelf} node @@ -20,5 +22,15 @@ export function SvelteSelf(node, context) { e.svelte_self_invalid_placement(node); } + if (context.state.analysis.runes) { + const name = filename === '(unknown)' ? 'Self' : context.state.analysis.name; + const basename = + filename === '(unknown)' + ? 'Self.svelte' + : /** @type {string} */ (filename.split(/[\/]/).pop()); + + w.svelte_self_deprecated(node, name, basename); + } + visit_component(node, context); } diff --git a/packages/svelte/src/compiler/warnings.js b/packages/svelte/src/compiler/warnings.js index 2d7d71028199..bd2895623050 100644 --- a/packages/svelte/src/compiler/warnings.js +++ b/packages/svelte/src/compiler/warnings.js @@ -121,7 +121,8 @@ export const codes = [ "script_unknown_attribute", "slot_element_deprecated", "svelte_component_deprecated", - "svelte_element_invalid_this" + "svelte_element_invalid_this", + "svelte_self_deprecated" ]; /** @@ -809,4 +810,14 @@ export function svelte_component_deprecated(node) { */ export function svelte_element_invalid_this(node) { w(node, "svelte_element_invalid_this", "`this` should be an `{expression}`. Using a string attribute value will cause an error in future versions of Svelte"); +} + +/** + * `` is deprecated — use self-imports (e.g. `import %name% from './%basename%'`) instead + * @param {null | NodeLike} node + * @param {string} name + * @param {string} basename + */ +export function svelte_self_deprecated(node, name, basename) { + w(node, "svelte_self_deprecated", `\`\` is deprecated — use self-imports (e.g. \`import ${name} from './${basename}'\`) instead`); } \ No newline at end of file diff --git a/packages/svelte/tests/validator/samples/attribute-quoted/input.svelte b/packages/svelte/tests/validator/samples/attribute-quoted/input.svelte index 9252d9b19a89..626630bb161a 100644 --- a/packages/svelte/tests/validator/samples/attribute-quoted/input.svelte +++ b/packages/svelte/tests/validator/samples/attribute-quoted/input.svelte @@ -15,6 +15,7 @@ {#if foo} + {/if} diff --git a/packages/svelte/tests/validator/samples/attribute-quoted/warnings.json b/packages/svelte/tests/validator/samples/attribute-quoted/warnings.json index d324e9f60fde..e42af44dac24 100644 --- a/packages/svelte/tests/validator/samples/attribute-quoted/warnings.json +++ b/packages/svelte/tests/validator/samples/attribute-quoted/warnings.json @@ -40,11 +40,11 @@ "message": "Quoted attributes on components and custom elements will be stringified in a future version of Svelte. If this isn't what you want, remove the quotes", "start": { "column": 14, - "line": 18 + "line": 19 }, "end": { "column": 27, - "line": 18 + "line": 19 } }, { @@ -52,11 +52,11 @@ "message": "Quoted attributes on components and custom elements will be stringified in a future version of Svelte. If this isn't what you want, remove the quotes", "start": { "column": 16, - "line": 21 + "line": 22 }, "end": { "column": 29, - "line": 21 + "line": 22 } } ] diff --git a/packages/svelte/tests/validator/samples/svelte-self-deprecated/input.svelte b/packages/svelte/tests/validator/samples/svelte-self-deprecated/input.svelte new file mode 100644 index 000000000000..764807819d49 --- /dev/null +++ b/packages/svelte/tests/validator/samples/svelte-self-deprecated/input.svelte @@ -0,0 +1,10 @@ + + +{#if n === 0} +

lift-off!

+{:else} +

{n}

+ +{/if} diff --git a/packages/svelte/tests/validator/samples/svelte-self-deprecated/warnings.json b/packages/svelte/tests/validator/samples/svelte-self-deprecated/warnings.json new file mode 100644 index 000000000000..4063f6866699 --- /dev/null +++ b/packages/svelte/tests/validator/samples/svelte-self-deprecated/warnings.json @@ -0,0 +1,14 @@ +[ + { + "code": "svelte_self_deprecated", + "message": "`` is deprecated — use self-imports (e.g. `import Self from './Self.svelte'`) instead", + "start": { + "line": 9, + "column": 1 + }, + "end": { + "line": 9, + "column": 26 + } + } +]