Skip to content

Commit ecbdb63

Browse files
committed
propagate declaration locations through HIR pipeline for hoisted variables
1 parent 65eec42 commit ecbdb63

File tree

7 files changed

+22
-4
lines changed

7 files changed

+22
-4
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -931,7 +931,7 @@ function lowerStatement(
931931
kind: InstructionKind.Let,
932932
place,
933933
},
934-
loc: id.node.loc ?? GeneratedSource,
934+
loc: stmt.node.loc ?? GeneratedSource,
935935
});
936936
} else {
937937
const typeAnnotation = id.get('typeAnnotation');
@@ -952,7 +952,7 @@ function lowerStatement(
952952
place,
953953
},
954954
type,
955-
loc: id.node.loc ?? GeneratedSource,
955+
loc: stmt.node.loc ?? GeneratedSource,
956956
});
957957
}
958958
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1649,6 +1649,7 @@ export type ReactiveScopeDependencies = Set<ReactiveScopeDependency>;
16491649
export type ReactiveScopeDeclaration = {
16501650
identifier: Identifier;
16511651
scope: ReactiveScope; // the scope in which the variable was originally declared
1652+
loc: SourceLocation;
16521653
};
16531654

16541655
const opaquePropertyLiteral = Symbol();

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
LoadContext,
2929
TInstruction,
3030
FunctionExpression,
31+
SourceLocation,
3132
ObjectMethod,
3233
PropertyLiteral,
3334
convertHoistedLValueKind,
@@ -393,6 +394,7 @@ function getProperty(
393394
type Decl = {
394395
id: InstructionId;
395396
scope: Stack<ReactiveScope>;
397+
loc: SourceLocation;
396398
};
397399

398400
export class DependencyCollectionContext {
@@ -602,6 +604,7 @@ export class DependencyCollectionContext {
602604
scope.declarations.set(maybeDependency.identifier.id, {
603605
identifier: maybeDependency.identifier,
604606
scope: originalDeclaration.scope.value!,
607+
loc: originalDeclaration.loc,
605608
});
606609
}
607610
});
@@ -686,6 +689,7 @@ export function handleInstruction(
686689
context.declare(lvalue.identifier, {
687690
id,
688691
scope: context.currentScope,
692+
loc: instr.loc,
689693
});
690694
if (
691695
context.isDeferredDependency({kind: HIRValue.Instruction, value: instr})
@@ -702,6 +706,7 @@ export function handleInstruction(
702706
context.declare(value.lvalue.place.identifier, {
703707
id,
704708
scope: context.currentScope,
709+
loc: instr.loc,
705710
});
706711
} else if (value.kind === 'DeclareLocal' || value.kind === 'DeclareContext') {
707712
/*
@@ -718,6 +723,7 @@ export function handleInstruction(
718723
context.declare(value.lvalue.place.identifier, {
719724
id,
720725
scope: context.currentScope,
726+
loc: instr.loc,
721727
});
722728
}
723729
} else if (value.kind === 'Destructure') {
@@ -729,6 +735,7 @@ export function handleInstruction(
729735
context.declare(place.identifier, {
730736
id,
731737
scope: context.currentScope,
738+
loc: instr.loc,
732739
});
733740
}
734741
} else if (value.kind === 'StoreContext') {
@@ -745,6 +752,7 @@ export function handleInstruction(
745752
context.declare(value.lvalue.place.identifier, {
746753
id,
747754
scope: context.currentScope,
755+
loc: instr.loc,
748756
});
749757
}
750758

@@ -775,11 +783,13 @@ function collectDependencies(
775783
context.declare(param.identifier, {
776784
id: makeInstructionId(0),
777785
scope: empty(),
786+
loc: param.identifier.loc,
778787
});
779788
} else {
780789
context.declare(param.place.identifier, {
781790
id: makeInstructionId(0),
782791
scope: empty(),
792+
loc: param.place.loc,
783793
});
784794
}
785795
}
@@ -812,6 +822,7 @@ function collectDependencies(
812822
context.declare(instr.lvalue.identifier, {
813823
id: instr.id,
814824
scope: context.currentScope,
825+
loc: instr.loc,
815826
});
816827
/**
817828
* Recursively visit the inner function to extract dependencies there

compiler/packages/babel-plugin-react-compiler/src/Inference/InferEffectDependencies.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,7 @@ function inferDependencies(
628628
context.declare(dep.identifier, {
629629
id: makeInstructionId(0),
630630
scope: empty(),
631+
loc: dep.identifier.loc,
631632
});
632633
}
633634
const placeholderScope: ReactiveScope = {
@@ -694,6 +695,7 @@ function inferDependenciesInFn(
694695
context.declare(instr.lvalue.identifier, {
695696
id: instr.id,
696697
scope: context.currentScope,
698+
loc: instr.loc,
697699
});
698700
/**
699701
* Recursively visit the inner function to extract dependencies

compiler/packages/babel-plugin-react-compiler/src/Optimization/InlineJsxTransform.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,7 @@ export function inlineJsxTransform(
411411
scope.declarations.set(decl.identifier.id, {
412412
identifier: newDecl,
413413
scope: decl.scope,
414+
loc: decl.loc,
414415
});
415416
}
416417
}

compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/CodegenReactiveFunction.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,7 @@ function codegenReactiveScope(
675675
}
676676
let firstOutputIndex: number | null = null;
677677

678-
for (const [, {identifier}] of [...scope.declarations].sort(([, a], [, b]) =>
678+
for (const [, {identifier, loc}] of [...scope.declarations].sort(([, a], [, b]) =>
679679
compareScopeDeclaration(a, b),
680680
)) {
681681
const index = cx.nextCacheIndex;
@@ -702,7 +702,9 @@ function codegenReactiveScope(
702702
outputComments.push(name.name);
703703
if (!cx.hasDeclared(identifier)) {
704704
statements.push(
705-
t.variableDeclaration('let', [createVariableDeclarator(name, null)]),
705+
createVariableDeclaration(loc, 'let', [
706+
createVariableDeclarator(name, null),
707+
]),
706708
);
707709
}
708710
cacheLoads.push({name, index, value: wrapCacheDep(cx, name)});

compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/PropagateEarlyReturns.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ class Transform extends ReactiveFunctionTransform<State> {
156156
scopeBlock.scope.declarations.set(earlyReturnValue.value.id, {
157157
identifier: earlyReturnValue.value,
158158
scope: scopeBlock.scope,
159+
loc: earlyReturnValue.loc,
159160
});
160161

161162
const instructions = scopeBlock.instructions;

0 commit comments

Comments
 (0)