Skip to content
This repository was archived by the owner on Jan 11, 2023. It is now read-only.

Commit 1fdb525

Browse files
committed
[breakpoints] Use symbols from store (#5191)
1 parent 322680b commit 1fdb525

File tree

3 files changed

+58
-54
lines changed

3 files changed

+58
-54
lines changed

src/actions/breakpoints/addBreakpoint.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
getASTLocation,
1010
assertLocation
1111
} from "../../utils/breakpoint";
12-
import { getSource } from "../../selectors";
12+
import { getSource, getSymbols } from "../../selectors";
1313
import { getGeneratedLocation } from "../../utils/source-maps";
1414

1515
export default async function addBreakpoint(
@@ -50,7 +50,8 @@ export default async function addBreakpoint(
5050
newGeneratedLocation
5151
);
5252

53-
const astLocation = await getASTLocation(sourceRecord, newLocation);
53+
const symbols = getSymbols(getState(), sourceRecord);
54+
const astLocation = await getASTLocation(sourceRecord, symbols, newLocation);
5455

5556
const newBreakpoint = {
5657
id,

src/utils/breakpoint/astBreakpointLocation.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { getSymbols } from "../../workers/parser";
88

99
import type { Scope, AstPosition } from "../../workers/parser/types";
1010
import type { Location, Source, ASTLocation } from "debugger-html";
11+
import type { Symbols } from "../../reducers/types";
1112

1213
export function containsPosition(a: AstPosition, b: AstPosition) {
1314
const startsBefore =
@@ -46,15 +47,15 @@ export function findClosestScope(functions: Scope[], location: Location) {
4647
}, null);
4748
}
4849

49-
export async function getASTLocation(
50+
export function getASTLocation(
5051
source: Source,
52+
symbols: Symbols,
5153
location: Location
52-
): Promise<ASTLocation> {
54+
): ASTLocation {
5355
if (source.isWasm) {
5456
return { name: undefined, offset: location };
5557
}
5658

57-
const symbols = await getSymbols(source);
5859
const functions = [...symbols.functions];
5960

6061
const scope = findClosestScope(functions, location);
Lines changed: 51 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,59 @@
11
import { getASTLocation } from "../astBreakpointLocation.js";
22
import { getSource } from "../../../workers/parser/tests/helpers";
3-
import * as I from "immutable";
4-
5-
describe("ast", () => {
6-
describe("valid location", () => {
7-
it("returns the scope and offset", async () => {
8-
const source = I.Map(getSource("math"));
9-
const location = { line: 6, column: 0 };
10-
const astLocation = await getASTLocation(source, location);
11-
expect(astLocation.name).toBe("math");
12-
expect(astLocation).toMatchSnapshot();
13-
});
3+
import getSymbols from "../../../workers/parser/getSymbols";
4+
import cases from "jest-in-case";
145

15-
it("returns name for a nested anon fn as the parent func", async () => {
16-
const source = I.Map(getSource("outOfScope"));
17-
const location = { line: 25, column: 0 };
18-
const astLocation = await getASTLocation(source, location);
19-
expect(astLocation.name).toBe("outer");
20-
expect(astLocation).toMatchSnapshot();
21-
});
6+
import * as I from "immutable";
227

23-
it("returns name for a nested named fn", async () => {
24-
const source = I.Map(getSource("outOfScope"));
25-
const location = { line: 5, column: 0 };
26-
const astLocation = await getASTLocation(source, location);
27-
expect(astLocation.name).toBe("inner");
28-
expect(astLocation).toMatchSnapshot();
29-
});
8+
async function setup({ fileName, location, functionName }) {
9+
const source = I.Map(getSource(fileName));
10+
const symbols = getSymbols(source.toJS());
3011

31-
it("returns name for an anon fn with a named variable", async () => {
32-
const source = I.Map(getSource("outOfScope"));
33-
const location = { line: 40, column: 0 };
34-
const astLocation = await getASTLocation(source, location);
35-
expect(astLocation.name).toBe("globalDeclaration");
36-
expect(astLocation).toMatchSnapshot();
37-
});
38-
});
12+
const astLocation = getASTLocation(source, symbols, location);
13+
expect(astLocation.name).toBe(functionName);
14+
expect(astLocation).toMatchSnapshot();
15+
}
3916

40-
describe("invalid location", () => {
41-
it("returns the scope name for global scope as undefined", async () => {
42-
const source = I.Map(getSource("class"));
43-
const location = { line: 10, column: 0 };
44-
const astLocation = await getASTLocation(source, location);
45-
expect(astLocation.name).toBe(undefined);
46-
expect(astLocation).toMatchSnapshot();
47-
});
17+
describe("ast", () => {
18+
cases("valid location", setup, [
19+
{
20+
name: "returns the scope and offset",
21+
fileName: "math",
22+
location: { line: 6, column: 0 },
23+
functionName: "math"
24+
},
25+
{
26+
name: "returns name for a nested anon fn as the parent func",
27+
fileName: "outOfScope",
28+
location: { line: 25, column: 0 },
29+
functionName: "outer"
30+
},
31+
{
32+
name: "returns name for a nested named fn",
33+
fileName: "outOfScope",
34+
location: { line: 5, column: 0 },
35+
functionName: "inner"
36+
},
37+
{
38+
name: "returns name for an anon fn with a named variable",
39+
fileName: "outOfScope",
40+
location: { line: 40, column: 0 },
41+
functionName: "globalDeclaration"
42+
}
43+
]);
4844

49-
it("returns name for an anon fn in global scope as undefined", async () => {
50-
const source = I.Map(getSource("outOfScope"));
51-
const location = { line: 44, column: 0 };
52-
const astLocation = await getASTLocation(source, location);
53-
expect(astLocation.name).toBe(undefined);
54-
expect(astLocation).toMatchSnapshot();
55-
});
56-
});
45+
cases("invalid location", setup, [
46+
{
47+
name: "returns the scope name for global scope as undefined",
48+
fileName: "class",
49+
location: { line: 10, column: 0 },
50+
functionName: undefined
51+
},
52+
{
53+
name: "returns name for an anon fn in global scope as undefined",
54+
fileName: "outOfScope",
55+
location: { line: 44, column: 0 },
56+
functionName: undefined
57+
}
58+
]);
5759
});

0 commit comments

Comments
 (0)