Skip to content

Commit 7bb66f2

Browse files
committed
[compiler][patch] Emit unary expressions instead of negative numbers
This is a babel bug + edge case. Babel compact mode produces invalid JavaScript (i.e. parse error) when given a `NumericLiteral` with a negative value. See https://codesandbox.io/p/devbox/5d47fr for repro. As a followup, we could change our test infra parse babel options (e.g. a babel transform options pragma) which could let us track regressions. We may add an "exhaustive" mode to the compiler test runner to test (1) different babel options and (2) commonly used versions.
1 parent 14094f8 commit 7bb66f2

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1726,7 +1726,7 @@ function codegenInstructionValue(
17261726
}
17271727
case 'UnaryExpression': {
17281728
value = t.unaryExpression(
1729-
instrValue.operator as 'throw', // todo
1729+
instrValue.operator,
17301730
codegenPlaceToExpression(cx, instrValue.value),
17311731
);
17321732
break;
@@ -2582,7 +2582,13 @@ function codegenValue(
25822582
value: boolean | number | string | null | undefined,
25832583
): t.Expression {
25842584
if (typeof value === 'number') {
2585-
return t.numericLiteral(value);
2585+
if (value < 0) {
2586+
// Babel compact mode produces invalid JS for negative numbers
2587+
// See https://codesandbox.io/p/devbox/5d47fr
2588+
return t.unaryExpression('-', t.numericLiteral(-value), false);
2589+
} else {
2590+
return t.numericLiteral(value);
2591+
}
25862592
} else if (typeof value === 'boolean') {
25872593
return t.booleanLiteral(value);
25882594
} else if (typeof value === 'string') {

0 commit comments

Comments
 (0)