Skip to content

[POC] Better debugging stack traces #3586

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

Closed
wants to merge 1 commit into from
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
18 changes: 18 additions & 0 deletions examples/quadratic/example.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
var Foo = React.createClass({
render: function() {
debugger;
return <div>{this.props.children}</div>;
}
});

var Z = React.createClass({
render: function() {
return <Foo>
a: {this.props.a},
b: {this.props.b},
c: {this.props.c}
</Foo>;
}
});

var QuadraticCalculator = React.createClass({
getInitialState: function() {
return {
Expand Down Expand Up @@ -31,6 +48,7 @@ var QuadraticCalculator = React.createClass({
<em>ax</em><sup>2</sup> + <em>bx</em> + <em>c</em> = 0
</strong>
<h4>Solve for <em>x</em>:</h4>
<Z a={a} b={b} c={c} />
<p>
<label>
a: <input type="number" value={a} onChange={this.handleInputChange.bind(null, 'a')} />
Expand Down
5 changes: 5 additions & 0 deletions src/browser/ui/React.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,15 @@ var warning = require('warning');
ReactDefaultInjection.inject();

var createElement = ReactElement.createElement;
var createElementDebug = function(stackHelper, ...args) {
return ReactElement.createElement.apply(ReactElement, args);
};
var createFactory = ReactElement.createFactory;
var cloneElement = ReactElement.cloneElement;

if (__DEV__) {
createElement = ReactElementValidator.createElement;
createElementDebug = ReactElementValidator.createElementDebug;
createFactory = ReactElementValidator.createFactory;
cloneElement = ReactElementValidator.cloneElement;
}
Expand All @@ -61,6 +65,7 @@ var React = {
PropTypes: ReactPropTypes,
createClass: ReactClass.createClass,
createElement: createElement,
createElementDebug: createElementDebug,
cloneElement: cloneElement,
createFactory: createFactory,
createMixin: function(mixin) {
Expand Down
6 changes: 5 additions & 1 deletion src/classic/element/ReactElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,11 @@ var ReactElement = function(type, key, ref, owner, context, props) {
// an external backing store so that we can freeze the whole object.
// This can be replaced with a WeakMap once they are implemented in
// commonly used development environments.
this._store = {props: props, originalProps: assign({}, props)};
this._store = {
props: props,
originalProps: assign({}, props),
stackHelper: null
};

// To make comparing ReactElements easier for testing purposes, we make
// the validation flag non-enumerable (where possible, which should
Expand Down
6 changes: 6 additions & 0 deletions src/classic/element/ReactElementValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,12 @@ var ReactElementValidator = {
return element;
},

createElementDebug: function(stackHelper, ...args) {
var element = ReactElementValidator.createElement.apply(null, args);
element._store.stackHelper = stackHelper;
return element;
},

createFactory: function(type) {
var validatedFactory = ReactElementValidator.createElement.bind(
null,
Expand Down
16 changes: 15 additions & 1 deletion src/core/ReactReconciler.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
'use strict';

var ReactRef = require('ReactRef');
var ReactElement = require('ReactElement');
var ReactElementValidator = require('ReactElementValidator');

/**
Expand All @@ -35,7 +36,20 @@ var ReactReconciler = {
* @internal
*/
mountComponent: function(internalInstance, rootID, transaction, context) {
var markup = internalInstance.mountComponent(rootID, transaction, context);
var markup;
if (__DEV__) {
var el = internalInstance._currentElement;
if (el._store && el._store.stackHelper) {
el._store.stackHelper(function() {
markup =
internalInstance.mountComponent(rootID, transaction, context);
});
} else {
markup = internalInstance.mountComponent(rootID, transaction, context);
}
} else {
markup = internalInstance.mountComponent(rootID, transaction, context);
}
if (__DEV__) {
ReactElementValidator.checkAndWarnForMutatedProps(
internalInstance._currentElement
Expand Down
13 changes: 12 additions & 1 deletion vendor/fbtransform/transforms/react.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,19 @@ function visitReactTag(traverse, object, path, state) {
throw new Error('Namespace tags are not supported. ReactJSX is not XML.');
}

var name;
if (nameObject.type === Syntax.JSXIdentifier) {
name = nameObject.name;
} else {
name = state.g.source.slice(nameObject.range[0], nameObject.range[1])
.replace(/[^a-z0-9]+/gi, '_');
}

// We assume that the React runtime is already in scope
utils.append('React.createElement(', state);
utils.append(
'React.createElementDebug(function element_' + name + '(x){x();},',
state
);

if (nameObject.type === Syntax.JSXIdentifier && isTagName(nameObject.name)) {
utils.append('"' + nameObject.name + '"', state);
Expand Down