Skip to content

Commit 39860e6

Browse files
committed
Handle & render the _source property
To take advantage of this, use the [transform-react-jsx-source](https://github.com/babel/babel/tree/master/packages/babel-plugin-transform-react-jsx-source) babel plugin. The `_source` property was added to react [last year](facebook/react@009b7c8) and can be used by development tools to track the source file & line of react elements. React Native is also [taking advantage of it](facebook/react-native#6351). Test Plan: - install the [transform-react-jsx-source](https://github.com/babel/babel/tree/master/packages/babel-plugin-transform-react-jsx-source) babel plugin - open the react devtools to an element that was created in your app (can't have been created by a library you're using, because those are probably pre-transpiled & ignored by babel)
1 parent 18bbe55 commit 39860e6

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

backend/getData.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ function getData(element: Object): DataType {
2626
var type = null;
2727
var key = null;
2828
var ref = null;
29+
var source = null;
2930
var text = null;
3031
var publicInstance = null;
3132
var nodeType = 'Native';
@@ -65,6 +66,7 @@ function getData(element: Object): DataType {
6566
if (element._currentElement.key) {
6667
key = String(element._currentElement.key);
6768
}
69+
source = element._currentElement._source;
6870
ref = element._currentElement.ref;
6971
if (typeof type === 'string') {
7072
name = type;
@@ -111,6 +113,7 @@ function getData(element: Object): DataType {
111113
type,
112114
key,
113115
ref,
116+
source,
114117
name,
115118
props,
116119
state,

backend/types.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export type DataType = {
1515
type: ?(string | AnyFn),
1616
key: ?string,
1717
ref: ?(string | AnyFn),
18+
source: ?Object,
1819
name: ?string,
1920
props: ?Object,
2021
state: ?Object,

frontend/PropState.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,18 @@ var React = require('react');
2020
var decorate = require('./decorate');
2121
var invariant = require('./invariant');
2222

23+
/**
24+
* Shorten a file path to something reasonable.
25+
* - if it has `/src/` in it, assume that's the project root
26+
* - if it starts w/ `/Users/name/`, replace that with `~/`
27+
*/
28+
var shortFilePath = text => {
29+
if (text.match(/\/src\//)) {
30+
return 'src/' + text.split(/\/src\//)[1];
31+
}
32+
return text.replace(/^\/Users\/[^\/]+\//, '~/');
33+
};
34+
2335
class PropState extends React.Component {
2436
getChildContext() {
2537
return {
@@ -29,6 +41,23 @@ class PropState extends React.Component {
2941
};
3042
}
3143

44+
renderSource(): React.Element {
45+
var source = this.props.node.get('source');
46+
if (!source) {
47+
return null;
48+
}
49+
return (
50+
<div style={styles.source}>
51+
<div style={styles.sourceName}>
52+
{shortFilePath(source.fileName)}
53+
</div>
54+
<div style={styles.sourcePos}>
55+
:{source.lineNumber}
56+
</div>
57+
</div>
58+
);
59+
}
60+
3261
render(): React.Element {
3362
if (!this.props.node) {
3463
// TODO(jared): style this
@@ -129,6 +158,8 @@ class PropState extends React.Component {
129158
</DetailPaneSection>}
130159
{this.props.extraPanes &&
131160
this.props.extraPanes.map(fn => fn && fn(this.props.node, this.props.id))}
161+
<div style={{flex: 1}} />
162+
{this.renderSource()}
132163
</DetailPane>
133164
);
134165
}
@@ -171,4 +202,20 @@ var WrappedPropState = decorate({
171202
},
172203
}, PropState);
173204

205+
var styles = {
206+
source: {
207+
padding: '5px 10px',
208+
display: 'flex',
209+
flexDirection: 'row',
210+
},
211+
212+
sourceName: {
213+
color: 'blue',
214+
},
215+
216+
sourcePos: {
217+
color: '#777',
218+
},
219+
};
220+
174221
module.exports = WrappedPropState;

0 commit comments

Comments
 (0)