Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use shim to detect constructor function name. #659

Merged
merged 1 commit into from
Nov 7, 2016
Merged
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"license": "MIT",
"dependencies": {
"cheerio": "^0.22.0",
"function.prototype.name": "^1.0.0",
"is-subset": "^0.1.1",
"lodash": "^4.16.4",
"object-is": "^1.0.1",
Expand Down
3 changes: 2 additions & 1 deletion src/Debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import without from 'lodash/without';
import escape from 'lodash/escape';
import compact from 'lodash/compact';
import objectValues from 'object.values';
import functionName from 'function.prototype.name';

import {
childrenOfNode,
Expand All @@ -22,7 +23,7 @@ import { REACT013 } from './version';

export function typeName(node) {
return typeof node.type === 'function'
? (node.type.displayName || node.type.name || 'Component')
? (node.type.displayName || functionName(node.type) || 'Component')
: node.type;
}

Expand Down
3 changes: 2 additions & 1 deletion src/ShallowTraversal.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';
import isEmpty from 'lodash/isEmpty';
import isSubset from 'is-subset';
import functionName from 'function.prototype.name';
import {
propsOfNode,
splitSelector,
Expand Down Expand Up @@ -143,7 +144,7 @@ export function getTextFromNode(node) {
}

if (node.type && typeof node.type === 'function') {
return `<${node.type.displayName || node.type.name} />`;
return `<${node.type.displayName || functionName(node.type)} />`;
}

return childrenOfNode(node).map(getTextFromNode)
Expand Down
9 changes: 6 additions & 3 deletions src/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import isEqual from 'lodash/isEqual';
import React from 'react';
import is from 'object-is';
import functionName from 'function.prototype.name';
import {
isDOMComponent,
findDOMNode,
Expand All @@ -24,7 +25,8 @@ export function internalInstance(inst) {
}

export function isFunctionalComponent(inst) {
return !!inst && !!inst.constructor && inst.constructor.name === 'StatelessComponent';
return !!inst && !!inst.constructor && typeof inst.constructor === 'function' &&
functionName(inst.constructor) === 'StatelessComponent';
}

export function isCustomComponentElement(inst) {
Expand Down Expand Up @@ -62,7 +64,8 @@ export function nodeHasType(node, type) {
if (!type || !node) return false;
if (!node.type) return false;
if (typeof node.type === 'string') return node.type === type;
return node.type.name === type || node.type.displayName === type;
return (typeof node.type === 'function' ?
functionName(node.type) === type : node.type.name === type) || node.type.displayName === type;
}

export function childrenEqual(a, b, lenComp) {
Expand Down Expand Up @@ -344,5 +347,5 @@ export function displayNameOfNode(node) {

if (!type) return null;

return type.displayName || type.name || type;
return type.displayName || (typeof type === 'function' ? functionName(type) : type.name) || type;
}