Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions src/isomorphic/ReactIntrospection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/**
* Copyright 2016-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule ReactIntrospection
*/

'use strict';

var invariant = require('invariant');

function validateInstance(instance) {
invariant(
typeof instance.mountComponent === 'function',
'ReactIntrospection: Argument is not an internal instance.'
);
}

function getChildren(instance) {
if (instance == null) {
return [];
}
validateInstance(instance);

if (instance._renderedComponent) {
if (instance._renderedComponent._currentElement) {
return [instance._renderedComponent];
} else {
return [];
}
} else if (instance._renderedChildren) {
var children = [];
for (var key in instance._renderedChildren) {
children.push(instance._renderedChildren[key]);
}
return children;
} else {
return [];
}
}

function getDisplayName(instance) {
if (instance == null) {
return 'Unknown';
}
validateInstance(instance);

var name;
if (instance.getName) {
name = instance.getName();
}
if (name) {
return name;
}
var element = instance._currentElement;
if (element == null) {
name = '#empty';
} else if (typeof element === 'string' || typeof element === 'number') {
name = '#text';
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively I can put these into getName() of ReactDOMEmptyComponent and ReactDOMTextComponent. I’m not sure which way would be preferable. This seems like a better catch-all for other renders but maybe this doesn’t matter?

} else if (typeof element.type === 'string') {
name = element.type;
} else {
name = element.type.displayName || element.type.name;
}
return name || 'Unknown';
}

function isComposite(instance) {
if (instance == null) {
return false;
}
validateInstance(instance);

var element = instance._currentElement;
if (element == null) {
return false;
}
return typeof element.type === 'function';
}

var ReactIntrospection = {
getChildren,
getDisplayName,
isComposite,
};

module.exports = ReactIntrospection;
Loading