Switch native codegen over to flow parser

Summary:
This diff switches the native codegen over to the flow parser

It does this by:
- Creating a new e2e directory
- Migrating the schema.js fixtures to flow types in  e2e/
- Updating the buck tests to use the flow type fixtures
- Finally, updating the rest of rn_codegen to use *NativeComponent instead of *Schema.js

Removing all of the schemas in the next diff to keep this one clean

Reviewed By: cpojer

Differential Revision: D15960603

fbshipit-source-id: 3df28b31e618491301578ab7f6e28a80f55404b2
This commit is contained in:
Rick Hanlon 2019-06-27 08:00:40 -07:00 коммит произвёл Facebook Github Bot
Родитель ea817fd7f5
Коммит 98b03fa1b9
24 изменённых файлов: 472 добавлений и 240 удалений

Просмотреть файл

@ -3,18 +3,7 @@ load("@fbsource//tools/build_defs:fb_native_wrapper.bzl", "fb_native")
load("@fbsource//tools/build_defs:fb_xplat_cxx_binary.bzl", "fb_xplat_cxx_binary")
load("@fbsource//tools/build_defs/oss:rn_defs.bzl", "rn_xplat_cxx_library")
load("@fbsource//tools/build_defs/third_party:yarn_defs.bzl", "yarn_workspace")
load("@fbsource//xplat/js/react-native-github/packages/react-native-codegen:DEFS.bzl", "rn_codegen_test")
fb_native.sh_binary(
name = "copy_fixture_schema",
main = "buck_tests/copy_fixture.sh",
resources = [
"buck_tests/copy-fixture.js",
"buck_tests/copy_fixture.sh",
"fbsource//xplat/js:setup_env",
"src/generators/__test_fixtures__/fixtures.js",
],
)
load("@fbsource//xplat/js/react-native-github/packages/react-native-codegen:DEFS.bzl", "rn_codegen")
fb_native.sh_binary(
name = "write_to_json",
@ -44,64 +33,20 @@ fb_native.sh_binary(
visibility = ["PUBLIC"],
)
rn_codegen_test(
fixture_name = "INTERFACE_ONLY",
fb_native.genrule(
name = "codegen_tests_schema",
srcs = glob(
[
"**/e2e/__test_fixtures__/*NativeComponent.js",
],
),
cmd = "$(exe fbsource//xplat/js/react-native-github/packages/react-native-codegen:write_to_json) $OUT $SRCS",
out = "schema-codegen_tests.json",
)
rn_codegen_test(
fixture_name = "BOOLEAN_PROP",
)
rn_codegen_test(
fixture_name = "STRING_PROP",
)
rn_codegen_test(
fixture_name = "INTEGER_PROPS",
)
rn_codegen_test(
fixture_name = "FLOAT_PROPS",
)
rn_codegen_test(
fixture_name = "COLOR_PROP",
)
rn_codegen_test(
fixture_name = "IMAGE_PROP",
)
rn_codegen_test(
fixture_name = "POINT_PROP",
)
rn_codegen_test(
fixture_name = "ARRAY_PROPS",
)
rn_codegen_test(
fixture_name = "MULTI_NATIVE_PROP",
)
rn_codegen_test(
fixture_name = "ENUM_PROP",
)
rn_codegen_test(
fixture_name = "EVENT_PROPS",
)
rn_codegen_test(
fixture_name = "EVENT_NESTED_OBJECT_PROPS",
)
rn_codegen_test(
fixture_name = "TWO_COMPONENTS_SAME_FILE",
)
rn_codegen_test(
fixture_name = "TWO_COMPONENTS_DIFFERENT_FILES",
rn_codegen(
name = "codegen_tests",
schema_target = ":codegen_tests_schema",
)
fb_xplat_cxx_binary(
@ -120,21 +65,7 @@ fb_xplat_cxx_binary(
],
visibility = ["PUBLIC"],
deps = [
":generated_components-ARRAY_PROPS",
":generated_components-BOOLEAN_PROP",
":generated_components-COLOR_PROP",
":generated_components-ENUM_PROP",
":generated_components-EVENT_NESTED_OBJECT_PROPS",
":generated_components-EVENT_PROPS",
":generated_components-FLOAT_PROPS",
":generated_components-IMAGE_PROP",
":generated_components-INTEGER_PROPS",
":generated_components-INTERFACE_ONLY",
":generated_components-MULTI_NATIVE_PROP",
":generated_components-POINT_PROP",
":generated_components-STRING_PROP",
":generated_components-TWO_COMPONENTS_DIFFERENT_FILES",
":generated_components-TWO_COMPONENTS_SAME_FILE",
":generated_components-codegen_tests",
],
)
@ -157,21 +88,7 @@ rn_xplat_cxx_library(
"PUBLIC",
],
deps = [
":generated_components-ARRAY_PROPS",
":generated_components-BOOLEAN_PROP",
":generated_components-COLOR_PROP",
":generated_components-ENUM_PROP",
":generated_components-EVENT_NESTED_OBJECT_PROPS",
":generated_components-EVENT_PROPS",
":generated_components-FLOAT_PROPS",
":generated_components-IMAGE_PROP",
":generated_components-INTEGER_PROPS",
":generated_components-INTERFACE_ONLY",
":generated_components-MULTI_NATIVE_PROP",
":generated_components-POINT_PROP",
":generated_components-STRING_PROP",
":generated_components-TWO_COMPONENTS_DIFFERENT_FILES",
":generated_components-TWO_COMPONENTS_SAME_FILE",
":generated_components-codegen_tests",
],
)

Просмотреть файл

@ -14,19 +14,6 @@ load(
"rn_xplat_cxx_library",
)
def rn_codegen_test(
fixture_name = ""):
copy_schema_name = "copy_schema-{}".format(fixture_name)
fb_native.genrule(
name = copy_schema_name,
srcs = [],
cmd = "$(exe fbsource//xplat/js/react-native-github/packages/react-native-codegen:copy_fixture_schema) {} $OUT".format(fixture_name),
out = "schema-{}.json".format(fixture_name),
)
rn_codegen(fixture_name, ":{}".format(copy_schema_name))
def rn_codegen(
name = "",
schema_target = ""):

Просмотреть файл

@ -1,33 +0,0 @@
/**
* 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.
*
* @flow
* @format
*/
'use strict';
const fs = require('fs');
const fixtures = require('../src/generators/__test_fixtures__/fixtures.js');
const args = process.argv.slice(2);
if (args.length !== 2) {
throw new Error(
'Expected to receive the fixture name and output directory as the only arg',
);
}
const fixtureName = args[0];
const outputPath = args[1];
const fixture = fixtures[fixtureName];
if (fixture == null) {
throw new Error(`Can't find fixture with name ${fixtureName}`);
}
fs.writeFileSync(outputPath, JSON.stringify(fixture, null, 2));

Просмотреть файл

@ -1,11 +0,0 @@
#!/bin/bash
set -e
set -u
THIS_DIR=$(cd -P "$(dirname "$(readlink "${BASH_SOURCE[0]}" || echo "${BASH_SOURCE[0]}")")" && pwd)
# shellcheck source=xplat/js/env-utils/setup_env_vars.sh
source "$THIS_DIR/../../../../env-utils/setup_env_vars.sh"
exec "$FLOW_NODE_BINARY" "$THIS_DIR/copy-fixture.js" "$@"

Просмотреть файл

@ -1,18 +1,6 @@
#import <react/components/ARRAY_PROPS/ComponentDescriptors.h>
#import <react/components/INTERFACE_ONLY/ComponentDescriptors.h>
#import <react/components/BOOLEAN_PROP/ComponentDescriptors.h>
#import <react/components/STRING_PROP/ComponentDescriptors.h>
#import <react/components/INTEGER_PROPS/ComponentDescriptors.h>
#import <react/components/FLOAT_PROPS/ComponentDescriptors.h>
#import <react/components/COLOR_PROP/ComponentDescriptors.h>
#import <react/components/IMAGE_PROP/ComponentDescriptors.h>
#import <react/components/POINT_PROP/ComponentDescriptors.h>
#import <react/components/MULTI_NATIVE_PROP/ComponentDescriptors.h>
#import <react/components/ENUM_PROP/ComponentDescriptors.h>
#import <react/components/EVENT_NESTED_OBJECT_PROPS/ComponentDescriptors.h>
#import <react/components/EVENT_PROPS/ComponentDescriptors.h>
#import <react/components/TWO_COMPONENTS_SAME_FILE/ComponentDescriptors.h>
#import <react/components/TWO_COMPONENTS_DIFFERENT_FILES/ComponentDescriptors.h>
#import <react/components/codegen_tests/ComponentDescriptors.h>
// TODO: Import every prop and event to asset they're generated
int main(){
return 0;

Просмотреть файл

@ -0,0 +1,40 @@
/**
* 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 {
PointValue,
ColorValue,
} from '../../../../Libraries/StyleSheet/StyleSheetTypes';
import type {ImageSource} from '../../../../Libraries/Image/ImageSource';
import type {
Int32,
Float,
WithDefault,
} from '../../../../Libraries/Types/CodegenTypes';
import type {ViewProps} from '../../../../Libraries/Components/View/ViewPropTypes';
import codegenNativeComponent from '../../../../Libraries/Utilities/codegenNativeComponent';
type NativeProps = $ReadOnly<{|
...ViewProps,
// Props
names?: $ReadOnlyArray<string>,
disableds?: $ReadOnlyArray<boolean>,
progress?: $ReadOnlyArray<Int32>,
radii?: $ReadOnlyArray<Float>,
colors?: $ReadOnlyArray<ColorValue>,
srcs?: $ReadOnlyArray<ImageSource>,
points?: $ReadOnlyArray<PointValue>,
sizes?: WithDefault<$ReadOnlyArray<'small' | 'large'>, 'small'>,
|}>;
export default codegenNativeComponent<NativeProps>('ArrayPropsNativeComponent');

Просмотреть файл

@ -0,0 +1,26 @@
/**
* 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 {WithDefault} from '../../../../Libraries/Types/CodegenTypes';
import type {ViewProps} from '../../../../Libraries/Components/View/ViewPropTypes';
import codegenNativeComponent from '../../../../Libraries/Utilities/codegenNativeComponent';
type NativeProps = $ReadOnly<{|
...ViewProps,
// Props
disabled?: WithDefault<boolean, false>,
|}>;
export default codegenNativeComponent<NativeProps>(
'BooleanPropNativeComponent',
);

Просмотреть файл

@ -0,0 +1,24 @@
/**
* 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 {ColorValue} from '../../../../Libraries/StyleSheet/StyleSheetTypes';
import type {ViewProps} from '../../../../Libraries/Components/View/ViewPropTypes';
import codegenNativeComponent from '../../../../Libraries/Utilities/codegenNativeComponent';
type NativeProps = $ReadOnly<{|
...ViewProps,
// Props
tintColor?: ColorValue,
|}>;
export default codegenNativeComponent<NativeProps>('ColorPropNativeComponent');

Просмотреть файл

@ -0,0 +1,24 @@
/**
* 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 {WithDefault} from '../../../../Libraries/Types/CodegenTypes';
import type {ViewProps} from '../../../../Libraries/Components/View/ViewPropTypes';
import codegenNativeComponent from '../../../../Libraries/Utilities/codegenNativeComponent';
type NativeProps = $ReadOnly<{|
...ViewProps,
// Props
alignment?: WithDefault<'top' | 'center' | 'bottom-right', 'center'>,
|}>;
export default codegenNativeComponent<NativeProps>('EnumPropNativeComponent');

Просмотреть файл

@ -0,0 +1,43 @@
/**
* 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 {
Int32,
BubblingEvent,
WithDefault,
} from '../../../../Libraries/Types/CodegenTypes';
import type {ViewProps} from '../../../../Libraries/Components/View/ViewPropTypes';
import codegenNativeComponent from '../../../../Libraries/Utilities/codegenNativeComponent';
type OnChangeEvent = $ReadOnly<{|
location: {
source: {
url: string,
},
x: Int32,
y: Int32,
},
|}>;
type NativeProps = $ReadOnly<{|
...ViewProps,
// Props
disabled?: WithDefault<boolean, false>,
// Events
onChange?: ?(event: BubblingEvent<OnChangeEvent>) => void,
|}>;
export default codegenNativeComponent<NativeProps>(
'EventNestedObjectPropsNativeComponent',
);

Просмотреть файл

@ -0,0 +1,51 @@
/**
* 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 {
Int32,
Float,
BubblingEvent,
DirectEvent,
WithDefault,
} from '../../../../Libraries/Types/CodegenTypes';
import type {ViewProps} from '../../../../Libraries/Components/View/ViewPropTypes';
import codegenNativeComponent from '../../../../Libraries/Utilities/codegenNativeComponent';
type OnChangeEvent = $ReadOnly<{|
value: boolean,
source?: string,
progress: ?Int32,
scale?: ?Float,
|}>;
type OnEventDirect = $ReadOnly<{|
value: boolean,
|}>;
type OnOrientationChangeEvent = $ReadOnly<{|
orientation: 'landscape' | 'portrait',
|}>;
type NativeProps = $ReadOnly<{|
...ViewProps,
// Props
disabled?: WithDefault<boolean, false>,
// Events
onChange?: ?(event: BubblingEvent<OnChangeEvent>) => void,
onEventDirect?: ?(event: DirectEvent<OnEventDirect>) => void,
onOrientationChange?: ?(event: DirectEvent<OnOrientationChangeEvent>) => void,
onEnd?: ?(event: BubblingEvent<null>) => void,
|}>;
export default codegenNativeComponent<NativeProps>('EventPropsNativeComponent');

Просмотреть файл

@ -0,0 +1,32 @@
/**
* 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 {
WithDefault,
Float,
} from '../../../../Libraries/Types/CodegenTypes';
import type {ViewProps} from '../../../../Libraries/Components/View/ViewPropTypes';
import codegenNativeComponent from '../../../../Libraries/Utilities/codegenNativeComponent';
type NativeProps = $ReadOnly<{|
...ViewProps,
// Props
blurRadius?: WithDefault<Float, 0.0>,
blurRadius2?: WithDefault<Float, 0.001>,
blurRadius3?: WithDefault<Float, 2.1>,
blurRadius4?: WithDefault<Float, 0>,
blurRadius5?: WithDefault<Float, 1>,
blurRadius6?: WithDefault<Float, -0.0>,
|}>;
export default codegenNativeComponent<NativeProps>('FloatPropsNativeComponent');

Просмотреть файл

@ -0,0 +1,24 @@
/**
* 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 {ImageSource} from '../../../../Libraries/Image/ImageSource';
import type {ViewProps} from '../../../../Libraries/Components/View/ViewPropTypes';
import codegenNativeComponent from '../../../../Libraries/Utilities/codegenNativeComponent';
type NativeProps = $ReadOnly<{|
...ViewProps,
// Props
thumbImage?: ImageSource,
|}>;
export default codegenNativeComponent<NativeProps>('ImagePropNativeComponent');

Просмотреть файл

@ -0,0 +1,31 @@
/**
* 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 {
WithDefault,
Int32,
} from '../../../../Libraries/Types/CodegenTypes';
import type {ViewProps} from '../../../../Libraries/Components/View/ViewPropTypes';
import codegenNativeComponent from '../../../../Libraries/Utilities/codegenNativeComponent';
type NativeProps = $ReadOnly<{|
...ViewProps,
// Props
progress1?: WithDefault<Int32, 0>,
progress2?: WithDefault<Int32, -1>,
progress3?: WithDefault<Int32, 10>,
|}>;
export default codegenNativeComponent<NativeProps>(
'IntegerPropNativeComponent',
);

Просмотреть файл

@ -0,0 +1,36 @@
/**
* 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 {
BubblingEvent,
WithDefault,
} from '../../../../Libraries/Types/CodegenTypes';
import type {ViewProps} from '../../../../Libraries/Components/View/ViewPropTypes';
import codegenNativeComponent from '../../../../Libraries/Utilities/codegenNativeComponent';
type NativeProps = $ReadOnly<{|
...ViewProps,
// Props
accessibilityHint?: WithDefault<string, ''>,
// Events
onChange?: ?(event: BubblingEvent<$ReadOnly<{|value: boolean|}>>) => void,
|}>;
export default codegenNativeComponent<NativeProps>(
'InterfaceOnlyNativeComponent',
{
interfaceOnly: true,
paperComponentName: 'RCTInterfaceOnlyComponent',
},
);

Просмотреть файл

@ -0,0 +1,33 @@
/**
* 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 {
PointValue,
ColorValue,
} from '../../../../Libraries/StyleSheet/StyleSheetTypes';
import type {ImageSource} from '../../../../Libraries/Image/ImageSource';
import type {ViewProps} from '../../../../Libraries/Components/View/ViewPropTypes';
import codegenNativeComponent from '../../../../Libraries/Utilities/codegenNativeComponent';
type NativeProps = $ReadOnly<{|
...ViewProps,
// Props
thumbImage?: ImageSource,
color?: ColorValue,
thumbTintColor?: ColorValue,
point?: PointValue,
|}>;
export default codegenNativeComponent<NativeProps>(
'MultiNativePropNativeComponent',
);

Просмотреть файл

@ -0,0 +1,24 @@
/**
* 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 '../../../../Libraries/Components/View/ViewPropTypes';
import codegenNativeComponent from '../../../../Libraries/Utilities/codegenNativeComponent';
type NativeProps = $ReadOnly<{|
...ViewProps,
// No Props or events
|}>;
export default codegenNativeComponent<NativeProps>(
'NoPropsNoEventsNativeComponent',
);

Просмотреть файл

@ -0,0 +1,24 @@
/**
* 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 {PointValue} from '../../../../Libraries/StyleSheet/StyleSheetTypes';
import type {ViewProps} from '../../../../Libraries/Components/View/ViewPropTypes';
import codegenNativeComponent from '../../../../Libraries/Utilities/codegenNativeComponent';
type NativeProps = $ReadOnly<{|
...ViewProps,
// Props
startPoint?: PointValue,
|}>;
export default codegenNativeComponent<NativeProps>('PointPropNativeComponent');

Просмотреть файл

@ -0,0 +1,25 @@
/**
* 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 {WithDefault} from '../../../../Libraries/Types/CodegenTypes';
import type {ViewProps} from '../../../../Libraries/Components/View/ViewPropTypes';
import codegenNativeComponent from '../../../../Libraries/Utilities/codegenNativeComponent';
type NativeProps = $ReadOnly<{|
...ViewProps,
// Props
accessibilityHint?: WithDefault<string, ''>,
accessibilityRole?: WithDefault<string, null>,
|}>;
export default codegenNativeComponent<NativeProps>('StringPropNativeComponent');

Просмотреть файл

@ -7,6 +7,7 @@
"url": "git@github.com:facebook/react-native.git"
},
"dependencies": {
"flow-parser": "^0.102.0",
"jscodeshift": "^0.6.2",
"nullthrows": "^1.1.0"
},

Просмотреть файл

@ -1,14 +0,0 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`should combine files 1`] = `
Object {
"modules": Object {
"ComponentOne": Object {
"foo": "baz",
},
"ComponentTwo": Object {
"foo": "bar",
},
},
}
`;

Просмотреть файл

@ -1,52 +0,0 @@
/**
* 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.
*
* @emails oncall+react_native
* @flow strict-local
* @format
*/
import combine from '../combine-js-to-schema';
jest.mock(
'/test/module/SchemaOne',
() => ({
modules: {
ComponentOne: {
foo: 'baz',
},
},
}),
{virtual: true},
);
jest.mock(
'/test/module/SchemaTwo',
() => ({
modules: {
ComponentTwo: {
foo: 'bar',
},
},
}),
{virtual: true},
);
jest.mock('/test/module/NotASchema', () => ({}), {virtual: true});
test('should combine files', () => {
const files = [
'/test/module/SchemaOne',
'/test/module/SchemaTwo',
'/test/module/NotASchema',
];
expect(combine(files)).toMatchSnapshot();
});
test('should not throw for failed require', () => {
const files = ['/test/module/does/not/exist'];
expect(() => combine(files)).not.toThrow();
});

Просмотреть файл

@ -11,14 +11,22 @@
'use strict';
import type {SchemaType} from '../../CodegenSchema.js';
const SchemaParser = require('../../parsers/schema');
const FlowParser = require('../../parsers/flow');
const fs = require('fs');
function combineSchemas(files: Array<string>): SchemaType {
return files.reduce(
(merged, filename) => {
const schema = SchemaParser.parse(filename);
if (schema && schema.modules) {
merged.modules = {...merged.modules, ...schema.modules};
const contents = fs.readFileSync(filename, 'utf8');
if (
contents &&
/export\s+default\s+codegenNativeComponent</.test(contents)
) {
const schema = FlowParser.parseFile(filename);
if (schema && schema.modules) {
merged.modules = {...merged.modules, ...schema.modules};
}
}
return merged;
},

Просмотреть файл

@ -8,4 +8,8 @@ THIS_DIR=$(cd -P "$(dirname "$(readlink "${BASH_SOURCE[0]}" || echo "${BASH_SOUR
# shellcheck source=xplat/js/env-utils/setup_env_vars.sh
source "$THIS_DIR/../../../../../../env-utils/setup_env_vars.sh"
pushd "$THIS_DIR/../../.." >/dev/null
"$INSTALL_NODE_MODULES"
popd >/dev/null
exec "$FLOW_NODE_BINARY" "$THIS_DIR/combine-js-to-schema-cli.js" "$@"