Skip to content

Commit 855ead8

Browse files
committed
chore: provide option to pass Svelte compiler
Tooling can use this to work around some Svelte major version differences
1 parent 10d387a commit 855ead8

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export interface PluginConfig {
66
svelteBracketNewLine?: boolean;
77
svelteAllowShorthand?: boolean;
88
svelteIndentScriptAndStyle?: boolean;
9+
svelte5CompilerPath?: string;
910
}
1011

1112
export type PrettierConfig = PluginConfig & Config;

src/index.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,12 @@ export const languages: Partial<SupportLanguage>[] = [
3131
export const parsers: Record<string, Parser> = {
3232
svelte: {
3333
hasPragma,
34-
parse: (text) => {
34+
parse: async (text, options: ParserOptions) => {
3535
try {
36-
return <ASTNode>{ ...parse(text), __isRoot: true };
36+
const _parse = options.svelte5CompilerPath
37+
? (await import(options.svelte5CompilerPath)).parse
38+
: parse;
39+
return <ASTNode>{ ..._parse(text), __isRoot: true };
3740
} catch (err: any) {
3841
if (err.start != null && err.end != null) {
3942
// Prettier expects error objects to have loc.start and loc.end fields.
@@ -57,8 +60,9 @@ export const parsers: Record<string, Parser> = {
5760
// Therefore we do it ourselves here.
5861
options.originalText = text;
5962
// Only Svelte 5 can have TS in the template
60-
options._svelte_ts = isSvelte5Plus && result.isTypescript;
61-
options._svelte_is5Plus = isSvelte5Plus;
63+
const is = !!options.svelte5CompilerPath || isSvelte5Plus;
64+
options._svelte_ts = is && result.isTypescript;
65+
options._svelte_is5Plus = is;
6266
return text;
6367
},
6468
locStart,

src/options.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ function makeChoice(choice: string) {
1919
}
2020

2121
export const options: Record<keyof PluginConfig, SupportOption> = {
22+
svelte5CompilerPath: {
23+
category: 'Svelte',
24+
type: 'string',
25+
default: '',
26+
description: 'Only set this when using Svelte 5! Path to the Svelte 5 compiler',
27+
},
2228
svelteSortOrder: {
2329
category: 'Svelte',
2430
type: 'choice',

0 commit comments

Comments
 (0)