Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix build with svelte 4 and estree-walker 3 #2176

Merged
merged 3 commits into from
Oct 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ export function findIfBlockEndTagStart(documentText: string, ifBlock: SvelteNode
return documentText.lastIndexOf('{', documentText.lastIndexOf('/if', ifBlock.end));
}

type ESTreeWaker = Parameters<typeof walk>[1];
type ESTreeEnterFunc = NonNullable<ESTreeWaker['enter']>;
type ESTreeLeaveFunc = NonNullable<ESTreeWaker['leave']>;

export interface SvelteNodeWalker {
enter?: (
this: {
Expand All @@ -134,8 +138,8 @@ export interface SvelteNodeWalker {
},
node: SvelteNode,
parent: SvelteNode,
key: string,
index: number
key: Parameters<ESTreeEnterFunc>[2],
index: Parameters<ESTreeEnterFunc>[3]
) => void;
leave?: (
this: {
Expand All @@ -145,18 +149,21 @@ export interface SvelteNodeWalker {
},
node: SvelteNode,
parent: SvelteNode,
key: string,
index: number
key: Parameters<ESTreeLeaveFunc>[2],
index: Parameters<ESTreeLeaveFunc>[3]
) => void;
}

// wrap the estree-walker to make it svelte specific
// the type casting is necessary because estree-walker is not designed for this
// especially in v3 which svelte 4 uses
export function walkSvelteAst(htmlAst: TemplateNode, walker: SvelteNodeWalker) {
walk(htmlAst as any, {
enter(node, parent, key, index) {
walker.enter?.call(this, node as SvelteNode, parent as SvelteNode, key, index);
walker.enter?.call(this as any, node as SvelteNode, parent as SvelteNode, key, index);
},
leave(node, parent, key, index) {
walker.leave?.call(this, node as SvelteNode, parent as SvelteNode, key, index);
walker.leave?.call(this as any, node as SvelteNode, parent as SvelteNode, key, index);
}
});
}
Expand Down