Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated react-error-overlay to latest Flow (0.54.0) #3065

Merged
merged 6 commits into from Sep 11, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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"
},
Expand Down
11 changes: 10 additions & 1 deletion packages/react-error-overlay/src/components/Collapsible.js
Expand Up @@ -9,6 +9,7 @@

/* @flow */
import React, { Component } from 'react';
import type { Element } from 'react';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please leave them typed as React.Element instead of import type? For explicitness (Element is a global in Flow that means DOM element).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without the import flow fails:
image

Flow doc suggests to import React as a namespace with import * as React from 'react' instead of as a default with import React from 'react'.
https://flow.org/en/docs/react/components/
But I thought it was a big change and I decided not to do it.
Cheers

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could use this syntax: React$Element.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I guess you'd have to import * as React from 'react' at which point you'd bump into a false positive warning about accessing PropTypes in 15. Let's keep it this way then.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you do import type { Element as ReactElement } from 'react'?

import { black } from '../styles';

const _collapsibleStyle = {
Expand All @@ -35,7 +36,15 @@ const collapsibleExpandedStyle = {
marginBottom: '0.6em',
};

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

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

class Collapsible extends Component<Props, State> {
state = {
collapsed: true,
};
Expand Down
14 changes: 12 additions & 2 deletions packages/react-error-overlay/src/components/ErrorOverlay.js
Expand Up @@ -9,6 +9,7 @@

/* @flow */
import React, { Component } from 'react';
import type { Node } from 'react';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here (and everywhere below).

import { black } from '../styles';

const overlayStyle = {
Expand All @@ -31,10 +32,19 @@ const overlayStyle = {
color: black,
};

class ErrorOverlay extends Component {
type Props = {|
children: Node,
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
Expand Up @@ -65,4 +65,5 @@ function RuntimeError({ errorRecord, launchEditorEndpoint }: Props) {
);
}

export type { ErrorRecord };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please just add export to type definition itself?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apparently I can't.
image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or you probably meant:

export type ErrorRecord = {|
  error: Error,
  unhandledRejection: boolean,
  contextSize: number,
  stackFrames: StackFrame[],
|};

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's what I meant.

export default RuntimeError;
Expand Up @@ -14,8 +14,19 @@ import CloseButton from '../components/CloseButton';
import NavigationBar from '../components/NavigationBar';
import RuntimeError from './RuntimeError';
import Footer from '../components/Footer';
import type { ErrorRecord } from './RuntimeError';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please always add newline before import types?

import x from 'x';
import y from 'y';

import type { z } from 'z';

// code


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

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

class RuntimeErrorContainer extends PureComponent<Props, State> {
state = {
currentIndex: 0,
};
Expand Down Expand Up @@ -54,13 +65,14 @@ class RuntimeErrorContainer extends PureComponent {
return (
<ErrorOverlay shortcutHandler={this.shortcutHandler}>
<CloseButton close={close} />
{totalErrors > 1 &&
{totalErrors > 1 && (
<NavigationBar
currentError={this.state.currentIndex + 1}
totalErrors={totalErrors}
previous={this.previous}
next={this.next}
/>}
/>
)}
<RuntimeError
errorRecord={errorRecords[this.state.currentIndex]}
launchEditorEndpoint={this.props.launchEditorEndpoint}
Expand Down
28 changes: 20 additions & 8 deletions packages/react-error-overlay/src/containers/StackFrame.js
Expand Up @@ -12,6 +12,7 @@ import React, { Component } from 'react';
import CodeBlock from './StackFrameCodeBlock';
import { getPrettyURL } from '../utils/getPrettyURL';
import { darkGray } from '../styles';
import type { StackFrame as StackFrameType } from '../utils/stack-frame';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here (and below).


const linkStyle = {
fontSize: '0.9em',
Expand Down Expand Up @@ -43,7 +44,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 Down Expand Up @@ -74,7 +87,7 @@ class StackFrame extends Component {
}

openInEditor = () => {
if (!this.canOpenInEditor()) {
if (!this.props.launchEditorEndpoint) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did this change? Seems like it's more permissive than it used to be.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was a stupid hack to fix this:
image
Basically launchEditorEndpoint prop is defined as nullable but flow doesn't understand that the check if (!this.props.launchEditorEndpoint) gets done in canOpenInEditor()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I have no idea how to fix this other than adding the same condition twice.

if (!this.props.launchEditorEndpoint && !this.canOpenInEditor()) {
      return;
    }

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can replace canOpen function with a getEditorEndpoint(): null | string function that returns null if it can't open. Then you can use this function in both places. Since it returns an endpoint you can exit if it's null early and now Flow knows it's a string.

Copy link
Contributor Author

@duvet86 duvet86 Sep 9, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I don't understand, sorry.
This is what I've done but it doesn't work and it still more permissive than before.
image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry still not working:
image

Copy link
Contributor

@gaearon gaearon Sep 11, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not working because you're not using the return value and instead read the same thing again from props. Flow doesn't know the props are immutable so it's bailing out thinking they might have changed. I am suggesting to use the return value of getEditorEndpoint that you currently ignore. If you compare it against null first and exit if it is, but then use it Flow will know it can't possibly be null.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry again, I'm even using the double equals to check for undefined and null but:
image

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are calling the function twice. Flow can’t know that the value hasn’t changed since last call.

You need to do this:

const endpointUrl = this.getEndpointUrl();
if (endpointUrl === null) {
  return;
}

// ...

// By now Flow knows endpointUrl can't possibly be null
fetch(
  `${endpointUrl}/...`
)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, now I understand.

return;
}
const {
Expand All @@ -90,7 +103,7 @@ class StackFrame extends Component {
).then(() => {}, () => {});
};

onKeyDown = (e: SyntheticKeyboardEvent) => {
onKeyDown = (e: SyntheticKeyboardEvent<>) => {
if (e.key === 'Enter') {
this.openInEditor();
}
Expand Down Expand Up @@ -155,9 +168,7 @@ class StackFrame extends Component {
const canOpenInEditor = this.canOpenInEditor();
return (
<div>
<div>
{functionName}
</div>
<div>{functionName}</div>
<div style={linkStyle}>
<a
style={canOpenInEditor ? anchorStyle : null}
Expand All @@ -168,7 +179,7 @@ class StackFrame extends Component {
{url}
</a>
</div>
{codeBlockProps &&
{codeBlockProps && (
<span>
<a
onClick={canOpenInEditor ? this.openInEditor : null}
Expand All @@ -179,7 +190,8 @@ class StackFrame extends Component {
<button style={toggleStyle} onClick={this.toggleCompiled}>
{'View ' + (compiled ? 'source' : 'compiled')}
</button>
</span>}
</span>
)}
</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
16 changes: 10 additions & 6 deletions packages/react-error-overlay/src/containers/StackTrace.js
Expand Up @@ -13,6 +13,7 @@ import StackFrame from './StackFrame';
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',
Expand All @@ -21,7 +22,14 @@ const traceStyle = {
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 Expand Up @@ -84,11 +92,7 @@ class StackTrace extends Component {
}

render() {
return (
<div style={traceStyle}>
{this.renderFrames()}
</div>
);
return <div style={traceStyle}>{this.renderFrames()}</div>;
}
}

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