Skip to content

Commit

Permalink
feat!: upgrade to prettier@3
Browse files Browse the repository at this point in the history
BREAKING CHANGE: due to prettier@3 forcing downstream adoption of their
asynchronous interface, `ResultFormatter` is no longer synchronous and can now
return a `Promise`.

BREAKING CHANGE: adoption of prettier@3 requires some versions of Node to be
executed with the `--experimental-vm-modules` option. E.g.
`NODE_OPTIONS="--no-warnings --experimental-vm-modules" npx jest`.

BREAKING CHANGE: attempting to install babel-plugin-tester alongside jest@<30
will cause NPM to fail with `ERESOLVE`. This is because only jest@>=30
(jest-snapshot) supports the prettier@3 asynchronous interface.
  • Loading branch information
Xunnamius committed Jan 19, 2024
1 parent 9b983a1 commit 3334248
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 15 deletions.
12 changes: 9 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
"core-js": "^3.33.2",
"debug": "^4.3.4",
"lodash.mergewith": "^4.6.2",
"prettier": "^2.8.3",
"prettier": "^3.2.4",
"strip-indent": "^3.0.0"
},
"devDependencies": {
Expand Down Expand Up @@ -113,7 +113,7 @@
"execa": "^5.1.1",
"glob-gitignore": "^1.0.14",
"husky": "^8.0.3",
"jest": "^29.7.0",
"jest": "^30.0.0-alpha.2",
"jest-circus": "^29.7.0",
"jest-extended": "^4.0.2",
"lint-staged": "^15.0.2",
Expand Down Expand Up @@ -169,7 +169,13 @@
"unique-filename": "^3.0.0"
},
"peerDependencies": {
"@babel/core": ">=7.11.6"
"@babel/core": ">=7.11.6",
"jest": ">=30 || >=30.0.0-alpha.2"
},
"peerDependenciesMeta": {
"jest": {
"optional": true
}
},
"engines": {
"node": "^14.20.0 || ^16.16.0 || >=18.5.0"
Expand Down
14 changes: 7 additions & 7 deletions src/formatters/prettier.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import path from 'node:path';
import debugFactory from 'debug';
import path from 'node:path';

import {
resolveConfig as resolvePrettierConfig,
format as formatWithPrettier,
resolveConfig as resolvePrettierConfig,
type Options as PrettierOptions
} from 'prettier';

Expand All @@ -14,9 +14,9 @@ const debug = debugFactory('babel-plugin-tester:formatter');
type MaybePrettierOptions = PrettierOptions | null;
const configDirectoryCache: Record<string, MaybePrettierOptions> = Object.create(null);

const getCachedConfig = (filepath: string) => {
const getCachedConfig = async (filepath: string) => {
if (!(filepath in configDirectoryCache)) {
configDirectoryCache[filepath] = resolvePrettierConfig.sync(filepath);
configDirectoryCache[filepath] = await resolvePrettierConfig(filepath);
debug(
`caching prettier configuration resolved from ${filepath}: %O`,
configDirectoryCache[filepath]
Expand Down Expand Up @@ -51,7 +51,7 @@ export const prettierFormatter: ResultFormatter<{
* @deprecated Use `prettierOptions` instead.
*/
config: MaybePrettierOptions;
}> = (
}> = async (
code,
{
cwd = process.cwd(),
Expand All @@ -63,15 +63,15 @@ export const prettierFormatter: ResultFormatter<{
) => {
const finalPrettierOptions = {
filepath,
...prettierOptions
...(await prettierOptions)
};

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

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

return formattedCode;
Expand Down
2 changes: 1 addition & 1 deletion src/plugin-tester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1198,7 +1198,7 @@ function pluginTester(options: PluginTesterOptions = {}) {
// ? erroneously captured when we only really care about errors thrown by
// ? babel
const result = trimAndFixLineEndings(
formatResult(rawBabelOutput || '', {
await formatResult(rawBabelOutput || '', {
cwd: formatResultFilepath ? path.dirname(formatResultFilepath) : undefined,
filepath: formatResultFilepath,
filename: formatResultFilepath
Expand Down
8 changes: 4 additions & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import type { Class, Promisable } from 'type-fest';
import type { $type } from './symbols';

import type {
validEndOfLineValues,
validTitleNumberingValues,
runPluginUnderTestHere,
runPresetUnderTestHere
runPresetUnderTestHere,
validEndOfLineValues,
validTitleNumberingValues
} from './plugin-tester';

/**
Expand Down Expand Up @@ -691,7 +691,7 @@ export type ResultFormatter<
*/
filename?: string;
} & Partial<AdditionalOptions>
) => string;
) => Promise<string> | string;

// * The transitive dependency "pretty-format" is a dependency of Jest
export type { Plugin as SnapshotSerializer } from 'pretty-format';
Expand Down

0 comments on commit 3334248

Please sign in to comment.