Skip to content
This repository was archived by the owner on Sep 21, 2021. It is now read-only.

Commit c63a534

Browse files
committed
Add a button to jump to a function definition.
Fixes #640. The Function rep would accept an onViewSourceInDebugger prop, and the icon would be shown when we do have this props as well as a location with a non-null url (so we don't show it on prototypes for example). Clicking on the button would call the prop with the location object. A test is added to make sure we handle the different possible cases.
1 parent 724543f commit c63a534

File tree

6 files changed

+109
-2
lines changed

6 files changed

+109
-2
lines changed

packages/devtools-reps/src/launchpad/components/Result.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ class Result extends Component {
8181
releaseActor,
8282
mode: MODE[modeKey],
8383
onInspectIconClick: nodeFront => console.log("inspectIcon click", nodeFront),
84+
onViewSourceInDebugger: location =>
85+
console.log("onViewSourceInDebugger", {location}),
8486
})
8587
);
8688
}

packages/devtools-reps/src/reps/function.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const {
1313
wrapRender,
1414
} = require("./rep-utils");
1515
const { MODE } = require("./constants");
16+
const Svg = require("../shared/images/Svg");
1617

1718
const dom = require("react-dom-factories");
1819
const { span } = dom;
@@ -23,10 +24,28 @@ const { span } = dom;
2324
FunctionRep.propTypes = {
2425
object: PropTypes.object.isRequired,
2526
parameterNames: PropTypes.array,
27+
onViewSourceInDebugger: PropTypes.func,
2628
};
2729

2830
function FunctionRep(props) {
29-
let grip = props.object;
31+
let {
32+
object: grip,
33+
onViewSourceInDebugger,
34+
} = props;
35+
36+
let jumpToDefinitionButton;
37+
if (onViewSourceInDebugger && grip.location && grip.location.url) {
38+
jumpToDefinitionButton = Svg("jump-definition", {
39+
element: "a",
40+
draggable: false,
41+
title: "Jump to definition",
42+
onClick: e => {
43+
// Stop the event propagation so we don't trigger ObjectInspector expand/collapse.
44+
e.stopPropagation();
45+
onViewSourceInDebugger(grip.location);
46+
}
47+
});
48+
}
3049

3150
return (
3251
span({
@@ -40,7 +59,8 @@ function FunctionRep(props) {
4059
getFunctionName(grip, props),
4160
"(",
4261
...renderParams(props),
43-
")"
62+
")",
63+
jumpToDefinitionButton,
4464
)
4565
);
4666
}

packages/devtools-reps/src/reps/reps.css

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,22 @@
212212
fill: var(--theme-highlight-blue);
213213
}
214214

215+
/******************************************************************************/
216+
/* Jump to definition button */
217+
218+
.jump-definition svg {
219+
stroke: var(--comment-node-color);
220+
height: 16px;
221+
width: 16px;
222+
margin-left: .25em;
223+
cursor: pointer;
224+
vertical-align: middle;
225+
}
226+
227+
.jump-definition svg:hover {
228+
stroke: var(--theme-highlight-blue);
229+
}
230+
215231
/******************************************************************************/
216232
/* "more…" ellipsis */
217233
.more-ellipsis {

packages/devtools-reps/src/reps/tests/function.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* License, v. 2.0. If a copy of the MPL was not distributed with this
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

5+
/* global jest */
56
const { shallow } = require("enzyme");
67
const { REPS } = require("../rep");
78
const { MODE } = require("../constants");
@@ -233,3 +234,53 @@ describe("Function - Anonymous generator function", () => {
233234
}).text()).toBe("* (a, b, c)");
234235
});
235236
});
237+
238+
describe("Function - Jump to definition", () => {
239+
it("renders an icon when onViewSourceInDebugger props is provided", () => {
240+
const onViewSourceInDebugger = jest.fn();
241+
const object = stubs.get("Named");
242+
const renderedComponent = renderRep(object, {
243+
onViewSourceInDebugger
244+
});
245+
246+
const node = renderedComponent.find(".jump-definition");
247+
node.simulate("click", {
248+
type: "click",
249+
stopPropagation: () => {},
250+
});
251+
252+
expect(node.exists()).toBeTruthy();
253+
expect(onViewSourceInDebugger.mock.calls.length).toEqual(1);
254+
expect(onViewSourceInDebugger.mock.calls[0][0]).toEqual(object.location);
255+
});
256+
257+
it("does not render an icon when onViewSourceInDebugger props is not provided", () => {
258+
const object = stubs.get("Named");
259+
const renderedComponent = renderRep(object);
260+
261+
const node = renderedComponent.find(".jump-definition");
262+
expect(node.exists()).toBeFalsy();
263+
});
264+
265+
it("does not render an icon when the object has no location", () => {
266+
const object = Object.assign({}, stubs.get("Named"));
267+
delete object.location;
268+
const renderedComponent = renderRep(object, {
269+
onViewSourceInDebugger: () => {}
270+
});
271+
272+
const node = renderedComponent.find(".jump-definition");
273+
expect(node.exists()).toBeFalsy();
274+
});
275+
276+
it("does not render an icon when the object has no url location", () => {
277+
const object = Object.assign({}, stubs.get("Named"));
278+
object.location.url = null;
279+
const renderedComponent = renderRep(object, {
280+
onViewSourceInDebugger: () => {}
281+
});
282+
283+
const node = renderedComponent.find(".jump-definition");
284+
expect(node.exists()).toBeFalsy();
285+
});
286+
});

packages/devtools-reps/src/shared/images/Svg.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import InlineSVG from "svg-inline-react";
88

99
const svg = {
1010
"open-inspector": require("./open-inspector.svg"),
11+
"jump-definition": require("./jump-definition.svg"),
1112
};
1213

1314
Svg.propTypes = {
Lines changed: 17 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)