-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathindex.ts
66 lines (59 loc) · 1.96 KB
/
index.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
import { SupportLanguage, Parser, Printer } from 'prettier';
import { print } from './print';
import { ASTNode } from './print/nodes';
import { embed } from './embed';
import { snipScriptAndStyleTagContent } from './lib/snipTagContent';
function locStart(node: any) {
return node.start;
}
function locEnd(node: any) {
return node.end;
}
export const languages: Partial<SupportLanguage>[] = [
{
name: 'svelte',
parsers: ['svelte'],
extensions: ['.svelte'],
vscodeLanguageIds: ['svelte'],
},
];
export const parsers: Record<string, Parser> = {
svelte: {
parse: (text) => {
try {
return <ASTNode>{ ...require(`svelte/compiler`).parse(text), __isRoot: true };
} catch (err) {
if (err.start != null && err.end != null) {
// Prettier expects error objects to have loc.start and loc.end fields.
// Svelte uses start and end directly on the error.
err.loc = {
start: err.start,
end: err.end,
};
}
throw err;
}
},
preprocess: (text, options) => {
text = snipScriptAndStyleTagContent(text);
text = text.trim();
// Prettier sets the preprocessed text as the originalText in case
// the Svelte formatter is called directly. In case it's called
// as an embedded parser (for example when there's a Svelte code block
// inside markdown), the originalText is not updated after preprocessing.
// Therefore we do it ourselves here.
options.originalText = text;
return text;
},
locStart,
locEnd,
astFormat: 'svelte-ast',
},
};
export const printers: Record<string, Printer> = {
'svelte-ast': {
print,
embed,
},
};
export { options } from './options';