-
Notifications
You must be signed in to change notification settings - Fork 41
fix: guard React transition runtime APIs #562
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
Draft
cursor
wants to merge
1
commit into
main
Choose a base branch
from
cursor/critical-bug-investigation-4439
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| "use client"; | ||
|
|
||
| import * as React from "react"; | ||
| import type { ViewTransitionClass } from "react"; | ||
|
|
||
| type ReactTransitionRuntime = typeof React & { | ||
| addTransitionType?: (type: string) => void; | ||
| ViewTransition?: React.ComponentType<OptionalViewTransitionProps>; | ||
| }; | ||
|
|
||
| type OptionalViewTransitionProps = { | ||
| children: React.ReactNode; | ||
| name?: string; | ||
| enter?: ViewTransitionClass; | ||
| exit?: ViewTransitionClass; | ||
| update?: ViewTransitionClass; | ||
| share?: ViewTransitionClass | string; | ||
| default?: string; | ||
| }; | ||
|
|
||
| const reactTransitionRuntime = React as ReactTransitionRuntime; | ||
|
|
||
| export function addTransitionType(type: string) { | ||
| reactTransitionRuntime.addTransitionType?.(type); | ||
| } | ||
|
|
||
| export function OptionalViewTransition(props: OptionalViewTransitionProps) { | ||
| const ViewTransition = reactTransitionRuntime.ViewTransition; | ||
|
|
||
| if (typeof ViewTransition !== "function") { | ||
| return <>{props.children}</>; | ||
| } | ||
|
|
||
| return <ViewTransition {...props} />; | ||
| } |
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
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
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
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
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import { readFileSync } from "node:fs"; | ||
| import { join, relative } from "node:path"; | ||
| import React from "react"; | ||
|
|
||
| const repoRoot = process.cwd(); | ||
|
|
||
| const filesToCheck = [ | ||
| "app/(app)/home/course-search.tsx", | ||
| "app/components/command-palette.tsx", | ||
| "app/components/common/directional-transition.tsx", | ||
| "app/components/mobile-tab-bar.tsx", | ||
| "app/components/nav-bar.tsx", | ||
| "app/components/notes/notes-course-search.tsx", | ||
| "app/components/past_papers/answer-key-toggle.tsx", | ||
| "app/components/past_papers/course-paper-grid.tsx", | ||
| "app/components/past_papers/filter-bar.tsx", | ||
| "app/components/past_papers/past-papers-course-search.tsx", | ||
| "app/components/past_papers/sort-dropdown.tsx", | ||
| "app/components/voice/voice-agent-provider.tsx", | ||
| ]; | ||
|
|
||
| const unsafeRuntimeImportPattern = | ||
| /import\s+(?!type\b)(?:[\s\S]*?\{[\s\S]*?\b(?:addTransitionType|ViewTransition)\b[\s\S]*?\}[\s\S]*?|(?:addTransitionType|ViewTransition))\s+from\s+["']react["'];?/m; | ||
|
|
||
| const reactRuntime = React as typeof React & { | ||
| addTransitionType?: unknown; | ||
| ViewTransition?: unknown; | ||
| }; | ||
|
|
||
| const failures: string[] = []; | ||
|
|
||
| for (const file of filesToCheck) { | ||
| const absolutePath = join(repoRoot, file); | ||
| const source = readFileSync(absolutePath, "utf8"); | ||
|
|
||
| if (unsafeRuntimeImportPattern.test(source)) { | ||
| failures.push(relative(repoRoot, absolutePath)); | ||
| } | ||
| } | ||
|
|
||
| console.log( | ||
| [ | ||
| `addTransitionType=${typeof reactRuntime.addTransitionType}`, | ||
| `ViewTransition=${typeof reactRuntime.ViewTransition}`, | ||
| `Activity=${typeof React.Activity}`, | ||
| ].join(" "), | ||
| ); | ||
|
|
||
| if (failures.length > 0) { | ||
| throw new Error( | ||
| `Unsafe React transition runtime imports found:\n${failures | ||
| .map((file) => `- ${file}`) | ||
| .join("\n")}`, | ||
| ); | ||
| } | ||
|
|
||
| console.log("No unsafe React transition runtime imports found."); | ||
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.
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.
This fixed list does not cover future client modules. A temporary unlisted client module containing
import { addTransitionType } from "react"was added, but this checker still exited successfully withNo unsafe React transition runtime imports found.The check is also absent from the configured package and CI commands, so that unsafe import can be introduced without an automatic failure. Discover applicable client source files instead of maintaining an allowlist, and run the check from an enforced command.Artifacts
Temporary unsafe React import fixture reproduction script
Configured package and CI invocation search output
Unsafe unlisted client fixture checker output