From b79a5289fc21647b444eca397b5a02d5b64f5d2b Mon Sep 17 00:00:00 2001 From: Joe Savona Date: Mon, 5 Feb 2024 21:51:13 -0800 Subject: [PATCH] Resolve type aliases The goal of this PR is to move towards a uniform representation for all type declarations, whether they are named type aliases, function declarations, or inline annotations. We now assign every non-primitive type declaration (named or anonymous) a unique DeclarationId. In the next PR, we'll also re-map inline annotations back to this declaration id when encountering them. This PR is extremely gross and my intent is to refactor a bunch of things in the HIR to allow this to be less gross. Challenges: * Babel name resolution requires using scopes but i really want to just work with plain nodes, since NodePath and TypeScript do _not_ get along. So here, i find all identifiers and store a mapping of identifier -> scope, so that i can later look them up if necessary. * HIR doesn't have a notion of a declaration id, and in general we don't want to extend HIR. So i end up with a whole bunch of side table information and indirection. For example, a function doesn't know it's own declaration id. So we have to look it up. Function params don't track their Forest type, so we have to look them up on the function declaration. Etc. --- .../babel-plugin-react-forget/src/HIR/BuildHIR.ts | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/compiler/packages/babel-plugin-react-forget/src/HIR/BuildHIR.ts b/compiler/packages/babel-plugin-react-forget/src/HIR/BuildHIR.ts index df9ff21c84..afffabe288 100644 --- a/compiler/packages/babel-plugin-react-forget/src/HIR/BuildHIR.ts +++ b/compiler/packages/babel-plugin-react-forget/src/HIR/BuildHIR.ts @@ -861,13 +861,24 @@ function lowerStatement( loc: id.node.loc ?? GeneratedSource, }); } else { + const typeAnnotation = id.get("typeAnnotation"); + let type: t.FlowType | t.TSType | null; + if (typeAnnotation.isTSTypeAnnotation()) { + const typePath = typeAnnotation.get("typeAnnotation"); + type = typePath.node; + } else if (typeAnnotation.isTypeAnnotation()) { + const typePath = typeAnnotation.get("typeAnnotation"); + type = typePath.node; + } else { + type = null; + } lowerValueToTemporary(builder, { kind: "DeclareLocal", lvalue: { kind, place, }, - type: null, + type, loc: id.node.loc ?? GeneratedSource, }); }