Skip to content
This repository was archived by the owner on Jun 26, 2020. It is now read-only.

Commit f2fe9fd

Browse files
authored
Handle & render the _source property (#406)
* 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) * add readme line * fix type check * remove `shortFilePath`
1 parent 6ffa5db commit f2fe9fd

File tree

5 files changed

+45
-0
lines changed

5 files changed

+45
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ Tools or use a pre-prelease version.
2525

2626
## Usage
2727

28+
### Supporting tools
29+
30+
- The babel plugin [trasnform-react-jsx-source](https://github.com/babel/babel/tree/master/packages/babel-plugin-transform-react-jsx-source) is required if you want react devtools to tell you the source file & line number of created react elements. It's display in the bottom of the right panel if the information is present.
31+
2832
### Tree View
2933

3034
- Arrow keys or hjkl for navigation

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;
@@ -114,6 +116,7 @@ function getData(element: Object): DataType {
114116
type,
115117
key,
116118
ref,
119+
source,
117120
name,
118121
props,
119122
state,

backend/getData012.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ function getData012(element: Object): DataType {
9191
type,
9292
key,
9393
ref,
94+
source: null,
9495
name,
9596
props,
9697
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: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ var React = require('react');
2020
var decorate = require('./decorate');
2121
var invariant = require('./invariant');
2222

23+
2324
class PropState extends React.Component {
2425
getChildContext() {
2526
return {
@@ -29,6 +30,23 @@ class PropState extends React.Component {
2930
};
3031
}
3132

33+
renderSource(): ?React.Element {
34+
var source = this.props.node.get('source');
35+
if (!source) {
36+
return null;
37+
}
38+
return (
39+
<div style={styles.source}>
40+
<div style={styles.sourceName}>
41+
{source.fileName}
42+
</div>
43+
<div style={styles.sourcePos}>
44+
:{source.lineNumber}
45+
</div>
46+
</div>
47+
);
48+
}
49+
3250
render(): React.Element {
3351
if (!this.props.node) {
3452
// TODO(jared): style this
@@ -129,6 +147,8 @@ class PropState extends React.Component {
129147
</DetailPaneSection>}
130148
{this.props.extraPanes &&
131149
this.props.extraPanes.map(fn => fn && fn(this.props.node, this.props.id))}
150+
<div style={{flex: 1}} />
151+
{this.renderSource()}
132152
</DetailPane>
133153
);
134154
}
@@ -171,4 +191,20 @@ var WrappedPropState = decorate({
171191
},
172192
}, PropState);
173193

194+
var styles = {
195+
source: {
196+
padding: '5px 10px',
197+
display: 'flex',
198+
flexDirection: 'row',
199+
},
200+
201+
sourceName: {
202+
color: 'blue',
203+
},
204+
205+
sourcePos: {
206+
color: '#777',
207+
},
208+
};
209+
174210
module.exports = WrappedPropState;

0 commit comments

Comments
 (0)