|
1 | 1 | import { |
2 | 2 | childrenOfNode, |
3 | 3 | } from './ShallowTraversal'; |
| 4 | +import { |
| 5 | + internalInstance, |
| 6 | + renderedChildrenOfInst, |
| 7 | +} from './MountedTraversal'; |
| 8 | +import { |
| 9 | + isDOMComponent, |
| 10 | + isCompositeComponent, |
| 11 | + isElement, |
| 12 | +} from './react-compat'; |
4 | 13 | import { |
5 | 14 | propsOfNode, |
6 | 15 | } from './Utils'; |
7 | 16 | import { without, escape, compact } from 'underscore'; |
| 17 | +import { REACT013, REACT014 } from './version'; |
| 18 | +import objectValues from 'object.values'; |
8 | 19 |
|
9 | 20 | export function typeName(node) { |
10 | 21 | return typeof node.type === 'function' |
@@ -63,3 +74,59 @@ export function debugNode(node, indentLength = 2) { |
63 | 74 | export function debugNodes(nodes) { |
64 | 75 | return nodes.map(debugNode).join('\n\n\n'); |
65 | 76 | } |
| 77 | + |
| 78 | +export function debugInst(inst, indentLength = 2) { |
| 79 | + if (typeof inst === 'string' || typeof inst === 'number') return escape(inst); |
| 80 | + if (!inst) return ''; |
| 81 | + |
| 82 | + if (!inst.getPublicInstance) { |
| 83 | + const internal = internalInstance(inst); |
| 84 | + return debugInst(internal, indentLength); |
| 85 | + } |
| 86 | + |
| 87 | + const publicInst = inst.getPublicInstance(); |
| 88 | + |
| 89 | + if (typeof publicInst === 'string' || typeof publicInst === 'number') return escape(publicInst); |
| 90 | + if (!publicInst) return ''; |
| 91 | + |
| 92 | + // do stuff with publicInst |
| 93 | + const currentElement = inst._currentElement; |
| 94 | + const type = typeName(currentElement); |
| 95 | + const props = propsString(currentElement); |
| 96 | + const children = []; |
| 97 | + if (isDOMComponent(publicInst)) { |
| 98 | + const renderedChildren = renderedChildrenOfInst(inst); |
| 99 | + if (!renderedChildren) { |
| 100 | + children.push(...childrenOfNode(currentElement)); |
| 101 | + } else { |
| 102 | + children.push(...objectValues(renderedChildren)); |
| 103 | + } |
| 104 | + } else if ( |
| 105 | + REACT014 && |
| 106 | + isElement(currentElement) && |
| 107 | + typeof currentElement.type === 'function' |
| 108 | + ) { |
| 109 | + children.push(inst._renderedComponent); |
| 110 | + } else if ( |
| 111 | + REACT013 && |
| 112 | + isCompositeComponent(publicInst) |
| 113 | + ) { |
| 114 | + children.push(inst._renderedComponent); |
| 115 | + } |
| 116 | + |
| 117 | + const childrenStrs = compact(children.map(n => debugInst(n, indentLength))); |
| 118 | + |
| 119 | + const beforeProps = props ? ' ' : ''; |
| 120 | + const nodeClose = childrenStrs.length ? `</${type}>` : '/>'; |
| 121 | + const afterProps = childrenStrs.length |
| 122 | + ? '>' |
| 123 | + : ' '; |
| 124 | + const childrenIndented = childrenStrs.length |
| 125 | + ? `\n${childrenStrs.map(x => indent(indentLength + 2, x)).join('\n')}\n` |
| 126 | + : ''; |
| 127 | + return `<${type}${beforeProps}${props}${afterProps}${childrenIndented}${nodeClose}`; |
| 128 | +} |
| 129 | + |
| 130 | +export function debugInsts(insts) { |
| 131 | + return insts.map(debugInst).join('\n\n\n'); |
| 132 | +} |
0 commit comments