From 2daa703236019185deceadf747c94f4742a68a12 Mon Sep 17 00:00:00 2001 From: Sathya Gunsasekaran Date: Wed, 31 Jul 2024 14:35:27 +0100 Subject: [PATCH] [compiler] Refactor makeTemporary outside HIRBuilder This is a useful utility function similar to the existing `makeInstructionId` and `makeIdentifierId` functions. This PR moves it outside the HIRBuilder so we can use this in passes that don't have access to the builder instance. [ghstack-poisoned] --- .../babel-plugin-react-compiler/src/HIR/HIR.ts | 16 +++++++++++++++- .../src/HIR/HIRBuilder.ts | 10 ++-------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/compiler/packages/babel-plugin-react-compiler/src/HIR/HIR.ts b/compiler/packages/babel-plugin-react-compiler/src/HIR/HIR.ts index c71a0cea878..4578a9b874a 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/HIR/HIR.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/HIR/HIR.ts @@ -11,7 +11,7 @@ import {CompilerError, CompilerErrorDetailOptions} from '../CompilerError'; import {assertExhaustive} from '../Utils/utils'; import {Environment, ReactFunctionType} from './Environment'; import {HookKind} from './ObjectShape'; -import {Type} from './Types'; +import {Type, makeType} from './Types'; /* * ******************************************************************************************* @@ -1205,6 +1205,20 @@ export type ValidIdentifierName = string & { [opaqueValidIdentifierName]: 'ValidIdentifierName'; }; +export function makeTemporary( + id: IdentifierId, + loc: SourceLocation, +): Identifier { + return { + id, + name: null, + mutableRange: {start: makeInstructionId(0), end: makeInstructionId(0)}, + scope: null, + type: makeType(), + loc, + }; +} + /** * Creates a valid identifier name. This should *not* be used for synthesizing * identifier names: only call this method for identifier names that appear in the diff --git a/compiler/packages/babel-plugin-react-compiler/src/HIR/HIRBuilder.ts b/compiler/packages/babel-plugin-react-compiler/src/HIR/HIRBuilder.ts index d3a25d2fa3c..890a27f4d4c 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/HIR/HIRBuilder.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/HIR/HIRBuilder.ts @@ -27,6 +27,7 @@ import { makeBlockId, makeIdentifierName, makeInstructionId, + makeTemporary, makeType, } from './HIR'; import {printInstruction} from './PrintHIR'; @@ -182,14 +183,7 @@ export default class HIRBuilder { makeTemporary(loc: SourceLocation): Identifier { const id = this.nextIdentifierId; - return { - id, - name: null, - mutableRange: {start: makeInstructionId(0), end: makeInstructionId(0)}, - scope: null, - type: makeType(), - loc, - }; + return makeTemporary(id, loc); } #resolveBabelBinding(