Skip to content

Commit

Permalink
[C] Vendor hoist-non-react-statics
Browse files Browse the repository at this point in the history
Project is outdated and unmaintained for last 5 years. We still use it
in some HOCs, so for now I've put the code (it's tiny) directly in the
repo and removed the bits supporting old React versions.
  • Loading branch information
dananjohnson committed Feb 2, 2025
1 parent 0d8f168 commit d4bcdce
Show file tree
Hide file tree
Showing 18 changed files with 125 additions and 18 deletions.
1 change: 0 additions & 1 deletion client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@
"filesize": "^10.1.6",
"focus-trap-react": "11.x",
"he": "^1.2.0",
"hoist-non-react-statics": "^3.0",
"html-react-parser": "^5.2.2",
"http-proxy-middleware": "^0.17.2",
"http-shutdown": "^1.2.2",
Expand Down
2 changes: 1 addition & 1 deletion client/src/global/components/form/setter.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from "react";
import PropTypes from "prop-types";
import hoistStatics from "hoist-non-react-statics";
import hoistStatics from "hoc/hoist-non-react-statics";
import get from "lodash/get";
import has from "lodash/has";
import withFormContext from "hoc/withFormContext";
Expand Down
2 changes: 1 addition & 1 deletion client/src/helpers/router/switchFactory.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from "react";
import PropTypes from "prop-types";
import hoistStatics from "hoist-non-react-statics";
import hoistStatics from "hoc/hoist-non-react-statics";
import { withRouter, matchPath } from "react-router-dom";

function getDisplayName(WrappedComponent) {
Expand Down
2 changes: 1 addition & 1 deletion client/src/hoc/analytics/withAnalyticsReport.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react";
import hoistStatics from "hoist-non-react-statics";
import hoistStatics from "../hoist-non-react-statics";
import { connect } from "react-redux";
import { entityStoreActions } from "actions";
import { select, meta } from "utils/entityUtils";
Expand Down
2 changes: 1 addition & 1 deletion client/src/hoc/analytics/withEventTracker.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react";
import hoistStatics from "hoist-non-react-statics";
import hoistStatics from "../hoist-non-react-statics";
import { ManifoldAnalyticsContext } from "helpers/contexts";

function getDisplayName(WrappedComponent) {
Expand Down
109 changes: 109 additions & 0 deletions client/src/hoc/hoist-non-react-statics/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/**
* Vendored (w/ small modification) from
* https://github.com/mridgway/hoist-non-react-statics/blob/main/src/index.js
* since project is unmaintained.
*
* Copyright 2015, Yahoo! Inc.
* Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
*/
import { ForwardRef, Memo } from "react";

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
};

const KNOWN_STATICS = {
name: true,
length: true,
prototype: true,
caller: true,
callee: true,
arguments: true,
arity: true
};

const FORWARD_REF_STATICS = {
$$typeof: true,
render: true,
defaultProps: true,
displayName: true,
propTypes: true
};

const MEMO_STATICS = {
$$typeof: true,
compare: true,
defaultProps: true,
displayName: true,
propTypes: true,
type: true
};

const TYPE_STATICS = {};
TYPE_STATICS[ForwardRef] = FORWARD_REF_STATICS;
TYPE_STATICS[Memo] = MEMO_STATICS;

function getStatics(component) {
return TYPE_STATICS[component["$$typeof"]] || REACT_STATICS;
}

const defineProperty = Object.defineProperty;
const getOwnPropertyNames = Object.getOwnPropertyNames;
const getOwnPropertySymbols = Object.getOwnPropertySymbols;
const getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor;
const getPrototypeOf = Object.getPrototypeOf;
const objectPrototype = Object.prototype;

export default function hoistNonReactStatics(
targetComponent,
sourceComponent,
excludelist
) {
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 = getOwnPropertyNames(sourceComponent);

if (getOwnPropertySymbols) {
keys = keys.concat(getOwnPropertySymbols(sourceComponent));
}

const targetStatics = getStatics(targetComponent);
const sourceStatics = getStatics(sourceComponent);

for (let i = 0; i < keys.length; ++i) {
const key = keys[i];
if (
!KNOWN_STATICS[key] &&
!(excludelist && excludelist[key]) &&
!(sourceStatics && sourceStatics[key]) &&
!(targetStatics && targetStatics[key])
) {
const descriptor = getOwnPropertyDescriptor(sourceComponent, key);
try {
// Avoid failures from read-only properties
defineProperty(targetComponent, key, descriptor);
} catch (e) {}
}
}
}

return targetComponent;
}
2 changes: 1 addition & 1 deletion client/src/hoc/redirectIfLibraryDisabled/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react";
import hoistStatics from "hoist-non-react-statics";
import hoistStatics from "../hoist-non-react-statics";
import withSettings from "hoc/withSettings";
import { Route } from "react-router-dom";
import frontendRoutes from "frontend/routes";
Expand Down
2 changes: 1 addition & 1 deletion client/src/hoc/withConfirmation/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { Component } from "react";
import Dialog from "global/components/dialog";
import hoistStatics from "hoist-non-react-statics";
import hoistStatics from "../hoist-non-react-statics";

function getDisplayName(WrappedComponent) {
return WrappedComponent.displayName || WrappedComponent.name || "Component";
Expand Down
2 changes: 1 addition & 1 deletion client/src/hoc/withCurrentUser/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react";
import hoistStatics from "hoist-non-react-statics";
import hoistStatics from "../hoist-non-react-statics";
import { useCurrentUser } from "hooks";

function getDisplayName(WrappedComponent) {
Expand Down
2 changes: 1 addition & 1 deletion client/src/hoc/withDispatch/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react";
import hoistStatics from "hoist-non-react-statics";
import hoistStatics from "../hoist-non-react-statics";
import { connect } from "react-redux";

function getDisplayName(WrappedComponent) {
Expand Down
2 changes: 1 addition & 1 deletion client/src/hoc/withFilteredLists/hoc.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { Component } from "react";
import hoistStatics from "hoist-non-react-statics";
import hoistStatics from "../hoist-non-react-statics";
import memoize from "lodash/memoize";
import isPlainObject from "lodash/isPlainObject";
import pickBy from "lodash/pickBy";
Expand Down
2 changes: 1 addition & 1 deletion client/src/hoc/withFormSession/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from "react";
import PropTypes from "prop-types";
import hoistStatics from "hoist-non-react-statics";
import hoistStatics from "../hoist-non-react-statics";
import { connect } from "react-redux";
import get from "lodash/get";
import has from "lodash/has";
Expand Down
2 changes: 1 addition & 1 deletion client/src/hoc/withPluginReplacement/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from "react";
import PropTypes from "prop-types";
import hoistStatics from "hoist-non-react-statics";
import hoistStatics from "../hoist-non-react-statics";
import { connect } from "react-redux";
import pluginRegistry from "services/plugin/registry";

Expand Down
2 changes: 1 addition & 1 deletion client/src/hoc/withProjectContext/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react";
import hoistStatics from "hoist-non-react-statics";
import hoistStatics from "../hoist-non-react-statics";
import { FrontendModeContext } from "helpers/contexts";
import { RegisterBreadcrumbs } from "global/components/atomic/Breadcrumbs";
import lh from "helpers/linkHandler";
Expand Down
2 changes: 1 addition & 1 deletion client/src/hoc/withReadingGroups/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from "react";
import PropTypes from "prop-types";
import { requests } from "api";
import { select, loaded } from "utils/entityUtils";
import hoistStatics from "hoist-non-react-statics";
import hoistStatics from "../hoist-non-react-statics";
import { connect } from "react-redux";
import { uiReadingGroupActions } from "actions";
import { ReaderContext } from "helpers/contexts";
Expand Down
2 changes: 1 addition & 1 deletion client/src/hoc/withScreenReaderStatus/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { Component } from "react";
import hoistStatics from "hoist-non-react-statics";
import hoistStatics from "../hoist-non-react-statics";

function getDisplayName(WrappedComponent) {
return WrappedComponent.displayName || WrappedComponent.name || "Component";
Expand Down
2 changes: 1 addition & 1 deletion client/src/hoc/withSettings/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react";
import hoistStatics from "hoist-non-react-statics";
import hoistStatics from "../hoist-non-react-statics";
import { connect } from "react-redux";
import { select } from "utils/entityUtils";
import { requests } from "api";
Expand Down
3 changes: 1 addition & 2 deletions client/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6722,7 +6722,7 @@ __metadata:
languageName: node
linkType: hard

"hoist-non-react-statics@npm:^3.0, hoist-non-react-statics@npm:^3.1.0, hoist-non-react-statics@npm:^3.3.0, hoist-non-react-statics@npm:^3.3.1, hoist-non-react-statics@npm:^3.3.2":
"hoist-non-react-statics@npm:^3.1.0, hoist-non-react-statics@npm:^3.3.0, hoist-non-react-statics@npm:^3.3.1, hoist-non-react-statics@npm:^3.3.2":
version: 3.3.2
resolution: "hoist-non-react-statics@npm:3.3.2"
dependencies:
Expand Down Expand Up @@ -8520,7 +8520,6 @@ __metadata:
filesize: "npm:^10.1.6"
focus-trap-react: "npm:11.x"
he: "npm:^1.2.0"
hoist-non-react-statics: "npm:^3.0"
html-react-parser: "npm:^5.2.2"
http-proxy-middleware: "npm:^0.17.2"
http-shutdown: "npm:^1.2.2"
Expand Down

0 comments on commit d4bcdce

Please sign in to comment.