Skip to content

Commit d550fbf

Browse files
authored
(fix) only provide extension version of prettier plugin if there is no workspace version (#492)
Only provide our version of the svelte plugin if the user doesn't have one in the workspace already. If we did it, Prettier would - for some reason - use the workspace version for parsing and the extension version for printing, which could crash if the contract of the parser output changed. (which it did from 1.1.0 to 1.1.1)
1 parent ed72d15 commit d550fbf

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export class SveltePlugin
6969
(await prettier.resolveConfig(filePath, { editorconfig: true })) || this.prettierConfig;
7070
const formattedCode = prettier.format(document.getText(), {
7171
...config,
72-
plugins: [require.resolve('prettier-plugin-svelte')],
72+
plugins: getSveltePlugin(),
7373
parser: 'svelte' as any,
7474
});
7575

@@ -79,6 +79,17 @@ export class SveltePlugin
7979
formattedCode,
8080
),
8181
];
82+
83+
function getSveltePlugin() {
84+
// Only provide our version of the svelte plugin if the user doesn't have one in
85+
// the workspace already. If we did it, Prettier would - for some reason - use
86+
// the workspace version for parsing and the extension version for printing,
87+
// which could crash if the contract of the parser output changed.
88+
const hasPluginLoadedAlready = prettier
89+
.getSupportInfo()
90+
.languages.some((l) => l.name === 'svelte');
91+
return hasPluginLoadedAlready ? [] : [require.resolve('prettier-plugin-svelte')];
92+
}
8293
}
8394

8495
async getCompletions(document: Document, position: Position): Promise<CompletionList | null> {

0 commit comments

Comments
 (0)