@samitouri / QOS-React-2 / commits / d270e2b1c7

Don't add displayName in transform if already defined

This ensures we don't add displayName in the transform if it's already been defined. This is especially important when in strict mode, where duplicate properties in an object is an exception.

Nick Thompson committed Jan 3, 2014 at 16:07 UTC d270e2b1c700ab44f1343c743800fa7e5d5b6c62
1 file changed +21 -11
vendor/fbtransform/transforms/reactDisplayName.js
+21 -11
@@ -34,7 +34,18 @@ var utils = require('jstransform/src/utils');
34 * });
35 */
36 function visitReactDisplayName(traverse, object, path, state) {
37 - if (object.id.type === Syntax.Identifier &&
37 + var displayName = object.id.name;
38 + utils.catchup(object.init['arguments'][0].range[0] + 1, state);
39 + utils.append("displayName: '" + displayName + "',", state);
40 +}
41 +
42 +/**
43 + * Will only run on @jsx files for now.
44 + */
45 +visitReactDisplayName.test = function(object, path, state) {
46 + if (!!utils.getDocblock(state).jsx &&
47 + object.type === Syntax.VariableDeclarator &&
48 + object.id.type === Syntax.Identifier &&
49 object.init &&
50 object.init.type === Syntax.CallExpression &&
51 object.init.callee.type === Syntax.MemberExpression &&
@@ -45,17 +56,16 @@ function visitReactDisplayName(traverse, object, path, state) {
56 object.init['arguments'].length === 1 &&
57 object.init['arguments'][0].type === Syntax.ObjectExpression) {
58
48 - var displayName = object.id.name;
49 - utils.catchup(object.init['arguments'][0].range[0] + 1, state);
50 - utils.append("displayName: '" + displayName + "',", state);
59 + // Verify that the displayName property isn't already set
60 + var properties = object.init['arguments'][0].properties;
61 + return properties.reduce(function (safe, property) {
62 + var value = property.key.type === 'Identifier'
63 + ? property.key.name
64 + : property.key.value;
65 + return safe && value !== 'displayName';
66 + }, true);
67 }
52 -}
53 -
54 -/**
55 - * Will only run on @jsx files for now.
56 - */
57 -visitReactDisplayName.test = function(object, path, state) {
58 - return object.type === Syntax.VariableDeclarator && !!utils.getDocblock(state).jsx;
68 + return false;
69 };
70
71 exports.visitReactDisplayName = visitReactDisplayName;