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

Commit d77574a

Browse files
iocalebsjasonLaster
authored andcommitted
Refactor findClosestScope -> findClosestFunction (#5789)
1 parent b05236c commit d77574a

File tree

4 files changed

+62
-57
lines changed

4 files changed

+62
-57
lines changed

src/components/Editor/EditorMenu.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { isOriginalId } from "devtools-source-map";
99

1010
import { copyToTheClipboard } from "../../utils/clipboard";
1111
import { findFunctionText } from "../../utils/function";
12-
import { findClosestScope } from "../../utils/breakpoint/astBreakpointLocation";
12+
import { findClosestFunction } from "../../utils/ast";
1313
import {
1414
getSourceLocationFromMouseEvent,
1515
toSourceLine
@@ -248,10 +248,10 @@ export default connect(
248248
findFunctionText(
249249
line,
250250
selectedSource.toJS(),
251-
getSymbols(state, selectedSource.toJS())
251+
getSymbols(state, selectedSource)
252252
),
253253
getFunctionLocation: line =>
254-
findClosestScope(getSymbols(state, selectedSource.toJS()).functions, {
254+
findClosestFunction(getSymbols(state, selectedSource).functions, {
255255
line,
256256
column: Infinity
257257
})

src/utils/ast.js

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,14 @@
66

77
import { without, range } from "lodash";
88

9-
import type { Source, Position } from "../types";
10-
import type { PausePoint, SymbolDeclarations } from "../workers/parser";
9+
import type { Location, Source, Position } from "../types";
10+
import type {
11+
AstPosition,
12+
AstLocation,
13+
PausePoint,
14+
SymbolDeclarations,
15+
SymbolDeclaration
16+
} from "../workers/parser";
1117

1218
export function findBestMatchExpression(
1319
symbols: SymbolDeclarations,
@@ -48,3 +54,46 @@ export function findEmptyLines(
4854
const sourceLines = range(1, lineCount + 1);
4955
return without(sourceLines, ...breakpointLines);
5056
}
57+
58+
export function containsPosition(a: AstLocation, b: AstPosition) {
59+
const startsBefore =
60+
a.start.line < b.line ||
61+
(a.start.line === b.line && a.start.column <= b.column);
62+
const endsAfter =
63+
a.end.line > b.line || (a.end.line === b.line && a.end.column >= b.column);
64+
65+
return startsBefore && endsAfter;
66+
}
67+
68+
export function findClosestFunction(
69+
functions: SymbolDeclaration[],
70+
location: Location
71+
) {
72+
return functions.reduce((found, currNode) => {
73+
if (
74+
currNode.name === "anonymous" ||
75+
!containsPosition(currNode.location, {
76+
line: location.line,
77+
column: location.column || 0
78+
})
79+
) {
80+
return found;
81+
}
82+
83+
if (!found) {
84+
return currNode;
85+
}
86+
87+
if (found.location.start.line > currNode.location.start.line) {
88+
return found;
89+
}
90+
if (
91+
found.location.start.line === currNode.location.start.line &&
92+
found.location.start.column > currNode.location.start.column
93+
) {
94+
return found;
95+
}
96+
97+
return currNode;
98+
}, null);
99+
}

src/utils/breakpoint/astBreakpointLocation.js

Lines changed: 3 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -5,59 +5,12 @@
55
// @flow
66

77
import { getSymbols } from "../../workers/parser";
8+
import { findClosestFunction } from "../ast";
89

9-
import type {
10-
AstPosition,
11-
AstLocation,
12-
SymbolDeclarations,
13-
SymbolDeclaration
14-
} from "../../workers/parser";
10+
import type { SymbolDeclarations } from "../../workers/parser";
1511

1612
import type { Location, Source, ASTLocation } from "../../types";
1713

18-
export function containsPosition(a: AstLocation, b: AstPosition) {
19-
const startsBefore =
20-
a.start.line < b.line ||
21-
(a.start.line === b.line && a.start.column <= b.column);
22-
const endsAfter =
23-
a.end.line > b.line || (a.end.line === b.line && a.end.column >= b.column);
24-
25-
return startsBefore && endsAfter;
26-
}
27-
28-
export function findClosestScope(
29-
functions: SymbolDeclaration[],
30-
location: Location
31-
) {
32-
return functions.reduce((found, currNode) => {
33-
if (
34-
currNode.name === "anonymous" ||
35-
!containsPosition(currNode.location, {
36-
line: location.line,
37-
column: location.column || 0
38-
})
39-
) {
40-
return found;
41-
}
42-
43-
if (!found) {
44-
return currNode;
45-
}
46-
47-
if (found.location.start.line > currNode.location.start.line) {
48-
return found;
49-
}
50-
if (
51-
found.location.start.line === currNode.location.start.line &&
52-
found.location.start.column > currNode.location.start.column
53-
) {
54-
return found;
55-
}
56-
57-
return currNode;
58-
}, null);
59-
}
60-
6114
export function getASTLocation(
6215
source: Source,
6316
symbols: SymbolDeclarations,
@@ -69,7 +22,7 @@ export function getASTLocation(
6922

7023
const functions = [...symbols.functions];
7124

72-
const scope = findClosestScope(functions, location);
25+
const scope = findClosestFunction(functions, location);
7326
if (scope) {
7427
// we only record the line, but at some point we may
7528
// also do column offsets

src/utils/function.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
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-
import { findClosestScope } from "./breakpoint/astBreakpointLocation";
5+
import { findClosestFunction } from "./ast";
66
import { correctIndentation } from "./indentation";
77

88
export function findFunctionText(line, source, symbols) {
9-
const func = findClosestScope(symbols.functions, { line, column: Infinity });
9+
const func = findClosestFunction(symbols.functions, {
10+
line,
11+
column: Infinity
12+
});
1013
if (!func) {
1114
return null;
1215
}

0 commit comments

Comments
 (0)