Skip to content

Commit

Permalink
Rename openInEditor to editorHandler
Browse files Browse the repository at this point in the history
 -  Remove indirection of openInEditorListener
 -  Check editorHandler for null before styling error clickable
  • Loading branch information
tharakawj committed Oct 4, 2017
1 parent 8d36cd9 commit a0b3566
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 33 deletions.
2 changes: 1 addition & 1 deletion packages/react-dev-utils/webpackHotDevClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var launchEditorEndpoint = require('./launchEditorEndpoint');
var formatWebpackMessages = require('./formatWebpackMessages');
var ErrorOverlay = require('react-error-overlay');

ErrorOverlay.listenToOpenInEditor(function OpenInEditor(errorLocation) {
ErrorOverlay.setEditorHandler(function editorHandler(errorLocation) {
// Keep this sync with errorOverlayMiddleware.js
fetch(
`${launchEditorEndpoint}?fileName=` +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,20 @@ const codeAnchorStyle = {

type Props = {|
error: string,
openInEditor: (errorLoc: ErrorLocation) => void,
editorHandler: (errorLoc: ErrorLocation) => void,
|};

class CompileErrorContainer extends PureComponent<Props, void> {
render() {
const { error, openInEditor } = this.props;
const { error, editorHandler } = this.props;
const errLoc: ?ErrorLocation = parseCompileError(error);
const canOpenInEditor = errLoc !== null && editorHandler !== null;
return (
<ErrorOverlay>
<Header headerText="Failed to compile" />
<a
onClick={errLoc ? () => openInEditor(errLoc) : null}
style={errLoc ? codeAnchorStyle : null}
onClick={canOpenInEditor ? () => editorHandler(errLoc) : null}
style={canOpenInEditor ? codeAnchorStyle : null}
>
<CodeBlock main={true} codeHTML={generateAnsiHTML(error)} />
</a>
Expand Down
6 changes: 3 additions & 3 deletions packages/react-error-overlay/src/containers/RuntimeError.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ export type ErrorRecord = {|

type Props = {|
errorRecord: ErrorRecord,
openInEditor: (errorLoc: ErrorLocation) => void,
editorHandler: (errorLoc: ErrorLocation) => void,
|};

function RuntimeError({ errorRecord, openInEditor }: Props) {
function RuntimeError({ errorRecord, editorHandler }: Props) {
const { error, unhandledRejection, contextSize, stackFrames } = errorRecord;
const errorName = unhandledRejection
? 'Unhandled Rejection (' + error.name + ')'
Expand Down Expand Up @@ -59,7 +59,7 @@ function RuntimeError({ errorRecord, openInEditor }: Props) {
stackFrames={stackFrames}
errorName={errorName}
contextSize={contextSize}
openInEditor={openInEditor}
editorHandler={editorHandler}
/>
</div>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import type { ErrorLocation } from '../utils/parseCompileError';
type Props = {|
errorRecords: ErrorRecord[],
close: () => void,
openInEditor: (errorLoc: ErrorLocation) => void,
editorHandler: (errorLoc: ErrorLocation) => void,
|};

type State = {|
Expand Down Expand Up @@ -75,7 +75,7 @@ class RuntimeErrorContainer extends PureComponent<Props, State> {
)}
<RuntimeError
errorRecord={errorRecords[this.state.currentIndex]}
openInEditor={this.props.openInEditor}
editorHandler={this.props.editorHandler}
/>
<Footer
line1="This screen is visible only in development. It will not appear if the app crashes in production."
Expand Down
15 changes: 8 additions & 7 deletions packages/react-error-overlay/src/containers/StackFrame.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type Props = {|
contextSize: number,
critical: boolean,
showCode: boolean,
openInEditor: (errorLoc: ErrorLocation) => void,
editorHandler: (errorLoc: ErrorLocation) => void,
|};

type State = {|
Expand Down Expand Up @@ -85,17 +85,17 @@ class StackFrame extends Component<Props, State> {
return { fileName, lineNumber: lineNumber || 1 };
}

openInEditor = () => {
editorHandler = () => {
const errorLoc = this.getErrorLocation();
if (!errorLoc) {
return;
}
this.props.openInEditor(errorLoc);
this.props.editorHandler(errorLoc);
};

onKeyDown = (e: SyntheticKeyboardEvent<>) => {
if (e.key === 'Enter') {
this.openInEditor();
this.editorHandler();
}
};
Expand Down Expand Up @@ -155,14 +155,15 @@ class StackFrame extends Component<Props, State> {
}
}
const canOpenInEditor = this.getErrorLocation() !== null;
const canOpenInEditor =
this.getErrorLocation() !== null && this.props.editorHandler !== null;
return (
<div>
<div>{functionName}</div>
<div style={linkStyle}>
<a
style={canOpenInEditor ? anchorStyle : null}
onClick={canOpenInEditor ? this.openInEditor : null}
onClick={canOpenInEditor ? this.editorHandler : null}
onKeyDown={canOpenInEditor ? this.onKeyDown : null}
tabIndex={canOpenInEditor ? '0' : null}
>
Expand All @@ -172,7 +173,7 @@ class StackFrame extends Component<Props, State> {
{codeBlockProps && (
<span>
<a
onClick={canOpenInEditor ? this.openInEditor : null}
onClick={canOpenInEditor ? this.editorHandler : null}
style={canOpenInEditor ? codeAnchorStyle : null}
>
<CodeBlock {...codeBlockProps} />
Expand Down
6 changes: 3 additions & 3 deletions packages/react-error-overlay/src/containers/StackTrace.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ type Props = {|
stackFrames: StackFrameType[],
errorName: string,
contextSize: number,
openInEditor: (errorLoc: ErrorLocation) => void,
editorHandler: (errorLoc: ErrorLocation) => void,
|};

class StackTrace extends Component<Props> {
renderFrames() {
const { stackFrames, errorName, contextSize, openInEditor } = this.props;
const { stackFrames, errorName, contextSize, editorHandler } = this.props;
const renderedFrames = [];
let hasReachedAppCode = false,
currentBundle = [],
Expand All @@ -55,7 +55,7 @@ class StackTrace extends Component<Props> {
contextSize={contextSize}
critical={index === 0}
showCode={!shouldCollapse}
openInEditor={openInEditor}
editorHandler={editorHandler}
/>
);
const lastElement = index === stackFrames.length - 1;
Expand Down
6 changes: 3 additions & 3 deletions packages/react-error-overlay/src/iframeScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ function render({
currentBuildError,
currentRuntimeErrorRecords,
dismissRuntimeErrors,
openInEditor,
editorHandler,
}) {
if (currentBuildError) {
return (
<CompileErrorContainer
error={currentBuildError}
openInEditor={openInEditor}
editorHandler={editorHandler}
/>
);
}
Expand All @@ -34,7 +34,7 @@ function render({
<RuntimeErrorContainer
errorRecords={currentRuntimeErrorRecords}
close={dismissRuntimeErrors}
openInEditor={openInEditor}
editorHandler={editorHandler}
/>
);
}
Expand Down
17 changes: 7 additions & 10 deletions packages/react-error-overlay/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,22 @@ type RuntimeReportingOptions = {|
filename?: string,
|};

type OpenInEditorListener = (errorLoc: ErrorLocation) => void;
type EditorHandler = (errorLoc: ErrorLocation) => void;

let iframe: null | HTMLIFrameElement = null;
let isLoadingIframe: boolean = false;
var isIframeReady: boolean = false;

let openInEditorListener: null | OpenInEditorListener = null;
let editorHandler: null | OpenInEditorListener = null;
let currentBuildError: null | string = null;
let currentRuntimeErrorRecords: Array<ErrorRecord> = [];
let currentRuntimeErrorOptions: null | RuntimeReportingOptions = null;
let stopListeningToRuntimeErrors: null | (() => void) = null;

export function listenToOpenInEditor(listener: OpenInEditorListener) {
openInEditorListener = listener;
}

function openInEditor(errorLoc: ErrorLocation) {
if (typeof openInEditorListener === 'function') {
openInEditorListener(errorLoc);
export function setEditorHandler(handler: EditorHandler | null) {
editorHandler = handler;
if (iframe) {
update();
}
}

Expand Down Expand Up @@ -153,7 +150,7 @@ function updateIframeContent() {
currentBuildError,
currentRuntimeErrorRecords,
dismissRuntimeErrors,
openInEditor,
editorHandler,
});

if (!isRendered) {
Expand Down

0 comments on commit a0b3566

Please sign in to comment.