<commit_before>import React, { Component } from "react";
import ReactDOM from "react-dom";
import invariant from "invariant";
import PropTypes from "prop-types";
import hoistStatics from "hoist-non-react-statics";

function makeComponentWrapper(
    buildMenu,
    {
        // Options object
        stopGathering = false,
        reverseOrder
    } = {}
) {
    return WrappedComponent => {
        const getDisplayName = name => `ContextMenuConnect(${name})`;
        const displayName = getDisplayName(WrappedComponent.displayName);

        class ContextMenuConnect extends Component {
            static contextTypes = {
                contextMenuContext: PropTypes.shape({
                    addMenuItems: PropTypes.func,
                    shouldGather: PropTypes.func,
                    stopGathering: PropTypes.func,
                    reverseOrder: PropTypes.func
                })
            };

            componentDidMount() {
                this.nearestNode = ReactDOM.findDOMNode(this.innerNode);
                invariant(
                    this.nearestNode,
                    "Could not find a DOM node to attach contextMenu to"
                );
                this.nearestNode.addEventListener(
                    "contextmenu",
                    this.onContextMenu
                );
            }

            componentWillUpdate() {
                this.componentWillUnmount();
            }

            componentDidUpdate() {
                this.componentDidMount();
            }

            componentWillUnmount() {
                if (this.nearestNode) {
                    this.nearestNode.removeEventListener(
                        "contextmenu",
                        this.onContextMenu
                    );
                }
            }

            onContextMenu = () => {
                // Default will be prevented by the ContextMenuProvider, after the event bubbles.
                // Other nodes have the opportunity to capture. All we need to do here is
                // build the menu.
                this.appendContextMenu();
            };

            appendContextMenu() {
                const context = this.context.contextMenuContext;
                if (context.shouldGather()) {
                    const localItems = buildMenu(this.props);
                    context.addMenuItems(localItems);
                    if (stopGathering) {
                        context.stopGathering();
                    }
                    if (typeof reverseOrder !== "undefined") {
                        context.reverseOrder(reverseOrder);
                    }
                }
            }

            render() {
                return (
                    <WrappedComponent
                        ref={el => {
                            this.innerNode = el;
                        }}
                        {...this.props}
                    />
                );
            }
        }

        ContextMenuConnect.WrappedComponent = WrappedComponent;
        ContextMenuConnect.displayName = displayName;
        return hoistStatics(ContextMenuConnect, WrappedComponent);
    };
}

export default makeComponentWrapper;<commit_msg>Improve method of setting the displayName<commit_after>import React, { Component } from "react";
import ReactDOM from "react-dom";
import invariant from "invariant";
import PropTypes from "prop-types";
import hoistStatics from "hoist-non-react-statics";

function makeComponentWrapper(
    buildMenu,
    {
        // Options object
        stopGathering = false,
        reverseOrder
    } = {}
) {
    return WrappedComponent => {
        const displayName =
            WrappedComponent.displayName ||
            WrappedComponent.name ||
            "Component";

        class ContextMenuConnect extends Component {
            static contextTypes = {
                contextMenuContext: PropTypes.shape({
                    addMenuItems: PropTypes.func,
                    shouldGather: PropTypes.func,
                    stopGathering: PropTypes.func,
                    reverseOrder: PropTypes.func
                })
            };

            static displayName = `ContextMenuConnect(${displayName})`;

            static WrappedComponent = WrappedComponent;

            componentDidMount() {
                this.nearestNode = ReactDOM.findDOMNode(this.innerNode);
                invariant(
                    this.nearestNode,
                    "Could not find a DOM node to attach contextMenu to"
                );
                this.nearestNode.addEventListener(
                    "contextmenu",
                    this.onContextMenu
                );
            }

            componentWillUpdate() {
                this.componentWillUnmount();
            }

            componentDidUpdate() {
                this.componentDidMount();
            }

            componentWillUnmount() {
                if (this.nearestNode) {
                    this.nearestNode.removeEventListener(
                        "contextmenu",
                        this.onContextMenu
                    );
                }
            }

            onContextMenu = () => {
                // Default will be prevented by the ContextMenuProvider, after the event bubbles.
                // Other nodes have the opportunity to capture. All we need to do here is
                // build the menu.
                this.appendContextMenu();
            };

            appendContextMenu() {
                const context = this.context.contextMenuContext;
                if (context.shouldGather()) {
                    const localItems = buildMenu(this.props);
                    context.addMenuItems(localItems);
                    if (stopGathering) {
                        context.stopGathering();
                    }
                    if (typeof reverseOrder !== "undefined") {
                        context.reverseOrder(reverseOrder);
                    }
                }
            }

            render() {
                return (
                    <WrappedComponent
                        ref={el => {
                            this.innerNode = el;
                        }}
                        {...this.props}
                    />
                );
            }
        }

        return hoistStatics(ContextMenuConnect, WrappedComponent);
    };
}

export default makeComponentWrapper;
{'fraction_non_alphanumeric': 0.07689927278353706, 'fraction_numerical': 0.0, 'mean_word_length': 1.206896551724138, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7195083', 'n_tokens_mistral': 1465, 'n_tokens_neox': 1352, 'n_words': 298}