Skip to content

Commit

Permalink
Update on "[compiler] Type inference for tagged template literals"
Browse files Browse the repository at this point in the history
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]
  • Loading branch information
josephsavona committed Sep 4, 2024
2 parents bea8495 + 6645318 commit fbf79e0
Show file tree
Hide file tree
Showing 10 changed files with 15 additions and 549 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import {
Type,
ValidatedIdentifier,
ValueKind,
getHookKindForType,
makeBlockId,
makeIdentifierId,
makeIdentifierName,
Expand Down Expand Up @@ -789,9 +788,14 @@ export class Environment {
);
} else {
const moduleType = this.#resolveModuleType(binding.module, loc);
let propertyType: Type | null = null;
if (moduleType !== null) {
propertyType = this.getPropertyType(moduleType, binding.imported);
const importedType = this.getPropertyType(
moduleType,
binding.imported,
);
if (importedType != null) {
return importedType;
}
}

/**
Expand All @@ -802,18 +806,9 @@ export class Environment {
* `import {useHook as foo} ...`
* `import {foo as useHook} ...`
*/
const expectHook =
isHookName(binding.imported) || isHookName(binding.name);
if (expectHook) {
if (
propertyType &&
getHookKindForType(this, propertyType) !== null
) {
return propertyType;
}
return this.#getCustomHookType();
}
return propertyType;
return isHookName(binding.imported) || isHookName(binding.name)
? this.#getCustomHookType()
: null;
}
}
case 'ImportDefault':
Expand All @@ -826,27 +821,17 @@ export class Environment {
);
} else {
const moduleType = this.#resolveModuleType(binding.module, loc);
let importedType: Type | null = null;
if (moduleType !== null) {
if (binding.kind === 'ImportDefault') {
const defaultType = this.getPropertyType(moduleType, 'default');
if (defaultType !== null) {
importedType = defaultType;
return defaultType;
}
} else {
importedType = moduleType;
}
}
if (isHookName(binding.name)) {
if (
importedType !== null &&
getHookKindForType(this, importedType) !== null
) {
return importedType;
return moduleType;
}
return this.#getCustomHookType();
}
return importedType;
return isHookName(binding.name) ? this.#getCustomHookType() : null;
}
}
}
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit fbf79e0

Please sign in to comment.