<commit_before>'use strict';
var React = require('react/addons');
var TestUtils = React.addons.TestUtils;

/**
 * Renders a component. Method signature is the same as
 * https://facebook.github.io/react/docs/top-level-api.html#react.createelement
 *
 * @method renderComponent
 * @param {ReactComponent} Component class
 * @param {Object} props used to initialize the componen
 * @param {Object|Object[]} children children to populate inside Component
 * @return {ReactElement} rendered react element
 **/
function renderComponent() {
    var element = React.createElement.apply(React, arguments);
    return TestUtils.renderIntoDocument(element);
}

/**
 * Simulates an event triggered on given element
 *
 * @method simulateEvent
 * @param {ReactElement or DOMNode} element that will trigger the event
 * @param {String} event that will be triggered
 **/
function simulateEvent(element, event) {
    TestUtils.Simulate[event](
       React.findDOMNode(element) || element
    );
}

/**
 * Simulates an native event on given element
 *
 * @method simulateNativeEvent
 * @param {ReactElement or DOMNode} element that will trigger the event
 * @param {String} event that will be triggered
 **/
function simulateNativeEvent(element, event) {
    TestUtils.SimulateNative[event](
       React.findDOMNode(element) || element
    );
}

/**
 * Element by query selector
 *
 * @method elementQuerySelector
 * @param {ReactElement} element node where you will search
 * @param {string} query css selector
 * @return {DOMNode} the first DOM node that matches the query
 **/
function elementQuerySelector(element, query) {
    return React.findDOMNode(element).querySelector(query);
}

/**
 * Elements by query selector
 *
 * @method elementQuerySelectorAll
 * @param {ReactElement} element node where you will search
 * @param {String} query css selector
 * @return {DOMNode} all DOM nodes that matches the query
 **/
function elementQuerySelectorAll(element, query) {
    return React.findDOMNode(element).querySelectorAll(query);
}

/**
 * Creates a ComponentStub
 *
 * @method stubComponent
 * @param {ReactElement|String} tag element to create
 * @param {Object} assertProps raises an error if component props don't match
 * @return {ReactComponent} ComponentStub
 **/
function stubComponent(tag, children, showDataProps) {
    return React.createClass({
        displayName: tag.displayName || tag,

        getStubProps: function () {
            var props = this.props;

            return Object.keys(props).reduce(function (clonedProps, key) {
                clonedProps[key] = props[key];
                if (showDataProps) {
                    clonedProps['data-' + key.toLowerCase()] = props[key];
                }
                return clonedProps;
            }, {});
        },

        render: function () {
            return React.createElement(tag, this.getStubProps(), children);
        }
    });
}

/**
 * wraps component in a context creating component
 *
 * @method withcontext
 * @param {reactcomponent} component class
 * @param {object} context the context to be passed to component
 * @return {reactcomponent} react class with context setup
 **/
function withContext(Component, context) {
    var childContextTypes = {};

    // Do not use hasOwnProperty, we need all keys from the entire prototype chain
    for (var key in context) {
        childContextTypes[key] = React.PropTypes.any;
    }

    return React.createClass({
        displayName: (Component.displayName || 'Component') + ':withContext',
        childContextTypes: childContextTypes,
        getDefaultProps: function () {
            return {
                ref: 'child'
            };
        },
        runChildMethod: function (name, args) {
            var child = this.refs.child;
            return child[name].apply(child, args);
        },
        getChildContext: function () {
            return context;
        },
        render: function () {
            return React.createElement(Component, this.props);
        }
    });
}

/**
 * Compiles jsx files on require
 * this method is not needed if you are using babel
 *
 * @method jsxTranspile
 * @param {Boolean} instrument the original code
 **/
function jsxTranspile(instrument) {
    var fs = require('fs');
    var babel = require('babel');

    if (instrument && instrument === 'true') {
        var istanbul = require('istanbul');
        var coverageVariable = Object.keys(global).filter(
            function (key) {
                return /^\$\$cov_/.test(key);
            })[0];
        var instrumenter = new istanbul.Instrumenter({
                coverageVariable: coverageVariable
            });
        var content;
        var compiled;

        require.extensions['.jsx'] = function (module, filename) {
            if (/(tests|node_modules)/.test(filename)) {
                // No instrument
                content = fs.readFileSync(filename, 'utf8');
                compiled = babel.transform(content, {nonStandard: true, retainLines: true});
                return module._compile(compiled.code, filename);
            }
            else {
                // Instrument
                content = fs.readFileSync(filename, 'utf8');
                compiled = babel.transform(content, {nonStandard: true, retainLines: true});
                var instrumented = instrumenter.instrumentSync(compiled.code, filename);
                return module._compile(instrumented, filename);
            }
        };
    }
    else {
        require.extensions['.jsx'] = function (module, filename) {
            var content = fs.readFileSync(filename, 'utf8');
            var compiled = babel.transform(content, {nonStandard: true, retainLines: true});
            return module._compile(compiled.code, filename);
        };
    }

    return this;
}

/**
 * @module helper
 **/
module.exports = {
    simulateEvent: simulateEvent,
    simulateNativeEvent: simulateNativeEvent,
    renderComponent: renderComponent,
    elementQuerySelector: elementQuerySelector,
    elementQuerySelectorAll: elementQuerySelectorAll,
    stubComponent: stubComponent,
    withContext: withContext,
    jsxTranspile: jsxTranspile
};<commit_msg>simplifying the jsxTranspile to get rid of else statement.<commit_after>'use strict';
var React = require('react/addons');
var TestUtils = React.addons.TestUtils;

/**
 * Renders a component. Method signature is the same as
 * https://facebook.github.io/react/docs/top-level-api.html#react.createelement
 *
 * @method renderComponent
 * @param {ReactComponent} Component class
 * @param {Object} props used to initialize the componen
 * @param {Object|Object[]} children children to populate inside Component
 * @return {ReactElement} rendered react element
 **/
function renderComponent() {
    var element = React.createElement.apply(React, arguments);
    return TestUtils.renderIntoDocument(element);
}

/**
 * Simulates an event triggered on given element
 *
 * @method simulateEvent
 * @param {ReactElement or DOMNode} element that will trigger the event
 * @param {String} event that will be triggered
 **/
function simulateEvent(element, event) {
    TestUtils.Simulate[event](
       React.findDOMNode(element) || element
    );
}

/**
 * Simulates an native event on given element
 *
 * @method simulateNativeEvent
 * @param {ReactElement or DOMNode} element that will trigger the event
 * @param {String} event that will be triggered
 **/
function simulateNativeEvent(element, event) {
    TestUtils.SimulateNative[event](
       React.findDOMNode(element) || element
    );
}

/**
 * Element by query selector
 *
 * @method elementQuerySelector
 * @param {ReactElement} element node where you will search
 * @param {string} query css selector
 * @return {DOMNode} the first DOM node that matches the query
 **/
function elementQuerySelector(element, query) {
    return React.findDOMNode(element).querySelector(query);
}

/**
 * Elements by query selector
 *
 * @method elementQuerySelectorAll
 * @param {ReactElement} element node where you will search
 * @param {String} query css selector
 * @return {DOMNode} all DOM nodes that matches the query
 **/
function elementQuerySelectorAll(element, query) {
    return React.findDOMNode(element).querySelectorAll(query);
}

/**
 * Creates a ComponentStub
 *
 * @method stubComponent
 * @param {ReactElement|String} tag element to create
 * @param {Object} assertProps raises an error if component props don't match
 * @return {ReactComponent} ComponentStub
 **/
function stubComponent(tag, children, showDataProps) {
    return React.createClass({
        displayName: tag.displayName || tag,

        getStubProps: function () {
            var props = this.props;

            return Object.keys(props).reduce(function (clonedProps, key) {
                clonedProps[key] = props[key];
                if (showDataProps) {
                    clonedProps['data-' + key.toLowerCase()] = props[key];
                }
                return clonedProps;
            }, {});
        },

        render: function () {
            return React.createElement(tag, this.getStubProps(), children);
        }
    });
}

/**
 * wraps component in a context creating component
 *
 * @method withcontext
 * @param {reactcomponent} component class
 * @param {object} context the context to be passed to component
 * @return {reactcomponent} react class with context setup
 **/
function withContext(Component, context) {
    var childContextTypes = {};

    // Do not use hasOwnProperty, we need all keys from the entire prototype chain
    for (var key in context) {
        childContextTypes[key] = React.PropTypes.any;
    }

    return React.createClass({
        displayName: (Component.displayName || 'Component') + ':withContext',
        childContextTypes: childContextTypes,
        getDefaultProps: function () {
            return {
                ref: 'child'
            };
        },
        runChildMethod: function (name, args) {
            var child = this.refs.child;
            return child[name].apply(child, args);
        },
        getChildContext: function () {
            return context;
        },
        render: function () {
            return React.createElement(Component, this.props);
        }
    });
}

/**
 * Compiles jsx files on require
 * this method is not needed if you are using babel
 *
 * @method jsxTranspile
 * @param {Boolean} instrument the original code
 **/
function jsxTranspile(instrument) {
    var fs = require('fs');
    var babel = require('babel');

    require.extensions['.jsx'] = function (module, filename) {
        var content = fs.readFileSync(filename, 'utf8');
        var compiled = babel.transform(content, {nonStandard: true, retainLines: true});

        if (instrument && instrument === 'true' && !/(tests|node_modules)/.test(filename)) {
            var istanbul = require('istanbul');
            var coverageVariable = Object.keys(global).filter(
                function (key) {
                    return /^\$\$cov_/.test(key);
                })[0];
            var instrumenter = new istanbul.Instrumenter({
                    coverageVariable: coverageVariable
                });
            var content = fs.readFileSync(filename, 'utf8');
            var compiled = babel.transform(content, {nonStandard: true, retainLines: true});
            var instrumented = instrumenter.instrumentSync(compiled.code, filename);

            return module._compile(instrumented, filename);
        }

        return module._compile(compiled.code, filename);
    };

    return this;
}

/**
 * @module helper
 **/
module.exports = {
    simulateEvent: simulateEvent,
    simulateNativeEvent: simulateNativeEvent,
    renderComponent: renderComponent,
    elementQuerySelector: elementQuerySelector,
    elementQuerySelectorAll: elementQuerySelectorAll,
    stubComponent: stubComponent,
    withContext: withContext,
    jsxTranspile: jsxTranspile
};
{'fraction_non_alphanumeric': 0.10744423092990225, 'fraction_numerical': 0.0005848441808004011, 'mean_word_length': 2.8563144329896906, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '2826490', 'n_tokens_mistral': 3154, 'n_tokens_neox': 3107, 'n_words': 910}