Skip to content

Commit a0b3566

Browse files
committed
Rename openInEditor to editorHandler
- Remove indirection of openInEditorListener - Check editorHandler for null before styling error clickable
1 parent 8d36cd9 commit a0b3566

File tree

8 files changed

+32
-33
lines changed

8 files changed

+32
-33
lines changed

Diff for: packages/react-dev-utils/webpackHotDevClient.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ var launchEditorEndpoint = require('./launchEditorEndpoint');
2323
var formatWebpackMessages = require('./formatWebpackMessages');
2424
var ErrorOverlay = require('react-error-overlay');
2525

26-
ErrorOverlay.listenToOpenInEditor(function OpenInEditor(errorLocation) {
26+
ErrorOverlay.setEditorHandler(function editorHandler(errorLocation) {
2727
// Keep this sync with errorOverlayMiddleware.js
2828
fetch(
2929
`${launchEditorEndpoint}?fileName=` +

Diff for: packages/react-error-overlay/src/containers/CompileErrorContainer.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,20 @@ const codeAnchorStyle = {
2121

2222
type Props = {|
2323
error: string,
24-
openInEditor: (errorLoc: ErrorLocation) => void,
24+
editorHandler: (errorLoc: ErrorLocation) => void,
2525
|};
2626

2727
class CompileErrorContainer extends PureComponent<Props, void> {
2828
render() {
29-
const { error, openInEditor } = this.props;
29+
const { error, editorHandler } = this.props;
3030
const errLoc: ?ErrorLocation = parseCompileError(error);
31+
const canOpenInEditor = errLoc !== null && editorHandler !== null;
3132
return (
3233
<ErrorOverlay>
3334
<Header headerText="Failed to compile" />
3435
<a
35-
onClick={errLoc ? () => openInEditor(errLoc) : null}
36-
style={errLoc ? codeAnchorStyle : null}
36+
onClick={canOpenInEditor ? () => editorHandler(errLoc) : null}
37+
style={canOpenInEditor ? codeAnchorStyle : null}
3738
>
3839
<CodeBlock main={true} codeHTML={generateAnsiHTML(error)} />
3940
</a>

Diff for: packages/react-error-overlay/src/containers/RuntimeError.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ export type ErrorRecord = {|
2727

2828
type Props = {|
2929
errorRecord: ErrorRecord,
30-
openInEditor: (errorLoc: ErrorLocation) => void,
30+
editorHandler: (errorLoc: ErrorLocation) => void,
3131
|};
3232

33-
function RuntimeError({ errorRecord, openInEditor }: Props) {
33+
function RuntimeError({ errorRecord, editorHandler }: Props) {
3434
const { error, unhandledRejection, contextSize, stackFrames } = errorRecord;
3535
const errorName = unhandledRejection
3636
? 'Unhandled Rejection (' + error.name + ')'
@@ -59,7 +59,7 @@ function RuntimeError({ errorRecord, openInEditor }: Props) {
5959
stackFrames={stackFrames}
6060
errorName={errorName}
6161
contextSize={contextSize}
62-
openInEditor={openInEditor}
62+
editorHandler={editorHandler}
6363
/>
6464
</div>
6565
);

Diff for: packages/react-error-overlay/src/containers/RuntimeErrorContainer.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import type { ErrorLocation } from '../utils/parseCompileError';
1919
type Props = {|
2020
errorRecords: ErrorRecord[],
2121
close: () => void,
22-
openInEditor: (errorLoc: ErrorLocation) => void,
22+
editorHandler: (errorLoc: ErrorLocation) => void,
2323
|};
2424

2525
type State = {|
@@ -75,7 +75,7 @@ class RuntimeErrorContainer extends PureComponent<Props, State> {
7575
)}
7676
<RuntimeError
7777
errorRecord={errorRecords[this.state.currentIndex]}
78-
openInEditor={this.props.openInEditor}
78+
editorHandler={this.props.editorHandler}
7979
/>
8080
<Footer
8181
line1="This screen is visible only in development. It will not appear if the app crashes in production."

Diff for: packages/react-error-overlay/src/containers/StackFrame.js

+8-7
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ type Props = {|
4949
contextSize: number,
5050
critical: boolean,
5151
showCode: boolean,
52-
openInEditor: (errorLoc: ErrorLocation) => void,
52+
editorHandler: (errorLoc: ErrorLocation) => void,
5353
|};
5454

5555
type State = {|
@@ -85,17 +85,17 @@ class StackFrame extends Component<Props, State> {
8585
return { fileName, lineNumber: lineNumber || 1 };
8686
}
8787

88-
openInEditor = () => {
88+
editorHandler = () => {
8989
const errorLoc = this.getErrorLocation();
9090
if (!errorLoc) {
9191
return;
9292
}
93-
this.props.openInEditor(errorLoc);
93+
this.props.editorHandler(errorLoc);
9494
};
9595

9696
onKeyDown = (e: SyntheticKeyboardEvent<>) => {
9797
if (e.key === 'Enter') {
98-
this.openInEditor();
98+
this.editorHandler();
9999
}
100100
};
101101

@@ -155,14 +155,15 @@ class StackFrame extends Component<Props, State> {
155155
}
156156
}
157157

158-
const canOpenInEditor = this.getErrorLocation() !== null;
158+
const canOpenInEditor =
159+
this.getErrorLocation() !== null && this.props.editorHandler !== null;
159160
return (
160161
<div>
161162
<div>{functionName}</div>
162163
<div style={linkStyle}>
163164
<a
164165
style={canOpenInEditor ? anchorStyle : null}
165-
onClick={canOpenInEditor ? this.openInEditor : null}
166+
onClick={canOpenInEditor ? this.editorHandler : null}
166167
onKeyDown={canOpenInEditor ? this.onKeyDown : null}
167168
tabIndex={canOpenInEditor ? '0' : null}
168169
>
@@ -172,7 +173,7 @@ class StackFrame extends Component<Props, State> {
172173
{codeBlockProps && (
173174
<span>
174175
<a
175-
onClick={canOpenInEditor ? this.openInEditor : null}
176+
onClick={canOpenInEditor ? this.editorHandler : null}
176177
style={canOpenInEditor ? codeAnchorStyle : null}
177178
>
178179
<CodeBlock {...codeBlockProps} />

Diff for: packages/react-error-overlay/src/containers/StackTrace.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ type Props = {|
2626
stackFrames: StackFrameType[],
2727
errorName: string,
2828
contextSize: number,
29-
openInEditor: (errorLoc: ErrorLocation) => void,
29+
editorHandler: (errorLoc: ErrorLocation) => void,
3030
|};
3131

3232
class StackTrace extends Component<Props> {
3333
renderFrames() {
34-
const { stackFrames, errorName, contextSize, openInEditor } = this.props;
34+
const { stackFrames, errorName, contextSize, editorHandler } = this.props;
3535
const renderedFrames = [];
3636
let hasReachedAppCode = false,
3737
currentBundle = [],
@@ -55,7 +55,7 @@ class StackTrace extends Component<Props> {
5555
contextSize={contextSize}
5656
critical={index === 0}
5757
showCode={!shouldCollapse}
58-
openInEditor={openInEditor}
58+
editorHandler={editorHandler}
5959
/>
6060
);
6161
const lastElement = index === stackFrames.length - 1;

Diff for: packages/react-error-overlay/src/iframeScript.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ function render({
1919
currentBuildError,
2020
currentRuntimeErrorRecords,
2121
dismissRuntimeErrors,
22-
openInEditor,
22+
editorHandler,
2323
}) {
2424
if (currentBuildError) {
2525
return (
2626
<CompileErrorContainer
2727
error={currentBuildError}
28-
openInEditor={openInEditor}
28+
editorHandler={editorHandler}
2929
/>
3030
);
3131
}
@@ -34,7 +34,7 @@ function render({
3434
<RuntimeErrorContainer
3535
errorRecords={currentRuntimeErrorRecords}
3636
close={dismissRuntimeErrors}
37-
openInEditor={openInEditor}
37+
editorHandler={editorHandler}
3838
/>
3939
);
4040
}

Diff for: packages/react-error-overlay/src/index.js

+7-10
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,22 @@ type RuntimeReportingOptions = {|
2323
filename?: string,
2424
|};
2525

26-
type OpenInEditorListener = (errorLoc: ErrorLocation) => void;
26+
type EditorHandler = (errorLoc: ErrorLocation) => void;
2727

2828
let iframe: null | HTMLIFrameElement = null;
2929
let isLoadingIframe: boolean = false;
3030
var isIframeReady: boolean = false;
3131

32-
let openInEditorListener: null | OpenInEditorListener = null;
32+
let editorHandler: null | OpenInEditorListener = null;
3333
let currentBuildError: null | string = null;
3434
let currentRuntimeErrorRecords: Array<ErrorRecord> = [];
3535
let currentRuntimeErrorOptions: null | RuntimeReportingOptions = null;
3636
let stopListeningToRuntimeErrors: null | (() => void) = null;
3737

38-
export function listenToOpenInEditor(listener: OpenInEditorListener) {
39-
openInEditorListener = listener;
40-
}
41-
42-
function openInEditor(errorLoc: ErrorLocation) {
43-
if (typeof openInEditorListener === 'function') {
44-
openInEditorListener(errorLoc);
38+
export function setEditorHandler(handler: EditorHandler | null) {
39+
editorHandler = handler;
40+
if (iframe) {
41+
update();
4542
}
4643
}
4744

@@ -153,7 +150,7 @@ function updateIframeContent() {
153150
currentBuildError,
154151
currentRuntimeErrorRecords,
155152
dismissRuntimeErrors,
156-
openInEditor,
153+
editorHandler,
157154
});
158155

159156
if (!isRendered) {

0 commit comments

Comments
 (0)