Skip to content

Commit

Permalink
fix: adjust parse return type
Browse files Browse the repository at this point in the history
It returns the root which contains the Svelte nodes
  • Loading branch information
dummdidumm committed Jan 11, 2024
1 parent c7cb90c commit a271878
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/sour-weeks-fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: adjust `parse` return type
4 changes: 2 additions & 2 deletions packages/svelte/src/compiler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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} */
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions packages/svelte/src/compiler/legacy.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ 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 =
/** @type {import('./types/template.js').SvelteNode | import('./types/legacy-nodes.js').LegacySvelteNode} */ (
ast
);

return /** @type {import('./types/legacy-nodes.js').LegacySvelteNode} */ (
return /** @type {import('./types/legacy-nodes.js').LegacyRoot} */ (
walk(root, null, {
_(node, { next }) {
// @ts-ignore
Expand Down
7 changes: 7 additions & 0 deletions packages/svelte/src/compiler/types/legacy-nodes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ interface BaseElement extends BaseNode {
children: Array<LegacyElementLike>;
}

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` */
Expand Down
9 changes: 8 additions & 1 deletion packages/svelte/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'`
* */
Expand Down Expand Up @@ -779,6 +779,13 @@ declare module 'svelte/compiler' {
children: Array<LegacyElementLike>;
}

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` */
Expand Down

1 comment on commit a271878

@vercel
Copy link

@vercel vercel bot commented on a271878 Jan 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

svelte-5-preview – ./sites/svelte-5-preview

svelte-5-preview-svelte.vercel.app
svelte-5-preview.vercel.app
svelte-octane.vercel.app
svelte-5-preview-git-main-svelte.vercel.app

Please sign in to comment.