Skip to content

fix: use parsed source text for Prettier formatting #15764

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

JoostK
Copy link

@JoostK JoostK commented Aug 1, 2025

Summary

Jest 30 introduced support for Prettier 3+ in #14566, but this may result in mangled source code when run on source files with CRLF line endings. In particular, empty line breaks would get removed, but more complex inputs/snapshots could result in entirely broken code.

The problem occurs due to how jest-snapshot invokes Prettier using private APIs in two separate phases: the source text is parsed and formatted using individual API calls and the formatting phase takes the original options, whereas the parsing stage has operated on preprocessed options. Those preprocessed options aren't part of the return value of parse so cannot be used wholesale when calling formatAST, but the normalized source text is available and can be passed through.

Test plan

A new test project has been added which enables CRLF line endings and verifies that the formatted source file with inline snapshot has been formatted correctly. Before this change, the empty newline between the two expect(...) calls would be removed.

Copy link

linux-foundation-easycla bot commented Aug 1, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

  • ✅ login: JoostK / name: Joost Koehoorn (c632e10)

Copy link

netlify bot commented Aug 1, 2025

Deploy Preview for jestjs ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit c632e10
🔍 Latest deploy log https://app.netlify.com/projects/jestjs/deploys/688dc60eeab7520008122625
😎 Deploy Preview https://deploy-preview-15764--jestjs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

Copy link

pkg-pr-new bot commented Aug 1, 2025

Open in StackBlitz

babel-jest

npm i https://pkg.pr.new/babel-jest@15764

babel-plugin-jest-hoist

npm i https://pkg.pr.new/babel-plugin-jest-hoist@15764

babel-preset-jest

npm i https://pkg.pr.new/babel-preset-jest@15764

create-jest

npm i https://pkg.pr.new/create-jest@15764

@jest/diff-sequences

npm i https://pkg.pr.new/@jest/diff-sequences@15764

expect

npm i https://pkg.pr.new/expect@15764

@jest/expect-utils

npm i https://pkg.pr.new/@jest/expect-utils@15764

jest

npm i https://pkg.pr.new/jest@15764

jest-changed-files

npm i https://pkg.pr.new/jest-changed-files@15764

jest-circus

npm i https://pkg.pr.new/jest-circus@15764

jest-cli

npm i https://pkg.pr.new/jest-cli@15764

jest-config

npm i https://pkg.pr.new/jest-config@15764

@jest/console

npm i https://pkg.pr.new/@jest/console@15764

@jest/core

npm i https://pkg.pr.new/@jest/core@15764

@jest/create-cache-key-function

npm i https://pkg.pr.new/@jest/create-cache-key-function@15764

jest-diff

npm i https://pkg.pr.new/jest-diff@15764

jest-docblock

npm i https://pkg.pr.new/jest-docblock@15764

jest-each

npm i https://pkg.pr.new/jest-each@15764

@jest/environment

npm i https://pkg.pr.new/@jest/environment@15764

jest-environment-jsdom

npm i https://pkg.pr.new/jest-environment-jsdom@15764

@jest/environment-jsdom-abstract

npm i https://pkg.pr.new/@jest/environment-jsdom-abstract@15764

jest-environment-node

npm i https://pkg.pr.new/jest-environment-node@15764

@jest/expect

npm i https://pkg.pr.new/@jest/expect@15764

@jest/fake-timers

npm i https://pkg.pr.new/@jest/fake-timers@15764

@jest/get-type

npm i https://pkg.pr.new/@jest/get-type@15764

@jest/globals

npm i https://pkg.pr.new/@jest/globals@15764

jest-haste-map

npm i https://pkg.pr.new/jest-haste-map@15764

jest-jasmine2

npm i https://pkg.pr.new/jest-jasmine2@15764

jest-leak-detector

npm i https://pkg.pr.new/jest-leak-detector@15764

jest-matcher-utils

npm i https://pkg.pr.new/jest-matcher-utils@15764

jest-message-util

npm i https://pkg.pr.new/jest-message-util@15764

jest-mock

npm i https://pkg.pr.new/jest-mock@15764

@jest/pattern

npm i https://pkg.pr.new/@jest/pattern@15764

jest-phabricator

npm i https://pkg.pr.new/jest-phabricator@15764

jest-regex-util

npm i https://pkg.pr.new/jest-regex-util@15764

@jest/reporters

npm i https://pkg.pr.new/@jest/reporters@15764

jest-resolve

npm i https://pkg.pr.new/jest-resolve@15764

jest-resolve-dependencies

npm i https://pkg.pr.new/jest-resolve-dependencies@15764

jest-runner

npm i https://pkg.pr.new/jest-runner@15764

jest-runtime

npm i https://pkg.pr.new/jest-runtime@15764

@jest/schemas

npm i https://pkg.pr.new/@jest/schemas@15764

jest-snapshot

npm i https://pkg.pr.new/jest-snapshot@15764

@jest/snapshot-utils

npm i https://pkg.pr.new/@jest/snapshot-utils@15764

@jest/source-map

npm i https://pkg.pr.new/@jest/source-map@15764

@jest/test-result

npm i https://pkg.pr.new/@jest/test-result@15764

@jest/test-sequencer

npm i https://pkg.pr.new/@jest/test-sequencer@15764

@jest/transform

npm i https://pkg.pr.new/@jest/transform@15764

@jest/types

npm i https://pkg.pr.new/@jest/types@15764

jest-util

npm i https://pkg.pr.new/jest-util@15764

jest-validate

npm i https://pkg.pr.new/jest-validate@15764

jest-watcher

npm i https://pkg.pr.new/jest-watcher@15764

jest-worker

npm i https://pkg.pr.new/jest-worker@15764

pretty-format

npm i https://pkg.pr.new/pretty-format@15764

commit: c632e10

@JoostK JoostK force-pushed the prettier-3-mangled-inline-snapshots-crlf branch 4 times, most recently from a9c4c30 to bd66d5a Compare August 2, 2025 08:02
In Prettier's `parse` function, the input source text is preprocessed using
`normalizeInputAndOptions`. This normalizes line endings and therefore affects
the AST positions if CLRF was replaced with LF, typically in Windows environments.
Consequently, formatting the parsed AST has to use the preprocessed source text
in order for the AST positions to align, as otherwise the formatted output is
likely to be mangled.
@JoostK JoostK force-pushed the prettier-3-mangled-inline-snapshots-crlf branch from bd66d5a to c632e10 Compare August 2, 2025 08:02
`,
});
writeFiles(DIR, {
'.prettierrc': '{"endOfLine": "crlf"}',
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this file cannot exist in the to-match-inline-snapshot-crlf test directory, as it would cause Jest's own usage of Prettier to format everything in the directory to have CRLF line endings, which then get normalized to LF by git and cause Prettier checks to fail, as CRLF is expected. An alternative could be to set up a dedicated .gitattributes file in the folder to instruct Git to use CRLF for this directory wholesale, but I'd rather not check in any CRLF into Git at all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant