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

Commit ddcb297

Browse files
y9127jasonLaster
authored andcommitted
Improve parser flow coverage (#5766)
1 parent ae2b851 commit ddcb297

File tree

19 files changed

+62
-255
lines changed

19 files changed

+62
-255
lines changed

src/actions/preview.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import { getMappedExpression } from "./expressions";
2424
import { isEqual } from "lodash";
2525

2626
import type { ThunkArgs } from "./types";
27-
import type { AstLocation } from "../workers/parser";
27+
import type { Range, Position } from "../types";
2828

2929
async function getReactProps(evaluate) {
3030
const reactDisplayName = await evaluate(
@@ -142,8 +142,8 @@ export function updatePreview(target: HTMLElement, editor: any) {
142142

143143
export function setPreview(
144144
expression: string,
145-
location: AstLocation,
146-
tokenPos: AstLocation,
145+
location: Range,
146+
tokenPos: Position,
147147
cursorPos: any
148148
) {
149149
return async ({ dispatch, getState, client, sourceMaps }: ThunkArgs) => {

src/components/Editor/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ import EmptyLines from "./EmptyLines";
4141
import GutterMenu from "./GutterMenu";
4242
import EditorMenu from "./EditorMenu";
4343
import ConditionalPanel from "./ConditionalPanel";
44-
import type { SymbolDeclarations } from "../../workers/parser/types";
44+
import type { SymbolDeclarations } from "../../workers/parser";
4545

4646
import {
4747
showSourceText,

src/reducers/ast.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import type { Action } from "../actions/types";
2626
import type { Record } from "../utils/makeRecord";
2727

2828
type EmptyLinesType = number[];
29-
export type Symbols = SymbolDeclarations | { loading: true };
29+
export type Symbols = SymbolDeclarations | {| loading: true |};
3030
export type SymbolsMap = Map<string, Symbols>;
3131
export type EmptyLinesMap = Map<string, EmptyLinesType>;
3232

@@ -148,10 +148,7 @@ function update(
148148
// https://github.com/devtools-html/debugger.html/blob/master/src/reducers/sources.js#L179-L185
149149
type OuterState = { ast: Record<ASTState> };
150150

151-
export function getSymbols(
152-
state: OuterState,
153-
source: Source
154-
): ?SymbolDeclarations {
151+
export function getSymbols(state: OuterState, source: Source): ?Symbols {
155152
if (!source) {
156153
return null;
157154
}
@@ -166,7 +163,7 @@ export function hasSymbols(state: OuterState, source: Source): boolean {
166163
return false;
167164
}
168165

169-
return !symbols.loading;
166+
return !symbols.hasOwnProperty("loading");
170167
}
171168

172169
export function isSymbolsLoading(state: OuterState, source: Source): boolean {
@@ -175,7 +172,7 @@ export function isSymbolsLoading(state: OuterState, source: Source): boolean {
175172
return false;
176173
}
177174

178-
return !!symbols.loading;
175+
return symbols.hasOwnProperty("loading");
179176
}
180177

181178
export function isEmptyLineInSource(

src/types.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ export type Location = {
6060
sourceUrl?: string
6161
};
6262

63+
export type Position = {
64+
line: number,
65+
column: ?number
66+
};
67+
68+
export type Range = { end: Position, start: Position };
69+
6370
export type PendingLocation = {
6471
line: number,
6572
column: ?number,
@@ -332,7 +339,3 @@ export type Worker = {
332339
type: number,
333340
url: string
334341
};
335-
336-
export type Position = { line: number, column: number };
337-
338-
export type Range = { end: Position, start: Position };

src/utils/breakpoint/astBreakpointLocation.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@
77
import { getSymbols } from "../../workers/parser";
88

99
import type {
10-
Scope,
1110
AstPosition,
12-
SymbolDeclarations
11+
AstLocation,
12+
SymbolDeclarations,
13+
SymbolDeclaration
1314
} from "../../workers/parser";
1415

1516
import type { Location, Source, ASTLocation } from "../../types";
1617

17-
export function containsPosition(a: AstPosition, b: AstPosition) {
18+
export function containsPosition(a: AstLocation, b: AstPosition) {
1819
const startsBefore =
1920
a.start.line < b.line ||
2021
(a.start.line === b.line && a.start.column <= b.column);
@@ -24,11 +25,17 @@ export function containsPosition(a: AstPosition, b: AstPosition) {
2425
return startsBefore && endsAfter;
2526
}
2627

27-
export function findClosestScope(functions: Scope[], location: Location) {
28+
export function findClosestScope(
29+
functions: SymbolDeclaration[],
30+
location: Location
31+
) {
2832
return functions.reduce((found, currNode) => {
2933
if (
3034
currNode.name === "anonymous" ||
31-
!containsPosition(currNode.location, location)
35+
!containsPosition(currNode.location, {
36+
line: location.line,
37+
column: location.column || 0
38+
})
3239
) {
3340
return found;
3441
}

src/utils/editor/get-expression.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@
44

55
// @flow
66

7-
type Pos = {
8-
line: number,
9-
column: number
10-
};
7+
import type { Position } from "../../types";
118

129
type Token = {
1310
startColumn: number,
@@ -17,7 +14,7 @@ type Token = {
1714

1815
export function tokenAtTextPosition(
1916
cm: any,
20-
{ line, column }: Pos
17+
{ line, column }: Position
2118
): Token | null {
2219
if (line < 0 || line >= cm.lineCount()) {
2320
return null;
@@ -33,7 +30,7 @@ export function tokenAtTextPosition(
3330

3431
// The strategy of querying codeMirror tokens was borrowed
3532
// from Chrome's inital implementation in JavaScriptSourceFrame.js#L414
36-
export function getExpressionFromCoords(cm: any, coord: Pos) {
33+
export function getExpressionFromCoords(cm: any, coord: Position) {
3734
const token = tokenAtTextPosition(cm, coord);
3835
if (!token) {
3936
return null;

src/utils/editor/get-token-location.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@
33
* file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */
44

55
// @flow
6+
import type { Position } from "../../types";
67

7-
export function getTokenLocation(codeMirror: any, tokenEl: HTMLElement) {
8+
export function getTokenLocation(
9+
codeMirror: any,
10+
tokenEl: HTMLElement
11+
): Position {
812
const { left, top, width, height } = tokenEl.getBoundingClientRect();
913
const { line, ch } = codeMirror.coordsChar({
1014
left: left + width / 2,

src/utils/editor/index.js

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ import { findNext, findPrev } from "./source-search";
1616
import { isWasm, lineToWasmOffset, wasmOffsetToLine } from "../wasm";
1717
import { isOriginalId } from "devtools-source-map";
1818

19-
import type { AstPosition, AstLocation } from "../../workers/parser/types";
19+
import type { AstLocation } from "../../workers/parser";
2020
import type { EditorPosition, EditorRange } from "../editor/types";
21+
import type { Location } from "../../types";
2122

2223
export function shouldShowPrettyPrint(selectedSource) {
2324
if (!selectedSource) {
@@ -60,7 +61,7 @@ export function toEditorLine(sourceId: string, lineOrOffset: number): number {
6061
return lineOrOffset ? lineOrOffset - 1 : 1;
6162
}
6263

63-
export function toEditorPosition(location: AstPosition): EditorPosition {
64+
export function toEditorPosition(location: Location): EditorPosition {
6465
return {
6566
line: toEditorLine(location.sourceId, location.line),
6667
column: isWasm(location.sourceId) || !location.column ? 0 : location.column
@@ -121,16 +122,6 @@ function isVisible(codeMirror: any, top: number, left: number) {
121122
return inXView && inYView;
122123
}
123124

124-
export function toSourceLocation(
125-
sourceId: string,
126-
location: EditorPosition
127-
): AstPosition {
128-
return {
129-
line: toSourceLine(sourceId, location.line),
130-
column: isWasm(sourceId) ? undefined : location.column
131-
};
132-
}
133-
134125
export function markText(editor: any, className, { start, end }: EditorRange) {
135126
return editor.codeMirror.markText(
136127
{ ch: start.column, line: start.line },

src/utils/editor/tests/editor.spec.js

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
toEditorPosition,
88
toEditorRange,
99
toSourceLine,
10-
toSourceLocation,
1110
scrollToColumn,
1211
markText,
1312
lineAtHeight,
@@ -123,17 +122,6 @@ describe("toSourceLine", () => {
123122
});
124123
});
125124

126-
describe("toSourceLocation", () => {
127-
const testId = "test-123";
128-
const loc = { line: 100, column: 30 };
129-
it("returns a source location", () => {
130-
expect(toSourceLocation(testId, loc)).toEqual({
131-
line: 101,
132-
column: 30
133-
});
134-
});
135-
});
136-
137125
const codeMirror = {
138126
doc: {
139127
iter: jest.fn((_, __, cb) => cb())

src/utils/quick-open.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@ import { isPretty, getSourcePath } from "./source";
88

99
import type { Location as BabelLocation } from "@babel/types";
1010
import type { SourcesMap } from "../reducers/sources";
11+
import type { Symbols } from "../reducers/ast";
1112
import type { QuickOpenType } from "../reducers/quick-open";
12-
import type {
13-
SymbolDeclaration,
14-
SymbolDeclarations
15-
} from "../workers/parser/types";
13+
import type { SymbolDeclaration } from "../workers/parser";
1614

1715
export const MODIFIERS = {
1816
"@": "functions",
@@ -75,10 +73,8 @@ export function formatSymbol(symbol: SymbolDeclaration): QuickOpenResult {
7573
};
7674
}
7775

78-
export function formatSymbols(
79-
symbols: ?SymbolDeclarations
80-
): FormattedSymbolDeclarations {
81-
if (!symbols) {
76+
export function formatSymbols(symbols: ?Symbols): FormattedSymbolDeclarations {
77+
if (!symbols || symbols.loading) {
8278
return { variables: [], functions: [] };
8379
}
8480

0 commit comments

Comments
 (0)