From f9abdb6428a037a1ba1ced23bfe135eded020865 Mon Sep 17 00:00:00 2001 From: Elvin Seudieu Date: Thu, 23 Apr 2026 18:45:22 -0400 Subject: [PATCH] added compiler warning functionality for head warnings (untested) --- .../docs/98-reference/.generated/compile-warnings.md | 6 ++++++ .../svelte/messages/compile-warnings/template.md | 3 +++ .../phases/2-analyze/visitors/RegularElement.js | 6 +++++- packages/svelte/src/compiler/warnings.js | 12 ++++++++++++ 4 files changed, 26 insertions(+), 1 deletion(-) diff --git a/documentation/docs/98-reference/.generated/compile-warnings.md b/documentation/docs/98-reference/.generated/compile-warnings.md index 7dfbe75888..c218867beb 100644 --- a/documentation/docs/98-reference/.generated/compile-warnings.md +++ b/documentation/docs/98-reference/.generated/compile-warnings.md @@ -697,6 +697,12 @@ Using `on:%name%` to listen to the %name% event is deprecated. Use the event att See [the migration guide](v5-migration-guide#Event-changes) for more info. +### head_in_component + +``` +A `` tag was detected in file `%filename%`, component `%component%`, at line %line%. This can lead to runtime errors. Did you mean to use ``? +``` + ### export_let_unused ``` diff --git a/packages/svelte/messages/compile-warnings/template.md b/packages/svelte/messages/compile-warnings/template.md index 3650e07b47..2a9fc8716f 100644 --- a/packages/svelte/messages/compile-warnings/template.md +++ b/packages/svelte/messages/compile-warnings/template.md @@ -83,6 +83,9 @@ In a future version of Svelte, self-closing tags may be upgraded from a warning See [the migration guide](v5-migration-guide#Event-changes) for more info. +## head_in_component +> A `` tag was detected in file `%filename%`, component `%component%`, at line %line%. This can lead to runtime errors. Did you mean to use ``? + ## node_invalid_placement_ssr > %message%. When rendering this component on the server, the resulting HTML will be modified by the browser (by moving, removing, or inserting elements), likely resulting in a `hydration_mismatch` warning diff --git a/packages/svelte/src/compiler/phases/2-analyze/visitors/RegularElement.js b/packages/svelte/src/compiler/phases/2-analyze/visitors/RegularElement.js index bc65bd65db..17f1aed59c 100644 --- a/packages/svelte/src/compiler/phases/2-analyze/visitors/RegularElement.js +++ b/packages/svelte/src/compiler/phases/2-analyze/visitors/RegularElement.js @@ -17,7 +17,7 @@ import { check_element } from './shared/a11y/index.js'; import { validate_element } from './shared/element.js'; import { mark_subtree_dynamic } from './shared/fragment.js'; import { object } from '../../../utils/ast.js'; -import { runes } from '../../../state.js'; +import { component_name, filename, locator, runes } from '../../../state.js'; /** * @param {AST.RegularElement} node @@ -126,6 +126,10 @@ export function RegularElement(node, context) { w.component_name_lowercase(node, node.name); } + if (node.name === 'head' && !context.state.parent_element) { + w.head_in_component(node, filename, component_name, `${locator(node.start).line}`); + } + node.metadata.has_spread = node.attributes.some( (attribute) => attribute.type === 'SpreadAttribute' ); diff --git a/packages/svelte/src/compiler/warnings.js b/packages/svelte/src/compiler/warnings.js index 089cb1e118..27da0465ba 100644 --- a/packages/svelte/src/compiler/warnings.js +++ b/packages/svelte/src/compiler/warnings.js @@ -113,6 +113,7 @@ export const codes = [ 'element_implicitly_closed', 'element_invalid_self_closing_tag', 'event_directive_deprecated', + 'head_in_component', 'node_invalid_placement_ssr', 'script_context_deprecated', 'script_unknown_attribute', @@ -785,6 +786,17 @@ export function event_directive_deprecated(node, name) { w(node, 'event_directive_deprecated', `Using \`on:${name}\` to listen to the ${name} event is deprecated. Use the event attribute \`on${name}\` instead\nhttps://svelte.dev/e/event_directive_deprecated`); } +/** + * A `` tag was detected in file `%filename%`, component `%component%`, at line %line%. This can lead to runtime errors. Did you mean to use ``? + * @param {null | NodeLike} node + * @param {string} filename + * @param {string} component + * @param {string} line + */ +export function head_in_component(node, filename, component, line) { + w(node, 'head_in_component', `A \`\` tag was detected in file \`${filename}\`, component \`${component}\`, at line ${line}. This can lead to runtime errors. Did you mean to use \`\`?\nhttps://svelte.dev/e/head_in_component`); +} + /** * %message%. When rendering this component on the server, the resulting HTML will be modified by the browser (by moving, removing, or inserting elements), likely resulting in a `hydration_mismatch` warning * @param {null | NodeLike} node