react / react-0.13.3 / examples / basic-commonjs / node_modules / reactify / node_modules / react-tools / src / core / ReactLegacyElement.js
81152 views/**1* Copyright 2014, Facebook, Inc.2* All rights reserved.3*4* This source code is licensed under the BSD-style license found in the5* LICENSE file in the root directory of this source tree. An additional grant6* of patent rights can be found in the PATENTS file in the same directory.7*8* @providesModule ReactLegacyElement9*/1011"use strict";1213var ReactCurrentOwner = require('ReactCurrentOwner');1415var invariant = require('invariant');16var monitorCodeUse = require('monitorCodeUse');17var warning = require('warning');1819var legacyFactoryLogs = {};20function warnForLegacyFactoryCall() {21if (!ReactLegacyElementFactory._isLegacyCallWarningEnabled) {22return;23}24var owner = ReactCurrentOwner.current;25var name = owner && owner.constructor ? owner.constructor.displayName : '';26if (!name) {27name = 'Something';28}29if (legacyFactoryLogs.hasOwnProperty(name)) {30return;31}32legacyFactoryLogs[name] = true;33warning(34false,35name + ' is calling a React component directly. ' +36'Use a factory or JSX instead. See: http://fb.me/react-legacyfactory'37);38monitorCodeUse('react_legacy_factory_call', { version: 3, name: name });39}4041function warnForPlainFunctionType(type) {42var isReactClass =43type.prototype &&44typeof type.prototype.mountComponent === 'function' &&45typeof type.prototype.receiveComponent === 'function';46if (isReactClass) {47warning(48false,49'Did not expect to get a React class here. Use `Component` instead ' +50'of `Component.type` or `this.constructor`.'51);52} else {53if (!type._reactWarnedForThisType) {54try {55type._reactWarnedForThisType = true;56} catch (x) {57// just incase this is a frozen object or some special object58}59monitorCodeUse(60'react_non_component_in_jsx',61{ version: 3, name: type.name }62);63}64warning(65false,66'This JSX uses a plain function. Only React components are ' +67'valid in React\'s JSX transform.'68);69}70}7172function warnForNonLegacyFactory(type) {73warning(74false,75'Do not pass React.DOM.' + type.type + ' to JSX or createFactory. ' +76'Use the string "' + type.type + '" instead.'77);78}7980/**81* Transfer static properties from the source to the target. Functions are82* rebound to have this reflect the original source.83*/84function proxyStaticMethods(target, source) {85if (typeof source !== 'function') {86return;87}88for (var key in source) {89if (source.hasOwnProperty(key)) {90var value = source[key];91if (typeof value === 'function') {92var bound = value.bind(source);93// Copy any properties defined on the function, such as `isRequired` on94// a PropTypes validator.95for (var k in value) {96if (value.hasOwnProperty(k)) {97bound[k] = value[k];98}99}100target[key] = bound;101} else {102target[key] = value;103}104}105}106}107108// We use an object instead of a boolean because booleans are ignored by our109// mocking libraries when these factories gets mocked.110var LEGACY_MARKER = {};111var NON_LEGACY_MARKER = {};112113var ReactLegacyElementFactory = {};114115ReactLegacyElementFactory.wrapCreateFactory = function(createFactory) {116var legacyCreateFactory = function(type) {117if (typeof type !== 'function') {118// Non-function types cannot be legacy factories119return createFactory(type);120}121122if (type.isReactNonLegacyFactory) {123// This is probably a factory created by ReactDOM we unwrap it to get to124// the underlying string type. It shouldn't have been passed here so we125// warn.126if (__DEV__) {127warnForNonLegacyFactory(type);128}129return createFactory(type.type);130}131132if (type.isReactLegacyFactory) {133// This is probably a legacy factory created by ReactCompositeComponent.134// We unwrap it to get to the underlying class.135return createFactory(type.type);136}137138if (__DEV__) {139warnForPlainFunctionType(type);140}141142// Unless it's a legacy factory, then this is probably a plain function,143// that is expecting to be invoked by JSX. We can just return it as is.144return type;145};146return legacyCreateFactory;147};148149ReactLegacyElementFactory.wrapCreateElement = function(createElement) {150var legacyCreateElement = function(type, props, children) {151if (typeof type !== 'function') {152// Non-function types cannot be legacy factories153return createElement.apply(this, arguments);154}155156var args;157158if (type.isReactNonLegacyFactory) {159// This is probably a factory created by ReactDOM we unwrap it to get to160// the underlying string type. It shouldn't have been passed here so we161// warn.162if (__DEV__) {163warnForNonLegacyFactory(type);164}165args = Array.prototype.slice.call(arguments, 0);166args[0] = type.type;167return createElement.apply(this, args);168}169170if (type.isReactLegacyFactory) {171// This is probably a legacy factory created by ReactCompositeComponent.172// We unwrap it to get to the underlying class.173if (type._isMockFunction) {174// If this is a mock function, people will expect it to be called. We175// will actually call the original mock factory function instead. This176// future proofs unit testing that assume that these are classes.177type.type._mockedReactClassConstructor = type;178}179args = Array.prototype.slice.call(arguments, 0);180args[0] = type.type;181return createElement.apply(this, args);182}183184if (__DEV__) {185warnForPlainFunctionType(type);186}187188// This is being called with a plain function we should invoke it189// immediately as if this was used with legacy JSX.190return type.apply(null, Array.prototype.slice.call(arguments, 1));191};192return legacyCreateElement;193};194195ReactLegacyElementFactory.wrapFactory = function(factory) {196invariant(197typeof factory === 'function',198'This is suppose to accept a element factory'199);200var legacyElementFactory = function(config, children) {201// This factory should not be called when JSX is used. Use JSX instead.202if (__DEV__) {203warnForLegacyFactoryCall();204}205return factory.apply(this, arguments);206};207proxyStaticMethods(legacyElementFactory, factory.type);208legacyElementFactory.isReactLegacyFactory = LEGACY_MARKER;209legacyElementFactory.type = factory.type;210return legacyElementFactory;211};212213// This is used to mark a factory that will remain. E.g. we're allowed to call214// it as a function. However, you're not suppose to pass it to createElement215// or createFactory, so it will warn you if you do.216ReactLegacyElementFactory.markNonLegacyFactory = function(factory) {217factory.isReactNonLegacyFactory = NON_LEGACY_MARKER;218return factory;219};220221// Checks if a factory function is actually a legacy factory pretending to222// be a class.223ReactLegacyElementFactory.isValidFactory = function(factory) {224// TODO: This will be removed and moved into a class validator or something.225return typeof factory === 'function' &&226factory.isReactLegacyFactory === LEGACY_MARKER;227};228229ReactLegacyElementFactory.isValidClass = function(factory) {230if (__DEV__) {231warning(232false,233'isValidClass is deprecated and will be removed in a future release. ' +234'Use a more specific validator instead.'235);236}237return ReactLegacyElementFactory.isValidFactory(factory);238};239240ReactLegacyElementFactory._isLegacyCallWarningEnabled = true;241242module.exports = ReactLegacyElementFactory;243244245