From 855ead8b54c070f287568ad32d8b4187372a448d Mon Sep 17 00:00:00 2001 From: Simon Holthausen Date: Sat, 9 Nov 2024 20:55:42 +0100 Subject: [PATCH 1/2] chore: provide option to pass Svelte compiler Tooling can use this to work around some Svelte major version differences --- index.d.ts | 1 + src/index.ts | 12 ++++++++---- src/options.ts | 6 ++++++ 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/index.d.ts b/index.d.ts index 9c8975a..1adfd0e 100644 --- a/index.d.ts +++ b/index.d.ts @@ -6,6 +6,7 @@ export interface PluginConfig { svelteBracketNewLine?: boolean; svelteAllowShorthand?: boolean; svelteIndentScriptAndStyle?: boolean; + svelte5CompilerPath?: string; } export type PrettierConfig = PluginConfig & Config; diff --git a/src/index.ts b/src/index.ts index e661d5f..7c10c61 100644 --- a/src/index.ts +++ b/src/index.ts @@ -31,9 +31,12 @@ export const languages: Partial[] = [ export const parsers: Record = { svelte: { hasPragma, - parse: (text) => { + parse: async (text, options: ParserOptions) => { try { - return { ...parse(text), __isRoot: true }; + const _parse = options.svelte5CompilerPath + ? (await import(options.svelte5CompilerPath)).parse + : parse; + return { ..._parse(text), __isRoot: true }; } catch (err: any) { if (err.start != null && err.end != null) { // Prettier expects error objects to have loc.start and loc.end fields. @@ -57,8 +60,9 @@ export const parsers: Record = { // Therefore we do it ourselves here. options.originalText = text; // Only Svelte 5 can have TS in the template - options._svelte_ts = isSvelte5Plus && result.isTypescript; - options._svelte_is5Plus = isSvelte5Plus; + const is = !!options.svelte5CompilerPath || isSvelte5Plus; + options._svelte_ts = is && result.isTypescript; + options._svelte_is5Plus = is; return text; }, locStart, diff --git a/src/options.ts b/src/options.ts index 18d65fd..f5c504b 100644 --- a/src/options.ts +++ b/src/options.ts @@ -19,6 +19,12 @@ function makeChoice(choice: string) { } export const options: Record = { + svelte5CompilerPath: { + category: 'Svelte', + type: 'string', + default: '', + description: 'Only set this when using Svelte 5! Path to the Svelte 5 compiler', + }, svelteSortOrder: { category: 'Svelte', type: 'choice', From 2a39313bf0d61bdaf9daaac4daa9bee823941b45 Mon Sep 17 00:00:00 2001 From: Simon Holthausen Date: Sat, 9 Nov 2024 20:58:35 +0100 Subject: [PATCH 2/2] make robust --- src/index.ts | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/index.ts b/src/index.ts index 7c10c61..dea7ba6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -33,9 +33,19 @@ export const parsers: Record = { hasPragma, parse: async (text, options: ParserOptions) => { try { - const _parse = options.svelte5CompilerPath - ? (await import(options.svelte5CompilerPath)).parse - : parse; + let _parse = parse; + if (options.svelte5CompilerPath) { + try { + _parse = (await import(options.svelte5CompilerPath)).parse; + } catch (e) { + console.warn( + `Failed to load Svelte 5 compiler from ${options.svelte5CompilerPath}`, + ); + console.warn(e); + options.svelte5CompilerPath = undefined; + } + } + return { ..._parse(text), __isRoot: true }; } catch (err: any) { if (err.start != null && err.end != null) {