fix(snapshot): make inline comment-skip regex non-greedy#10594
Open
martypdx wants to merge 1 commit into
Open
Conversation
When multiple `toMatchInlineSnapshot` / `toThrowErrorMatchingInlineSnapshot` calls in one file carry block-comment directives (e.g. `/* HTML */`), the greedy comment-skip `\/\*[\s\S]*\*\/` in `defaultStartObjectRegex` and `defaultStartRegex` matched from the first call's `/*` to the LAST call's `*/`. On `--update`, every earlier call's write coalesced onto the last call's location: only the final snapshot was written — mangled, e.g. `toMatchInlineSnapshot(/* HTML */`"a"``"b"``"c"`)` — and the earlier calls were left empty. Silently, since the run still reports success. Make the comment-skip lazy (`[\s\S]*?`) in both regexes. A block comment cannot contain `*/`, so the lazy form is equivalent for valid input. Adds an e2e regression (fixtures/inline-multiple-calls/comment-directive + the inline-comment-directive driver) that fails before this change and passes after.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Multiple
toMatchInlineSnapshot/toThrowErrorMatchingInlineSnapshotcalls in a single file that carry block-comment directives (e.g./* HTML */) corrupt each other on--update: only the last snapshot is written — mangled — and the earlier ones are silently left empty.Root cause. The comment-skip alternative in
defaultStartObjectRegexanddefaultStartRegex(packages/snapshot/src/port/inlineSnapshot.ts) uses a greedy\/\*[\s\S]*\*\/. When several such calls share a file, the per-call match runs from that call's/*to the last call's*/, so each earlier call's computed start index lands inside the last call.MagicStringthen coalesces the overlappingoverwrite()s onto that one location. The run still reports success, so the corruption is silent.Repro (
vitest --update):Before — only the last call writes, mangled;
a/bare left empty:After — each call writes on its own line:
Fix. Make the comment-skip lazy (
[\s\S]*→[\s\S]*?) in both regexes. A block comment cannot legally contain*/, so the lazy form is equivalent for valid input — it simply stops at each call's own comment close.Related: #8632 is a different comment-position case (a comment between the method name and
(), single call); it is not addressed by this change.Tests
Adds an e2e regression —
test/e2e/snapshots/inline-comment-directive.test.tswith fixturetest/e2e/snapshots/fixtures/inline-multiple-calls/comment-directive.test.ts. It fails before this change (onlygammawritten, mangled) and passes after.Verified locally:
main, passes with the fix.inline-multiple-calls,soft-inline,domain-inlinee2e suites: pass.snapshot-inlinesuite: pass.pnpm-lock.yamlchanges.