Skip to content

Commit fbf79e0

Browse files
committed
Update on "[compiler] Type inference for tagged template literals"
At Meta we have a pattern of using tagged template literals for features that are compiled away: ``` // Relay: graphql`...graphql text...` ``` In many cases these tags produce a primitive value, and we can get even more optimal output if we can tell the compiler about these types. The new moduleTypeProvider gives us the ability to declare such types, this PR extends the compiler to use this type information for TaggedTemplateExpression values. [ghstack-poisoned]
2 parents bea8495 + 6645318 commit fbf79e0

10 files changed

+15
-549
lines changed

compiler/packages/babel-plugin-react-compiler/src/HIR/Environment.ts

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ import {
3333
Type,
3434
ValidatedIdentifier,
3535
ValueKind,
36-
getHookKindForType,
3736
makeBlockId,
3837
makeIdentifierId,
3938
makeIdentifierName,
@@ -789,9 +788,14 @@ export class Environment {
789788
);
790789
} else {
791790
const moduleType = this.#resolveModuleType(binding.module, loc);
792-
let propertyType: Type | null = null;
793791
if (moduleType !== null) {
794-
propertyType = this.getPropertyType(moduleType, binding.imported);
792+
const importedType = this.getPropertyType(
793+
moduleType,
794+
binding.imported,
795+
);
796+
if (importedType != null) {
797+
return importedType;
798+
}
795799
}
796800

797801
/**
@@ -802,18 +806,9 @@ export class Environment {
802806
* `import {useHook as foo} ...`
803807
* `import {foo as useHook} ...`
804808
*/
805-
const expectHook =
806-
isHookName(binding.imported) || isHookName(binding.name);
807-
if (expectHook) {
808-
if (
809-
propertyType &&
810-
getHookKindForType(this, propertyType) !== null
811-
) {
812-
return propertyType;
813-
}
814-
return this.#getCustomHookType();
815-
}
816-
return propertyType;
809+
return isHookName(binding.imported) || isHookName(binding.name)
810+
? this.#getCustomHookType()
811+
: null;
817812
}
818813
}
819814
case 'ImportDefault':
@@ -826,27 +821,17 @@ export class Environment {
826821
);
827822
} else {
828823
const moduleType = this.#resolveModuleType(binding.module, loc);
829-
let importedType: Type | null = null;
830824
if (moduleType !== null) {
831825
if (binding.kind === 'ImportDefault') {
832826
const defaultType = this.getPropertyType(moduleType, 'default');
833827
if (defaultType !== null) {
834-
importedType = defaultType;
828+
return defaultType;
835829
}
836830
} else {
837-
importedType = moduleType;
838-
}
839-
}
840-
if (isHookName(binding.name)) {
841-
if (
842-
importedType !== null &&
843-
getHookKindForType(this, importedType) !== null
844-
) {
845-
return importedType;
831+
return moduleType;
846832
}
847-
return this.#getCustomHookType();
848833
}
849-
return importedType;
834+
return isHookName(binding.name) ? this.#getCustomHookType() : null;
850835
}
851836
}
852837
}

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/type-provider-hooklike-name-not-typed-as-hook-import.expect.md

Lines changed: 0 additions & 123 deletions
This file was deleted.

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/type-provider-hooklike-name-not-typed-as-hook-import.tsx

Lines changed: 0 additions & 31 deletions
This file was deleted.

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/type-provider-import-object-typed-module-as-hook-name.expect.md

Lines changed: 0 additions & 145 deletions
This file was deleted.

0 commit comments

Comments
 (0)