Skip to content

Commit c26ca80

Browse files
committed
Update on "[compiler] Validate type configs for hooks/non-hooks"
Alternative to #30868. The goal is to ensure that the types coming out of moduleTypeProvider are valid wrt to hook typing. If something is named like a hook, then it must be typed as a hook (or don't type it). [ghstack-poisoned]
1 parent c37ac41 commit c26ca80

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -795,10 +795,12 @@ export class Environment {
795795
binding.imported,
796796
);
797797
if (importedType != null) {
798-
// Check that hook-like export names are hook types, and non-hook names are non-hook types.
799-
// The user-assigned alias isn't decidable by the type provider, so we ignore that for the check.
800-
// Thus we allow `import {fooNonHook as useFoo} from ...` because the name and type both say
801-
// that it's not a hook.
798+
/*
799+
* Check that hook-like export names are hook types, and non-hook names are non-hook types.
800+
* The user-assigned alias isn't decidable by the type provider, so we ignore that for the check.
801+
* Thus we allow `import {fooNonHook as useFoo} from ...` because the name and type both say
802+
* that it's not a hook.
803+
*/
802804
const expectHook = isHookName(binding.imported);
803805
const isHook = getHookKindForType(this, importedType) != null;
804806
if (expectHook !== isHook) {
@@ -846,8 +848,10 @@ export class Environment {
846848
importedType = moduleType;
847849
}
848850
if (importedType !== null) {
849-
// Check that the hook-like modules are defined as types, and non hook-like modules are not typed as hooks.
850-
// So `import Foo from 'useFoo'` is expected to be a hook based on the module name
851+
/*
852+
* Check that the hook-like modules are defined as types, and non hook-like modules are not typed as hooks.
853+
* So `import Foo from 'useFoo'` is expected to be a hook based on the module name
854+
*/
851855
const expectHook = isHookName(binding.module);
852856
const isHook = getHookKindForType(this, importedType) != null;
853857
if (expectHook !== isHook) {

compiler/packages/snap/src/sprout/shared-runtime-type-provider.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ export function makeSharedRuntimeTypeProvider({
7272
},
7373
};
7474
} else if (moduleName === 'ReactCompilerTest') {
75+
/**
76+
* Fake module used for testing validation that type providers return hook
77+
* types for hook names and non-hook types for non-hook names
78+
*/
7579
return {
7680
kind: 'object',
7781
properties: {
@@ -86,6 +90,10 @@ export function makeSharedRuntimeTypeProvider({
8690
},
8791
};
8892
} else if (moduleName === 'useDefaultExportNotTypedAsHook') {
93+
/**
94+
* Fake module used for testing validation that type providers return hook
95+
* types for hook names and non-hook types for non-hook names
96+
*/
8997
return {
9098
kind: 'object',
9199
properties: {

0 commit comments

Comments
 (0)