File tree 3 files changed +25
-4
lines changed
3 files changed +25
-4
lines changed Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ export interface PluginConfig {
6
6
svelteBracketNewLine ?: boolean ;
7
7
svelteAllowShorthand ?: boolean ;
8
8
svelteIndentScriptAndStyle ?: boolean ;
9
+ svelte5CompilerPath ?: string ;
9
10
}
10
11
11
12
export type PrettierConfig = PluginConfig & Config ;
Original file line number Diff line number Diff line change @@ -31,9 +31,22 @@ export const languages: Partial<SupportLanguage>[] = [
31
31
export const parsers : Record < string , Parser > = {
32
32
svelte : {
33
33
hasPragma,
34
- parse : ( text ) => {
34
+ parse : async ( text , options : ParserOptions ) => {
35
35
try {
36
- return < ASTNode > { ...parse ( text ) , __isRoot : true } ;
36
+ let _parse = parse ;
37
+ if ( options . svelte5CompilerPath ) {
38
+ try {
39
+ _parse = ( await import ( options . svelte5CompilerPath ) ) . parse ;
40
+ } catch ( e ) {
41
+ console . warn (
42
+ `Failed to load Svelte 5 compiler from ${ options . svelte5CompilerPath } ` ,
43
+ ) ;
44
+ console . warn ( e ) ;
45
+ options . svelte5CompilerPath = undefined ;
46
+ }
47
+ }
48
+
49
+ return < ASTNode > { ..._parse ( text ) , __isRoot : true } ;
37
50
} catch ( err : any ) {
38
51
if ( err . start != null && err . end != null ) {
39
52
// Prettier expects error objects to have loc.start and loc.end fields.
@@ -57,8 +70,9 @@ export const parsers: Record<string, Parser> = {
57
70
// Therefore we do it ourselves here.
58
71
options . originalText = text ;
59
72
// Only Svelte 5 can have TS in the template
60
- options . _svelte_ts = isSvelte5Plus && result . isTypescript ;
61
- options . _svelte_is5Plus = isSvelte5Plus ;
73
+ const is = ! ! options . svelte5CompilerPath || isSvelte5Plus ;
74
+ options . _svelte_ts = is && result . isTypescript ;
75
+ options . _svelte_is5Plus = is ;
62
76
return text ;
63
77
} ,
64
78
locStart,
Original file line number Diff line number Diff line change @@ -19,6 +19,12 @@ function makeChoice(choice: string) {
19
19
}
20
20
21
21
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
+ } ,
22
28
svelteSortOrder : {
23
29
category : 'Svelte' ,
24
30
type : 'choice' ,
You can’t perform that action at this time.
0 commit comments