babel-plugin-flow-react-proptypes icon indicating copy to clipboard operation
babel-plugin-flow-react-proptypes copied to clipboard

Undefined variable in output when input file contains two classes using same prop types

Open kevinbarabash opened this issue 5 years ago • 4 comments

Example code:

import * as React from "react";
 import type {RowProps} from "./other-row.jsx";
 type StaticProps = RowProps & {
    domain: string,
};
 class TableOfContentsRow extends React.Component<StaticProps> {
    render() {
        return <div>I'm a row</div>;
    }
}
 export default class TableOfContentsRowWithQuery extends React.Component<StaticProps> {
    render() {
        return <TableOfContentsRow/>;
    }
}

Error:

ReferenceError: bpfrpt_proptype_RowProps is not defined

This error is trigged by the following code in the output:

_defineProperty(TableOfContentsRowWithQuery, \\"propTypes\\", Object.assign({}, bpfrpt_proptype_RowProps === _propTypes.default.any ? {} : _otherRow.bpfrpt_proptype_RowProps, {
  domain: _propTypes.default.string.isRequired
}));"

https://github.com/Khan/babel-plugin-flow-react-proptypes/commit/f43dc4087b2de43d94d3c1e05c0a267c7aed2f59 contains the test case and a snapshot with the error.

kevinbarabash avatar Apr 16 '19 21:04 kevinbarabash

~I think this is an issue with the babel plugins/presets I have configured. I'm trying to make my setup match the setup used in the tests.~

kevinbarabash avatar Apr 16 '19 22:04 kevinbarabash

It turns out it wasn't a configuration issue, but rather an issue when there are multiple components in the same file while using imported types. I'll try to create a minimal reproducible test case, but in the meantime I should be able to work around this by splitting the components into separate files.

kevinbarabash avatar Apr 16 '19 23:04 kevinbarabash

Splitting up the files did indeed solve the issue.

kevinbarabash avatar Apr 16 '19 23:04 kevinbarabash

Disabling the @babel/env preset in the test outputs the following code which has no undefined variables:

// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`import-object 1`] = `
"function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

import PropTypes from \\"prop-types\\";
import { bpfrpt_proptype_RowProps } from \\"./other-row.jsx\\";
import * as React from \\"react\\";

class TableOfContentsRow extends React.Component {
  render() {
    return React.createElement(\\"div\\", null, \\"I'm a row\\");
  }

}

_defineProperty(TableOfContentsRow, \\"propTypes\\", Object.assign({}, bpfrpt_proptype_RowProps === PropTypes.any ? {} : bpfrpt_proptype_RowProps, {
  domain: PropTypes.string.isRequired
}));

export default class TableOfContentsRowWithQuery extends React.Component {
  render() {
    return React.createElement(TableOfContentsRow, null);
  }

}

_defineProperty(TableOfContentsRowWithQuery, \\"propTypes\\", Object.assign({}, bpfrpt_proptype_RowProps === PropTypes.any ? {} : bpfrpt_proptype_RowProps, {
  domain: PropTypes.string.isRequired
}));"
`;

I tried pasting this into the babeljs.io repl and turned on @babel/env and it does the right thing. There must be some subtle interaction when @babel/env is run on the modified AST chunks that this plugin generates.

kevinbarabash avatar Apr 17 '19 16:04 kevinbarabash