From a271878abe7018923839401129b18082eb2c811a Mon Sep 17 00:00:00 2001 From: Simon Holthausen Date: Thu, 11 Jan 2024 14:32:13 +0100 Subject: [PATCH] fix: adjust `parse` return type It returns the root which contains the Svelte nodes --- .changeset/sour-weeks-fix.md | 5 +++++ packages/svelte/src/compiler/index.js | 4 ++-- packages/svelte/src/compiler/legacy.js | 4 ++-- packages/svelte/src/compiler/types/legacy-nodes.d.ts | 7 +++++++ packages/svelte/types/index.d.ts | 9 ++++++++- 5 files changed, 24 insertions(+), 5 deletions(-) create mode 100644 .changeset/sour-weeks-fix.md diff --git a/.changeset/sour-weeks-fix.md b/.changeset/sour-weeks-fix.md new file mode 100644 index 0000000000..5d639f3a60 --- /dev/null +++ b/.changeset/sour-weeks-fix.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: adjust `parse` return type diff --git a/packages/svelte/src/compiler/index.js b/packages/svelte/src/compiler/index.js index df7034a75a..3e83a17798 100644 --- a/packages/svelte/src/compiler/index.js +++ b/packages/svelte/src/compiler/index.js @@ -91,7 +91,7 @@ function handle_compile_error(error, filename, source) { * https://svelte.dev/docs/svelte-compiler#svelte-parse * @param {string} source * @param {{ filename?: string; modern?: boolean }} [options] - * @returns {import('#compiler').SvelteNode | import('./types/legacy-nodes.js').LegacySvelteNode} + * @returns {import('#compiler').Root | import('./types/legacy-nodes.js').LegacyRoot} */ export function parse(source, options = {}) { /** @type {import('#compiler').Root} */ @@ -108,7 +108,7 @@ export function parse(source, options = {}) { if (options.modern) { // remove things that we don't want to treat as public API - return walk(/** @type {import('#compiler').SvelteNode} */ (ast), null, { + return walk(ast, null, { _(node, { next }) { // @ts-ignore delete node.parent; diff --git a/packages/svelte/src/compiler/legacy.js b/packages/svelte/src/compiler/legacy.js index da20b90696..8d67f654c2 100644 --- a/packages/svelte/src/compiler/legacy.js +++ b/packages/svelte/src/compiler/legacy.js @@ -33,7 +33,7 @@ function remove_surrounding_whitespace_nodes(nodes) { * Transform our nice modern AST into the monstrosity emitted by Svelte 4 * @param {string} source * @param {import('#compiler').Root} ast - * @returns {import('./types/legacy-nodes.js').LegacySvelteNode} + * @returns {import('./types/legacy-nodes.js').LegacyRoot} */ export function convert(source, ast) { const root = @@ -41,7 +41,7 @@ export function convert(source, ast) { ast ); - return /** @type {import('./types/legacy-nodes.js').LegacySvelteNode} */ ( + return /** @type {import('./types/legacy-nodes.js').LegacyRoot} */ ( walk(root, null, { _(node, { next }) { // @ts-ignore diff --git a/packages/svelte/src/compiler/types/legacy-nodes.d.ts b/packages/svelte/src/compiler/types/legacy-nodes.d.ts index 41c1ad7a3b..ba89520e20 100644 --- a/packages/svelte/src/compiler/types/legacy-nodes.d.ts +++ b/packages/svelte/src/compiler/types/legacy-nodes.d.ts @@ -21,6 +21,13 @@ interface BaseElement extends BaseNode { children: Array; } +export interface LegacyRoot extends BaseNode { + html: LegacySvelteNode; + css?: any; + instance?: any; + module?: any; +} + export interface LegacyAction extends BaseNode { type: 'Action'; /** The 'x' in `use:x` */ diff --git a/packages/svelte/types/index.d.ts b/packages/svelte/types/index.d.ts index 9945df7735..399b4442c4 100644 --- a/packages/svelte/types/index.d.ts +++ b/packages/svelte/types/index.d.ts @@ -514,7 +514,7 @@ declare module 'svelte/compiler' { export function parse(source: string, options?: { filename?: string | undefined; modern?: boolean | undefined; - } | undefined): SvelteNode | LegacySvelteNode; + } | undefined): Root | LegacyRoot; /** * @deprecated Replace this with `import { walk } from 'estree-walker'` * */ @@ -779,6 +779,13 @@ declare module 'svelte/compiler' { children: Array; } + interface LegacyRoot extends BaseNode_1 { + html: LegacySvelteNode; + css?: any; + instance?: any; + module?: any; + } + interface LegacyAction extends BaseNode_1 { type: 'Action'; /** The 'x' in `use:x` */