From 11dc847ec9c491f5a8eeff1300bca02e040f5889 Mon Sep 17 00:00:00 2001 From: Joshua Gross Date: Tue, 20 Aug 2019 22:26:57 -0700 Subject: [PATCH] Give a more explicit message about why `number` isn't supported Summary: Give a more explicit message about why `number` isn't supported Reviewed By: rickhanlonii Differential Revision: D16926698 fbshipit-source-id: 292cb13aa11205e1350209178bde1977a2a7ad4c --- .../components/__test_fixtures__/failures.js | 30 +++++++++++++++++++ .../component-parser-test.js.snap | 2 ++ .../src/parsers/flow/components/props.js | 4 +++ 3 files changed, 36 insertions(+) diff --git a/packages/react-native-codegen/src/parsers/flow/components/__test_fixtures__/failures.js b/packages/react-native-codegen/src/parsers/flow/components/__test_fixtures__/failures.js index 7ce34a5e5de..c814c352948 100644 --- a/packages/react-native-codegen/src/parsers/flow/components/__test_fixtures__/failures.js +++ b/packages/react-native-codegen/src/parsers/flow/components/__test_fixtures__/failures.js @@ -403,6 +403,35 @@ export default (codegenNativeComponent( ): NativeComponent); `; +const PROP_NUMBER_TYPE = ` +/** + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @format + * @flow + */ + +'use strict'; + +import type {ViewProps} from 'ViewPropTypes'; +import type {NativeComponent} from 'codegenNativeComponent'; + +const codegenNativeComponent = require('codegenNativeComponent'); + +export type ModuleProps = $ReadOnly<{| + ...ViewProps, + + someProp: number +|}>; + +export default (codegenNativeComponent( + 'Module', +): NativeComponent); +`; + module.exports = { COMMANDS_DEFINED_INLINE, COMMANDS_DEFINED_MULTIPLE_TIMES, @@ -415,4 +444,5 @@ module.exports = { PROPS_CONFLICT_NAMES, PROPS_CONFLICT_WITH_SPREAD_PROPS, PROPS_SPREAD_CONFLICTS_WITH_PROPS, + PROP_NUMBER_TYPE, }; diff --git a/packages/react-native-codegen/src/parsers/flow/components/__tests__/__snapshots__/component-parser-test.js.snap b/packages/react-native-codegen/src/parsers/flow/components/__tests__/__snapshots__/component-parser-test.js.snap index 958a44b947e..83b2d69a2d2 100644 --- a/packages/react-native-codegen/src/parsers/flow/components/__tests__/__snapshots__/component-parser-test.js.snap +++ b/packages/react-native-codegen/src/parsers/flow/components/__tests__/__snapshots__/component-parser-test.js.snap @@ -16,6 +16,8 @@ exports[`RN Codegen Flow Parser Fails with error message NON_OPTIONAL_KEY_WITH_D exports[`RN Codegen Flow Parser Fails with error message NULLABLE_WITH_DEFAULT 1`] = `"WithDefault<> is optional and does not need to be marked as optional. Please remove the ? annotation in front of it."`; +exports[`RN Codegen Flow Parser Fails with error message PROP_NUMBER_TYPE 1`] = `"Cannot use \\"NumberTypeAnnotation\\" type annotation for \\"someProp\\": must use a specific numeric type like Int32, Double, or Float"`; + exports[`RN Codegen Flow Parser Fails with error message PROPS_CONFLICT_NAMES 1`] = `"A prop was already defined with the name isEnabled"`; exports[`RN Codegen Flow Parser Fails with error message PROPS_CONFLICT_WITH_SPREAD_PROPS 1`] = `"A prop was already defined with the name isEnabled"`; diff --git a/packages/react-native-codegen/src/parsers/flow/components/props.js b/packages/react-native-codegen/src/parsers/flow/components/props.js index 4489691da06..3bd8591d9f4 100644 --- a/packages/react-native-codegen/src/parsers/flow/components/props.js +++ b/packages/react-native-codegen/src/parsers/flow/components/props.js @@ -229,6 +229,10 @@ function getTypeAnnotation(name, annotation, defaultValue, types) { }; } throw new Error(`A default enum value is required for "${name}"`); + case 'NumberTypeAnnotation': + throw new Error( + `Cannot use "${type}" type annotation for "${name}": must use a specific numeric type like Int32, Double, or Float`, + ); default: (type: empty); throw new Error(`Unknown prop type for "${name}": "${type}"`);