-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathmustache.ts
91 lines (87 loc) · 2.17 KB
/
mustache.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import type {
SvelteDebugTag,
SvelteMustacheTag,
SvelteMustacheTagRaw,
SvelteMustacheTagText,
} from "../../ast";
import type { Context } from "../../context";
import type * as SvAST from "../svelte-ast-types";
import { hasTypeInfo } from "../../utils";
/** Convert for MustacheTag */
export function convertMustacheTag(
node: SvAST.MustacheTag,
parent: SvelteMustacheTag["parent"],
typing: string | null,
ctx: Context,
): SvelteMustacheTagText {
return convertMustacheTag0(node, "text", parent, typing, ctx);
}
/** Convert for MustacheTag */
export function convertRawMustacheTag(
node: SvAST.RawMustacheTag,
parent: SvelteMustacheTag["parent"],
ctx: Context,
): SvelteMustacheTagRaw {
const mustache: SvelteMustacheTagRaw = convertMustacheTag0(
node,
"raw",
parent,
null,
ctx,
);
const atHtmlStart = ctx.code.indexOf("@html", mustache.range[0]);
ctx.addToken("MustacheKeyword", {
start: atHtmlStart,
end: atHtmlStart + 5,
});
return mustache;
}
/** Convert for DebugTag */
export function convertDebugTag(
node: SvAST.DebugTag,
parent: SvelteDebugTag["parent"],
ctx: Context,
): SvelteDebugTag {
const mustache: SvelteDebugTag = {
type: "SvelteDebugTag",
identifiers: [],
parent,
...ctx.getConvertLocation(node),
};
for (const id of node.identifiers) {
ctx.scriptLet.addExpression(id, mustache, null, (es) => {
mustache.identifiers.push(es);
});
}
const atDebugStart = ctx.code.indexOf("@debug", mustache.range[0]);
ctx.addToken("MustacheKeyword", {
start: atDebugStart,
end: atDebugStart + 6,
});
return mustache;
}
/** Convert to MustacheTag */
function convertMustacheTag0<T extends SvelteMustacheTag>(
node: SvAST.MustacheTag | SvAST.RawMustacheTag,
kind: T["kind"],
parent: T["parent"],
typing: string | null,
ctx: Context,
): T {
const mustache = {
type: "SvelteMustacheTag",
kind,
expression: null as any,
parent,
...ctx.getConvertLocation(node),
} as T;
ctx.scriptLet.addExpression(
node.expression,
mustache,
hasTypeInfo(node.expression) ? null : typing,
(es) => {
mustache.expression = es;
},
);
return mustache;
}