Skip to content

Commit

Permalink
fix(src): ensure deprecated config option is still supported by `pr…
Browse files Browse the repository at this point in the history
…ettierFormatter`

Addresses comment by @layershifter in #139
  • Loading branch information
Xunnamius committed Jan 18, 2023
1 parent b6b05c9 commit e48badf
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 9 deletions.
28 changes: 21 additions & 7 deletions src/formatters/prettier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,26 +40,40 @@ export type { PrettierOptions };
* @see https://prettier.io/docs/en/options.html#file-path
*/
export const prettierFormatter: ResultFormatter<{
/**
* Options passed directly to prettier, allowing you to override the defaults.
*/
prettierOptions: MaybePrettierOptions;
/**
* If this deprecated parameter is given as an argument, treat it as the value
* of `prettierOptions`. Otherwise, it should not be used.
*
* @deprecated Use `prettierOptions` instead.
*/
config: MaybePrettierOptions;
}> = (
code,
{
cwd = process.cwd(),
filename,
filepath = filename || path.join(cwd, 'dummy.js'),
prettierOptions = getCachedConfig(cwd)
config,
prettierOptions = config || getCachedConfig(cwd)
} = {}
) => {
debug('cwd: %O', cwd);
debug('filepath: %O', filepath);
debug('original code: %O', code);

const formattedCode = formatWithPrettier(code, {
const finalPrettierOptions = {
filepath,
...prettierOptions
});
};

debug('cwd: %O', cwd);
debug('filepath: %O', filepath);
debug('prettier options: %O', finalPrettierOptions);
debug('original code: %O', code);

const formattedCode = formatWithPrettier(code, finalPrettierOptions);
debug('formatted code: %O', code);

return formattedCode;
};

Expand Down
43 changes: 41 additions & 2 deletions test/unit-formatters-prettier.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,55 @@
import os from 'node:os';
import path from 'node:path';

import prettier from 'prettier';

import { prettierFormatter } from '../src/formatters/prettier';

test('uses default format with no available config', () => {
import type { AnyFunction } from '@xunnamius/jest-types';

type SpiedFunction<T extends AnyFunction> = jest.SpyInstance<
ReturnType<T>,
Parameters<T>
>;

let prettierSpy: SpiedFunction<typeof prettier.format>;

beforeEach(() => {
prettierSpy = jest.spyOn(prettier, 'format');
});

it('uses default prettier options when no user-supplied config is available', () => {
expect.hasAssertions();

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

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

const result = prettierFormatter(`var a = "hi";`);
expect(result).toBe(`var a = 'hi';\n`);
});

it('treats deprecated `filename` option as if it were `filepath`', () => {
expect.hasAssertions();

const expectedFilename = path.join(__dirname, 'fake.js');
prettierFormatter(` var a = 'hi' `, { filename: expectedFilename });

expect(prettierSpy.mock.calls).toMatchObject([
[expect.any(String), expect.objectContaining({ filepath: expectedFilename })]
]);
});

it('treats deprecated `config` option as if it were `prettierOptions`', () => {
expect.hasAssertions();

const expectedConfig = { endOfLine: 'crlf' } as const;
prettierFormatter(` var a = 'hi' `, { config: expectedConfig });

expect(prettierSpy.mock.calls).toMatchObject([
[expect.any(String), expect.objectContaining(expectedConfig)]
]);
});

0 comments on commit e48badf

Please sign in to comment.