Skip to content

Commit

Permalink
Update on "[compiler] Validate type configs for hooks/non-hooks"
Browse files Browse the repository at this point in the history
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]
  • Loading branch information
josephsavona committed Sep 5, 2024
1 parent c37ac41 commit c26ca80
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -795,10 +795,12 @@ export class Environment {
binding.imported,
);
if (importedType != null) {
// Check that hook-like export names are hook types, and non-hook names are non-hook types.
// The user-assigned alias isn't decidable by the type provider, so we ignore that for the check.
// Thus we allow `import {fooNonHook as useFoo} from ...` because the name and type both say
// that it's not a hook.
/*
* Check that hook-like export names are hook types, and non-hook names are non-hook types.
* The user-assigned alias isn't decidable by the type provider, so we ignore that for the check.
* Thus we allow `import {fooNonHook as useFoo} from ...` because the name and type both say
* that it's not a hook.
*/
const expectHook = isHookName(binding.imported);
const isHook = getHookKindForType(this, importedType) != null;
if (expectHook !== isHook) {
Expand Down Expand Up @@ -846,8 +848,10 @@ export class Environment {
importedType = moduleType;
}
if (importedType !== null) {
// Check that the hook-like modules are defined as types, and non hook-like modules are not typed as hooks.
// So `import Foo from 'useFoo'` is expected to be a hook based on the module name
/*
* Check that the hook-like modules are defined as types, and non hook-like modules are not typed as hooks.
* So `import Foo from 'useFoo'` is expected to be a hook based on the module name
*/
const expectHook = isHookName(binding.module);
const isHook = getHookKindForType(this, importedType) != null;
if (expectHook !== isHook) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ export function makeSharedRuntimeTypeProvider({
},
};
} else if (moduleName === 'ReactCompilerTest') {
/**
* Fake module used for testing validation that type providers return hook
* types for hook names and non-hook types for non-hook names
*/
return {
kind: 'object',
properties: {
Expand All @@ -86,6 +90,10 @@ export function makeSharedRuntimeTypeProvider({
},
};
} else if (moduleName === 'useDefaultExportNotTypedAsHook') {
/**
* Fake module used for testing validation that type providers return hook
* types for hook names and non-hook types for non-hook names
*/
return {
kind: 'object',
properties: {
Expand Down

0 comments on commit c26ca80

Please sign in to comment.