Skip to content

Commit e48badf

Browse files
committed
fix(src): ensure deprecated config option is still supported by prettierFormatter
Addresses comment by @layershifter in #139
1 parent b6b05c9 commit e48badf

File tree

2 files changed

+62
-9
lines changed

2 files changed

+62
-9
lines changed

src/formatters/prettier.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,26 +40,40 @@ export type { PrettierOptions };
4040
* @see https://prettier.io/docs/en/options.html#file-path
4141
*/
4242
export const prettierFormatter: ResultFormatter<{
43+
/**
44+
* Options passed directly to prettier, allowing you to override the defaults.
45+
*/
4346
prettierOptions: MaybePrettierOptions;
47+
/**
48+
* If this deprecated parameter is given as an argument, treat it as the value
49+
* of `prettierOptions`. Otherwise, it should not be used.
50+
*
51+
* @deprecated Use `prettierOptions` instead.
52+
*/
53+
config: MaybePrettierOptions;
4454
}> = (
4555
code,
4656
{
4757
cwd = process.cwd(),
4858
filename,
4959
filepath = filename || path.join(cwd, 'dummy.js'),
50-
prettierOptions = getCachedConfig(cwd)
60+
config,
61+
prettierOptions = config || getCachedConfig(cwd)
5162
} = {}
5263
) => {
53-
debug('cwd: %O', cwd);
54-
debug('filepath: %O', filepath);
55-
debug('original code: %O', code);
56-
57-
const formattedCode = formatWithPrettier(code, {
64+
const finalPrettierOptions = {
5865
filepath,
5966
...prettierOptions
60-
});
67+
};
6168

69+
debug('cwd: %O', cwd);
70+
debug('filepath: %O', filepath);
71+
debug('prettier options: %O', finalPrettierOptions);
72+
debug('original code: %O', code);
73+
74+
const formattedCode = formatWithPrettier(code, finalPrettierOptions);
6275
debug('formatted code: %O', code);
76+
6377
return formattedCode;
6478
};
6579

test/unit-formatters-prettier.test.ts

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,55 @@
11
import os from 'node:os';
2+
import path from 'node:path';
3+
4+
import prettier from 'prettier';
5+
26
import { prettierFormatter } from '../src/formatters/prettier';
37

4-
test('uses default format with no available config', () => {
8+
import type { AnyFunction } from '@xunnamius/jest-types';
9+
10+
type SpiedFunction<T extends AnyFunction> = jest.SpyInstance<
11+
ReturnType<T>,
12+
Parameters<T>
13+
>;
14+
15+
let prettierSpy: SpiedFunction<typeof prettier.format>;
16+
17+
beforeEach(() => {
18+
prettierSpy = jest.spyOn(prettier, 'format');
19+
});
20+
21+
it('uses default prettier options when no user-supplied config is available', () => {
522
expect.hasAssertions();
623

724
const result = prettierFormatter(` var a = 'hi' `, { cwd: os.tmpdir() });
825
expect(result).toBe('var a = "hi";\n');
926
});
1027

11-
test('defaults all options', () => {
28+
it('uses user-supplied prettier config at project root if available (found starting at cwd)', () => {
1229
expect.hasAssertions();
1330

1431
const result = prettierFormatter(`var a = "hi";`);
1532
expect(result).toBe(`var a = 'hi';\n`);
1633
});
34+
35+
it('treats deprecated `filename` option as if it were `filepath`', () => {
36+
expect.hasAssertions();
37+
38+
const expectedFilename = path.join(__dirname, 'fake.js');
39+
prettierFormatter(` var a = 'hi' `, { filename: expectedFilename });
40+
41+
expect(prettierSpy.mock.calls).toMatchObject([
42+
[expect.any(String), expect.objectContaining({ filepath: expectedFilename })]
43+
]);
44+
});
45+
46+
it('treats deprecated `config` option as if it were `prettierOptions`', () => {
47+
expect.hasAssertions();
48+
49+
const expectedConfig = { endOfLine: 'crlf' } as const;
50+
prettierFormatter(` var a = 'hi' `, { config: expectedConfig });
51+
52+
expect(prettierSpy.mock.calls).toMatchObject([
53+
[expect.any(String), expect.objectContaining(expectedConfig)]
54+
]);
55+
});

0 commit comments

Comments
 (0)