From 0d6e63e879f939bd387e06b9e0d8a5cf187d6a72 Mon Sep 17 00:00:00 2001 From: Anshul Date: Mon, 28 Jan 2019 15:02:45 -0500 Subject: [PATCH] Bug 1523386 - [release 121] strict flow and remove some unused code in project search (#7669). r=dwalsh --- .../new/src/components/ProjectSearch.js | 39 +++++++++++-------- .../src/components/test/ProjectSearch.spec.js | 4 +- 2 files changed, 24 insertions(+), 19 deletions(-) diff --git a/devtools/client/debugger/new/src/components/ProjectSearch.js b/devtools/client/debugger/new/src/components/ProjectSearch.js index bc9d620105bd..f89fa0b16954 100644 --- a/devtools/client/debugger/new/src/components/ProjectSearch.js +++ b/devtools/client/debugger/new/src/components/ProjectSearch.js @@ -16,7 +16,6 @@ import { highlightMatches } from "../utils/project-search"; import { statusType } from "../reducers/project-text-search"; import { getRelativePath } from "../utils/sources-tree"; import { - getSources, getActiveSearch, getTextSearchResults, getTextSearchStatus, @@ -59,7 +58,6 @@ type State = { }; type Props = { - sources: Object, query: string, results: List, status: StatusType, @@ -83,13 +81,18 @@ function sanitizeQuery(query: string): string { return query.replace(/\\$/, ""); } +type FileItem = { + setExpanded: (Result, boolean) => mixed, + file: Result, + expanded: boolean +}; +type MatchItem = { + expanded: null, + match: Match +}; + export class ProjectSearch extends Component { - focusedItem: ?{ - setExpanded?: any, - file?: any, - expanded?: any, - match?: Match - }; + focusedItem: ?(FileItem | MatchItem); constructor(props: Props) { super(props); this.state = { @@ -182,13 +185,16 @@ export class ProjectSearch extends Component { }; onEnterPress = () => { - if (this.focusedItem && !this.state.inputFocused) { - const { setExpanded, file, expanded, match } = this.focusedItem; - if (setExpanded) { - setExpanded(file, !expanded); - } else if (match) { - this.selectMatchItem(match); - } + if (!this.focusedItem || this.state.inputFocused) { + return; + } + if (this.focusedItem.expanded !== null) { + // expanded is not null implies this is a `FileItem` + const { setExpanded, file, expanded } = this.focusedItem; + setExpanded(file, !expanded); + } else { + const { match } = this.focusedItem; + this.selectMatchItem(match); } }; @@ -229,7 +235,7 @@ export class ProjectSearch extends Component { renderMatch = (match: Match, focused: boolean) => { if (focused) { - this.focusedItem = { match }; + this.focusedItem = { match, expanded: null }; } return (
({ - sources: getSources(state), activeSearch: getActiveSearch(state), results: getTextSearchResults(state), query: getTextSearchQuery(state), diff --git a/devtools/client/debugger/new/src/components/test/ProjectSearch.spec.js b/devtools/client/debugger/new/src/components/test/ProjectSearch.spec.js index 06bc39a18640..2e2d3652badb 100644 --- a/devtools/client/debugger/new/src/components/test/ProjectSearch.spec.js +++ b/devtools/client/debugger/new/src/components/test/ProjectSearch.spec.js @@ -195,7 +195,7 @@ describe("ProjectSearch", () => { }, true ); - component.instance().focusedItem = {}; + component.instance().focusedItem = null; shortcuts.dispatch("Enter"); expect(selectSpecificLocation).not.toHaveBeenCalled(); }); @@ -209,7 +209,7 @@ describe("ProjectSearch", () => { }, true ); - component.instance().focusedItem = { match: testMatch }; + component.instance().focusedItem = { match: testMatch, expanded: null }; shortcuts.dispatch("Enter"); expect(selectSpecificLocation).toHaveBeenCalledWith({ sourceId: "some-target/source42",