Skip to content

Commit c14c395

Browse files
committed
fix: use user's Svelte parser when formatting if necessary
This makes formatting "just work" if users use Svelte 5 but don't have prettier (and the plugin) installed in their workspace Depends on sveltejs/prettier-plugin-svelte#471
1 parent 0698bc7 commit c14c395

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

packages/language-server/src/plugins/svelte/SveltePlugin.ts

+11-4
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,9 @@ export class SveltePlugin
127127
/**
128128
* Prettier v2 can't use v3 plugins and vice versa. Therefore, we need to check
129129
* which version of prettier is used in the workspace and import the correct
130-
* version of the Svelte plugin. If user uses Prettier >= 3 and has no Svelte plugin
131-
* then fall back to our built-in versions which are both v2 and compatible with
130+
* version of the Svelte plugin. If user uses Prettier < 3 and has no Svelte plugin
131+
* then fall back to our built-in versions which are both v3 and compatible with
132132
* each other.
133-
* TODO switch this around at some point to load Prettier v3 by default because it's
134-
* more likely that users have that installed.
135133
*/
136134
const importFittingPrettier = async () => {
137135
const getConfig = async (p: any) => {
@@ -206,6 +204,15 @@ export class SveltePlugin
206204
return [];
207205
}
208206

207+
if (isFallback || !(await hasSveltePluginLoaded(prettier, resolvedPlugins))) {
208+
// If the user uses Svelte 5 but doesn't have prettier installed, we need to provide
209+
// the compiler path to the plugin so it can use its parser method; else it will crash.
210+
const svelteCompilerInfo = getPackageInfo('svelte', filePath);
211+
if (svelteCompilerInfo.version.major >= 5) {
212+
config.svelte5CompilerPath = svelteCompilerInfo.path + '/compiler';
213+
}
214+
}
215+
209216
// Prettier v3 format is async, v2 is not
210217
const formattedCode = await prettier.format(document.getText(), {
211218
...config,

0 commit comments

Comments
 (0)