Skip to content

Commit 4220834

Browse files
committed
handle null init in createVariableDeclarator for source locations
1 parent cfec744 commit 4220834

File tree

3 files changed

+221
-164
lines changed

3 files changed

+221
-164
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,9 @@ function codegenReactiveScope(
702702
outputComments.push(name.name);
703703
if (!cx.hasDeclared(identifier)) {
704704
statements.push(
705-
t.variableDeclaration('let', [t.variableDeclarator(name)]),
705+
t.variableDeclaration('let', [
706+
createVariableDeclarator(name, null),
707+
]),
706708
);
707709
}
708710
cacheLoads.push({name, index, value: wrapCacheDep(cx, name)});
@@ -1735,11 +1737,13 @@ function createVariableDeclarator(
17351737
* The variable declarator location is not preserved in HIR, however, we can use the
17361738
* start location of the id and the end location of the init to recreate the
17371739
* exact original variable declarator location.
1740+
*
1741+
* Or if init is null, we likely have a declaration without an initializer, so we can use the id.loc.end as the end location.
17381742
*/
1739-
if (id.loc && init?.loc) {
1743+
if (id.loc && (init === null || init?.loc)) {
17401744
node.loc = {
17411745
start: id.loc.start,
1742-
end: init.loc.end,
1746+
end: init?.loc?.end ?? id.loc.end,
17431747
filename: id.loc.filename,
17441748
identifierName: undefined,
17451749
};

0 commit comments

Comments
 (0)