fix: adjust `parse` return type

It returns the root which contains the Svelte nodes
compile-return-ast
Simon Holthausen 12 months ago
parent c7cb90c91c
commit a271878abe

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: adjust `parse` return type

@ -91,7 +91,7 @@ function handle_compile_error(error, filename, source) {
* https://svelte.dev/docs/svelte-compiler#svelte-parse * https://svelte.dev/docs/svelte-compiler#svelte-parse
* @param {string} source * @param {string} source
* @param {{ filename?: string; modern?: boolean }} [options] * @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 = {}) { export function parse(source, options = {}) {
/** @type {import('#compiler').Root} */ /** @type {import('#compiler').Root} */
@ -108,7 +108,7 @@ export function parse(source, options = {}) {
if (options.modern) { if (options.modern) {
// remove things that we don't want to treat as public API // 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 }) { _(node, { next }) {
// @ts-ignore // @ts-ignore
delete node.parent; delete node.parent;

@ -33,7 +33,7 @@ function remove_surrounding_whitespace_nodes(nodes) {
* Transform our nice modern AST into the monstrosity emitted by Svelte 4 * Transform our nice modern AST into the monstrosity emitted by Svelte 4
* @param {string} source * @param {string} source
* @param {import('#compiler').Root} ast * @param {import('#compiler').Root} ast
* @returns {import('./types/legacy-nodes.js').LegacySvelteNode} * @returns {import('./types/legacy-nodes.js').LegacyRoot}
*/ */
export function convert(source, ast) { export function convert(source, ast) {
const root = const root =
@ -41,7 +41,7 @@ export function convert(source, ast) {
ast ast
); );
return /** @type {import('./types/legacy-nodes.js').LegacySvelteNode} */ ( return /** @type {import('./types/legacy-nodes.js').LegacyRoot} */ (
walk(root, null, { walk(root, null, {
_(node, { next }) { _(node, { next }) {
// @ts-ignore // @ts-ignore

@ -21,6 +21,13 @@ interface BaseElement extends BaseNode {
children: Array<LegacyElementLike>; children: Array<LegacyElementLike>;
} }
export interface LegacyRoot extends BaseNode {
html: LegacySvelteNode;
css?: any;
instance?: any;
module?: any;
}
export interface LegacyAction extends BaseNode { export interface LegacyAction extends BaseNode {
type: 'Action'; type: 'Action';
/** The 'x' in `use:x` */ /** The 'x' in `use:x` */

@ -514,7 +514,7 @@ declare module 'svelte/compiler' {
export function parse(source: string, options?: { export function parse(source: string, options?: {
filename?: string | undefined; filename?: string | undefined;
modern?: boolean | undefined; modern?: boolean | undefined;
} | undefined): SvelteNode | LegacySvelteNode; } | undefined): Root | LegacyRoot;
/** /**
* @deprecated Replace this with `import { walk } from 'estree-walker'` * @deprecated Replace this with `import { walk } from 'estree-walker'`
* */ * */
@ -779,6 +779,13 @@ declare module 'svelte/compiler' {
children: Array<LegacyElementLike>; children: Array<LegacyElementLike>;
} }
interface LegacyRoot extends BaseNode_1 {
html: LegacySvelteNode;
css?: any;
instance?: any;
module?: any;
}
interface LegacyAction extends BaseNode_1 { interface LegacyAction extends BaseNode_1 {
type: 'Action'; type: 'Action';
/** The 'x' in `use:x` */ /** The 'x' in `use:x` */

Loading…
Cancel
Save