Skip to content

Commit 9e54835

Browse files
committed
fix: support for new ConstTag node
1 parent c805b05 commit 9e54835

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

src/context/script-let.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -243,11 +243,11 @@ export class ScriptLetContext {
243243
}
244244

245245
public addVariableDeclarator(
246-
expression: ESTree.AssignmentExpression,
246+
declarator: ESTree.VariableDeclarator | ESTree.AssignmentExpression,
247247
parent: SvelteNode,
248248
...callbacks: ScriptLetCallback<ESTree.VariableDeclarator>[]
249249
): ScriptLetCallback<ESTree.VariableDeclarator>[] {
250-
const range = getNodeRange(expression);
250+
const range = getNodeRange(declarator);
251251
const part = this.ctx.code.slice(...range);
252252
this.appendScript(
253253
`const ${part};`,

src/parser/compat.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,6 @@ export function getThenFromAwaitBlock(
204204
}
205205
return then.skip ? null : then;
206206
}
207-
208207
export function getCatchFromAwaitBlock(
209208
block: SvAST.AwaitBlock | Compiler.AwaitBlock,
210209
): Compiler.Fragment | SvAST.CatchBlock | null {
@@ -217,3 +216,15 @@ export function getCatchFromAwaitBlock(
217216
}
218217
return catchFragment.skip ? null : catchFragment;
219218
}
219+
220+
// ConstTag
221+
export function getDeclaratorFromConstTag(
222+
node: SvAST.ConstTag | Compiler.ConstTag,
223+
):
224+
| ESTree.AssignmentExpression
225+
| Compiler.ConstTag["declaration"]["declarations"][0] {
226+
return (
227+
(node as Compiler.ConstTag).declaration?.declarations?.[0] ??
228+
(node as SvAST.ConstTag).expression
229+
);
230+
}

src/parser/converts/const.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import type { SvelteConstTag } from "../../ast";
22
import type { Context } from "../../context";
3+
import { getDeclaratorFromConstTag } from "../compat";
34
import type * as SvAST from "../svelte-ast-types";
5+
import type * as Compiler from "svelte/compiler";
46

57
/** Convert for ConstTag */
68
export function convertConstTag(
7-
node: SvAST.ConstTag,
9+
node: SvAST.ConstTag | Compiler.ConstTag,
810
parent: SvelteConstTag["parent"],
911
ctx: Context,
1012
): SvelteConstTag {
@@ -15,7 +17,7 @@ export function convertConstTag(
1517
...ctx.getConvertLocation(node),
1618
};
1719
ctx.scriptLet.addVariableDeclarator(
18-
node.expression,
20+
getDeclaratorFromConstTag(node),
1921
mustache,
2022
(declaration) => {
2123
mustache.declaration = declaration;

0 commit comments

Comments
 (0)