Skip to content

Commit 34aee7f

Browse files
duvet86thongdong7
authored andcommitted
Updated react-error-overlay to latest Flow (0.54.0) (facebook#3065)
* Updated react-error-overlay to latest Flow (0.54.0) * Revert "Updated react-error-overlay to latest Flow (0.54.0)" This reverts commit 6deaffb. * Fixed unit tests. * Updated code as per code review. * Fixed code as per code review. * Updated the code as per review.
1 parent a2bcbcf commit 34aee7f

11 files changed

+93
-22
lines changed

packages/react-error-overlay/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
"eslint-plugin-import": "2.7.0",
5252
"eslint-plugin-jsx-a11y": "5.1.1",
5353
"eslint-plugin-react": "7.1.0",
54-
"flow-bin": "0.52.0",
54+
"flow-bin": "^0.54.0",
5555
"jest": "20.0.4",
5656
"jest-fetch-mock": "1.2.1",
5757
"rimraf": "^2.6.1"

packages/react-error-overlay/src/components/Collapsible.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import React, { Component } from 'react';
1212
import { black } from '../styles';
1313

14+
import type { Element as ReactElement } from 'react';
15+
1416
const _collapsibleStyle = {
1517
color: black,
1618
cursor: 'pointer',
@@ -35,7 +37,15 @@ const collapsibleExpandedStyle = {
3537
marginBottom: '0.6em',
3638
};
3739

38-
class Collapsible extends Component {
40+
type Props = {|
41+
children: ReactElement<any>[],
42+
|};
43+
44+
type State = {|
45+
collapsed: boolean,
46+
|};
47+
48+
class Collapsible extends Component<Props, State> {
3949
state = {
4050
collapsed: true,
4151
};

packages/react-error-overlay/src/components/ErrorOverlay.js

+13-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import React, { Component } from 'react';
1212
import { black } from '../styles';
1313

14+
import type { Node as ReactNode } from 'react';
15+
1416
const overlayStyle = {
1517
position: 'relative',
1618
display: 'inline-flex',
@@ -31,10 +33,19 @@ const overlayStyle = {
3133
color: black,
3234
};
3335

34-
class ErrorOverlay extends Component {
36+
type Props = {|
37+
children: ReactNode,
38+
shortcutHandler?: (eventKey: string) => void,
39+
|};
40+
41+
type State = {|
42+
collapsed: boolean,
43+
|};
44+
45+
class ErrorOverlay extends Component<Props, State> {
3546
iframeWindow: window = null;
3647

37-
getIframeWindow = (element: HTMLDivElement) => {
48+
getIframeWindow = (element: ?HTMLDivElement) => {
3849
if (element) {
3950
const document = element.ownerDocument;
4051
this.iframeWindow = document.defaultView;

packages/react-error-overlay/src/containers/CompileErrorContainer.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ import Header from '../components/Header';
1515
import CodeBlock from '../components/CodeBlock';
1616
import generateAnsiHTML from '../utils/generateAnsiHTML';
1717

18-
class CompileErrorContainer extends PureComponent {
18+
type Props = {|
19+
error: string,
20+
|};
21+
22+
class CompileErrorContainer extends PureComponent<Props, void> {
1923
render() {
2024
const { error } = this.props;
2125
return (

packages/react-error-overlay/src/containers/RuntimeError.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@
1111
import React from 'react';
1212
import Header from '../components/Header';
1313
import StackTrace from './StackTrace';
14+
1415
import type { StackFrame } from '../utils/stack-frame';
1516

1617
const wrapperStyle = {
1718
display: 'flex',
1819
flexDirection: 'column',
1920
};
2021

21-
type ErrorRecord = {|
22+
export type ErrorRecord = {|
2223
error: Error,
2324
unhandledRejection: boolean,
2425
contextSize: number,

packages/react-error-overlay/src/containers/RuntimeErrorContainer.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,19 @@ import NavigationBar from '../components/NavigationBar';
1515
import RuntimeError from './RuntimeError';
1616
import Footer from '../components/Footer';
1717

18-
class RuntimeErrorContainer extends PureComponent {
18+
import type { ErrorRecord } from './RuntimeError';
19+
20+
type Props = {|
21+
errorRecords: ErrorRecord[],
22+
close: () => void,
23+
launchEditorEndpoint: ?string,
24+
|};
25+
26+
type State = {|
27+
currentIndex: number,
28+
|};
29+
30+
class RuntimeErrorContainer extends PureComponent<Props, State> {
1931
state = {
2032
currentIndex: 0,
2133
};

packages/react-error-overlay/src/containers/StackFrame.js

+26-10
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import CodeBlock from './StackFrameCodeBlock';
1313
import { getPrettyURL } from '../utils/getPrettyURL';
1414
import { darkGray } from '../styles';
1515

16+
import type { StackFrame as StackFrameType } from '../utils/stack-frame';
17+
1618
const linkStyle = {
1719
fontSize: '0.9em',
1820
marginBottom: '0.9em',
@@ -43,7 +45,19 @@ const toggleStyle = {
4345
lineHeight: '1.5',
4446
};
4547

46-
class StackFrame extends Component {
48+
type Props = {|
49+
frame: StackFrameType,
50+
launchEditorEndpoint: ?string,
51+
contextSize: number,
52+
critical: boolean,
53+
showCode: boolean,
54+
|};
55+
56+
type State = {|
57+
compiled: boolean,
58+
|};
59+
60+
class StackFrame extends Component<Props, State> {
4761
state = {
4862
compiled: false,
4963
};
@@ -54,43 +68,45 @@ class StackFrame extends Component {
5468
}));
5569
};
5670

57-
canOpenInEditor() {
71+
getEndpointUrl(): string | null {
5872
if (!this.props.launchEditorEndpoint) {
59-
return;
73+
return null;
6074
}
6175
const { _originalFileName: sourceFileName } = this.props.frame;
6276
// Unknown file
6377
if (!sourceFileName) {
64-
return false;
78+
return null;
6579
}
6680
// e.g. "/path-to-my-app/webpack/bootstrap eaddeb46b67d75e4dfc1"
6781
const isInternalWebpackBootstrapCode =
6882
sourceFileName.trim().indexOf(' ') !== -1;
6983
if (isInternalWebpackBootstrapCode) {
70-
return false;
84+
return null;
7185
}
7286
// Code is in a real file
73-
return true;
87+
return this.props.launchEditorEndpoint || null;
7488
}
7589

7690
openInEditor = () => {
77-
if (!this.canOpenInEditor()) {
91+
const endpointUrl = this.getEndpointUrl();
92+
if (endpointUrl === null) {
7893
return;
7994
}
95+
8096
const {
8197
_originalFileName: sourceFileName,
8298
_originalLineNumber: sourceLineNumber,
8399
} = this.props.frame;
84100
// Keep this in sync with react-error-overlay/middleware.js
85101
fetch(
86-
`${this.props.launchEditorEndpoint}?fileName=` +
102+
`${endpointUrl}?fileName=` +
87103
window.encodeURIComponent(sourceFileName) +
88104
'&lineNumber=' +
89105
window.encodeURIComponent(sourceLineNumber || 1)
90106
).then(() => {}, () => {});
91107
};
92108

93-
onKeyDown = (e: SyntheticKeyboardEvent) => {
109+
onKeyDown = (e: SyntheticKeyboardEvent<>) => {
94110
if (e.key === 'Enter') {
95111
this.openInEditor();
96112
}
@@ -152,7 +168,7 @@ class StackFrame extends Component {
152168
}
153169
}
154170

155-
const canOpenInEditor = this.canOpenInEditor();
171+
const canOpenInEditor = this.getEndpointUrl() !== null;
156172
return (
157173
<div>
158174
<div>{functionName}</div>

packages/react-error-overlay/src/containers/StackFrameCodeBlock.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,16 @@ import codeFrame from 'babel-code-frame';
2121
type StackFrameCodeBlockPropsType = {|
2222
lines: ScriptLine[],
2323
lineNum: number,
24-
columnNum: number,
24+
columnNum: ?number,
2525
contextSize: number,
2626
main: boolean,
2727
|};
2828

29-
function StackFrameCodeBlock(props: StackFrameCodeBlockPropsType) {
29+
// Exact type workaround for spread operator.
30+
// See: https://github.com/facebook/flow/issues/2405
31+
type Exact<T> = $Shape<T>;
32+
33+
function StackFrameCodeBlock(props: Exact<StackFrameCodeBlockPropsType>) {
3034
const { lines, lineNum, columnNum, contextSize, main } = props;
3135
const sourceCode = [];
3236
let whiteSpace = Infinity;

packages/react-error-overlay/src/containers/StackTrace.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,23 @@ import Collapsible from '../components/Collapsible';
1414
import { isInternalFile } from '../utils/isInternalFile';
1515
import { isBultinErrorName } from '../utils/isBultinErrorName';
1616

17+
import type { StackFrame as StackFrameType } from '../utils/stack-frame';
18+
1719
const traceStyle = {
1820
fontSize: '1em',
1921
flex: '0 1 auto',
2022
minHeight: '0px',
2123
overflow: 'auto',
2224
};
2325

24-
class StackTrace extends Component {
26+
type Props = {|
27+
stackFrames: StackFrameType[],
28+
errorName: string,
29+
contextSize: number,
30+
launchEditorEndpoint: ?string,
31+
|};
32+
33+
class StackTrace extends Component<Props> {
2534
renderFrames() {
2635
const {
2736
stackFrames,

packages/react-error-overlay/src/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
/* @flow */
1111
import React from 'react';
12+
import type { Element } from 'react';
1213
import ReactDOM from 'react-dom';
1314
import CompileErrorContainer from './containers/CompileErrorContainer';
1415
import RuntimeErrorContainer from './containers/RuntimeErrorContainer';
@@ -27,7 +28,7 @@ type RuntimeReportingOptions = {|
2728
let iframe: null | HTMLIFrameElement = null;
2829
let isLoadingIframe: boolean = false;
2930

30-
let renderedElement: null | React.Element<any> = null;
31+
let renderedElement: null | Element<any> = null;
3132
let currentBuildError: null | string = null;
3233
let currentRuntimeErrorRecords: Array<ErrorRecord> = [];
3334
let currentRuntimeErrorOptions: null | RuntimeReportingOptions = null;

packages/react-error-overlay/src/utils/getSourceMap.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,10 @@ class SourceMap {
7777
}
7878
}
7979

80-
function extractSourceMapUrl(fileUri: string, fileContents: string) {
80+
function extractSourceMapUrl(
81+
fileUri: string,
82+
fileContents: string
83+
): Promise<string> {
8184
const regex = /\/\/[#@] ?sourceMappingURL=([^\s'"]+)\s*$/gm;
8285
let match = null;
8386
for (;;) {

0 commit comments

Comments
 (0)