better typing of some globals

This commit is contained in:
Mike Vitousek
2024-08-08 13:49:41 -07:00
parent ade006f8da
commit 032ab3f765
3 changed files with 61 additions and 5 deletions
@@ -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')
);
@@ -83,7 +83,7 @@ const UNTYPED_GLOBALS: Set<string> = 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',
@@ -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, [], {