react / react-0.13.3 / examples / basic-commonjs / node_modules / reactify / node_modules / react-tools / vendor / fbtransform / transforms / reactDisplayName.js
81158 views/**1* Copyright 2013-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/*global exports:true*/9"use strict";1011var Syntax = require('jstransform').Syntax;12var utils = require('jstransform/src/utils');1314function addDisplayName(displayName, object, state) {15if (object &&16object.type === Syntax.CallExpression &&17object.callee.type === Syntax.MemberExpression &&18object.callee.object.type === Syntax.Identifier &&19object.callee.object.name === 'React' &&20object.callee.property.type === Syntax.Identifier &&21object.callee.property.name === 'createClass' &&22object['arguments'].length === 1 &&23object['arguments'][0].type === Syntax.ObjectExpression) {24// Verify that the displayName property isn't already set25var properties = object['arguments'][0].properties;26var safe = properties.every(function(property) {27var value = property.key.type === Syntax.Identifier ?28property.key.name :29property.key.value;30return value !== 'displayName';31});3233if (safe) {34utils.catchup(object['arguments'][0].range[0] + 1, state);35utils.append('displayName: "' + displayName + '",', state);36}37}38}3940/**41* Transforms the following:42*43* var MyComponent = React.createClass({44* render: ...45* });46*47* into:48*49* var MyComponent = React.createClass({50* displayName: 'MyComponent',51* render: ...52* });53*54* Also catches:55*56* MyComponent = React.createClass(...);57* exports.MyComponent = React.createClass(...);58* module.exports = {MyComponent: React.createClass(...)};59*/60function visitReactDisplayName(traverse, object, path, state) {61var left, right;6263if (object.type === Syntax.AssignmentExpression) {64left = object.left;65right = object.right;66} else if (object.type === Syntax.Property) {67left = object.key;68right = object.value;69} else if (object.type === Syntax.VariableDeclarator) {70left = object.id;71right = object.init;72}7374if (left && left.type === Syntax.MemberExpression) {75left = left.property;76}77if (left && left.type === Syntax.Identifier) {78addDisplayName(left.name, right, state);79}80}8182visitReactDisplayName.test = function(object, path, state) {83return (84object.type === Syntax.AssignmentExpression ||85object.type === Syntax.Property ||86object.type === Syntax.VariableDeclarator87);88};8990exports.visitorList = [91visitReactDisplayName92];939495