Skip to content

Commit 03c2520

Browse files
authored
(fix) Fall back to FormattingOptions if configs are empty (#565)
#539
1 parent 3a96d97 commit 03c2520

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ export class SveltePlugin
6868
const prettier = importPrettier(filePath);
6969
// Try resolving the config through prettier and fall back to possible editor config
7070
const config =
71-
(await prettier.resolveConfig(filePath, { editorconfig: true })) ||
72-
this.prettierConfig ||
71+
returnObjectIfHasKeys(await prettier.resolveConfig(filePath, { editorconfig: true })) ||
72+
returnObjectIfHasKeys(this.prettierConfig) ||
7373
// Be defensive here because IDEs other than VSCode might not have these settings
7474
(options && {
7575
tabWidth: options.tabSize,
@@ -107,6 +107,12 @@ export class SveltePlugin
107107
.languages.some((l) => l.name === 'svelte');
108108
return hasPluginLoadedAlready ? [] : [require.resolve('prettier-plugin-svelte')];
109109
}
110+
111+
function returnObjectIfHasKeys<T>(obj: T | undefined): T | undefined {
112+
if (Object.keys(obj || {}).length > 0) {
113+
return obj;
114+
}
115+
}
110116
}
111117

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

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

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ describe('Svelte Plugin', () => {
4747
});
4848

4949
describe('#formatDocument', () => {
50-
function stubPrettier(configExists: boolean) {
50+
function stubPrettier(config: any) {
5151
const formatStub = sinon.stub().returns('formatted');
5252

5353
sinon.stub(importPackage, 'importPrettier').returns(<any>{
54-
resolveConfig: () => Promise.resolve(configExists && { fromConfig: true }),
54+
resolveConfig: () => Promise.resolve(config),
5555
getFileInfo: () => ({ ignored: false }),
5656
format: formatStub,
5757
getSupportInfo: () => ({ languages: [{ name: 'svelte' }] }),
@@ -60,12 +60,9 @@ describe('Svelte Plugin', () => {
6060
return formatStub;
6161
}
6262

63-
async function testFormat(configExists: boolean, fallbackPrettierConfigExists: boolean) {
64-
const { plugin, document } = setup(
65-
'unformatted',
66-
fallbackPrettierConfigExists ? { fallbackConfig: true } : undefined,
67-
);
68-
const formatStub = stubPrettier(configExists);
63+
async function testFormat(config: any, fallbackPrettierConfig: any) {
64+
const { plugin, document } = setup('unformatted', fallbackPrettierConfig);
65+
const formatStub = stubPrettier(config);
6966

7067
const formatted = await plugin.formatDocument(document, {
7168
insertSpaces: true,
@@ -95,7 +92,7 @@ describe('Svelte Plugin', () => {
9592
});
9693

9794
it('should use config for formatting', async () => {
98-
const formatStub = await testFormat(true, true);
95+
const formatStub = await testFormat({ fromConfig: true }, { fallbackConfig: true });
9996
sinon.assert.calledOnceWithExactly(formatStub, 'unformatted', {
10097
fromConfig: true,
10198
plugins: [],
@@ -104,7 +101,7 @@ describe('Svelte Plugin', () => {
104101
});
105102

106103
it('should use prettier fallback config for formatting', async () => {
107-
const formatStub = await testFormat(false, true);
104+
const formatStub = await testFormat(undefined, { fallbackConfig: true });
108105
sinon.assert.calledOnceWithExactly(formatStub, 'unformatted', {
109106
fallbackConfig: true,
110107
plugins: [],
@@ -113,7 +110,17 @@ describe('Svelte Plugin', () => {
113110
});
114111

115112
it('should use FormattingOptions for formatting', async () => {
116-
const formatStub = await testFormat(false, false);
113+
const formatStub = await testFormat(undefined, undefined);
114+
sinon.assert.calledOnceWithExactly(formatStub, 'unformatted', {
115+
tabWidth: 4,
116+
useTabs: false,
117+
plugins: [],
118+
parser: 'svelte',
119+
});
120+
});
121+
122+
it('should use FormattingOptions for formatting when configs are empty objects', async () => {
123+
const formatStub = await testFormat({}, {});
117124
sinon.assert.calledOnceWithExactly(formatStub, 'unformatted', {
118125
tabWidth: 4,
119126
useTabs: false,

0 commit comments

Comments
 (0)