Bug 1541633 - Part 4: Move sourceMapURL usage into centralized selectors. r=jlast

Depends on D42935

Differential Revision: https://phabricator.services.mozilla.com/D42936

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Logan Smyth 2019-08-21 20:23:49 +00:00
Родитель 6a5359388c
Коммит 2614729648
8 изменённых файлов: 85 добавлений и 89 удалений

Просмотреть файл

@ -16,13 +16,12 @@ import { getSourcesForTabs } from "../../reducers/tabs";
import { setSymbols } from "./symbols"; import { setSymbols } from "./symbols";
import { setInScopeLines } from "../ast"; import { setInScopeLines } from "../ast";
import { closeActiveSearch, updateActiveFileSearch } from "../ui"; import { closeActiveSearch, updateActiveFileSearch } from "../ui";
import { isFulfilled } from "../../utils/async-value";
import { togglePrettyPrint } from "./prettyPrint"; import { togglePrettyPrint } from "./prettyPrint";
import { addTab, closeTab } from "../tabs"; import { addTab, closeTab } from "../tabs";
import { loadSourceText } from "./loadSourceText"; import { loadSourceText } from "./loadSourceText";
import { prefs } from "../../utils/prefs"; import { prefs } from "../../utils/prefs";
import { shouldPrettyPrint, isMinified } from "../../utils/source"; import { isMinified } from "../../utils/source";
import { createLocation } from "../../utils/location"; import { createLocation } from "../../utils/location";
import { mapLocation } from "../../utils/source-maps"; import { mapLocation } from "../../utils/source-maps";
@ -33,6 +32,7 @@ import {
getActiveSearch, getActiveSearch,
getSelectedLocation, getSelectedLocation,
getSelectedSource, getSelectedSource,
canPrettyPrintSource,
} from "../../selectors"; } from "../../selectors";
import type { import type {
@ -169,19 +169,12 @@ export function selectLocation(
return; return;
} }
const sourceWithContent = getSourceWithContent(getState(), source.id); const sourceWithContent = getSourceWithContent(getState(), source.id);
const sourceContent =
sourceWithContent.content && isFulfilled(sourceWithContent.content)
? sourceWithContent.content.value
: null;
if ( if (
keepContext && keepContext &&
prefs.autoPrettyPrint && prefs.autoPrettyPrint &&
!getPrettySource(getState(), loadedSource.id) && !getPrettySource(getState(), loadedSource.id) &&
shouldPrettyPrint( canPrettyPrintSource(getState(), loadedSource.id) &&
loadedSource,
sourceContent || { type: "text", value: "", contentType: undefined }
) &&
isMinified(sourceWithContent) isMinified(sourceWithContent)
) { ) {
await dispatch(togglePrettyPrint(cx, loadedSource.id)); await dispatch(togglePrettyPrint(cx, loadedSource.id));

Просмотреть файл

@ -7,13 +7,16 @@
import { Component } from "react"; import { Component } from "react";
import { connect } from "../../utils/connect"; import { connect } from "../../utils/connect";
import { showMenu } from "devtools-contextmenu"; import { showMenu } from "devtools-contextmenu";
import { isOriginalId } from "devtools-source-map";
import { getSourceLocationFromMouseEvent } from "../../utils/editor"; import { getSourceLocationFromMouseEvent } from "../../utils/editor";
import { isPretty } from "../../utils/source";
import { import {
getPrettySource, getPrettySource,
getIsPaused, getIsPaused,
getCurrentThread, getCurrentThread,
getThreadContext, getThreadContext,
isSourceWithMap,
} from "../../selectors"; } from "../../selectors";
import { editorMenuItems, editorItemActions } from "./menus/editor"; import { editorMenuItems, editorItemActions } from "./menus/editor";
@ -28,7 +31,7 @@ type Props = {
editorActions: EditorItemActions, editorActions: EditorItemActions,
clearContextMenu: () => void, clearContextMenu: () => void,
editor: SourceEditor, editor: SourceEditor,
hasPrettySource: boolean, hasMappedLocation: boolean,
isPaused: boolean, isPaused: boolean,
selectedSource: SourceWithContent, selectedSource: SourceWithContent,
}; };
@ -49,7 +52,7 @@ class EditorMenu extends Component<Props> {
editor, editor,
selectedSource, selectedSource,
editorActions, editorActions,
hasPrettySource, hasMappedLocation,
isPaused, isPaused,
contextMenu: event, contextMenu: event,
} = props; } = props;
@ -67,7 +70,7 @@ class EditorMenu extends Component<Props> {
cx, cx,
editorActions, editorActions,
selectedSource, selectedSource,
hasPrettySource, hasMappedLocation,
location, location,
isPaused, isPaused,
selectionText: editor.codeMirror.getSelection().trim(), selectionText: editor.codeMirror.getSelection().trim(),
@ -84,7 +87,11 @@ class EditorMenu extends Component<Props> {
const mapStateToProps = (state, props) => ({ const mapStateToProps = (state, props) => ({
cx: getThreadContext(state), cx: getThreadContext(state),
isPaused: getIsPaused(state, getCurrentThread(state)), isPaused: getIsPaused(state, getCurrentThread(state)),
hasPrettySource: !!getPrettySource(state, props.selectedSource.id), hasMappedLocation:
(isOriginalId(props.selectedSource.id) ||
isSourceWithMap(state, props.selectedSource.id) ||
isPretty(props.selectedSource)) &&
!getPrettySource(state, props.selectedSource.id),
}); });
const mapDispatchToProps = dispatch => ({ const mapDispatchToProps = dispatch => ({

Просмотреть файл

@ -14,15 +14,16 @@ import {
getContext, getContext,
} from "../../selectors"; } from "../../selectors";
import { isFulfilled } from "../../utils/async-value";
import { import {
isPretty, isPretty,
getFilename, getFilename,
isOriginal, isOriginal,
shouldBlackbox, shouldBlackbox,
} from "../../utils/source"; } from "../../utils/source";
import { getGeneratedSource } from "../../reducers/sources"; import {
import { shouldShowPrettyPrint } from "../../utils/editor"; getGeneratedSource,
canPrettyPrintSource,
} from "../../reducers/sources";
import { PaneToggleButton } from "../shared/Button"; import { PaneToggleButton } from "../shared/Button";
import AccessibleImage from "../shared/AccessibleImage"; import AccessibleImage from "../shared/AccessibleImage";
@ -42,6 +43,7 @@ type Props = {
mappedSource: Source, mappedSource: Source,
endPanelCollapsed: boolean, endPanelCollapsed: boolean,
horizontal: boolean, horizontal: boolean,
canPrettyPrint: boolean,
togglePrettyPrint: typeof actions.togglePrettyPrint, togglePrettyPrint: typeof actions.togglePrettyPrint,
toggleBlackBox: typeof actions.toggleBlackBox, toggleBlackBox: typeof actions.toggleBlackBox,
jumpToMappedLocation: typeof actions.jumpToMappedLocation, jumpToMappedLocation: typeof actions.jumpToMappedLocation,
@ -84,7 +86,12 @@ class SourceFooter extends PureComponent<Props, State> {
} }
prettyPrintButton() { prettyPrintButton() {
const { cx, selectedSource, togglePrettyPrint } = this.props; const {
cx,
selectedSource,
canPrettyPrint,
togglePrettyPrint,
} = this.props;
if (!selectedSource) { if (!selectedSource) {
return; return;
@ -98,16 +105,7 @@ class SourceFooter extends PureComponent<Props, State> {
); );
} }
const sourceContent = if (!canPrettyPrint) {
selectedSource.content && isFulfilled(selectedSource.content)
? selectedSource.content.value
: null;
if (
!shouldShowPrettyPrint(
selectedSource,
sourceContent || { type: "text", value: "", contentType: undefined }
)
) {
return; return;
} }
@ -280,6 +278,9 @@ const mapStateToProps = state => {
selectedSource ? selectedSource.id : null selectedSource ? selectedSource.id : null
), ),
endPanelCollapsed: getPaneCollapse(state, "end"), endPanelCollapsed: getPaneCollapse(state, "end"),
canPrettyPrint: selectedSource
? canPrettyPrintSource(state, selectedSource.id)
: false,
}; };
}; };

Просмотреть файл

@ -9,7 +9,6 @@ import { isOriginalId } from "devtools-source-map";
import { copyToTheClipboard } from "../../../utils/clipboard"; import { copyToTheClipboard } from "../../../utils/clipboard";
import { import {
isPretty,
getRawSourceURL, getRawSourceURL,
getFilename, getFilename,
shouldBlackbox, shouldBlackbox,
@ -29,10 +28,6 @@ import type {
ThreadContext, ThreadContext,
} from "../../../types"; } from "../../../types";
function isMapped(selectedSource) {
return isOriginalId(selectedSource.id) || !!selectedSource.sourceMapURL;
}
export const continueToHereItem = ( export const continueToHereItem = (
cx: ThreadContext, cx: ThreadContext,
location: SourceLocation, location: SourceLocation,
@ -88,7 +83,7 @@ const jumpToMappedLocationItem = (
cx: Context, cx: Context,
selectedSource: Source, selectedSource: Source,
location: SourceLocation, location: SourceLocation,
hasPrettySource: boolean, hasMappedLocation: boolean,
editorActions: EditorItemActions editorActions: EditorItemActions
) => ({ ) => ({
id: "node-menu-jump", id: "node-menu-jump",
@ -99,8 +94,7 @@ const jumpToMappedLocationItem = (
: L10N.getStr("original") : L10N.getStr("original")
), ),
accesskey: L10N.getStr("editor.jumpToMappedLocation1.accesskey"), accesskey: L10N.getStr("editor.jumpToMappedLocation1.accesskey"),
disabled: disabled: !hasMappedLocation,
(!isMapped(selectedSource) && !isPretty(selectedSource)) || hasPrettySource,
click: () => editorActions.jumpToMappedLocation(cx, location), click: () => editorActions.jumpToMappedLocation(cx, location),
}); });
@ -171,7 +165,7 @@ export function editorMenuItems({
selectedSource, selectedSource,
location, location,
selectionText, selectionText,
hasPrettySource, hasMappedLocation,
isTextSelected, isTextSelected,
isPaused, isPaused,
}: { }: {
@ -180,7 +174,7 @@ export function editorMenuItems({
selectedSource: SourceWithContent, selectedSource: SourceWithContent,
location: SourceLocation, location: SourceLocation,
selectionText: string, selectionText: string,
hasPrettySource: boolean, hasMappedLocation: boolean,
isTextSelected: boolean, isTextSelected: boolean,
isPaused: boolean, isPaused: boolean,
}) { }) {
@ -196,7 +190,7 @@ export function editorMenuItems({
cx, cx,
selectedSource, selectedSource,
location, location,
hasPrettySource, hasMappedLocation,
editorActions editorActions
), ),
continueToHereItem(cx, location, isPaused, editorActions), continueToHereItem(cx, location, isPaused, editorActions),

Просмотреть файл

@ -17,6 +17,8 @@ import {
isGenerated, isGenerated,
isOriginal as isOriginalSource, isOriginal as isOriginalSource,
getPlainUrl, getPlainUrl,
isPretty,
isJavaScript,
} from "../utils/source"; } from "../utils/source";
import { import {
createInitial, createInitial,
@ -37,7 +39,14 @@ import {
} from "../utils/resource"; } from "../utils/resource";
import { findPosition } from "../utils/breakpoint/breakpointPositions"; import { findPosition } from "../utils/breakpoint/breakpointPositions";
import * as asyncValue from "../utils/async-value"; import {
pending,
fulfilled,
rejected,
asSettled,
isFulfilled,
} from "../utils/async-value";
import type { AsyncValue, SettledValue } from "../utils/async-value"; import type { AsyncValue, SettledValue } from "../utils/async-value";
import { originalToGeneratedId } from "devtools-source-map"; import { originalToGeneratedId } from "devtools-source-map";
import { prefs } from "../utils/prefs"; import { prefs } from "../utils/prefs";
@ -260,7 +269,7 @@ const resourceAsSourceBase = memoizeResourceShallow(
const resourceAsSourceWithContent = memoizeResourceShallow( const resourceAsSourceWithContent = memoizeResourceShallow(
({ content, ...source }: SourceResource): SourceWithContent => ({ ({ content, ...source }: SourceResource): SourceWithContent => ({
...source, ...source,
content: asyncValue.asSettled(content), content: asSettled(content),
}) })
); );
@ -417,17 +426,17 @@ function updateLoadedState(
let content; let content;
if (action.status === "start") { if (action.status === "start") {
content = asyncValue.pending(); content = pending();
} else if (action.status === "error") { } else if (action.status === "error") {
content = asyncValue.rejected(action.error); content = rejected(action.error);
} else if (typeof action.value.text === "string") { } else if (typeof action.value.text === "string") {
content = asyncValue.fulfilled({ content = fulfilled({
type: "text", type: "text",
value: action.value.text, value: action.value.text,
contentType: action.value.contentType, contentType: action.value.contentType,
}); });
} else { } else {
content = asyncValue.fulfilled({ content = fulfilled({
type: "wasm", type: "wasm",
value: action.value.text, value: action.value.text,
}); });
@ -789,7 +798,7 @@ export function getSourceContent(
id: SourceId id: SourceId
): SettledValue<SourceContent> | null { ): SettledValue<SourceContent> | null {
const { content } = getResource(state.sources.sources, id); const { content } = getResource(state.sources.sources, id);
return asyncValue.asSettled(content); return asSettled(content);
} }
export function getSelectedSourceId(state: OuterState) { export function getSelectedSourceId(state: OuterState) {
@ -926,6 +935,38 @@ export function canLoadSource(
return actors.length != 0; return actors.length != 0;
} }
export function isSourceWithMap(
state: OuterState & SourceActorOuterState,
id: SourceId
): boolean {
const source = getSource(state, id);
return source ? !!source.sourceMapURL : false;
}
export function canPrettyPrintSource(
state: OuterState & SourceActorOuterState,
id: SourceId
): boolean {
const source: SourceWithContent = getSourceWithContent(state, id);
if (
!source ||
isPretty(source) ||
isOriginalSource(source) ||
(prefs.clientSourceMapsEnabled && isSourceWithMap(state, id))
) {
return false;
}
const sourceContent =
source.content && isFulfilled(source.content) ? source.content.value : null;
if (!sourceContent || !isJavaScript(source, sourceContent)) {
return false;
}
return true;
}
export function getBreakpointPositions( export function getBreakpointPositions(
state: OuterState state: OuterState
): BreakpointPositionsMap { ): BreakpointPositionsMap {

Просмотреть файл

@ -11,19 +11,13 @@ export * from "../ui";
export { onMouseOver } from "./token-events"; export { onMouseOver } from "./token-events";
import { createEditor } from "./create-editor"; import { createEditor } from "./create-editor";
import { shouldPrettyPrint } from "../source";
import { findNext, findPrev } from "./source-search"; import { findNext, findPrev } from "./source-search";
import { isWasm, lineToWasmOffset, wasmOffsetToLine } from "../wasm"; import { isWasm, lineToWasmOffset, wasmOffsetToLine } from "../wasm";
import type { AstLocation } from "../../workers/parser"; import type { AstLocation } from "../../workers/parser";
import type { EditorPosition, EditorRange } from "../editor/types"; import type { EditorPosition, EditorRange } from "../editor/types";
import type { import type { SearchModifiers, Source, SourceLocation } from "../../types";
SearchModifiers,
Source,
SourceContent,
SourceLocation,
} from "../../types";
type Editor = Object; type Editor = Object;
let editor: ?Editor; let editor: ?Editor;
@ -63,10 +57,6 @@ export function endOperation() {
codeMirror.endOperation(); codeMirror.endOperation();
} }
export function shouldShowPrettyPrint(source: Source, content: SourceContent) {
return shouldPrettyPrint(source, content);
}
export function traverseResults( export function traverseResults(
e: Event, e: Event,
ctx: any, ctx: any,

Просмотреть файл

@ -5,7 +5,6 @@
// @flow // @flow
import { import {
shouldShowPrettyPrint,
traverseResults, traverseResults,
toEditorLine, toEditorLine,
toEditorPosition, toEditorPosition,
@ -22,19 +21,7 @@ import {
getCursorLine, getCursorLine,
} from "../index"; } from "../index";
import { makeMockSource, makeMockSourceAndContent } from "../../test-mockup"; import { makeMockSource } from "../../test-mockup";
describe("shouldShowPrettyPrint", () => {
it("shows pretty print for a source", () => {
const { content, ...source } = makeMockSourceAndContent(
"http://example.com/index.js",
"test-id-123",
"text/javascript",
"some text here"
);
expect(shouldShowPrettyPrint(source, content)).toEqual(true);
});
});
describe("traverseResults", () => { describe("traverseResults", () => {
const e: any = { stopPropagation: jest.fn(), preventDefault: jest.fn() }; const e: any = { stopPropagation: jest.fn(), preventDefault: jest.fn() };

Просмотреть файл

@ -20,7 +20,7 @@ import { renderWasmText } from "./wasm";
import { toEditorLine } from "./editor"; import { toEditorLine } from "./editor";
export { isMinified } from "./isMinified"; export { isMinified } from "./isMinified";
import { getURL, getFileExtension } from "./sources-tree"; import { getURL, getFileExtension } from "./sources-tree";
import { prefs, features } from "./prefs"; import { features } from "./prefs";
import type { import type {
SourceId, SourceId,
@ -78,23 +78,6 @@ export function shouldBlackbox(source: ?Source) {
return true; return true;
} }
export function shouldPrettyPrint(
source: Source,
content: SourceContent
): boolean {
if (
!source ||
isPretty(source) ||
!isJavaScript(source, content) ||
isOriginal(source) ||
(prefs.clientSourceMapsEnabled && source.sourceMapURL)
) {
return false;
}
return true;
}
/** /**
* Returns true if the specified url and/or content type are specific to * Returns true if the specified url and/or content type are specific to
* javascript files. * javascript files.