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

babelPluginFlowReactPropTypes_proptype_Element is not defined #115

Open
rosskevin opened this issue Jul 10, 2017 · 5 comments
Open

babelPluginFlowReactPropTypes_proptype_Element is not defined #115

rosskevin opened this issue Jul 10, 2017 · 5 comments

Comments

@rosskevin
Copy link
Contributor

rosskevin commented Jul 10, 2017

Source (mimicing result of findDOMNode()):

/**
 * return type of ReactDOM.findDOMNode()
 *
 * NOTE: `Element` is NOT the same as `type { Element } from 'react'` a.k.a React$Element
 *
 * To use it as a typical node, check with `if (node instanceof HTMLElement) { ... }`
 */
export type DOMNode = Element | Text | null;

Usage is causing:

types.js:15 Uncaught ReferenceError: babelPluginFlowReactPropTypes_proptype_Element is not defined
    at Object.defineProperty.value (types.js:15)
    at __webpack_require__ (bootstrap 67889448ccd6ec73efec:49)
    at Object.<anonymous> (Transition.js:67)
    at __webpack_require__ (bootstrap 67889448ccd6ec73efec:49)
    at Object.defineProperty.value (Fade.js:45)
    at __webpack_require__ (bootstrap 67889448ccd6ec73efec:49)
    at Object.defineProperty.value (Modal.js:90)
    at __webpack_require__ (bootstrap 67889448ccd6ec73efec:49)
    at Object.defineProperty.value (Dialog.js:36)
    at __webpack_require__ (bootstrap 67889448ccd6ec73efec:49)

/see mui/material-ui#7400

Does use of the DOM Element require an explicit import type for compatibility with this plugin?

@rosskevin
Copy link
Contributor Author

It seems like we are missing a declaration or require for

Here is the entire build file for that:

"use strict";

/**
 * return type of ReactDOM.findDOMNode()
 *
 * NOTE: `Element` is NOT the same as `type { Element } from 'react'` a.k.a React$Element
 *
 * To use it as a typical node, check with `if (node instanceof HTMLElement) { ... }`
 */
if (typeof exports !== "undefined") Object.defineProperty(exports, "babelPluginFlowReactPropTypes_proptype_DOMNode", {
  value:
    require("prop-types").oneOfType(
      [
        typeof babelPluginFlowReactPropTypes_proptype_Element === "function" ? babelPluginFlowReactPropTypes_proptype_Element : require("prop-types").shape(babelPluginFlowReactPropTypes_proptype_Element),
        typeof Text === "function" ? require("prop-types").instanceOf(Text) : require("prop-types").any
      ]
    ),
  configurable: true
});

Nowhere in this file is babelPluginFlowReactPropTypes_proptype_Element declared or required.

@rosskevin
Copy link
Contributor Author

Compared to when we encounter import type { Element } from 'react', which generates:

var babelPluginFlowReactPropTypes_proptype_Element = require('react').babelPluginFlowReactPropTypes_proptype_Element || require('prop-types').any;

It seems like this plugin isn't recognizing global types from the dom. Am I on the right track?

brigand added a commit that referenced this issue Jul 10, 2017
@brigand
Copy link
Owner

brigand commented Jul 10, 2017

Yeah, we don't have a list of global types.

So, currently the solution is:

const Element = window.Element;

type FooProps = {
  element: Element,
}

class Foo extends React.Component {
  props: FooProps;

  render() { return null; }
}

This does an instanceOf check.

element: typeof Element === \\"function\\" 
  ? require(\\"prop-types\\").instanceOf(Element).isRequired 
  : require(\\"prop-types\\").any.isRequired

Are you thinking to have a list of global types flow supports, and do instanceOf checks on them if no type with that name is imported or defined?

@rosskevin
Copy link
Contributor Author

Thanks for the workaround. I was wondering what to do, it seems a bit overzealous to interpret as a global if not defined, but I guess that's how it works in the browser anyway.

So, I think implementing a whitelisted set of globals could be the correct solution here.

@rosskevin
Copy link
Contributor Author

It seems the workaround didn't work.

Source:

const Element = window.Element;
const Text = window.Text;
export type DOMNode = Element | Text | null;

build:

var Element = window.Element;
var Text = window.Text;
if (typeof exports !== "undefined") Object.defineProperty(exports, "babelPluginFlowReactPropTypes_proptype_DOMNode", {
  value:
    require("prop-types").oneOfType([
      typeof babelPluginFlowReactPropTypes_proptype_Element === "function" ? babelPluginFlowReactPropTypes_proptype_Element : require("prop-types").shape(babelPluginFlowReactPropTypes_proptype_Element),
      typeof Text === "function" ? require("prop-types").instanceOf(Text) : require("prop-types").any
    ]),
  configurable: true
});

Mayank1791989 pushed a commit to Mayank1791989/babel-plugin-flow-react-proptypes that referenced this issue Jul 21, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants