From be3845adec324e4b3ae6efd8f85d3569f1cb60b8 Mon Sep 17 00:00:00 2001 From: "Zihan Chen (MSFT)" <53799235+ZihanChen-MSFT@users.noreply.github.com> Date: Mon, 13 Feb 2023 04:10:20 -0800 Subject: [PATCH] Add minimum necessary .d.ts files to react-native-codegen (#36102) Summary: Add minimum necessary .d.ts files to react-native-codegen. I found .d.ts files will be copied to `lib` so I guess no additional script is needed. ## Changelog [GENERAL] [CHANGED] - Add minimum necessary .d.ts files to react-native-codegen Pull Request resolved: https://github.com/facebook/react-native/pull/36102 Test Plan: `npm run build` in `packages/react-native-codegen` and see all .d.ts files appear in `lib`. Reviewed By: cortinico Differential Revision: D43157233 Pulled By: cipolleschi fbshipit-source-id: 6f122f0f4cda693ba22af6dd534e9d34d069ecac --- .../src/CodegenSchema.d.ts | 348 ++++++++++++++++++ .../src/SchemaValidator.d.ts | 11 + .../src/parsers/errors.d.ts | 10 + .../src/parsers/flow/parser.d.ts | 10 + .../src/parsers/parser.d.ts | 17 + .../src/parsers/schema/index.d.ts | 10 + .../src/parsers/typescript/parser.d.ts | 10 + 7 files changed, 416 insertions(+) create mode 100644 packages/react-native-codegen/src/CodegenSchema.d.ts create mode 100644 packages/react-native-codegen/src/SchemaValidator.d.ts create mode 100644 packages/react-native-codegen/src/parsers/errors.d.ts create mode 100644 packages/react-native-codegen/src/parsers/flow/parser.d.ts create mode 100644 packages/react-native-codegen/src/parsers/parser.d.ts create mode 100644 packages/react-native-codegen/src/parsers/schema/index.d.ts create mode 100644 packages/react-native-codegen/src/parsers/typescript/parser.d.ts diff --git a/packages/react-native-codegen/src/CodegenSchema.d.ts b/packages/react-native-codegen/src/CodegenSchema.d.ts new file mode 100644 index 00000000000000..cc12da452933d1 --- /dev/null +++ b/packages/react-native-codegen/src/CodegenSchema.d.ts @@ -0,0 +1,348 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +export type PlatformType = + | 'iOS' + | 'android'; + +export interface SchemaType { + readonly modules: { + [hasteModuleName: string]: ComponentSchema | NativeModuleSchema; + }; +} + +export interface DoubleTypeAnnotation { + readonly type: 'DoubleTypeAnnotation'; +} + +export interface FloatTypeAnnotation { + readonly type: 'FloatTypeAnnotation'; +} + +export interface BooleanTypeAnnotation { + readonly type: 'BooleanTypeAnnotation'; +} + +export interface Int32TypeAnnotation { + readonly type: 'Int32TypeAnnotation'; +} + +export interface StringTypeAnnotation { + readonly type: 'StringTypeAnnotation'; +} + +export interface StringEnumTypeAnnotation { + readonly type: 'StringEnumTypeAnnotation'; + readonly options: readonly string[]; +} + +export interface VoidTypeAnnotation { + readonly type: 'VoidTypeAnnotation'; +} + +export interface ObjectTypeAnnotation { + readonly type: 'ObjectTypeAnnotation'; + readonly properties: readonly NamedShape[]; + readonly baseTypes?: readonly string[]; +} + +export interface FunctionTypeAnnotation { + readonly type: 'FunctionTypeAnnotation'; + readonly params: readonly NamedShape

[]; + readonly returnTypeAnnotation: R; +} + +export interface NamedShape { + readonly name: string; + readonly optional: boolean; + readonly typeAnnotation: T; +} + +export interface ComponentSchema { + readonly type: 'Component'; + readonly components: { + [componentName: string]: ComponentShape; + }; +} + +export interface ComponentShape extends OptionsShape { + readonly extendsProps: readonly ExtendsPropsShape[]; + readonly events: readonly EventTypeShape[]; + readonly props: readonly NamedShape[]; + readonly commands: readonly NamedShape[]; +} + +export interface OptionsShape { + readonly interfaceOnly?: boolean; + readonly paperComponentName?: string; + readonly excludedPlatforms?: readonly PlatformType[]; + readonly paperComponentNameDeprecated?: string; +} + +export interface ExtendsPropsShape { + readonly type: 'ReactNativeBuiltInType'; + readonly knownTypeName: 'ReactNativeCoreViewProps'; +} + +export interface EventTypeShape { + readonly name: string; + readonly bubblingType: + | 'direct' + | 'bubble'; + readonly optional: boolean; + readonly paperTopLevelNameDeprecated?: string; + readonly typeAnnotation: { + readonly type: 'EventTypeAnnotation'; + readonly argument?: ObjectTypeAnnotation; + }; +} + +export type EventTypeAnnotation = + | BooleanTypeAnnotation + | StringTypeAnnotation + | DoubleTypeAnnotation + | FloatTypeAnnotation + | Int32TypeAnnotation + | StringEnumTypeAnnotation + | ObjectTypeAnnotation; + +export type PropTypeAnnotation = + | { + readonly type: 'BooleanTypeAnnotation'; + readonly default: + | boolean + | null; + } + | { + readonly type: 'StringTypeAnnotation'; + readonly default: + | string + | null; + } + | { + readonly type: 'DoubleTypeAnnotation'; + readonly default: number; + } + | { + readonly type: 'FloatTypeAnnotation'; + readonly default: + | number + | null; + } + | { + readonly type: 'Int32TypeAnnotation'; + readonly default: number; + } + | { + readonly type: 'StringEnumTypeAnnotation'; + readonly default: string; + readonly options: readonly string[]; + } + | { + readonly type: 'Int32EnumTypeAnnotation'; + readonly default: number; + readonly options: readonly number[]; + } + | ReservedPropTypeAnnotation + | ObjectTypeAnnotation + | { + readonly type: 'ArrayTypeAnnotation'; + readonly elementType: + | BooleanTypeAnnotation + | StringTypeAnnotation + | DoubleTypeAnnotation + | FloatTypeAnnotation + | Int32TypeAnnotation + | { + readonly type: 'StringEnumTypeAnnotation'; + readonly default: string; + readonly options: readonly string[]; + } + | ObjectTypeAnnotation + | ReservedPropTypeAnnotation + | { + readonly type: 'ArrayTypeAnnotation'; + readonly elementType: ObjectTypeAnnotation; + }; + }; + +export interface ReservedPropTypeAnnotation { + readonly type: 'ReservedPropTypeAnnotation'; + readonly name: + | 'ColorPrimitive' + | 'ImageSourcePrimitive' + | 'PointPrimitive' + | 'EdgeInsetsPrimitive' + | 'ImageRequestPrimitive' + | 'DimensionPrimitive'; +} + +export type CommandTypeAnnotation = FunctionTypeAnnotation; + +export type CommandParamTypeAnnotation = + | ReservedTypeAnnotation + | BooleanTypeAnnotation + | Int32TypeAnnotation + | DoubleTypeAnnotation + | FloatTypeAnnotation + | StringTypeAnnotation; + +export interface ReservedTypeAnnotation { + readonly type: 'ReservedTypeAnnotation'; + readonly name: 'RootTag'; +} + +export type Nullable = + | NullableTypeAnnotation + | T; + +export interface NullableTypeAnnotation { + readonly type: 'NullableTypeAnnotation'; + readonly typeAnnotation: T; +} + +export interface NativeModuleSchema { + readonly type: 'NativeModule'; + readonly aliasMap: NativeModuleAliasMap; + readonly enumMap: NativeModuleEnumMap; + readonly spec: NativeModuleSpec; + readonly moduleName: string; + readonly excludedPlatforms?: readonly PlatformType[]; +} + +export interface NativeModuleSpec { + readonly properties: readonly NativeModulePropertyShape[]; +} + +export type NativeModulePropertyShape = NamedShape>; + +export interface NativeModuleEnumMap { + readonly [enumName: string]: NativeModuleEnumDeclarationWithMembers; +} + +export interface NativeModuleAliasMap { + readonly [aliasName: string]: NativeModuleObjectTypeAnnotation; +} + +export type NativeModuleFunctionTypeAnnotation = FunctionTypeAnnotation, Nullable>; + +export type NativeModuleObjectTypeAnnotation = ObjectTypeAnnotation>; + +export interface NativeModuleArrayTypeAnnotation> { + readonly type: 'ArrayTypeAnnotation'; + readonly elementType?: T; +} + +export interface NativeModuleStringTypeAnnotation { + readonly type: 'StringTypeAnnotation'; +} + +export interface NativeModuleNumberTypeAnnotation { + readonly type: 'NumberTypeAnnotation'; +} + +export interface NativeModuleInt32TypeAnnotation { + readonly type: 'Int32TypeAnnotation'; +} + +export interface NativeModuleDoubleTypeAnnotation { + readonly type: 'DoubleTypeAnnotation'; +} + +export interface NativeModuleFloatTypeAnnotation { + readonly type: 'FloatTypeAnnotation'; +} + +export interface NativeModuleBooleanTypeAnnotation { + readonly type: 'BooleanTypeAnnotation'; +} + +export type NativeModuleEnumMembers = readonly { + readonly name: string; + readonly value: string; +}[]; + +export type NativeModuleEnumMemberType = + | 'NumberTypeAnnotation' + | 'StringTypeAnnotation'; + +export interface NativeModuleEnumDeclaration { + readonly name: string; + readonly type: 'EnumDeclaration'; + readonly memberType: NativeModuleEnumMemberType; +} + +export interface NativeModuleEnumDeclarationWithMembers { + name: string; + type: 'EnumDeclarationWithMembers'; + memberType: NativeModuleEnumMemberType; + members: NativeModuleEnumMembers; +} + +export interface NativeModuleGenericObjectTypeAnnotation { + readonly type: 'GenericObjectTypeAnnotation'; +} + +export interface NativeModuleTypeAliasTypeAnnotation { + readonly type: 'TypeAliasTypeAnnotation'; + readonly name: string; +} + +export interface NativeModulePromiseTypeAnnotation { + readonly type: 'PromiseTypeAnnotation'; + readonly elementType?: Nullable; +} + +export type UnionTypeAnnotationMemberType = + | 'NumberTypeAnnotation' + | 'ObjectTypeAnnotation' + | 'StringTypeAnnotation'; + +export interface NativeModuleUnionTypeAnnotation { + readonly type: 'UnionTypeAnnotation'; + readonly memberType: UnionTypeAnnotationMemberType; +} + +export interface NativeModuleMixedTypeAnnotation { + readonly type: 'MixedTypeAnnotation'; +} + +export type NativeModuleBaseTypeAnnotation = + | NativeModuleStringTypeAnnotation + | NativeModuleNumberTypeAnnotation + | NativeModuleInt32TypeAnnotation + | NativeModuleDoubleTypeAnnotation + | NativeModuleFloatTypeAnnotation + | NativeModuleBooleanTypeAnnotation + | NativeModuleEnumDeclaration + | NativeModuleGenericObjectTypeAnnotation + | ReservedTypeAnnotation + | NativeModuleTypeAliasTypeAnnotation + | NativeModuleArrayTypeAnnotation> + | NativeModuleObjectTypeAnnotation + | NativeModuleUnionTypeAnnotation + | NativeModuleMixedTypeAnnotation; + +export type NativeModuleParamTypeAnnotation = + | NativeModuleBaseTypeAnnotation + | NativeModuleParamOnlyTypeAnnotation; + +export type NativeModuleReturnTypeAnnotation = + | NativeModuleBaseTypeAnnotation + | NativeModuleReturnOnlyTypeAnnotation; + +export type NativeModuleTypeAnnotation = + | NativeModuleBaseTypeAnnotation + | NativeModuleParamOnlyTypeAnnotation + | NativeModuleReturnOnlyTypeAnnotation; + +export type NativeModuleParamOnlyTypeAnnotation = NativeModuleFunctionTypeAnnotation; + +export type NativeModuleReturnOnlyTypeAnnotation = + | NativeModulePromiseTypeAnnotation + | VoidTypeAnnotation; + diff --git a/packages/react-native-codegen/src/SchemaValidator.d.ts b/packages/react-native-codegen/src/SchemaValidator.d.ts new file mode 100644 index 00000000000000..c5b90a6aa2b058 --- /dev/null +++ b/packages/react-native-codegen/src/SchemaValidator.d.ts @@ -0,0 +1,11 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +import type { SchemaType } from "./CodegenSchema"; + +export declare function getErrors(schema: SchemaType): readonly string[]; +export declare function validate(schema: SchemaType): void; diff --git a/packages/react-native-codegen/src/parsers/errors.d.ts b/packages/react-native-codegen/src/parsers/errors.d.ts new file mode 100644 index 00000000000000..c99bd85e0d3f3d --- /dev/null +++ b/packages/react-native-codegen/src/parsers/errors.d.ts @@ -0,0 +1,10 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +export type ParserType = 'Flow' | 'TypeScript'; + +export declare class ParserError extends Error {} diff --git a/packages/react-native-codegen/src/parsers/flow/parser.d.ts b/packages/react-native-codegen/src/parsers/flow/parser.d.ts new file mode 100644 index 00000000000000..0d73f1309b4cb6 --- /dev/null +++ b/packages/react-native-codegen/src/parsers/flow/parser.d.ts @@ -0,0 +1,10 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +import type {Parser} from '../parser'; + +export declare class FlowParser implements Parser{} diff --git a/packages/react-native-codegen/src/parsers/parser.d.ts b/packages/react-native-codegen/src/parsers/parser.d.ts new file mode 100644 index 00000000000000..2906c303d51ee9 --- /dev/null +++ b/packages/react-native-codegen/src/parsers/parser.d.ts @@ -0,0 +1,17 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +import type { SchemaType } from "../CodegenSchema"; +import type { ParserType } from "./errors"; + +// useful members only for downstream +export interface Parser { + language(): ParserType; + parseFile(filename: string): SchemaType; + parseString(contents: string, filename?: string): SchemaType; + parseModuleFixture(filename: string): SchemaType; +} diff --git a/packages/react-native-codegen/src/parsers/schema/index.d.ts b/packages/react-native-codegen/src/parsers/schema/index.d.ts new file mode 100644 index 00000000000000..ccfbef8c54303e --- /dev/null +++ b/packages/react-native-codegen/src/parsers/schema/index.d.ts @@ -0,0 +1,10 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +import type { SchemaType } from "../../CodegenSchema"; + +export declare function parse(filename: string): SchemaType | undefined; diff --git a/packages/react-native-codegen/src/parsers/typescript/parser.d.ts b/packages/react-native-codegen/src/parsers/typescript/parser.d.ts new file mode 100644 index 00000000000000..e5d7de87ba35e2 --- /dev/null +++ b/packages/react-native-codegen/src/parsers/typescript/parser.d.ts @@ -0,0 +1,10 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +import type {Parser} from '../parser'; + +export declare class TypeScriptParser implements Parser{}