-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
chore(react): Inline hoist-non-react-statics package
#18102
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
onurtemizkan
wants to merge
6
commits into
develop
Choose a base branch
from
onur/inline-hoist-non-react-statics
base: develop
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.
+472
−16
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c1bd22d
chore(react): Inline `hoist-non-react-statics` package
onurtemizkan 1c5ab97
Fix package.json
onurtemizkan 09dd869
Update lockfile
onurtemizkan 5cf39a9
Merge branch 'develop' into onur/inline-hoist-non-react-statics
onurtemizkan 1223695
Fix package.json / lockfile
onurtemizkan b7aba33
Merge branch 'develop' into onur/inline-hoist-non-react-statics
chargome 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,8 +40,7 @@ | |
| }, | ||
| "dependencies": { | ||
| "@sentry/browser": "10.30.0", | ||
| "@sentry/core": "10.30.0", | ||
| "hoist-non-react-statics": "^3.3.2" | ||
| "@sentry/core": "10.30.0" | ||
| }, | ||
| "peerDependencies": { | ||
| "react": "^16.14.0 || 17.x || 18.x || 19.x" | ||
|
|
@@ -51,7 +50,7 @@ | |
| "@testing-library/react-hooks": "^7.0.2", | ||
| "@types/history-4": "npm:@types/[email protected]", | ||
| "@types/history-5": "npm:@types/[email protected]", | ||
| "@types/hoist-non-react-statics": "^3.3.5", | ||
| "@types/node-fetch": "^2.6.11", | ||
| "@types/react": "17.0.3", | ||
| "@types/react-router-4": "npm:@types/[email protected]", | ||
| "@types/react-router-5": "npm:@types/[email protected]", | ||
|
|
||
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 |
|---|---|---|
| @@ -1,5 +1,169 @@ | ||
| import * as hoistNonReactStaticsImport from 'hoist-non-react-statics'; | ||
| /** | ||
| * Inlined implementation of hoist-non-react-statics | ||
| * Original library: https://github.com/mridgway/hoist-non-react-statics | ||
| * License: BSD-3-Clause | ||
| * Copyright 2015, Yahoo! Inc. | ||
| * | ||
| * This is an inlined version to avoid ESM compatibility issues with the original package. | ||
| */ | ||
|
|
||
| // Ensure we use the default export from hoist-non-react-statics if available, | ||
| // falling back to the module itself. This handles both ESM and CJS usage. | ||
| export const hoistNonReactStatics = hoistNonReactStaticsImport.default || hoistNonReactStaticsImport; | ||
| import type * as React from 'react'; | ||
|
|
||
| /** | ||
| * React statics that should not be hoisted | ||
| */ | ||
| const REACT_STATICS = { | ||
| childContextTypes: true, | ||
| contextType: true, | ||
| contextTypes: true, | ||
| defaultProps: true, | ||
| displayName: true, | ||
| getDefaultProps: true, | ||
| getDerivedStateFromError: true, | ||
| getDerivedStateFromProps: true, | ||
| mixins: true, | ||
| propTypes: true, | ||
| type: true, | ||
| } as const; | ||
|
|
||
| /** | ||
| * Known JavaScript function statics that should not be hoisted | ||
| */ | ||
| const KNOWN_STATICS = { | ||
| name: true, | ||
| length: true, | ||
| prototype: true, | ||
| caller: true, | ||
| callee: true, | ||
| arguments: true, | ||
| arity: true, | ||
| } as const; | ||
|
|
||
| /** | ||
| * Statics specific to ForwardRef components | ||
| */ | ||
| const FORWARD_REF_STATICS = { | ||
| $$typeof: true, | ||
| render: true, | ||
| defaultProps: true, | ||
| displayName: true, | ||
| propTypes: true, | ||
| } as const; | ||
|
|
||
| /** | ||
| * Statics specific to Memo components | ||
| */ | ||
| const MEMO_STATICS = { | ||
| $$typeof: true, | ||
| compare: true, | ||
| defaultProps: true, | ||
| displayName: true, | ||
| propTypes: true, | ||
| type: true, | ||
| } as const; | ||
|
|
||
| /** | ||
| * Inlined react-is utilities | ||
| * We only need to detect ForwardRef and Memo types | ||
| */ | ||
| const ForwardRefType = Symbol.for('react.forward_ref'); | ||
| const MemoType = Symbol.for('react.memo'); | ||
|
|
||
| /** | ||
| * Check if a component is a Memo component | ||
| */ | ||
| function isMemo(component: unknown): boolean { | ||
| return ( | ||
| typeof component === 'object' && component !== null && (component as { $$typeof?: symbol }).$$typeof === MemoType | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Map of React component types to their specific statics | ||
| */ | ||
| const TYPE_STATICS: Record<symbol, Record<string, boolean>> = {}; | ||
| TYPE_STATICS[ForwardRefType] = FORWARD_REF_STATICS; | ||
| TYPE_STATICS[MemoType] = MEMO_STATICS; | ||
|
|
||
| /** | ||
| * Get the appropriate statics object for a given component | ||
| */ | ||
| function getStatics(component: React.ComponentType<unknown>): Record<string, boolean> { | ||
| // React v16.11 and below | ||
| if (isMemo(component)) { | ||
| return MEMO_STATICS; | ||
| } | ||
|
|
||
| // React v16.12 and above | ||
| const componentType = (component as { $$typeof?: symbol }).$$typeof; | ||
| return (componentType && TYPE_STATICS[componentType]) || REACT_STATICS; | ||
| } | ||
|
|
||
| const defineProperty = Object.defineProperty.bind(Object); | ||
| const getOwnPropertyNames = Object.getOwnPropertyNames.bind(Object); | ||
| const getOwnPropertySymbols = Object.getOwnPropertySymbols?.bind(Object); | ||
| const getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor.bind(Object); | ||
| const getPrototypeOf = Object.getPrototypeOf.bind(Object); | ||
| const objectPrototype = Object.prototype; | ||
|
|
||
| /** | ||
| * Copies non-react specific statics from a child component to a parent component. | ||
| * Similar to Object.assign, but copies all static properties from source to target, | ||
| * excluding React-specific statics and known JavaScript statics. | ||
| * | ||
| * @param targetComponent - The component to copy statics to | ||
| * @param sourceComponent - The component to copy statics from | ||
| * @param excludelist - An optional object of keys to exclude from hoisting | ||
| * @returns The target component with hoisted statics | ||
| */ | ||
| export function hoistNonReactStatics< | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| T extends React.ComponentType<any>, | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| S extends React.ComponentType<any>, | ||
| C extends Record<string, boolean> = Record<string, never>, | ||
| >(targetComponent: T, sourceComponent: S, excludelist?: C): T { | ||
| if (typeof sourceComponent !== 'string') { | ||
| // Don't hoist over string (html) components | ||
| if (objectPrototype) { | ||
| const inheritedComponent = getPrototypeOf(sourceComponent); | ||
|
|
||
| if (inheritedComponent && inheritedComponent !== objectPrototype) { | ||
| hoistNonReactStatics(targetComponent, inheritedComponent, excludelist); | ||
| } | ||
| } | ||
|
|
||
| let keys: (string | symbol)[] = getOwnPropertyNames(sourceComponent); | ||
|
|
||
| if (getOwnPropertySymbols) { | ||
| keys = keys.concat(getOwnPropertySymbols(sourceComponent)); | ||
| } | ||
|
|
||
| const targetStatics = getStatics(targetComponent); | ||
| const sourceStatics = getStatics(sourceComponent); | ||
|
|
||
| for (const key of keys) { | ||
| const keyStr = String(key); | ||
| if ( | ||
| !KNOWN_STATICS[keyStr as keyof typeof KNOWN_STATICS] && | ||
| !excludelist?.[keyStr] && | ||
| !sourceStatics?.[keyStr] && | ||
| !targetStatics?.[keyStr] && | ||
| !getOwnPropertyDescriptor(targetComponent, key) // Don't overwrite existing properties | ||
| ) { | ||
| const descriptor = getOwnPropertyDescriptor(sourceComponent, key); | ||
|
|
||
| if (descriptor) { | ||
| try { | ||
| // Avoid failures from read-only properties | ||
| defineProperty(targetComponent, key, descriptor); | ||
| } catch (e) { | ||
| // Silently ignore errors | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return targetComponent; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
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.
Bug: Unrelated
@types/node-fetchdependency accidentally addedThe
@types/node-fetchpackage was added todevDependencieswhere@types/hoist-non-react-staticswas removed. There's no usage ofnode-fetchanywhere in the react package, so this appears to be accidentally included during the edit. The dependency is unused and adds unnecessary bloat to the dev dependencies.