From ce00c5bc8e3439a2b2b2da9ee90aa22ba1fdb96b Mon Sep 17 00:00:00 2001 From: liuxingbaoyu <30521560+liuxingbaoyu@users.noreply.github.com> Date: Sun, 24 Sep 2023 19:52:23 +0800 Subject: [PATCH] fix --- constraints.pro | 4 +++- packages/jest-snapshot/package.json | 4 ++-- packages/jest-snapshot/src/utils.ts | 8 ++++---- packages/jest-snapshot/src/worker.ts | 9 ++++++++- yarn.lock | 4 ++-- 5 files changed, 19 insertions(+), 10 deletions(-) diff --git a/constraints.pro b/constraints.pro index 62235e22de6b..d40b057ca82b 100644 --- a/constraints.pro +++ b/constraints.pro @@ -22,7 +22,9 @@ gen_enforced_dependency(WorkspaceCwd, DependencyIdent, DependencyRange2, Depende % @types/node in the root need to stay on ~14.14.45 '@types/node', % upgrading the entire repository is a breaking change - 'glob' + 'glob', + % repository and snapshot + 'prettier' ]). % Enforces that a dependency doesn't appear in both `dependencies` and `devDependencies` diff --git a/packages/jest-snapshot/package.json b/packages/jest-snapshot/package.json index 3db971cec781..be7cec46a21e 100644 --- a/packages/jest-snapshot/package.json +++ b/packages/jest-snapshot/package.json @@ -35,7 +35,7 @@ "jest-message-util": "workspace:*", "jest-util": "workspace:*", "natural-compare": "^1.4.0", - "pretty-format": "workspace:^", + "pretty-format": "workspace:*", "semver": "^7.5.3", "synckit": "^0.8.5" }, @@ -61,4 +61,4 @@ "publishConfig": { "access": "public" } -} +} \ No newline at end of file diff --git a/packages/jest-snapshot/src/utils.ts b/packages/jest-snapshot/src/utils.ts index 62199667978b..4d0ece527a74 100644 --- a/packages/jest-snapshot/src/utils.ts +++ b/packages/jest-snapshot/src/utils.ts @@ -426,7 +426,7 @@ export const processInlineSnapshotsWithBabel = ( export const processPrettierAst = ( ast: File, - options: Record, + options: Record | null, snapshotMatcherNames: Array, keepNode?: boolean, ): void => { @@ -463,16 +463,16 @@ export const processPrettierAst = ( ? parent.loc.start.column : callee.loc.start.column; - const useSpaces = !options.useTabs; + const useSpaces = !options?.useTabs; snapshot = indent( snapshot, Math.ceil( useSpaces - ? startColumn / (options.tabWidth ?? 1) + ? startColumn / (options?.tabWidth ?? 1) : // Each tab is 2 characters. startColumn / 2, ), - useSpaces ? ' '.repeat(options.tabWidth ?? 1) : '\t', + useSpaces ? ' '.repeat(options?.tabWidth ?? 1) : '\t', ); if (keepNode) { diff --git a/packages/jest-snapshot/src/worker.ts b/packages/jest-snapshot/src/worker.ts index fdcaef235ce6..74dfab8fdcf2 100644 --- a/packages/jest-snapshot/src/worker.ts +++ b/packages/jest-snapshot/src/worker.ts @@ -1,3 +1,10 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + import {runAsWorker} from 'synckit'; import {processPrettierAst} from './utils'; @@ -38,7 +45,7 @@ runAsWorker( originalText: sourceFileWithSnapshots, parser: inferredParser, }); - processPrettierAst(ast, config!, snapshotMatcherNames, true); + processPrettierAst(ast, config, snapshotMatcherNames, true); // Snapshots have now been inserted. Run prettier to make sure that the code is // formatted, except snapshot indentation. Snapshots cannot be formatted until // after the initial format because we don't know where the call expression diff --git a/yarn.lock b/yarn.lock index b0d8d2e4d8db..fbcd0d08a277 100644 --- a/yarn.lock +++ b/yarn.lock @@ -13094,7 +13094,7 @@ __metadata: natural-compare: ^1.4.0 prettier: ^3.0.3 prettier-v2: "npm:prettier@^2.1.5" - pretty-format: "workspace:^" + pretty-format: "workspace:*" semver: ^7.5.3 synckit: ^0.8.5 tsd-lite: ^0.8.0 @@ -17346,7 +17346,7 @@ __metadata: languageName: node linkType: hard -"pretty-format@workspace:*, pretty-format@workspace:^, pretty-format@workspace:packages/pretty-format": +"pretty-format@workspace:*, pretty-format@workspace:packages/pretty-format": version: 0.0.0-use.local resolution: "pretty-format@workspace:packages/pretty-format" dependencies: