Skip to content

Commit

Permalink
Updated react-error-overlay to latest Flow (0.54.0) (facebook#3065)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
duvet86 authored and thongdong7 committed Sep 24, 2017
1 parent 40a1118 commit dd638a9
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 22 deletions.
2 changes: 1 addition & 1 deletion packages/react-error-overlay/package.json
Expand Up @@ -51,7 +51,7 @@
"eslint-plugin-import": "2.7.0",
"eslint-plugin-jsx-a11y": "5.1.1",
"eslint-plugin-react": "7.1.0",
"flow-bin": "0.52.0",
"flow-bin": "^0.54.0",
"jest": "20.0.4",
"jest-fetch-mock": "1.2.1",
"rimraf": "^2.6.1"
Expand Down
12 changes: 11 additions & 1 deletion packages/react-error-overlay/src/components/Collapsible.js
Expand Up @@ -11,6 +11,8 @@
import React, { Component } from 'react';
import { black } from '../styles';

import type { Element as ReactElement } from 'react';

const _collapsibleStyle = {
color: black,
cursor: 'pointer',
Expand All @@ -35,7 +37,15 @@ const collapsibleExpandedStyle = {
marginBottom: '0.6em',
};

class Collapsible extends Component {
type Props = {|
children: ReactElement<any>[],
|};

type State = {|
collapsed: boolean,
|};

class Collapsible extends Component<Props, State> {
state = {
collapsed: true,
};
Expand Down
15 changes: 13 additions & 2 deletions packages/react-error-overlay/src/components/ErrorOverlay.js
Expand Up @@ -11,6 +11,8 @@
import React, { Component } from 'react';
import { black } from '../styles';

import type { Node as ReactNode } from 'react';

const overlayStyle = {
position: 'relative',
display: 'inline-flex',
Expand All @@ -31,10 +33,19 @@ const overlayStyle = {
color: black,
};

class ErrorOverlay extends Component {
type Props = {|
children: ReactNode,
shortcutHandler?: (eventKey: string) => void,
|};

type State = {|
collapsed: boolean,
|};

class ErrorOverlay extends Component<Props, State> {
iframeWindow: window = null;

getIframeWindow = (element: HTMLDivElement) => {
getIframeWindow = (element: ?HTMLDivElement) => {
if (element) {
const document = element.ownerDocument;
this.iframeWindow = document.defaultView;
Expand Down
Expand Up @@ -15,7 +15,11 @@ import Header from '../components/Header';
import CodeBlock from '../components/CodeBlock';
import generateAnsiHTML from '../utils/generateAnsiHTML';

class CompileErrorContainer extends PureComponent {
type Props = {|
error: string,
|};

class CompileErrorContainer extends PureComponent<Props, void> {
render() {
const { error } = this.props;
return (
Expand Down
3 changes: 2 additions & 1 deletion packages/react-error-overlay/src/containers/RuntimeError.js
Expand Up @@ -11,14 +11,15 @@
import React from 'react';
import Header from '../components/Header';
import StackTrace from './StackTrace';

import type { StackFrame } from '../utils/stack-frame';

const wrapperStyle = {
display: 'flex',
flexDirection: 'column',
};

type ErrorRecord = {|
export type ErrorRecord = {|
error: Error,
unhandledRejection: boolean,
contextSize: number,
Expand Down
Expand Up @@ -15,7 +15,19 @@ import NavigationBar from '../components/NavigationBar';
import RuntimeError from './RuntimeError';
import Footer from '../components/Footer';

class RuntimeErrorContainer extends PureComponent {
import type { ErrorRecord } from './RuntimeError';

type Props = {|
errorRecords: ErrorRecord[],
close: () => void,
launchEditorEndpoint: ?string,
|};

type State = {|
currentIndex: number,
|};

class RuntimeErrorContainer extends PureComponent<Props, State> {
state = {
currentIndex: 0,
};
Expand Down
36 changes: 26 additions & 10 deletions packages/react-error-overlay/src/containers/StackFrame.js
Expand Up @@ -13,6 +13,8 @@ import CodeBlock from './StackFrameCodeBlock';
import { getPrettyURL } from '../utils/getPrettyURL';
import { darkGray } from '../styles';

import type { StackFrame as StackFrameType } from '../utils/stack-frame';

const linkStyle = {
fontSize: '0.9em',
marginBottom: '0.9em',
Expand Down Expand Up @@ -43,7 +45,19 @@ const toggleStyle = {
lineHeight: '1.5',
};

class StackFrame extends Component {
type Props = {|
frame: StackFrameType,
launchEditorEndpoint: ?string,
contextSize: number,
critical: boolean,
showCode: boolean,
|};

type State = {|
compiled: boolean,
|};

class StackFrame extends Component<Props, State> {
state = {
compiled: false,
};
Expand All @@ -54,43 +68,45 @@ class StackFrame extends Component {
}));
};

canOpenInEditor() {
getEndpointUrl(): string | null {
if (!this.props.launchEditorEndpoint) {
return;
return null;
}
const { _originalFileName: sourceFileName } = this.props.frame;
// Unknown file
if (!sourceFileName) {
return false;
return null;
}
// e.g. "/path-to-my-app/webpack/bootstrap eaddeb46b67d75e4dfc1"
const isInternalWebpackBootstrapCode =
sourceFileName.trim().indexOf(' ') !== -1;
if (isInternalWebpackBootstrapCode) {
return false;
return null;
}
// Code is in a real file
return true;
return this.props.launchEditorEndpoint || null;
}

openInEditor = () => {
if (!this.canOpenInEditor()) {
const endpointUrl = this.getEndpointUrl();
if (endpointUrl === null) {
return;
}

const {
_originalFileName: sourceFileName,
_originalLineNumber: sourceLineNumber,
} = this.props.frame;
// Keep this in sync with react-error-overlay/middleware.js
fetch(
`${this.props.launchEditorEndpoint}?fileName=` +
`${endpointUrl}?fileName=` +
window.encodeURIComponent(sourceFileName) +
'&lineNumber=' +
window.encodeURIComponent(sourceLineNumber || 1)
).then(() => {}, () => {});
};

onKeyDown = (e: SyntheticKeyboardEvent) => {
onKeyDown = (e: SyntheticKeyboardEvent<>) => {
if (e.key === 'Enter') {
this.openInEditor();
}
Expand Down Expand Up @@ -152,7 +168,7 @@ class StackFrame extends Component {
}
}
const canOpenInEditor = this.canOpenInEditor();
const canOpenInEditor = this.getEndpointUrl() !== null;
return (
<div>
<div>{functionName}</div>
Expand Down
Expand Up @@ -21,12 +21,16 @@ import codeFrame from 'babel-code-frame';
type StackFrameCodeBlockPropsType = {|
lines: ScriptLine[],
lineNum: number,
columnNum: number,
columnNum: ?number,
contextSize: number,
main: boolean,
|};

function StackFrameCodeBlock(props: StackFrameCodeBlockPropsType) {
// Exact type workaround for spread operator.
// See: https://github.com/facebook/flow/issues/2405
type Exact<T> = $Shape<T>;

function StackFrameCodeBlock(props: Exact<StackFrameCodeBlockPropsType>) {
const { lines, lineNum, columnNum, contextSize, main } = props;
const sourceCode = [];
let whiteSpace = Infinity;
Expand Down
11 changes: 10 additions & 1 deletion packages/react-error-overlay/src/containers/StackTrace.js
Expand Up @@ -14,14 +14,23 @@ import Collapsible from '../components/Collapsible';
import { isInternalFile } from '../utils/isInternalFile';
import { isBultinErrorName } from '../utils/isBultinErrorName';

import type { StackFrame as StackFrameType } from '../utils/stack-frame';

const traceStyle = {
fontSize: '1em',
flex: '0 1 auto',
minHeight: '0px',
overflow: 'auto',
};

class StackTrace extends Component {
type Props = {|
stackFrames: StackFrameType[],
errorName: string,
contextSize: number,
launchEditorEndpoint: ?string,
|};

class StackTrace extends Component<Props> {
renderFrames() {
const {
stackFrames,
Expand Down
3 changes: 2 additions & 1 deletion packages/react-error-overlay/src/index.js
Expand Up @@ -9,6 +9,7 @@

/* @flow */
import React from 'react';
import type { Element } from 'react';
import ReactDOM from 'react-dom';
import CompileErrorContainer from './containers/CompileErrorContainer';
import RuntimeErrorContainer from './containers/RuntimeErrorContainer';
Expand All @@ -27,7 +28,7 @@ type RuntimeReportingOptions = {|
let iframe: null | HTMLIFrameElement = null;
let isLoadingIframe: boolean = false;

let renderedElement: null | React.Element<any> = null;
let renderedElement: null | Element<any> = null;
let currentBuildError: null | string = null;
let currentRuntimeErrorRecords: Array<ErrorRecord> = [];
let currentRuntimeErrorOptions: null | RuntimeReportingOptions = null;
Expand Down
5 changes: 4 additions & 1 deletion packages/react-error-overlay/src/utils/getSourceMap.js
Expand Up @@ -77,7 +77,10 @@ class SourceMap {
}
}

function extractSourceMapUrl(fileUri: string, fileContents: string) {
function extractSourceMapUrl(
fileUri: string,
fileContents: string
): Promise<string> {
const regex = /\/\/[#@] ?sourceMappingURL=([^\s'"]+)\s*$/gm;
let match = null;
for (;;) {
Expand Down

0 comments on commit dd638a9

Please sign in to comment.