Repository: brigand/babel-plugin-flow-react-proptypes
Pull request #93 (2017-05-28 13:41:31)

Fix required for imported types
I just noticed this was broken while working on #92. The isRequired flag was never set on imported types.

This PR includes #92; so perhaps review that one first. I'll be happy to rebase, but I do require most changes from that PR here.

See e167278fb15835f41b986aebc80d62a292e9219a and newer.

brigand: Merged the other PR, please rebase.
mhaas: @brigand Done. I'd be very happy if you could push another release :)

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 2753f0b..cf462da 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,7 @@
 
   - Add support for top-level propTypes assignment of imported types (#88)
   - Add support for instanceOf (see PR #6, #92)
+  - Fix isRequired for imported types (if they are not functions)
 
 ## 3.0.0
 
diff --git a/src/makePropTypesAst.js b/src/makePropTypesAst.js
index ab4555f..b2b8df5 100644
--- a/src/makePropTypesAst.js
+++ b/src/makePropTypesAst.js
@@ -214,13 +214,16 @@ function makePropType(data, isExact) {
     // These are either - at run-time - objects or functions. Objects are wrapped in a shape;
     // for functions, we assume that the variable already contains a proptype assertion
     const variableNode = t.identifier(data.value);
-    const shapeNode = t.callExpression(
+    let shapeNode = t.callExpression(
         t.memberExpression(
             makePropTypeImportNode(),
             t.identifier('shape'),
         ),
         [variableNode],
     );
+    if (data.isRequired) {
+      shapeNode = markNodeAsRequired(shapeNode);
+    }
     const functionCheckNode = makeFunctionCheckAST(variableNode);
     node = t.conditionalExpression(functionCheckNode, variableNode, shapeNode);
   }


Test patch:
diff --git a/src/__tests__/__snapshots__/class-inline-props-test.js.snap b/src/__tests__/__snapshots__/class-inline-props-test.js.snap
index 05f8e0c..3a583d3 100644
--- a/src/__tests__/__snapshots__/class-inline-props-test.js.snap
+++ b/src/__tests__/__snapshots__/class-inline-props-test.js.snap
@@ -38,7 +38,7 @@ var Foo = function (_React$Component) {
 
 Foo.propTypes = {
   a_number: require(\'prop-types\').number.isRequired,
-  external: typeof babelPluginFlowReactPropTypes_proptype_ExternalType === \'function\' ? babelPluginFlowReactPropTypes_proptype_ExternalType : require(\'prop-types\').shape(babelPluginFlowReactPropTypes_proptype_ExternalType)
+  external: typeof babelPluginFlowReactPropTypes_proptype_ExternalType === \'function\' ? babelPluginFlowReactPropTypes_proptype_ExternalType : require(\'prop-types\').shape(babelPluginFlowReactPropTypes_proptype_ExternalType).isRequired
 };
 exports.default = Foo;"
 `;
diff --git a/src/__tests__/__snapshots__/import-non-relative.js.snap b/src/__tests__/__snapshots__/import-non-relative.js.snap
index 82fe2d5..c0babe7 100644
--- a/src/__tests__/__snapshots__/import-non-relative.js.snap
+++ b/src/__tests__/__snapshots__/import-non-relative.js.snap
@@ -24,8 +24,8 @@ var C = function (_React$Component) {
 }(React.Component);
 
 C.propTypes = {
-  an_imported_named_type: typeof babelPluginFlowReactPropTypes_proptype_NamedType === \'function\' ? babelPluginFlowReactPropTypes_proptype_NamedType : require(\'prop-types\').shape(babelPluginFlowReactPropTypes_proptype_NamedType),
-  an_imported_default_type: typeof babelPluginFlowReactPropTypes_proptype_DefaultType === \'function\' ? babelPluginFlowReactPropTypes_proptype_DefaultType : require(\'prop-types\').shape(babelPluginFlowReactPropTypes_proptype_DefaultType),
+  an_imported_named_type: typeof babelPluginFlowReactPropTypes_proptype_NamedType === \'function\' ? babelPluginFlowReactPropTypes_proptype_NamedType : require(\'prop-types\').shape(babelPluginFlowReactPropTypes_proptype_NamedType).isRequired,
+  an_imported_default_type: typeof babelPluginFlowReactPropTypes_proptype_DefaultType === \'function\' ? babelPluginFlowReactPropTypes_proptype_DefaultType : require(\'prop-types\').shape(babelPluginFlowReactPropTypes_proptype_DefaultType).isRequired,
   a_global_type: typeof Date === \'function\' ? require(\'prop-types\').instanceOf(Date).isRequired : require(\'prop-types\').any.isRequired,
   a_undefined_type: typeof FooBarBaz === \'function\' ? require(\'prop-types\').instanceOf(FooBarBaz).isRequired : require(\'prop-types\').any.isRequired
 };
diff --git a/src/__tests__/__snapshots__/import-object-test.js.snap b/src/__tests__/__snapshots__/import-object-test.js.snap
index c61071c..f39edcb 100644
--- a/src/__tests__/__snapshots__/import-object-test.js.snap
+++ b/src/__tests__/__snapshots__/import-object-test.js.snap
@@ -24,8 +24,10 @@ var C = function (_React$Component) {
 }(React.Component);
 
 C.propTypes = {
-  an_imported_named_type: typeof babelPluginFlowReactPropTypes_proptype_NamedType === \'function\' ? babelPluginFlowReactPropTypes_proptype_NamedType : require(\'prop-types\').shape(babelPluginFlowReactPropTypes_proptype_NamedType),
-  an_imported_default_type: typeof babelPluginFlowReactPropTypes_proptype_DefaultType === \'function\' ? babelPluginFlowReactPropTypes_proptype_DefaultType : require(\'prop-types\').shape(babelPluginFlowReactPropTypes_proptype_DefaultType),
+  an_imported_named_type: typeof babelPluginFlowReactPropTypes_proptype_NamedType === \'function\' ? babelPluginFlowReactPropTypes_proptype_NamedType : require(\'prop-types\').shape(babelPluginFlowReactPropTypes_proptype_NamedType).isRequired,
+  an_imported_default_type: typeof babelPluginFlowReactPropTypes_proptype_DefaultType === \'function\' ? babelPluginFlowReactPropTypes_proptype_DefaultType : require(\'prop-types\').shape(babelPluginFlowReactPropTypes_proptype_DefaultType).isRequired,
+  optional_property: typeof babelPluginFlowReactPropTypes_proptype_NamedType === \'function\' ? babelPluginFlowReactPropTypes_proptype_NamedType : require(\'prop-types\').shape(babelPluginFlowReactPropTypes_proptype_NamedType),
+  can_be_null_property: typeof babelPluginFlowReactPropTypes_proptype_DefaultType === \'function\' ? babelPluginFlowReactPropTypes_proptype_DefaultType : require(\'prop-types\').shape(babelPluginFlowReactPropTypes_proptype_DefaultType),
   a_global_type: typeof Date === \'function\' ? require(\'prop-types\').instanceOf(Date).isRequired : require(\'prop-types\').any.isRequired,
   a_undefined_type: typeof FooBarBaz === \'function\' ? require(\'prop-types\').instanceOf(FooBarBaz).isRequired : require(\'prop-types\').any.isRequired
 };
diff --git a/src/__tests__/__snapshots__/intersection-complex-example.js.snap b/src/__tests__/__snapshots__/intersection-complex-example.js.snap
index 8a4f4b3..46a6729 100644
--- a/src/__tests__/__snapshots__/intersection-complex-example.js.snap
+++ b/src/__tests__/__snapshots__/intersection-complex-example.js.snap
@@ -29,7 +29,7 @@ if (typeof exports !== \"undefined\") Object.defineProperty(exports, \"babelPlug
 });
 if (typeof exports !== \"undefined\") Object.defineProperty(exports, \"babelPluginFlowReactPropTypes_proptype_X\", {
   value: Object.assign({}, babelPluginFlowReactPropTypes_proptype_NamedType === require(\"prop-types\").any ? {} : babelPluginFlowReactPropTypes_proptype_NamedType, {
-    a: typeof babelPluginFlowReactPropTypes_proptype_NamedType === \"function\" ? babelPluginFlowReactPropTypes_proptype_NamedType : require(\"prop-types\").shape(babelPluginFlowReactPropTypes_proptype_NamedType),
+    a: typeof babelPluginFlowReactPropTypes_proptype_NamedType === \"function\" ? babelPluginFlowReactPropTypes_proptype_NamedType : require(\"prop-types\").shape(babelPluginFlowReactPropTypes_proptype_NamedType).isRequired,
     b: require(\"prop-types\").string.isRequired
   }, babelPluginFlowReactPropTypes_proptype_DefaultType === require(\"prop-types\").any ? {} : babelPluginFlowReactPropTypes_proptype_DefaultType, {
     bar: require(\"prop-types\").number.isRequired
@@ -81,9 +81,9 @@ var D = function (_React$Component2) {
 
 D.propTypes = Object.assign({}, {
   c: require(\"prop-types\").string.isRequired,
-  b: typeof babelPluginFlowReactPropTypes_proptype_NamedType === \"function\" ? babelPluginFlowReactPropTypes_proptype_NamedType : require(\"prop-types\").shape(babelPluginFlowReactPropTypes_proptype_NamedType)
+  b: typeof babelPluginFlowReactPropTypes_proptype_NamedType === \"function\" ? babelPluginFlowReactPropTypes_proptype_NamedType : require(\"prop-types\").shape(babelPluginFlowReactPropTypes_proptype_NamedType).isRequired
 }, Object.assign({}, babelPluginFlowReactPropTypes_proptype_NamedType === require(\"prop-types\").any ? {} : babelPluginFlowReactPropTypes_proptype_NamedType, {
-  a: typeof babelPluginFlowReactPropTypes_proptype_NamedType === \"function\" ? babelPluginFlowReactPropTypes_proptype_NamedType : require(\"prop-types\").shape(babelPluginFlowReactPropTypes_proptype_NamedType),
+  a: typeof babelPluginFlowReactPropTypes_proptype_NamedType === \"function\" ? babelPluginFlowReactPropTypes_proptype_NamedType : require(\"prop-types\").shape(babelPluginFlowReactPropTypes_proptype_NamedType).isRequired,
   b: require(\"prop-types\").string.isRequired
 }, babelPluginFlowReactPropTypes_proptype_DefaultType === require(\"prop-types\").any ? {} : babelPluginFlowReactPropTypes_proptype_DefaultType, {
   bar: require(\"prop-types\").number.isRequired
diff --git a/src/__tests__/__snapshots__/intersection-deeply-nested.js.snap b/src/__tests__/__snapshots__/intersection-deeply-nested.js.snap
index 932d69f..ed6e694 100644
--- a/src/__tests__/__snapshots__/intersection-deeply-nested.js.snap
+++ b/src/__tests__/__snapshots__/intersection-deeply-nested.js.snap
@@ -33,7 +33,7 @@ var D = function (_React$Component) {
 
 D.propTypes = Object.assign({}, babelPluginFlowReactPropTypes_proptype_NamedType === require(\"prop-types\").any ? {} : babelPluginFlowReactPropTypes_proptype_NamedType, {
   foo: require(\"prop-types\").number.isRequired,
-  bar: typeof babelPluginFlowReactPropTypes_proptype_NamedType === \"function\" ? babelPluginFlowReactPropTypes_proptype_NamedType : require(\"prop-types\").shape(babelPluginFlowReactPropTypes_proptype_NamedType),
+  bar: typeof babelPluginFlowReactPropTypes_proptype_NamedType === \"function\" ? babelPluginFlowReactPropTypes_proptype_NamedType : require(\"prop-types\").shape(babelPluginFlowReactPropTypes_proptype_NamedType).isRequired,
   baz: require(\"prop-types\").shape(Object.assign({}, babelPluginFlowReactPropTypes_proptype_NamedType === require(\"prop-types\").any ? {} : babelPluginFlowReactPropTypes_proptype_NamedType, babelPluginFlowReactPropTypes_proptype_OtherNamedType === require(\"prop-types\").any ? {} : babelPluginFlowReactPropTypes_proptype_OtherNamedType, {
     foo: require(\"prop-types\").oneOf([\"number\"]).isRequired,
     bar: require(\"prop-types\").shape(Object.assign({}, babelPluginFlowReactPropTypes_proptype_NamedType === require(\"prop-types\").any ? {} : babelPluginFlowReactPropTypes_proptype_NamedType, babelPluginFlowReactPropTypes_proptype_OtherNamedType === require(\"prop-types\").any ? {} : babelPluginFlowReactPropTypes_proptype_OtherNamedType)).isRequired
diff --git a/src/__tests__/__snapshots__/intersection-inline-with-exported-and-nested-imported-type.js.snap b/src/__tests__/__snapshots__/intersection-inline-with-exported-and-nested-imported-type.js.snap
index c170c8c..2aa7ad3 100644
--- a/src/__tests__/__snapshots__/intersection-inline-with-exported-and-nested-imported-type.js.snap
+++ b/src/__tests__/__snapshots__/intersection-inline-with-exported-and-nested-imported-type.js.snap
@@ -29,7 +29,7 @@ var MyComponent = function (_React$Component) {
 
 MyComponent.propTypes = {
   foo: require(\"prop-types\").string.isRequired,
-  baz: typeof babelPluginFlowReactPropTypes_proptype_NamedType === \"function\" ? babelPluginFlowReactPropTypes_proptype_NamedType : require(\"prop-types\").shape(babelPluginFlowReactPropTypes_proptype_NamedType),
+  baz: typeof babelPluginFlowReactPropTypes_proptype_NamedType === \"function\" ? babelPluginFlowReactPropTypes_proptype_NamedType : require(\"prop-types\").shape(babelPluginFlowReactPropTypes_proptype_NamedType).isRequired,
   bar: require(\"prop-types\").number.isRequired
 };"
 `;
diff --git a/src/__tests__/import-object-test.js b/src/__tests__/import-object-test.js
index 2388051..81a1393 100644
--- a/src/__tests__/import-object-test.js
+++ b/src/__tests__/import-object-test.js
@@ -6,6 +6,8 @@ import type DefaultType from './bar';
 type FooProps = {
   an_imported_named_type: NamedType,
   an_imported_default_type: DefaultType,
+  optional_property?: NamedType,
+  can_be_null_property: ?DefaultType,
   a_global_type: Date,
   a_undefined_type: FooBarBaz,
 };