From 032ab3f76511ffac1d8091638cb51a1dc0958376 Mon Sep 17 00:00:00 2001 From: Mike Vitousek Date: Thu, 8 Aug 2024 13:49:41 -0700 Subject: [PATCH] better typing of some globals --- .../src/HIR/Environment.ts | 7 +-- .../src/HIR/Globals.ts | 49 ++++++++++++++++++- .../src/HIR/ObjectShape.ts | 10 ++++ 3 files changed, 61 insertions(+), 5 deletions(-) diff --git a/compiler/packages/babel-plugin-react-compiler/src/HIR/Environment.ts b/compiler/packages/babel-plugin-react-compiler/src/HIR/Environment.ts index dad27965af..f89665b075 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/HIR/Environment.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/HIR/Environment.ts @@ -679,7 +679,7 @@ export class Environment { ); } case 'ImportSpecifier': { - if (this.#isKnownReactModule(binding.module)) { + if (this.#isKnownTypedModule(binding.module)) { /** * For `import {imported as name} from "..."` form, we use the `imported` * name rather than the local alias. Because we don't have definitions for @@ -707,7 +707,7 @@ export class Environment { } case 'ImportDefault': case 'ImportNamespace': { - if (this.#isKnownReactModule(binding.module)) { + if (this.#isKnownTypedModule(binding.module)) { // only resolve imports to modules we know about return ( this.#globals.get(binding.name) ?? @@ -720,10 +720,11 @@ export class Environment { } } - #isKnownReactModule(moduleName: string): boolean { + #isKnownTypedModule(moduleName: string): boolean { return ( moduleName.toLowerCase() === 'react' || moduleName.toLowerCase() === 'react-dom' || + moduleName.toLowerCase() === 'fbt' || (this.config.enableSharedRuntime__testonly && moduleName === 'shared-runtime') ); diff --git a/compiler/packages/babel-plugin-react-compiler/src/HIR/Globals.ts b/compiler/packages/babel-plugin-react-compiler/src/HIR/Globals.ts index 5e90401c68..6827c34fa1 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/HIR/Globals.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/HIR/Globals.ts @@ -83,7 +83,7 @@ const UNTYPED_GLOBALS: Set = new Set([ const TYPED_GLOBALS: Array<[string, BuiltInType]> = [ [ 'Array', - addObject(DEFAULT_SHAPES, 'Array', [ + addFunction(DEFAULT_SHAPES, [ [ 'isArray', // Array.isArray(value) @@ -117,7 +117,12 @@ const TYPED_GLOBALS: Array<[string, BuiltInType]> = [ returnValueKind: ValueKind.Mutable, }), ], - ]), + ], {positionalParams: [], + restParam: Effect.Read, + returnType: {kind: 'Object', shapeId: BuiltInArrayId}, + calleeEffect: Effect.Read, + returnValueKind: ValueKind.Mutable + } , 'Array'), ], [ 'Math', @@ -391,6 +396,46 @@ const REACT_APIS: Array<[string, BuiltInType]> = [ ], ]; +const FBT_FUNCTION = { + positionalParams: [], + restParam: Effect.Read, + returnType: {kind: 'Primitive'} as const, + calleeEffect: Effect.Read, + returnValueKind: ValueKind.Primitive, +} + +const FBT_APIS: Array<[string, BuiltInType]> = [ + [ + 'param', + addFunction(DEFAULT_SHAPES, [], FBT_FUNCTION), + ], + [ + 'enum', + addFunction(DEFAULT_SHAPES, [], FBT_FUNCTION), + ], + [ + 'name', + addFunction(DEFAULT_SHAPES, [], FBT_FUNCTION), + ], + [ + 'plural', + addFunction(DEFAULT_SHAPES, [], FBT_FUNCTION), + ], + [ + 'pronoun', + addFunction(DEFAULT_SHAPES, [], FBT_FUNCTION), + ], + [ + 'isFbtInstance', + addFunction(DEFAULT_SHAPES, [], FBT_FUNCTION), + ], +] + +TYPED_GLOBALS.push([ + 'fbt', + addFunction(DEFAULT_SHAPES, FBT_APIS, FBT_FUNCTION) +]); + TYPED_GLOBALS.push( [ 'React', diff --git a/compiler/packages/babel-plugin-react-compiler/src/HIR/ObjectShape.ts b/compiler/packages/babel-plugin-react-compiler/src/HIR/ObjectShape.ts index 3d377dba59..df40fe9499 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/HIR/ObjectShape.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/HIR/ObjectShape.ts @@ -284,6 +284,16 @@ addObject(BUILTIN_SHAPES, BuiltInArrayId, [ returnValueKind: ValueKind.Primitive, }), ], + [ + 'fill', + addFunction(BUILTIN_SHAPES, [], { + positionalParams: [], + restParam: Effect.Read, + returnType: PRIMITIVE_TYPE, + calleeEffect: Effect.Store, + returnValueKind: ValueKind.Mutable, + }), + ], [ 'slice', addFunction(BUILTIN_SHAPES, [], {