-
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
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
base: main
Are you sure you want to change the base?
fix: use parsed source text for Prettier formatting #15764
Conversation
|
✅ Deploy Preview for jestjs ready!Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify project configuration. |
babel-jest
babel-plugin-jest-hoist
babel-preset-jest
create-jest
@jest/diff-sequences
expect
@jest/expect-utils
jest
jest-changed-files
jest-circus
jest-cli
jest-config
@jest/console
@jest/core
@jest/create-cache-key-function
jest-diff
jest-docblock
jest-each
@jest/environment
jest-environment-jsdom
@jest/environment-jsdom-abstract
jest-environment-node
@jest/expect
@jest/fake-timers
@jest/get-type
@jest/globals
jest-haste-map
jest-jasmine2
jest-leak-detector
jest-matcher-utils
jest-message-util
jest-mock
@jest/pattern
jest-phabricator
jest-regex-util
@jest/reporters
jest-resolve
jest-resolve-dependencies
jest-runner
jest-runtime
@jest/schemas
jest-snapshot
@jest/snapshot-utils
@jest/source-map
@jest/test-result
@jest/test-sequencer
@jest/transform
@jest/types
jest-util
jest-validate
jest-watcher
jest-worker
pretty-format
commit: |
a9c4c30
to
bd66d5a
Compare
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.
bd66d5a
to
c632e10
Compare
`, | ||
}); | ||
writeFiles(DIR, { | ||
'.prettierrc': '{"endOfLine": "crlf"}', |
There was a problem hiding this comment.
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.
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 ofparse
so cannot be used wholesale when callingformatAST
, 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.